Commit 7898becc authored by Sang D NGO's avatar Sang D NGO
Browse files

Merge branch 'review-api' into 'master'

Review api

See merge request !3
parents f4acb85c 689bc919
......@@ -11,9 +11,9 @@ module.exports = function(dependencies) {
});
const schema = new Schema({
userId: { type: ObjectId, ref: 'User', index: true, required: true },
proposalId: { type: ObjectId, ref: 'MostProposal' },
status: { type: String, enum: ['PENDING', 'ACCEPTED', 'REJECTED'], default: 'PENDING' },
creator: { type: ObjectId, ref: 'User', index: true, required: true },
proposal: { type: ObjectId, ref: 'MostProposal', required: true },
status: { type: String, enum: ['ACCEPTED', 'REJECTED'], required: true },
attachments: {type: [AttachmentSchema], required: false}
}, { timestamps: true });
......
......@@ -3,9 +3,11 @@
module.exports = function(dependencies) {
const models = require('./db')(dependencies);
const proposal = require('./proposal')(dependencies);
const review = require('./review')(dependencies);
return {
models,
proposal
proposal,
review
};
};
module.exports = denormalize;
function denormalize(review) {
return {
id: review.id,
timestamps: review.timestamps,
creator: review.creator,
proposal: review.proposal,
status: review.status,
attachments: review.attachments
};
}
const { DEFAULT_OFFSET, DEFAULT_LIMIT } = require('../constants');
const denormalize = require('./denormalize');
module.exports = function(dependencies) {
const mongoose = dependencies('db').mongo.mongoose;
const Review = mongoose.model('MostReview');
return {
create,
denormalize,
getById,
list,
updateById
};
function create(proposal) {
return Review.create(proposal);
}
function getById(id) {
return Review.findOne({ _id: id });
}
function list(options = {}) {
const query = {};
if (options.creator) {
query.creator = options.creator;
}
if (options.status) {
query.status = options.status;
}
return Review
.find(query)
.skip(+options.offset || DEFAULT_OFFSET)
.limit(+options.limit || DEFAULT_LIMIT)
.sort({ _id: -1 })
.exec();
}
function updateById(proposalId, modified) {
return Review.findOneAndUpdate(
{ _id: proposalId },
{ $set: modified },
{ new: true } // to return updated document
).exec();
}
};
......@@ -7,6 +7,7 @@ module.exports = function(dependencies, lib) {
const router = express.Router();
require('./proposals')(dependencies, lib, router);
require('./reviews')(dependencies, lib, router);
return router;
};
......@@ -4,6 +4,7 @@ module.exports = function(dependencies, lib) {
return {
createProposal,
getProposals,
getProposal,
updateProposal
};
......@@ -60,6 +61,10 @@ module.exports = function(dependencies, lib) {
});
}
function getProposal(req, res) {
res.status(200).json(lib.proposal.denormalize(req.proposal));
}
function updateProposal(req, res) {
var updateFields = {};
......
......@@ -14,6 +14,12 @@ module.exports = function(dependencies, lib, router) {
authorizationMW.requiresAPILogin,
controller.createProposal);
router.get('/proposals/:proposalId',
authorizationMW.requiresAPILogin,
helperMW.checkIdInParams('proposalId', 'Proposal'),
middleware.load,
controller.getProposal);
router.post('/proposals/:proposalId',
authorizationMW.requiresAPILogin,
helperMW.checkIdInParams('proposalId', 'Proposal'),
......
module.exports = function(dependencies, lib) {
const logger = dependencies('logger');
return {
createReview,
getReviews,
getReview,
updateReview
};
function createReview(req, res) {
const { proposal, status, attachments } = req.body;
lib.review.create({
creator: req.user.id,
proposal,
status,
attachments
})
.then(review => {
res.status(201).json(lib.review.denormalize(review));
})
.catch(err => {
const details = 'Error while creating review';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
function getReviews(req, res) {
var listOptions = {
creator: req.user.id,
status: req.query.status,
limit: Number(req.query.limit),
offset: Number(req.query.offset)
};
lib.review.list(listOptions)
.then(reviews => reviews.map(lib.review.denormalize))
.then(denormalizedReviews => res.status(200).json(denormalizedReviews))
.catch(err => {
const details = 'Error while gettings review';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
function getReview(req, res) {
res.status(200).json(lib.review.denormalize(req.review));
}
function updateReview(req, res) {
var updateFields = {};
if (req.body.status) {
updateFields.status = req.body.status;
}
if (req.body.attachments) {
updateFields.attachments = req.body.attachments;
}
lib.review.updateById(req.params.reviewId, updateFields)
.then(review => res.status(200).json(lib.review.denormalize(review)))
.catch(err => {
const details = 'Error while updating review';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
};
'use strict';
module.exports = function(dependencies, lib, router) {
const authorizationMW = dependencies('authorizationMW');
const helperMW = dependencies('helperMW');
const controller = require('./controller')(dependencies, lib);
const middleware = require('./middleware')(dependencies, lib);
router.get('/reviews',
authorizationMW.requiresAPILogin,
controller.getReviews);
router.post('/reviews',
authorizationMW.requiresAPILogin,
controller.createReview);
router.get('/reviews/:reviewId',
authorizationMW.requiresAPILogin,
helperMW.checkIdInParams('reviewId', 'Review'),
middleware.load,
controller.getReview);
router.post('/reviews/:reviewId',
authorizationMW.requiresAPILogin,
helperMW.checkIdInParams('reviewId', 'Review'),
middleware.load,
controller.updateReview);
};
module.exports = function(dependencies, lib) {
const logger = dependencies('logger');
return {
load
};
function load(req, res, next) {
lib.review.getById(req.params.reviewId)
.then(review => {
if (review) {
req.review = review;
next();
} else {
res.status(404).json({
error: {
code: 404,
message: 'Not Found',
details: 'Review not found'
}
});
}
})
.catch(err => {
const details = 'Error while loading review';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
};
(function(angular) {
'use strict';
angular.module('linagora.esn.most')
.factory('mostProposalApiClient', mostProposalApiClient);
var API_PATH = 'proposals';
function mostProposalApiClient(mostRestangular) {
return {
createProposal: createProposal,
getProposals: getProposals,
getProposal: getProposal,
updateProposal: updateProposal
};
function createProposal(data) {
return mostRestangular.all(API_PATH).post(data).then(_stripAndReturnData);
}
function getProposals(options) {
return mostRestangular.all(API_PATH).getList(options).then(_stripAndReturnData);
}
function getProposal(id) {
return mostRestangular.one(API_PATH, id).get().then(_stripAndReturnData);
}
function updateProposal(id, updateFields) {
return mostRestangular.one(API_PATH, id).customPOST(updateFields);
}
function _stripAndReturnData(resp) {
return mostRestangular.stripRestangular(resp.data);
}
}
})(angular);
(function(angular) {
'use strict';
angular.module('linagora.esn.most')
.factory('mostReviewApiClient', mostReviewApiClient);
var API_PATH = 'reviews';
function mostReviewApiClient(mostRestangular) {
return {
createReview: createReview,
getReviews: getReviews,
getReview: getReview,
updateReview: updateReview
};
function createReview(data) {
return mostRestangular.all(API_PATH).post(data).then(_stripAndReturnData);
}
function getReviews(options) {
return mostRestangular.all(API_PATH).getList(options).then(_stripAndReturnData);
}
function getReview(id) {
return mostRestangular.one(API_PATH, id).get().then(_stripAndReturnData);
}
function updateReview(id, updateFields) {
return mostRestangular.one(API_PATH, id).customPOST(updateFields);
}
function _stripAndReturnData(resp) {
return mostRestangular.stripRestangular(resp.data);
}
}
})(angular);
Markdown is supported
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