1. Models
-- Tạo file: ./src/app/models/TuThuoc.js
-- Cần cài đặt và import thư viện mongoose trước
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
const mongoose = require('mongoose'); const Schema = mongoose.Schema; const TuThuocSchema = new Schema({ TenThuoc: String, ThanhPhan: String, NhomThuoc: String, PhanLoai: { DangThuoc: String, //Viên,Ống,Gói,Lọ,Chai DuongDung: String, DongHop: Number, }, NguonGoc: { NuocSanXuat: String, NhaCungCap: Array, }, SoLuong: [{ SoHop: Number, SoLe: Number, GiaMua: Number, GiaBan: Number, HanSuDung: Number, }], createdAt: Number, updatedAt: Number, removedAt: Number, }, { timestamps: false, versionKey: false, } ); module.exports = mongoose.model('TuThuoc', TuThuocSchema); |
2. Controllers
**** Ở Controller có sử dụng rất nhiều Middleware để lấy dữ liệu. Cần lưu ý :sử dụng -body-parser để parser dữ liệu gửi lên từ Client:
-- Tại file ./src/server.js
1 2 |
app.use(express.json()); app.use(express.urlencoded({ extended: true })); |
-- Sau khi có model, -body-parser, ta tạo Controllers
-- Tạo file .src/app/controllers/collections/TuThuocController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
const Collection = require('../../models/TuThuoc'); class CollectionController { //--[GET]-- /list -- Get List -- list(req, res, next) { Collection.find({ removedAt: 0 }) .then(results => { res.json({ data: results }) }) .catch(err => next()) } //--[GET]-- /trash-list -- Get List Trash -- trashList(req, res, next) { Collection.find({ removedAt: { $ne: 0 } }) .then(results => res.json({ data: results })) .catch(err => next()) } //--[GET]-- /:id -- Get One By ID-- findOne(req, res, next) { Collection.findById(req.params.id) .then((results) => res.json(results)) .catch(next); } //--[POST]-- /add -- Add -- store(req, res, next) { Object.assign(req.body, { createdAt: Date.now(), updatedAt: Date.now(), removedAt: 0, }) const newData = new Collection(req.body); newData.save() .then((results) => res.json(results)) .catch(next); } //--[PUT]-- /:id -- Update By ID -- update(req, res, next) { Object.assign(req.body, { updatedAt: Date.now(), }) Collection.findByIdAndUpdate(req.params.id, req.body, { new: true }) .then((results) => res.json(results)) .catch(next); } // --[PATCH]-- /remove/:id -- Remove By ID -- remove(req, res, next) { Collection.findByIdAndUpdate(req.params.id, { removedAt: Date.now() }, { new: true }) .then((results) => res.json(results)) .catch(next) } //--[PATCH]-- /restore/:id -- Restore By ID -- restore(req, res, next) { Collection.findByIdAndUpdate(req.params.id, { removedAt: 0 }, { new: true }) .then((results) => res.json(results)) .catch(next) } //--[DELETE]-- /destroy/:id -- Destroy By ID -- destroy(req, res, next) { Collection.deleteOne({ _id: req.params.id }) .then((results) => res.json(results)) .catch(next); } // --[PATCH]-- /remove/:id -- Remove By ID -- removeList(req, res, next) { Collection.updateMany({ _id: { $in: req.body.ids } }, { removedAt: Date.now() }) .then((results) => res.json(results)) .catch(next) } // --[PATCH]-- /restore/:id -- Restore List By ID -- restoreList(req, res, next) { Collection.updateMany({ _id: { $in: req.body.ids } }, { removedAt: 0 }) .then((results) => res.json(results)) .catch(next) } // --[PATCH]-- /destroy/:id -- Destroy List By ID -- destroyList(req, res, next) { Collection.deleteMany({ _id: { $in: req.body.ids } }) .then((results) => res.json(results)) .catch(next) } } module.exports = new CollectionController(); |