-
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
12 changed files
with
665 additions
and
5 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
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; |
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,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'), | ||
}; |
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
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
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,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; | ||
}, | ||
}; |
Oops, something went wrong.