Skip to content

Commit

Permalink
Merge f1644c2 into debae5b
Browse files Browse the repository at this point in the history
  • Loading branch information
Proception committed Jan 18, 2019
2 parents debae5b + f1644c2 commit e56a80b
Show file tree
Hide file tree
Showing 15 changed files with 636 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"postinstall": "npm run build",
"db:migrate": "sequelize db:migrate",
"db:unmigrate": "sequelize db:migrate:undo:all",
"test": "NODE_ENV=test npm run db:unmigrate && NODE_ENV=test npm run db:migrate && NODE_ENV=test nyc mocha --exit --timeout 100000 --require @babel/register ./test/*",
"test": "NODE_ENV=test npm run db:unmigrate && NODE_ENV=test npm run db:migrate && sequelize db:seed:all && NODE_ENV=test nyc mocha --exit --timeout 100000 --require @babel/register ./test/*",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "./node_modules/.bin/eslint ."
},
Expand Down
114 changes: 114 additions & 0 deletions src/controllers/RatingController.js
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;
32 changes: 32 additions & 0 deletions src/db/migrations/20190117014046-create-rate-summary.js
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')
};
5 changes: 5 additions & 0 deletions src/db/models/art.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ module.exports = (sequelize, DataTypes) => {
Art.hasOne(models.Transaction, {
foreignKey: 'artId'
});
Art.hasOne(models.RateSummary, {
foreignKey: 'artId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
};
return Art;
};
15 changes: 15 additions & 0 deletions src/db/models/ratesummary.js
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;
};
37 changes: 37 additions & 0 deletions src/db/seeders/a_rateSeeder.js
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, {})
};
9 changes: 9 additions & 0 deletions src/db/seeders/b_rateSeeder.js
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, {})
};
25 changes: 25 additions & 0 deletions src/db/seeders/c_rateSeeder.js
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, {})
};
4 changes: 2 additions & 2 deletions src/db/seeders/userSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = {
userType: 'artist',
signUpType: 'local',
isVerified: true,
password: bcrypt.hashSync(process.env.PASSWORD, bcrypt.genSaltSync(8)),
email: process.env.EMAIL_SEED,
password: bcrypt.hashSync('vbvbbvhhdfjhdf', bcrypt.genSaltSync(8)),
email: 'email@ok.com',
createdAt: new Date(),
updatedAt: new Date()
}], {}),
Expand Down
53 changes: 53 additions & 0 deletions src/db/service/rate.js
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;
},
};
2 changes: 1 addition & 1 deletion src/helpers/TokenAuthenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TokenAuthenticate {
* @memberof TokenAuthenticate
*/
static async tokenVerify(req, res, next) {
const token = req.headers.Authorization || req.headers['x-access-token'] || req.query.token || req.body.token;
const token = req.headers.authorization || req.headers['x-access-token'] || req.query.token || req.body.token;
if (!token) {
return res.status(401).send({
status: 'error',
Expand Down
Loading

0 comments on commit e56a80b

Please sign in to comment.