feat: Currencies CRUD.

This commit is contained in:
Ahmed Bouhuolia
2020-04-19 18:26:22 +02:00
parent 090c744f57
commit 8c8ec1534e
5 changed files with 321 additions and 13 deletions

View File

@@ -243,6 +243,13 @@ factory.define('option', 'options', async () => {
};
});
factory.define('currency', 'currencies', async () => {
return {
currency_name: faker.lorem.slug(),
currency_code: 'USD',
};
});
factory.define('budget', 'budgets', async () => {
return {
name: faker.lorem.slug(),

View File

@@ -0,0 +1,12 @@
exports.up = function(knex) {
return knex.schema.createTable('currencies', table => {
table.increments();
table.string('currency_name');
table.string('currency_code', 4);
})
};
exports.down = function(knex) {
return knex.schema.dropTableIfExists('currencies');
};

View File

@@ -1,45 +1,136 @@
import express from 'express';
import { check, validationResult } from 'express-validator';
import { check, param, validationResult } from 'express-validator';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import Currency from '@/models/Currency';
import jwtAuth from '@/http/middleware/jwtAuth';
export default {
router() {
const router = express.Router();
router.use(jwtAuth);
router.get('/all',
router.get('/',
this.all.validation,
asyncMiddleware(this.all.handler));
router.get('/registered',
this.registered.validation,
asyncMiddleware(this.registered.handler));
router.post('/',
this.newCurrency.validation,
asyncMiddleware(this.newCurrency.handler));
router.post('/:id',
this.editCurrency.validation,
asyncMiddleware(this.editCurrency.handler));
router.delete('/:currency_code',
this.deleteCurrecy.validation,
asyncMiddleware(this.deleteCurrecy.handler));
return router;
},
/**
* Retrieve all registered currency details.
*/
all: {
validation: [],
async handler(req, res) {
const currencies = await Currency.query();
return res.status(200).send({
currencies: [
{ currency_code: 'USD', currency_sign: '$' },
{ currency_code: 'LYD', currency_sign: '' },
...currencies,
],
});
},
},
registered: {
validation: [],
newCurrency: {
validation: [
check('currency_name').exists().trim().escape(),
check('currency_code').exists().trim().escape(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const form = { ...req.body };
const foundCurrency = await Currency.query()
.where('currency_code', form.currency_code);
if (foundCurrency.length > 0) {
return res.status(400).send({
errors: [{ type: 'CURRENCY.CODE.ALREADY.EXISTS', code: 100 }],
});
}
await Currency.query()
.insert({ ...form });
return res.status(200).send({
currencies: [
{ currency_code: 'USD', currency_sign: '$' },
{ currency_code: 'LYD', currency_sign: '' },
],
currency: { ...form },
});
},
},
deleteCurrecy: {
validation: [
param('currency_code').exists().trim().escape(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const { currency_code: currencyCode } = req.params;
await Currency.query()
.where('currency_code', currencyCode)
.delete();
return res.status(200).send({ currency_code: currencyCode });
},
},
editCurrency: {
validation: [
param('id').exists().isNumeric().toInt(),
check('currency_name').exists().trim().escape(),
check('currency_code').exists().trim().escape(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const form = { ...req.body };
const { id } = req.params;
const foundCurrency = await Currency.query()
.where('currency_code', form.currency_code)
.whereNot('id', id);
if (foundCurrency.length > 0) {
return res.status(400).send({
errors: [{ type: 'CURRENCY.CODE.ALREADY.EXISTS', code: 100 }],
});
}
await Currency.query()
.where('id', id)
.update({ ...form });
return res.status(200).send({
currency: { ...form },
});
},
},

View File

@@ -0,0 +1,10 @@
import BaseModel from '@/models/Model';
export default class Currency extends BaseModel {
/**
* Table name
*/
static get tableName() {
return 'currencies';
}
}