-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
13 changed files
with
753 additions
and
6 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
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,183 @@ | ||
import likeService from '../services/likes.service'; | ||
import Util from '../helpers/util'; | ||
|
||
const util = new Util(); | ||
/** | ||
* @author codepirates | ||
* @exports LikesController | ||
* @class ArticleController | ||
* @description Handles all related articles functioanlities | ||
* */ | ||
class LikesController { | ||
/** | ||
* @param {object} req - Request object | ||
* @param {object} res - Response object | ||
* @returns {object} response | ||
* @static | ||
*/ | ||
static async unlike(req, res) { | ||
try { | ||
if (req.body.likeId) { | ||
const prof = { | ||
status: 'neutral', | ||
claps: 0, | ||
}; | ||
const like = await likeService.updateLikes(prof, req.body.likeId); | ||
const newLike = { | ||
status: like[1].status, | ||
claps: like[1].claps, | ||
ArticleSlug: like[1].ArticleSlug, | ||
}; | ||
util.setSuccess(200, 'Successfully unliked', newLike); | ||
return util.send(res); | ||
} | ||
util.setError(401, 'you cant unlike article you did not like'); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req - Request object | ||
* @param {object} res - Response object | ||
* @returns {object} response | ||
* @static | ||
*/ | ||
static async dislike(req, res) { | ||
try { | ||
if (req.body.author === req.body.userId) { | ||
util.setError(401, 'You can not dislike to your own post'); | ||
return util.send(res); | ||
} | ||
if (!req.body.likeId) { | ||
const prof = { | ||
userId: req.body.userId, | ||
ArticleSlug: req.params.Article, | ||
status: 'dislike', | ||
claps: 0, | ||
}; | ||
const like = await likeService.createLikes(prof); | ||
const newLike = { | ||
status: like.status, | ||
claps: like.claps, | ||
ArticleSlug: like.ArticleSlug, | ||
}; | ||
util.setSuccess(200, 'Successfully disliked', newLike); | ||
return util.send(res); | ||
} | ||
const prof = { | ||
status: 'dislike', | ||
claps: 0, | ||
}; | ||
if (req.body.status === 'dislike') { | ||
prof.status = 'neutral'; | ||
} | ||
const like = await likeService.updateLikes(prof, req.body.likeId); | ||
const newLike = { | ||
status: like[1].status, | ||
claps: like[1].claps, | ||
ArticleSlug: like[1].ArticleSlug, | ||
}; | ||
util.setSuccess(200, 'Successfully disliked', newLike); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req - Request object | ||
* @param {object} res - Response object | ||
* @returns {object} response | ||
* @static | ||
*/ | ||
static async clap(req, res) { | ||
try { | ||
if (req.body.author === req.body.userId) { | ||
util.setError(401, 'You can not clap to your own post'); | ||
return util.send(res); | ||
} | ||
if (req.body.likeId) { | ||
const prof = { | ||
status: 'like', | ||
claps: req.body.claps + 1, | ||
}; | ||
const like = await likeService.updateLikes(prof, req.body.likeId); | ||
const newLike = { | ||
status: like[1].status, | ||
claps: like[1].claps, | ||
ArticleSlug: like[1].ArticleSlug, | ||
}; | ||
util.setSuccess(200, 'Successfully claped', newLike); | ||
return util.send(res); | ||
} | ||
const prof = { | ||
userId: req.body.userId, | ||
ArticleSlug: req.params.Article, | ||
status: 'like', | ||
claps: 1, | ||
}; | ||
const like = await likeService.createLikes(prof); | ||
const newLike = { | ||
status: like.status, | ||
claps: like.claps, | ||
ArticleSlug: like.ArticleSlug, | ||
}; | ||
util.setSuccess(200, 'Successfully claped', newLike); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req - Request object | ||
* @param {object} res - Response object | ||
* @returns {object} response | ||
* @static | ||
*/ | ||
static async getDislikes(req, res) { | ||
try { | ||
const dislike = await likeService.getAllADislike(req.params.Article); | ||
if (dislike) { | ||
const data = { | ||
dislikes: dislike.count, | ||
}; | ||
util.setSuccess(200, 'Successfully dislike retrieved', data); | ||
return util.send(res); | ||
} | ||
} catch (error) { | ||
util.setError(400, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req - Request object | ||
* @param {object} res - Response object | ||
* @returns {object} response | ||
* @static | ||
*/ | ||
static async getClaps(req, res) { | ||
try { | ||
const claps = await likeService.getAllAClaps(req.params.Article); | ||
if (claps) { | ||
const data = { | ||
clapers: claps.count, | ||
claps: Object.values(claps)[1], | ||
}; | ||
util.setSuccess(200, 'Successfully claps retrieved', data); | ||
return util.send(res); | ||
} | ||
} catch (error) { | ||
util.setError(400, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
} | ||
|
||
export default LikesController; |
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,41 @@ | ||
import Util from '../helpers/util'; | ||
import models from '../models/index'; | ||
|
||
const util = new Util(); | ||
|
||
export default async (req, res, next) => { | ||
try { | ||
const user = await models.user.findOne({ where: { email: req.auth.email } }); | ||
if (!user) { | ||
util.setError(404, 'you are anonimous'); | ||
return util.send(res); | ||
} | ||
const userId = user.id; | ||
const ArticleSlug = req.params.Article; | ||
const post = await models.Article.findOne({ where: { slug: ArticleSlug } }); | ||
|
||
if (!post) { | ||
util.setError(404, 'post not found'); | ||
return util.send(res); | ||
} | ||
const like = await models.Likes.findOne({ where: { ArticleSlug, userId } }); | ||
if (!like) { | ||
req.body.author = post.authorId; | ||
req.body.userId = userId; | ||
req.body.likeId = null; | ||
req.body.claps = null; | ||
req.body.author = post.authorId; | ||
next(); | ||
} else { | ||
req.body.author = post.authorId; | ||
req.body.likeId = like.id; | ||
req.body.claps = like.claps; | ||
req.body.userId = userId; | ||
req.body.status = like.status; | ||
next(); | ||
} | ||
} catch (error) { | ||
util.setError(500, 'server error'); | ||
return util.send(res); | ||
} | ||
}; |
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,54 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Likes', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
ArticleSlug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
} | ||
}, | ||
claps: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
defaultValue: 0 | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
status: { | ||
type: Sequelize.ENUM, | ||
values: ['like', 'dislike', 'neutral'], | ||
defaultValue: 'neutral' | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Likes'); | ||
} | ||
}; | ||
|
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,25 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Likes = sequelize.define('Likes', { | ||
ArticleSlug: DataTypes.STRING, | ||
userId: DataTypes.INTEGER, | ||
claps: DataTypes.INTEGER, | ||
status: DataTypes.ENUM({ | ||
values: ['like', 'dislike', 'neutral'] | ||
}) | ||
}, {}); | ||
Likes.associate = (models) => { | ||
Likes.belongsTo(models.user, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Likes.belongsTo(models.Article, { | ||
foreignKey: 'ArticleSlug', | ||
targetKey: 'slug', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
}; | ||
return Likes; | ||
}; |
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,17 @@ | ||
import express from 'express'; | ||
import LikesController from '../../../controllers/likes.controller'; | ||
import validateToken from '../../../middlewares/auth'; | ||
import confirmEmaiAuth from '../../../middlewares/emailVarification.middleware'; | ||
import isLike from '../../../middlewares/likes.middleware'; | ||
|
||
const router = express.Router(); | ||
// greate edit a viewing profile handlers | ||
|
||
router.get('/claps/:Article', [validateToken, confirmEmaiAuth, isLike], LikesController.getClaps); | ||
router.get('/dislikes/:Article', [validateToken, confirmEmaiAuth, isLike], LikesController.getDislikes); | ||
router.put('/clap/:Article', [validateToken, confirmEmaiAuth, isLike], LikesController.clap); | ||
router.put('/dislike/:Article', [validateToken, confirmEmaiAuth, isLike], LikesController.dislike); | ||
router.put('/unlike/:Article', [validateToken, confirmEmaiAuth, isLike], LikesController.unlike); | ||
|
||
|
||
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.