Commit d94ba383 authored by Piyaphorn Arphornsri's avatar Piyaphorn Arphornsri

add db

parent 3e455993
const List = require("../../models/list"); const List = require("../../models/list");
const config = require("../../config"); const config = require("../../config");
createToken = id => {
return JWT.sign({ id }, config.JWT_SECRET);
};
exports.addlist = async (req, res) => { exports.addlist = async (req, res) => {
try { try {
const addlistData = req.body; const addlistData = req.body;
...@@ -25,7 +21,7 @@ exports.getList = async (req, res) => { ...@@ -25,7 +21,7 @@ exports.getList = async (req, res) => {
try { try {
let list = await List.findAll(); let list = await List.findAll();
res.status(200).send(list); res.status(200).send("success");
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.sendStatus(401); res.sendStatus(401);
...@@ -35,12 +31,12 @@ exports.getList = async (req, res) => { ...@@ -35,12 +31,12 @@ exports.getList = async (req, res) => {
exports.deletelist = async (req, res) => { exports.deletelist = async (req, res) => {
try { try {
const listId = req.params.listId; const listId = req.params.listId;
let list = await List.findOne({ await List.destroy({
where: { where: {
id: listId id: listId
} }
}); });
res.status(200).send(list.dataValues); res.status(200).send("success");
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.sendStatus(401); res.sendStatus(401);
...@@ -55,7 +51,7 @@ exports.getListId = async (req, res) => { ...@@ -55,7 +51,7 @@ exports.getListId = async (req, res) => {
id: listId id: listId
} }
}); });
res.status(200).send(list.dataValues); res.status(200).send("success");
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.sendStatus(401); res.sendStatus(401);
......
const Shop = require("../../models/shop"); const Shop = require("../../models/shop");
const config = require("../../config");
createToken = id => { exports.addShop = async (req, res) => {
return JWT.sign({ id }, config.JWT_SECRET); try {
const addshopData = req.body;
console.log(addshopData);
await Shop.create(addshopData);
res.send("create");
} catch (err) {
console.log(err);
res.sendStatus(401);
}
}; };
exports.addShop = async (req, res) => { exports.getShop = async (req, res) => {
try{ try {
const shopData =req.body; let shop = await Shop.findAll();
Shop.create(shopData); res.status(200).send("success");
}catch(err){ } catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.deleteshop = async (req, res) => {
try {
const shopId = req.params.shopId;
await Shop.destroy({
where: {
id: shopId
}
});
res.status(200).send("success");
} catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.getShopId = async (req, res) => {
try {
const shopId = req.params.id;
let shop = await Shop.findOne({
where: {
id: shopId
}
});
res.status(200).send("success");
} catch (err) {
console.log(err);
res.sendStatus(401);
}
};
exports.updateShop = async (req, res) => {
try {
const shopData = req.body;
const id = req.params.id;
await Shop.update(shopData, { where: { id: id } });
res.status(200).send({ status: "done" });
} catch (err) {
console.log(err); console.log(err);
res.sendStatus(401); res.sendStatus(401);
} }
......
...@@ -12,22 +12,22 @@ module.exports = db.sequelize.define("shops", { ...@@ -12,22 +12,22 @@ module.exports = db.sequelize.define("shops", {
type: Sequelize.INTEGER type: Sequelize.INTEGER
}, },
timeopen: { timeopen: {
type: Sequelize.TIME type: Sequelize.STRING
}, },
timeclose: { timeclose: {
type: Sequelize.TIME type: Sequelize.STRING
}, },
address: { address: {
type: Sequelize.STRING type: Sequelize.TEXT
}, },
tel: { tel: {
type: Sequelize.STRING type: Sequelize.STRING
}, },
detial: { detail: {
type: Sequelize.STRING type: Sequelize.TEXT
}, },
map: { map: {
type: Sequelize.STRING type: Sequelize.TEXT
}, },
type: { type: {
type: Sequelize.ENUM( type: Sequelize.ENUM(
......
const express = require("express"); const express = require("express");
const router = express.Router(); const router = express.Router();
const authController = require("../../controllers/shop"); const shopController = require("../../controllers/shop");
router.post("/add", shopController.addShop);
router.get('/all', shopController.getShop);
router.delete('/delete/:shopId',shopController.updateShop);
router.get('/getListId/:id',shopController.getShopId);
router.put('/updateList/id',shopController.updateShop);
router.post("/shop", authController.shop);
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