-
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
15 changed files
with
636 additions
and
4 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,114 @@ | ||
import ratingQuery from '../db/service/rate'; | ||
import Response from '../helpers/response'; | ||
|
||
|
||
let response; | ||
/** Rating Controller Class */ | ||
class RatingController { | ||
/** | ||
* @static | ||
* @desc POST /api/v1/rate/:item | ||
* @param {object} req | ||
* @param {object} res | ||
* @memberof RatingController | ||
* @returns {object} res | ||
*/ | ||
static async addItemRating(req, res) { | ||
const { artId } = req.params; | ||
const { verifyUser } = req; | ||
const { rating } = req.body; | ||
|
||
try { | ||
const addRatingResponse = await ratingQuery | ||
.addRating(artId, verifyUser.id, rating); | ||
if (addRatingResponse) { | ||
response = new Response( | ||
'Ok', | ||
201, | ||
'Rating has been added', | ||
addRatingResponse.dataValues | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} catch (error) { | ||
response = new Response( | ||
'Not Ok', | ||
500, | ||
`${error}` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @static | ||
* @desc GET /api/v1/rate/:item | ||
* @param {object} req | ||
* @param {object} res | ||
* @memberof RatingController | ||
* @returns {object} res | ||
*/ | ||
static async getItemRating(req, res) { | ||
const { artId } = req.params; | ||
|
||
try { | ||
const isRatingExists = await ratingQuery.getItemRating(artId); | ||
if (isRatingExists) { | ||
response = new Response( | ||
'Ok', | ||
200, | ||
'Art Rating Exists', | ||
isRatingExists.dataValues | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} catch (error) { | ||
response = new Response( | ||
'Not Ok', | ||
500, | ||
`${error}` | ||
); | ||
} | ||
response = new Response( | ||
'Ok', | ||
200, | ||
'Art Rating Does not exist', | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
|
||
/** | ||
* @static | ||
* @desc GET /api/v1/rate/:item/user | ||
* @param {object} req | ||
* @param {object} res | ||
* @memberof RatingController | ||
* @returns {object} res | ||
*/ | ||
static async getUserItemRating(req, res) { | ||
const { artId } = req.params; | ||
const { verifyUser } = req; | ||
const getUserItemRatingResponse = await ratingQuery | ||
.getUserItemRating(artId, verifyUser.id); | ||
|
||
if (getUserItemRatingResponse) { | ||
response = new Response( | ||
'Ok', | ||
200, | ||
`${verifyUser | ||
.username} Rated Art ID ${artId} : ${getUserItemRatingResponse | ||
.dataValues.rating}`, | ||
getUserItemRatingResponse.dataValues | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
response = new Response( | ||
'Ok', | ||
404, | ||
`${verifyUser.username} has no rating for Art ID: ${artId}`, | ||
getUserItemRatingResponse | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
|
||
export default RatingController; |
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,32 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('RateSummaries', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
artId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Arts', | ||
key: 'id', | ||
as: 'artId', | ||
} | ||
}, | ||
caculatedRate: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: queryInterface => queryInterface.dropTable('RateSummaries') | ||
}; |
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,15 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const RateSummary = sequelize.define('RateSummary', { | ||
artId: DataTypes.INTEGER, | ||
caculatedRate: DataTypes.INTEGER | ||
}, {}); | ||
RateSummary.associate = (models) => { | ||
// associations can be defined here | ||
RateSummary.belongsTo(models.Art, { | ||
foreignKey: 'artId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}); | ||
}; | ||
return RateSummary; | ||
}; |
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,37 @@ | ||
const bcrypt = require('bcryptjs'); | ||
const dotenv = require('dotenv'); | ||
|
||
dotenv.config(); | ||
/** Create seed user */ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Users', [{ | ||
username: 'Juliet', | ||
userType: 'artist', | ||
signUpType: 'local', | ||
isVerified: true, | ||
password: bcrypt.hashSync('femiok', bcrypt.genSaltSync(8)), | ||
email: 'julietezekwe@gmail.com', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, { | ||
username: 'Juliet', | ||
userType: 'artist', | ||
signUpType: 'local', | ||
isVerified: true, | ||
password: bcrypt.hashSync('femiok', bcrypt.genSaltSync(8)), | ||
email: 'createart@gmail.com', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, { | ||
username: 'Juliet', | ||
userType: 'artist', | ||
signUpType: 'local', | ||
isVerified: true, | ||
password: bcrypt.hashSync('femiok', bcrypt.genSaltSync(8)), | ||
email: 'mockuser@gmail.com', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}], {}), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Users', null, {}) | ||
}; |
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,9 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Categories', [{ | ||
categoryName: 1, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}], {}), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Categories', null, {}) | ||
}; |
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 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Arts', [{ | ||
artistId: 1, | ||
slug: 'painting-make-sense', | ||
title: 'My Painting', | ||
description: true, | ||
categoryId: 1, | ||
featuredImg: 'www.imageurl.com/myImage', | ||
status: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, { | ||
artistId: 2, | ||
slug: 'painting-make-sense', | ||
title: 'My Painting', | ||
description: true, | ||
categoryId: 1, | ||
featuredImg: 'www.imageurl.com/myImage', | ||
status: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}], {}), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Arts', null, {}) | ||
}; |
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,53 @@ | ||
import models from '../models'; | ||
|
||
const { | ||
Rate, | ||
Art, | ||
User, | ||
RateSummary | ||
} = models; | ||
|
||
module.exports = { | ||
async addRating(artId, userId, rating) { | ||
const query = await Rate.findOrCreate({ | ||
where: { | ||
artId, | ||
userId, | ||
}, | ||
defaults: { rating } | ||
}); | ||
const getSumRating = await Rate.sum('rating', { where: { artId } }); | ||
const getCountRating = await Rate.count({ where: { artId } }); | ||
const caculatedRating = (getSumRating / getCountRating); | ||
|
||
/** Check if rate summary exists, if so update */ | ||
const rateExists = await RateSummary.findOne({ where: { artId } }); | ||
if (rateExists) { | ||
rateExists.caculatedRate = caculatedRating; | ||
rateExists.save(); | ||
} else { | ||
RateSummary.create({ | ||
artId, | ||
caculatedRate: caculatedRating | ||
}); | ||
} | ||
|
||
return query; | ||
}, | ||
async getItemRating(artId) { | ||
const query = await RateSummary.findOne({ where: { artId } }); | ||
return query; | ||
}, | ||
async getUserItemRating(artId, userId) { | ||
const query = await Rate.findOne({ where: { userId, artId } }); | ||
return query; | ||
}, | ||
async getArt(artId) { | ||
const query = await Art.findOne({ where: { id: artId } }); | ||
return query; | ||
}, | ||
async getUser(userId) { | ||
const query = await User.count({ where: { id: userId } }); | ||
return query; | ||
}, | ||
}; |
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.