-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b555c2d
commit d7ce38c
Showing
9 changed files
with
365 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Like from '../models/like.model'; | ||
|
||
/** | ||
* Load like and append to req. | ||
*/ | ||
function load(req, res, next, id) { | ||
Like.get(id, req.user._id) | ||
.then((like) => { | ||
req.like = like; // eslint-disable-line no-param-reassign | ||
return next(); | ||
}) | ||
.catch(e => next(e)); | ||
} | ||
|
||
/** | ||
* Get like | ||
* @returns {Like} | ||
*/ | ||
function get(req, res) { | ||
return res.json(req.like); | ||
} | ||
|
||
/** | ||
* Create new like | ||
* @property {string} req.body.likename - The likename of like. | ||
* @property {string} req.body.mobileNumber - The mobileNumber of like. | ||
* @returns {Like} | ||
*/ | ||
function create(req, res, next) { | ||
const like = new Like({ | ||
likename: req.body.likename, | ||
mobileNumber: req.body.mobileNumber | ||
}); | ||
|
||
like.save() | ||
.then(savedLike => res.json(savedLike)) | ||
.catch(e => next(e)); | ||
} | ||
|
||
/** | ||
* Update existing like | ||
* @property {string} req.body.likename - The likename of like. | ||
* @property {string} req.body.mobileNumber - The mobileNumber of like. | ||
* @returns {Like} | ||
*/ | ||
function update(req, res, next) { | ||
const like = req.like; | ||
like.likename = req.body.likename; | ||
like.mobileNumber = req.body.mobileNumber; | ||
|
||
like.save() | ||
.then(savedLike => res.json(savedLike)) | ||
.catch(e => next(e)); | ||
} | ||
|
||
/** | ||
* Get like list. | ||
* @property {number} req.query.skip - Number of likes to be skipped. | ||
* @property {number} req.query.limit - Limit number of likes to be returned. | ||
* @returns {Like[]} | ||
*/ | ||
function list(req, res, next) { | ||
const { limit = 50, skip = 0 } = req.query; | ||
Like.list({ limit, skip }, req.user._id) | ||
.then(likes => res.json(likes)) | ||
.catch(e => next(e)); | ||
} | ||
|
||
/** | ||
* Delete like. | ||
* @returns {Like} | ||
*/ | ||
function remove(req, res, next) { | ||
const like = req.like; | ||
like.remove() | ||
.then(deletedLike => res.json(deletedLike)) | ||
.catch(e => next(e)); | ||
} | ||
|
||
export default { load, get, create, update, list, remove }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Promise from 'bluebird'; | ||
import mongoose from 'mongoose'; | ||
import httpStatus from 'http-status'; | ||
import APIError from '../helpers/APIError'; | ||
|
||
/** | ||
* Like Schema | ||
*/ | ||
const LikeSchema = new mongoose.Schema({ | ||
userId: String, | ||
postId: String, | ||
type: String, | ||
active: {type: Boolean, default: true}, | ||
}); | ||
|
||
/** | ||
* Add your | ||
* - pre-save hooks | ||
* - validations | ||
* - virtuals | ||
*/ | ||
|
||
/** | ||
* Methods | ||
*/ | ||
LikeSchema.method({ | ||
}); | ||
|
||
/** | ||
* Statics | ||
*/ | ||
LikeSchema.statics = { | ||
/** | ||
* Get user | ||
* @param {ObjectId} id - The objectId of user. | ||
* @returns {Promise<Like, APIError>} | ||
*/ | ||
get(id, userId) { | ||
return this.findOne({id, userId}) | ||
.exec() | ||
.then((user) => { | ||
if (user) { | ||
return user; | ||
} | ||
const err = new APIError('No such user exists!', httpStatus.NOT_FOUND); | ||
return Promise.reject(err); | ||
}); | ||
}, | ||
|
||
/** | ||
* List users in descending order of 'createdAt' timestamp. | ||
* @param {number} skip - Number of users to be skipped. | ||
* @param {number} limit - Limit number of users to be returned. | ||
* @returns {Promise<Like[]>} | ||
*/ | ||
list({ skip = 0, limit = 50 } = {}, userId) { | ||
return this.find({userId}) | ||
.sort({ createdAt: -1 }) | ||
.skip(+skip) | ||
.limit(+limit) | ||
.exec(); | ||
} | ||
}; | ||
|
||
/** | ||
* @typedef Like | ||
*/ | ||
export default mongoose.model('Like', LikeSchema); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import express from 'express'; | ||
import validate from 'express-validation'; | ||
import expressJwt from 'express-jwt'; | ||
import paramValidation from '../../config/param-validation'; | ||
import ctrl from '../controllers/like.controller'; | ||
import config from '../../config/config'; | ||
|
||
const router = express.Router(); // eslint-disable-line new-cap | ||
|
||
router.route('/') | ||
/** GET /api/likes - Get list of likes */ | ||
.get(expressJwt({ secret: config.jwtSecret }), ctrl.list) | ||
|
||
/** POST /api/likes - Create new like */ | ||
// .post(validate(paramValidation.createLike), ctrl.create); | ||
|
||
router.route('/:likeId') | ||
/** GET /api/likes/:likeId - Get like */ | ||
.get(expressJwt({ secret: config.jwtSecret }), ctrl.get) | ||
|
||
/** PUT /api/likes/:likeId - Update like */ | ||
// .put(validate(paramValidation.updateLike), ctrl.update) | ||
|
||
/** DELETE /api/likes/:likeId - Delete like */ | ||
// .delete(ctrl.remove); | ||
|
||
/** Load like when API with likeId route parameter is hit */ | ||
router.param('likeId', ctrl.load); | ||
|
||
export default router; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
import express from 'express'; | ||
import validate from 'express-validation'; | ||
import paramValidation from '../../config/param-validation'; | ||
import userCtrl from '../controllers/post.controller'; | ||
import ctrl from '../controllers/post.controller'; | ||
|
||
const router = express.Router(); // eslint-disable-line new-cap | ||
|
||
router.route('/') | ||
/** GET /api/posts - Get list of posts */ | ||
.get(userCtrl.list) | ||
.get(ctrl.list) | ||
|
||
/** POST /api/posts - Create new user */ | ||
.post(validate(paramValidation.createPost), userCtrl.create); | ||
.post(validate(paramValidation.createPost), ctrl.create); | ||
|
||
router.route('/:postId') | ||
/** GET /api/posts/:postId - Get user */ | ||
.get(userCtrl.get) | ||
.get(ctrl.get) | ||
|
||
/** PUT /api/posts/:postId - Update user */ | ||
// .put(validate(paramValidation.updatePost), userCtrl.update) | ||
// .put(validate(paramValidation.updatePost), ctrl.update) | ||
|
||
/** DELETE /api/posts/:postId - Delete user */ | ||
// .delete(userCtrl.remove); | ||
// .delete(ctrl.remove); | ||
|
||
router.route('/:postId/like') | ||
.post(ctrl.like); | ||
|
||
/** Load user when API with postId route parameter is hit */ | ||
router.param('postId', userCtrl.load); | ||
router.param('postId', ctrl.load); | ||
|
||
export default router; |
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
Oops, something went wrong.