Skip to content

Commit

Permalink
Merge 466c0a0 into f0ffb70
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellamarr committed Jan 28, 2019
2 parents f0ffb70 + 466c0a0 commit e9ace92
Show file tree
Hide file tree
Showing 13 changed files with 602 additions and 11 deletions.
157 changes: 156 additions & 1 deletion src/controllers/ArtsController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sequelize from 'sequelize';
import models from '../db/models';
import { Response, Slugify } from '../helpers/index';
import LikeUnlike from '../db/service/LikeUnlike';
import DisikeUndislike from '../db/service/DislikeUndislike';

const {
Art, Media, Category, User, Comment
Art, Media, Category, User, Comment, Like, Dislike
} = models;


Expand Down Expand Up @@ -417,6 +419,159 @@ class ArtsController {
return res.status(response.code).json(response);
}
}

/**
* @static
* @desc POST /api/v1/arts/:artId/like
* @param {object} req
* @param {object} res
* @memberof ArtsController
* @returns {object} successful like
*/
static async likeArticle(req, res) {
try {
const { artId } = req.params;
const { id } = req.verifyUser;

const like = await Like.findOrCreate({
where: {
artId,
userId: id
},
})
.spread((user, created) => {
user.get({ plain: true });
return created;
});

if (!like) {
await Like.destroy({
where: {
artId,
userId: id
},
});

LikeUnlike.unlike(artId);

const response = new Response(
'Ok',
200,
`You just unliked article ${artId}`,
);
return res.status(response.code).json(response);
}

LikeUnlike.like(artId);

const checkIfDisliked = await Like.findOne({
where: {
artId,
userId: id
},
});

if (checkIfDisliked) {
await Dislike.destroy({
where: {
artId,
userId: id
},
});
DisikeUndislike.undislike(artId);
}

const response = new Response(
'Ok',
201,
`You just liked article ${artId}`,
);
return res.status(response.code).json(response);
} catch (err) {
const response = new Response(
'Internal server error',
500,
`${err}`,
);
return res.status(response.code).json(response);
}
}

/**
* @static
* @desc POST /api/v1/arts/:artId/dislike
* @param {object} req
* @param {object} res
* @memberof ArtsController
* @returns {object} successful dislike
*/
static async dislikeArticle(req, res) {
try {
const { artId } = req.params;
const { id } = req.verifyUser;

const dislike = await Dislike.findOrCreate({
where: {
artId,
userId: id
},
})
.spread((user, created) => {
user.get({ plain: true });
return created;
});

if (!dislike) {
await Dislike.destroy({
where: {
artId,
userId: id
},
});

DisikeUndislike.undislike(artId);

const response = new Response(
'Ok',
200,
`You just undisliked article ${artId}`,
);
return res.status(response.code).json(response);
}

DisikeUndislike.dislike(artId);
const checkIfLiked = await Like.findOne({
where: {
artId,
userId: id
},
});

if (checkIfLiked) {
await Like.destroy({
where: {
artId,
userId: id
},
});
LikeUnlike.unlike(artId);
}

const response = new Response(
'Ok',
201,
`You just disliked article ${artId}`,
);
return res.status(response.code).json(response);
} catch (err) {
const response = new Response(
'Internal server error',
500,
`${err}`,
);
return res.status(response.code).json(response);
}
}
}

export default ArtsController;
42 changes: 42 additions & 0 deletions src/db/migrations/20190124101623-create-dislike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Dislikes', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
artId: {
type: Sequelize.INTEGER,
references: {
model: 'Arts',
key: 'id',
as: 'artId',
}
},
userId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'userId',
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Dislikes');
}
};
35 changes: 35 additions & 0 deletions src/db/migrations/20190124103119-create-dislike-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('DislikeSummaries', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
artId: {
type: Sequelize.INTEGER,
references: {
model: 'Arts',
key: 'id',
as: 'artId',
}
},
noOfLikes: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('DislikeSummaries');
}
};
6 changes: 6 additions & 0 deletions src/db/models/art.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ module.exports = (sequelize, DataTypes) => {
Art.hasMany(models.Like, {
foreignKey: 'artId'
});
Art.hasMany(models.Dislike, {
foreignKey: 'artId'
});
Art.hasOne(models.LikeSummary, {
foreignKey: 'artId'
});
Art.hasOne(models.DislikeSummary, {
foreignKey: 'artId'
});
Art.hasOne(models.Transaction, {
foreignKey: 'artId'
});
Expand Down
21 changes: 21 additions & 0 deletions src/db/models/dislike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Dislike = sequelize.define('Dislike', {
artId: DataTypes.INTEGER,
userId: DataTypes.INTEGER
}, {});
Dislike.associate = function(models) {
// associations can be defined here
Dislike.belongsTo(models.Art, {
foreignKey: 'artId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
Dislike.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
};
return Dislike;
};
16 changes: 16 additions & 0 deletions src/db/models/dislikesummary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const DislikeSummary = sequelize.define('DislikeSummary', {
artId: DataTypes.INTEGER,
noOfLikes: DataTypes.INTEGER
}, {});
DislikeSummary.associate = function (models) {
// associations can be defined here
DislikeSummary.belongsTo(models.Art, {
foreignKey: 'artId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
};
return DislikeSummary;
};
3 changes: 3 additions & 0 deletions src/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module.exports = (sequelize, DataTypes) => {
User.hasMany(models.Like, {
foreignKey: 'userId'
});
User.hasMany(models.Dislike, {
foreignKey: 'userId'
});
User.hasMany(models.Comment, {
foreignKey: 'userId'
});
Expand Down
61 changes: 61 additions & 0 deletions src/db/service/DislikeUndislike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable valid-jsdoc */
import models from '../models';

const { Dislike, DislikeSummary } = models;

/**
* LikeUnlike Service
*/
class DisikeUndislike {
/**
* @static
* @param {number} req
* @memberof DisikeUndislike
* @returns updated details | new detail
*/
static async dislike(id) {
const countDislike = await Dislike.count({
where: {
artId: id,
}
});
const findDislikeSummary = await DislikeSummary.findOne({
where: {
artId: id,
}
});
if (findDislikeSummary) {
const dislikes = findDislikeSummary.noOfLikes + 1;
findDislikeSummary.noOfLikes = dislikes;
findDislikeSummary.save();
} else {
DislikeSummary.create({
artId: id,
noOfLikes: countDislike,
});
}
}

/**
* @static
* @param {number} id
* @memberof DisikeUndislike
* @returns updated details | new detail
*/
static async undislike(id) {
const findDislikeSummary = await DislikeSummary.findOne({
where: {
artId: id,
}
});
if (findDislikeSummary) {
const dislikes = findDislikeSummary.noOfLikes - 1;
findDislikeSummary.noOfLikes = dislikes;
findDislikeSummary.save();
} else {
return false;
}
}
}

export default DisikeUndislike;
Loading

0 comments on commit e9ace92

Please sign in to comment.