Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
304CEM_resit_backend/api/controllers/messages.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
120 lines (116 sloc)
3.87 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Message = require('../models/messages'); | |
const User = require('../models/user'); | |
const Item = require('../models/item'); | |
const jwt = require('jsonwebtoken'); | |
const mongoose = require('mongoose'); | |
//Handles get request to show all messages in the database targetted at the user | |
exports.getInbox = (req, res, next) => { | |
//By obtaning the username from the token, finds the messages within the database that are specifically targetted at the user | |
const userId = req.userData._id; | |
Message.find({ destination: userId }).select('-__v').exec() | |
.then(docs => { | |
const response = { | |
items: docs.map(doc => { | |
return { | |
_id: doc._id, | |
sender: doc.sender, | |
title: doc.title, | |
//Also returns request link to get the full message from the API | |
request: { | |
type: 'GET', | |
url: 'https://caracold-304resit-api.herokuapp.com/inbox/' + doc._id | |
} | |
} | |
}) | |
} | |
res.status(200).json(response); | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ | |
error: err | |
}); | |
}); | |
} | |
//Handles post request to send a message, using the token to set the sender to the current user | |
exports.sendMessage = (req, res, next) => { | |
const userId = req.userData._id; | |
const message = new Message({ | |
_id: new mongoose.Types.ObjectId(), | |
sender: userId, | |
destination: req.body.destId, | |
item: req.body.item, | |
title: req.body.title, | |
content: req.body.content | |
}) | |
message.save() | |
.then(result => { | |
console.log(result); | |
res.status(201).json({ | |
message: 'Message sent.', | |
newMsg: { | |
_id: result._id, | |
sender: result.sender, | |
destination: result.destination, | |
item: result.item, | |
title: result.title, | |
content: result.content | |
} | |
}); | |
}) | |
.catch(err => { | |
console.log(err); | |
console.log(decoded); | |
res.status(500).json({ | |
error: err | |
}); | |
}); | |
} | |
//Handles get requests where for messages sent by, rather than to, the user | |
exports.getSent = (req, res, next) => { | |
const userId = req.userData._id; | |
Message.find({ sender: userId }).select('-__v').exec() | |
.then(docs => { | |
const response = { | |
count: docs.length, | |
items: docs.map(doc => { | |
return { | |
_id: doc._id, | |
sender: doc.sender, | |
title: doc.title, | |
request: { | |
type: 'GET', | |
url: 'https://caracold-304resit-api.herokuapp.com/inbox' + doc._id | |
} | |
} | |
}) | |
} | |
res.status(200).json(response); | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ | |
error: err | |
}); | |
}); | |
} | |
//Fetches the full information of a specific message from the database | |
exports.getMessage = (req, res, next) => { | |
const id = req.params.msgId; | |
Message.findById(id) | |
.exec() | |
.then(doc => { | |
console.log(doc); | |
if (doc) { | |
res.status(200).json(doc); | |
} else { | |
res.status(404).json({ | |
message: 'Message ID not found.' | |
}) | |
} | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ error: err }); | |
}) | |
} |