Commit 25995fc8 authored by dangdoan's avatar dangdoan

fix readme

parent 167417f7
## Project use nodejs version 15.0.1 and mysql in XAMPP##
## Generate data base ##
####
execute query in file mysql.sql
execute query in file mysql.sql.
####
## Install package ##
```npm install```
......
const ProductProvider = require('../provider/productProvider');
const { sendOK, sendError } = require('../helper/response');
class product {
constructor() {
this.producProvider = new ProductProvider()
}
init(router) {
const self = this;
router
.route("")
.get(async function (req, res, next) {
await self.getProducts(req, res, next);
});
router
.route("/:code")
.get(async function (req, res, next) {
await self.getByCode(req, res, next);
});
router
.route("")
.post(async function (req, res, next) {
await self.createProduct(req, res, next);
});
router
.route("/:code")
.put(async function (req, res, next) {
await self.updateProduct(req, res, next);
});
router
.route("/:code")
.delete(async function (req, res, next) {
await self.deleteProduct(req, res, next);
});
}
async getProducts(req, res, next) {
const self = this;
try {
const currentPage = parseInt(req.query.page) || 1
const pageSize = parseInt(req.query.size) || 10
// sort & dir
let sort = req.query.sort || "id"
let dir = req.query.dir || "asc"
const lstResult = await self.producProvider.getAll(currentPage, pageSize, sort, dir);
lstResult['totalPages'] = Math.ceil(lstResult['count'] / pageSize);
lstResult['currentPage'] = currentPage
return res.status(200).json(sendOK(lstResult));
} catch (error) {
res.status(500).json(sendError(error.message))
}
}
async getByCode(req, res, next) {
const self = this;
try {
const result = await self.producProvider.getByCode(req.params);
return res.status(200).json(sendOK(result));
} catch (error) {
res.status(500).json(sendError(error.message))
}
}
async createProduct(req, res, next) {
const self = this;
try {
let body = req.body;
body.createdAt = new Date;
const result = await self.producProvider.post(body);
return res.status(200).json(sendOK(result));
} catch (error) {
res.status(500).json(sendError(error.message))
}
}
async updateProduct(req, res, next) {
const self = this;
try {
let body = req.body;
body.updatedAt = new Date;
const result = await self.producProvider.put(body, req.params.code);
if (result == 1)
return res.status(200).json(sendOK(result));
else
res.status(500).json(sendError("update fail"));
} catch (error) {
res.status(500).json(sendError(error.message))
}
}
async deleteProduct(req, res, next) {
const self = this;
try {
const result = await self.producProvider.delete(req.params.code);
if (result == 1)
return res.status(200).json(sendOK(result));
else
res.status(500).json(sendError("delete fail"))
} catch (error) {
res.status(500).json(sendError(error.message));
}
}
}
module.exports = product;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment