diff --git a/.DS_Store b/.DS_Store index cca1169..c3f3c1c 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/app/.DS_Store b/app/.DS_Store index 1331956..bdaaa57 100644 Binary files a/app/.DS_Store and b/app/.DS_Store differ diff --git a/app/controllers/msg-custom-template.controller.js b/app/controllers/msg-custom-template.controller.js new file mode 100644 index 0000000..55877a3 --- /dev/null +++ b/app/controllers/msg-custom-template.controller.js @@ -0,0 +1,94 @@ +const { splitSite } = require("../middlewares"); +const db = require("../models"); +const MsgCustomTemplate = db.msg_custom_template; + +// Create a new template +exports.create = (req, res) => { + if (!req.body.title) { + res.status(400).send({ message: "Title can not be empty!" }); + return; + } + const site = splitSite.findSiteNumber(req); + const template = new MsgCustomTemplate({ + title: req.body.title, + chinese: req.body.chinese || '', + english: req.body.english || '', + status: req.body.status || 'active', + create_by: req.body.create_by, + create_date: req.body.create_date || new Date(), + edit_by: req.body.edit_by, + edit_date: req.body.edit_date, + site + }); + template.save(template) + .then(data => { + res.send(data); + }) + .catch(err => { + res.status(500).send({ + message: err.message || "Some error occurred while creating the template." + }); + }); +}; + +// Retrieve all templates +exports.getAll = (req, res) => { + var condition = {}; + condition = splitSite.splitSiteGet(req, condition); + MsgCustomTemplate.find(condition) + .then(data => { + res.send(data); + }) + .catch(err => { + res.status(500).send({ + message: err.message || "Some error occurred while retrieving templates." + }); + }); +}; + +// Get one template by id +exports.getOne = (req, res) => { + const id = req.params.id; + MsgCustomTemplate.findById(id) + .then(data => { + if (!data) + res.status(404).send({ message: "Not found template with id " + id }); + else res.send(data); + }) + .catch(err => { + res.status(500).send({ message: "Error retrieving template with id=" + id }); + }); +}; + +// Update a template by id +exports.update = (req, res) => { + if (!req.body) { + return res.status(400).send({ message: "Data to update can not be empty!" }); + } + const id = req.params.id; + MsgCustomTemplate.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) + .then(data => { + if (!data) { + res.status(404).send({ message: `Cannot update template with id=${id}.` }); + } else res.send({ success: true, message: "Template was updated successfully." }); + }) + .catch(err => { + res.status(500).send({ success: false, message: "Error updating template with id=" + id }); + }); +}; + +// Delete a template by id +exports.remove = (req, res) => { + const id = req.params.id; + MsgCustomTemplate.findByIdAndRemove(id) + .then(data => { + if (!data) { + res.status(404).send({ message: `Cannot delete template with id=${id}.` }); + } else { + res.send({ message: "Template was deleted successfully!" }); + } + }) + .catch(err => { + res.status(500).send({ message: "Could not delete template with id=" + id }); + }); +}; diff --git a/app/models/index.js b/app/models/index.js index 7485788..65ac962 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -35,4 +35,5 @@ db.attendance_note = require("./attendance-note.model")(mongoose); db.carousel = require("./carousel.model")(mongoose); db.fingerprint_attendance = require("./fingerprint-attendance.model")(mongoose); db.dailyRoutesTemplate = require("./daily-routes-template.model")(mongoose); +db.msg_custom_template = require("./msg-custom-template.model")(mongoose); module.exports = db; \ No newline at end of file diff --git a/app/models/msg-custom-template.model.js b/app/models/msg-custom-template.model.js new file mode 100644 index 0000000..01bc79f --- /dev/null +++ b/app/models/msg-custom-template.model.js @@ -0,0 +1,23 @@ +module.exports = mongoose => { + var schema = mongoose.Schema( + { + title: String, + chinese: String, + english: String, + status: String, + create_by: String, + create_date: Date, + edit_by: String, + edit_date: Date, + site: Number + }, + { collection: 'msg_custom_template', timestamps: true } + ); + schema.method("toJSON", function() { + const { __v, _id, ...object } = this.toObject(); + object.id = _id; + return object; + }); + const MsgCustomTemplate = mongoose.model("msg_custom_template", schema); + return MsgCustomTemplate; +}; diff --git a/app/routes/msg-custom-template.routes.js b/app/routes/msg-custom-template.routes.js new file mode 100644 index 0000000..1575cc2 --- /dev/null +++ b/app/routes/msg-custom-template.routes.js @@ -0,0 +1,19 @@ +const { authJwt } = require("../middlewares"); + +module.exports = app => { + const templates = require("../controllers/msg-custom-template.controller.js"); + app.use((req, res, next) => { + res.header( + "Access-Control-Allow-Headers", + "x-access-token, Origin, Content-Type, Accept" + ); + next(); + }); + var router = require("express").Router(); + router.get("/", [authJwt.verifyToken], templates.getAll); + router.post("/", [authJwt.verifyToken], templates.create); + router.get("/:id", [authJwt.verifyToken], templates.getOne); + router.put("/:id", [authJwt.verifyToken], templates.update); + router.delete("/:id", [authJwt.verifyToken], templates.remove); + app.use('/api/msg-custom-templates', router); +}; diff --git a/app/views/asset-manifest.json b/app/views/asset-manifest.json index 56aa689..a0d5942 100644 --- a/app/views/asset-manifest.json +++ b/app/views/asset-manifest.json @@ -1,16 +1,16 @@ { "files": { "main.css": "/static/css/main.6616f3cb.css", - "main.js": "/static/js/main.60ebc889.js", + "main.js": "/static/js/main.8452576d.js", "static/js/787.c4e7f8f9.chunk.js": "/static/js/787.c4e7f8f9.chunk.js", "static/media/landing.png": "/static/media/landing.d4c6072db7a67dff6a78.png", "index.html": "/index.html", "main.6616f3cb.css.map": "/static/css/main.6616f3cb.css.map", - "main.60ebc889.js.map": "/static/js/main.60ebc889.js.map", + "main.8452576d.js.map": "/static/js/main.8452576d.js.map", "787.c4e7f8f9.chunk.js.map": "/static/js/787.c4e7f8f9.chunk.js.map" }, "entrypoints": [ "static/css/main.6616f3cb.css", - "static/js/main.60ebc889.js" + "static/js/main.8452576d.js" ] } \ No newline at end of file diff --git a/app/views/index.html b/app/views/index.html index c9065c5..07f8ede 100644 --- a/app/views/index.html +++ b/app/views/index.html @@ -1 +1 @@ -
| ".concat(e.label," | ")})).join(""),"\n
|---|
| ".concat(t[e.key]||""," | ")})).join(""),"\n