Skip to content

Commit

Permalink
Merge 0c11336 into 7dfff99
Browse files Browse the repository at this point in the history
  • Loading branch information
Proception committed Jan 29, 2019
2 parents 7dfff99 + 0c11336 commit e436742
Show file tree
Hide file tree
Showing 12 changed files with 665 additions and 5 deletions.
112 changes: 112 additions & 0 deletions src/controllers/TransactionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import Response from '../helpers/response';
import transaction from '../db/service/transaction';

/** Transaction Controller Class */
class TransactionController {
/**
* @static
* @desc POST /api/v1/transaction/purchase/:item
* @param {object} req
* @param {object} res
* @memberof TransactionController
* @returns {object} res
*/
static async purchaseItem(req, res) {
const { artId } = req.params;
const { artistId, price } = req.body;
const { id } = req.verifyUser;
try {
const purchaseItemResponse = await transaction
.saveTransaction(artId, artistId, id, price);
if (purchaseItemResponse) {
const response = new Response(
'Created',
201,
'Art has been purchased',
purchaseItemResponse
);
return res.status(response.code).json(response);
}
} catch (error) {
const response = new Response(
'Internal Server Error',
500,
`${error}`
);
return res.status(response.code).json(response);
}
}

/**
* @static
* @desc GET /api/v1/transaction/receipt/:item
* @param {object} req
* @param {object} res
* @memberof TransactionController
* @returns {object} res
*/
static async getItemReceipt(req, res) {
const { artId } = req.params;

try {
const getItemReceiptResponse = await transaction.getItemReceipt(artId);
if (getItemReceiptResponse) {
const response = new Response(
'Ok',
200,
'Receipt was found',
getItemReceiptResponse
);
return res.status(response.code).json(response);
}
} catch (error) {
const response = new Response(
'Not Ok',
500,
`${error}`
);
return res.status(response.code).json(response);
}
}

/**
* @static
* @desc GET /api/v1/transaction
* @param {object} req
* @param {object} res
* @memberof TransactionController
* @returns {object} res
*/
static async getTransactions(req, res) {
const { id } = req.verifyUser;
try {
const allUserTransactions = await transaction.getTransactions(id);
if (allUserTransactions) {
const response = new Response(
'Ok',
200,
`${allUserTransactions.length} transaction(s) found`,
allUserTransactions
);
return res.status(response.code).json(response);
}
if (!allUserTransactions) {
const response = new Response(
'Not found',
404,
`${allUserTransactions.length} transaction(s) found`,
allUserTransactions
);
return res.status(response.code).json(response);
}
} catch (error) {
const response = new Response(
'Internal Server Error',
500,
`${error}`
);
return res.status(response.code).json(response);
}
}
}
export default TransactionController;
2 changes: 1 addition & 1 deletion src/db/migrations/20190115154728-create-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
}
},
amount: {
type: Sequelize.INTEGER
type: Sequelize.DECIMAL(10, 2),
},
createdAt: {
allowNull: false,
Expand Down
8 changes: 8 additions & 0 deletions src/db/migrations/20190126225837-add-column-price-to-art.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn('Arts', 'price', {
type: Sequelize.DECIMAL(10, 2),
defaultValue: 0.00,
after: 'featuredImg'
}),
down: queryInterface => queryInterface.removeColumn('Arts', 'price'),
};
1 change: 1 addition & 0 deletions src/db/models/art.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = (sequelize, DataTypes) => {
description: DataTypes.TEXT,
categoryId: DataTypes.INTEGER,
featuredImg: DataTypes.STRING,
price: DataTypes.DECIMAL(10, 2),
status: DataTypes.BOOLEAN
}, {});
Art.associate = function (models) {
Expand Down
5 changes: 4 additions & 1 deletion src/db/models/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ module.exports = (sequelize, DataTypes) => {
buyerId: DataTypes.INTEGER,
sellerId: DataTypes.INTEGER,
artId: DataTypes.INTEGER,
amount: DataTypes.INTEGER
amount: DataTypes.DECIMAL(10, 2),
}, {});
Transaction.associate = function (models) {
// associations can be defined here
Transaction.belongsTo(models.User, {
foreignKey: 'sellerId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
as: 'seller'
});
Transaction.belongsTo(models.User, {
foreignKey: 'buyerId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
as: 'buyer'
});
Transaction.belongsTo(models.Art, {
foreignKey: 'artId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
as: 'art'
});
};
return Transaction;
Expand Down
4 changes: 3 additions & 1 deletion src/db/seeders/c_artSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = {
description: 'Some descriptions',
categoryId: 1,
featuredImg: 'some link',
status: true,
status: false,
price: 2000,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -18,6 +19,7 @@ module.exports = {
categoryId: 1,
featuredImg: 'some link',
status: true,
price: 3000,
createdAt: new Date(),
updatedAt: new Date()
}
Expand Down
2 changes: 2 additions & 0 deletions src/db/seeders/c_rateSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
categoryId: 1,
featuredImg: 'www.imageurl.com/myImage',
status: true,
price: 6000,
createdAt: new Date(),
updatedAt: new Date()
}, {
Expand All @@ -17,6 +18,7 @@ module.exports = {
categoryId: 1,
featuredImg: 'www.imageurl.com/myImage',
status: true,
price: 9000,
createdAt: new Date(),
updatedAt: new Date()
}], {}),
Expand Down
101 changes: 101 additions & 0 deletions src/db/service/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import models from '../models';

const {
Transaction,
Art,
User,
} = models;

module.exports = {
async getItemReceipt(artId) {
const query = await Transaction
.findOne({
include: [
{
model: User,
as: 'buyer',
attributes: ['id', 'username'],
},
{
model: User,
as: 'seller',
attributes: ['id', 'username'],
},
{
model: Art,
as: 'art',
attributes: [
'id',
'title',
'description',
'featuredImg',
'price',
'createdAt'
],
}
],
attributes: ['id', 'amount', 'createdAt'],
where: { artId }
});
return query;
},
async saveTransaction(artId, artistId, userId, amount) {
const query = await Transaction.create({
artId,
sellerId: artistId,
buyerId: userId,
amount
});

if (query) {
/** Check if Art exists, if so update status for unavailability */
const artExists = await Art.findOne({ where: { id: artId } });
if (artExists) {
artExists.status = false;
artExists.save();
}
}
return query;
},
async getTransactions(userId) {
const query = await Transaction.findAll({
include: [
{
model: User,
as: 'buyer',
attributes: ['id', 'username'],
},
{
model: User,
as: 'seller',
attributes: ['id', 'username'],
},
{
model: Art,
as: 'art',
attributes: [
'id',
'title',
'description',
'featuredImg',
'price',
'createdAt'
],
}
],
attributes: ['id', 'amount', 'createdAt'],
where: {
$or: [
{
buyerId: userId
},
{
sellerId: userId
}
]
},
order: [['createdAt', 'DESC']]
});
return query;
},
};
Loading

0 comments on commit e436742

Please sign in to comment.