Skip to content
Snippets Groups Projects
Commit cd56901a authored by Daniel's avatar Daniel
Browse files

Merge remote-tracking branch 'origin/dev-mongodb' into dev-chat-firebase

parents 10239ad4 f3f64982
No related merge requests found
class ChatData {
constructor () {
this.mongoose = require('mongoose')
this.mongoose.connect('mongodb://localhost/wbd3_chat')
this.chatSchema = new this.mongoose.Schema({
participant_ids: [Number],
chats: [{
owner_id: Number,
content: String
}]
})
this.ChatModel = this.mongoose.model('Chat', this.chatSchema)
}
addChat (senderId, receiverId, content) {
let participantIds = [ senderId, receiverId ]
participantIds.sort()
let chat = {
owner_id: senderId,
content: content
}
this.ChatModel.findOneAndUpdate(
{ participant_ids: participantIds },
{ $push: { chats: chat } }, (err, done) => {
if (err) {
console.error(err)
}
if (!done) {
let chat = new this.ChatModel({
participant_ids: participantIds,
chats: [{
owner_id: senderId,
content: content
}]
})
chat.save()
}
})
}
findChat (firstParticipantId, secondParticipantId) {
let participantIds = [ firstParticipantId, secondParticipantId ]
participantIds.sort()
this.ChatModel.findOne({ participant_ids: participantIds }, function (err, result) {
if (err) {
return console.error(err)
}
if (result) {
let chats = result.chats
chats.forEach(function (chat) {
console.log(chat)
})
}
})
}
}
module.exports = ChatData
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment