-
Notifications
You must be signed in to change notification settings - Fork 1
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
602 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
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,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
35
src/db/migrations/20190124103119-create-dislike-summary.js
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,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'); | ||
} | ||
}; |
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,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; | ||
}; |
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,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; | ||
}; |
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,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; |
Oops, something went wrong.