Commit 3e455993 authored by Piyaphorn Arphornsri's avatar Piyaphorn Arphornsri

add db

parent 3783f660
...@@ -4,10 +4,70 @@ const config = require("../../config"); ...@@ -4,10 +4,70 @@ const config = require("../../config");
createToken = id => { createToken = id => {
return JWT.sign({ id }, config.JWT_SECRET); return JWT.sign({ id }, config.JWT_SECRET);
}; };
exports.list = async (req, res) => {
exports.addlist = async (req, res) => {
try {
const addlistData = req.body;
await List.create({
name: addlistData.name,
price: addlistData.price,
time: addlistData.time,
shop: addlistData.shop
});
res.send("create");
} catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.getList = async (req, res) => {
try {
let list = await List.findAll();
res.status(200).send(list);
} catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.deletelist = async (req, res) => {
try {
const listId = req.params.listId;
let list = await List.findOne({
where: {
id: listId
}
});
res.status(200).send(list.dataValues);
} catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.getListId = async (req, res) => {
try {
const listId = req.params.id;
let list = await List.findOne({
where: {
id: listId
}
});
res.status(200).send(list.dataValues);
} catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.updateList = async (req, res) => {
try { try {
const listData = req.body; const listData = req.body;
List.create(listData); const id = req.params.id;
await List.update(listData, { where: { id: id } });
res.status(200).send({ status: "done" });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.sendStatus(401); res.sendStatus(401);
......
...@@ -5,7 +5,7 @@ createToken = id => { ...@@ -5,7 +5,7 @@ createToken = id => {
return JWT.sign({ id }, config.JWT_SECRET); return JWT.sign({ id }, config.JWT_SECRET);
}; };
exports.Shop = async (req, res) => { exports.addShop = async (req, res) => {
try{ try{
const shopData =req.body; const shopData =req.body;
Shop.create(shopData); Shop.create(shopData);
......
const express = require("express"); const express = require("express");
const router = express.Router(); const router = express.Router();
const authController = require("../../controllers/list"); const listController = require("../../controllers/list");
router.post("/add", listController.addlist);
router.get('/all', listController.getList);
router.delete('/delete/:listId',listController.deletelist);
router.get('/getListId/:id',listController.getListId);
router.put('/updateList/id',listController.updateList);
router.post("/list", authController.list);
module.exports = router; module.exports = router;
...@@ -11,7 +11,7 @@ app.use(bodyParser.json()); ...@@ -11,7 +11,7 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.urlencoded({ extended: false }))
app.use('/api/auth', router.auth); app.use('/api/auth', router.auth);
app.use('/api/list', router.list); app.use('/api/list', router.list);
app.use('/api/shop', router.shop); //app.use('/api/shop', router.shop);
app.listen(port, () => { app.listen(port, () => {
console.log('Express server listening on port ' + port) console.log('Express server listening on port ' + port)
......
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