Skip to content

Commit

Permalink
feat(close #14): update blank template
Browse files Browse the repository at this point in the history
  • Loading branch information
phatpham9 committed Mar 9, 2018
1 parent 9c81525 commit 423dd40
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 162 deletions.
3 changes: 0 additions & 3 deletions templates/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# mysql config
MYSQL_URL=root:[email protected]:3306/{{name}}

# app config
PORT=9000
4 changes: 1 addition & 3 deletions templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
"joi": "^13.0.2",
"micro": "latest",
"micro-joi": "^3.1.0",
"microrouter": "^3.0.0",
"mysql2": "^1.5.1",
"sequelize": "^4.23.0"
"microrouter": "^3.0.0"
},
"license": "MIT",
"devDependencies": {
Expand Down
11 changes: 0 additions & 11 deletions templates/src/config/mysql.js

This file was deleted.

3 changes: 0 additions & 3 deletions templates/src/helpers/array-diff.js

This file was deleted.

11 changes: 0 additions & 11 deletions templates/src/helpers/mysql.js

This file was deleted.

134 changes: 32 additions & 102 deletions templates/src/{{name}}.controller.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,66 @@
const { json } = require('micro');

const arrayDiff = require('./helpers/array-diff');
const {{name}}Model = require('./helpers/mysql').load('{{name}}');

const BLACK_LIST = ['isDeleted'];
const items = [{
id: Date.now().toString(),
name: 'foo',
}];

const getList = async (req, res) => {
const select = req.query.select
? arrayDiff(req.query.select.split(','), BLACK_LIST)
: ['name', 'description', 'createdAt', 'updatedAt'];
const limit = req.query.limit ? +req.query.limit : 25;
const {
page = 0,
skip = page && page > 0 ? (page - 1) * limit : 0,
search,
name,
} = req.query;
const sort = req.query.sort ? req.query.sort.replace('-', '') : 'name';
const sortDir = req.query.sort && req.query.sort.charAt(0) === '-' ? 'DESC' : 'ASC';

const query = {
isDeleted: false,
};

if (name) {
query.name = name;
}

if (search) {
const q = `%${search}%`;
query.$or = [{
name: {
$like: q,
},
}, {
description: {
$like: q,
},
}];
}


const populate = [];

const {{name}}s = await {{name}}Model.findAndCountAll({
raw: true,
where: query,
offset: skip,
limit,
attributes: select,
order: [[sort, sortDir]],
include: populate,
});

res.send({{name}}s);
res.send(items);
};

const getDetails = async (req, res) => {
const { id } = req.params;
const {{name}} = await {{name}}Model.findOne({
where: {
id,
isDeleted: false,
},
raw: true,
});

if (!{{name}}) {
return res.sendNotFoundError(new Error('{{name}} not found'));

const foundItem = items.find(item => item.id === id);

if (!foundItem) {
return res.sendNotFoundError();
}

return res.send({{name}});
return res.send(foundItem);
};

const create = async (req, res) => {
const { name, description } = await json(req);
try {
const {{name}} = await {{name}}Model.create({
name,
description,
});

res.send({{name}});
} catch (err) {
res.sendServerError(err);
}
const { name } = await json(req);

const newItem = {
id: Date.now().toString(),
name,
};

items.push(newItem);

res.send(newItem);
};

const update = async (req, res) => {
const { id } = req.params;
const { name, description } = await json(req);
const { name } = await json(req);

const {{name}} = await {{name}}Model.findOne({
where: {
id,
isDeleted: false,
},
});
const foundItem = items.find(item => item.id === id);

if (!{{name}}) {
return res.sendNotFoundError(new Error('{{name}} not found'));
if (!foundItem) {
return res.sendNotFoundError();
}

await {{name}}.update({
name,
description,
updatedAt: new Date(),
});
foundItem.name = name;

return res.send({{name}});
return res.send(foundItem);
};

const remove = async (req, res) => {
const { id } = req.params;

const {{name}} = await {{name}}Model.findOne({
where: {
id,
isDeleted: false,
},
});
const foundIndex = items.findIndex(item => item.id === id);

if (!{{name}}) {
return res.sendNotFoundError(new Error('{{name}} not found'));
if (foundIndex === -1) {
return res.sendNotFoundError();
}

await {{name}}.update({
updatedAt: new Date(),
isDeleted: true,
});
const removedItem = items.splice(foundIndex, 1)[0];

return res.send({{name}});
return res.send(removedItem);
};

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion templates/src/{{name}}.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const Validation = require('micro-joi');

const validate = Validation(Joi.object({
name: Joi.string().required(),
description: Joi.string().required(),
}));

module.exports = {
Expand Down
28 changes: 0 additions & 28 deletions templates/src/{{name}}.model.js

This file was deleted.

0 comments on commit 423dd40

Please sign in to comment.