From 276ef4033be7387653241fd56fc81c3a7cc2652e Mon Sep 17 00:00:00 2001 From: ilnaz <237x237@gmail.com> Date: Sat, 21 Dec 2024 21:33:07 +0300 Subject: [PATCH] feat: add crud for Masters --- server/routers/dry-wash/arm-master.js | 87 +++++++++++++++++++ server/routers/dry-wash/arm-orders.js | 9 ++ server/routers/dry-wash/arm.js | 37 -------- server/routers/dry-wash/index.js | 6 +- .../postman/dry-wash.postman_collection.json | 55 +++++++++++- 5 files changed, 151 insertions(+), 43 deletions(-) create mode 100644 server/routers/dry-wash/arm-master.js create mode 100644 server/routers/dry-wash/arm-orders.js delete mode 100644 server/routers/dry-wash/arm.js diff --git a/server/routers/dry-wash/arm-master.js b/server/routers/dry-wash/arm-master.js new file mode 100644 index 0000000..7288d47 --- /dev/null +++ b/server/routers/dry-wash/arm-master.js @@ -0,0 +1,87 @@ +const router = require('express').Router() +const {MasterModel} = require('./model/master') +const mongoose = require("mongoose") + + +router.get('/masters', async (req, res,next) => { + try { + const master = await MasterModel.find({}) + res.status(200).send({success: true, body: master}) + } catch (error) { + next(error) + } +}) + +router.delete('/masters/:id', async (req, res,next) => { + const { id } = req.params; + + if (!mongoose.Types.ObjectId.isValid(id)){ + throw new Error('ID is required') + } + + try { + const master = await MasterModel.findByIdAndDelete(id, { + new: true, + }); + if (!master) { + throw new Error('master not found') + } + res.status(200).send({success: true, body: master}) + } catch (error) { + next(error) + } +}) + + +router.post('/masters', async (req, res,next) => { + + const {name, phone} = req.body + + if (!name || !phone ){ + throw new Error('Enter name and phone') + } + try { + const master = await MasterModel.create({name, phone}) + res.status(200).send({success: true, body: master}) + } catch (error) { + next(error) + } +}) + + +router.patch('/masters/:id', async (req, res, next) => { + const { id } = req.params; + + if (!mongoose.Types.ObjectId.isValid(id)) { + throw new Error('ID is required') + } + + const { name, phone } = req.body; + + if (!name && !phone) { + throw new Error('Enter name and phone') + } + + + try { + const updateData = {}; + if (name) updateData.name = name; + if (phone) updateData.phone = phone; + + const master = await MasterModel.findByIdAndUpdate( + id, + updateData, + { new: true } + ); + + if (!master) { + throw new Error('master not found') + } + + res.status(200).send({ success: true, body: master }); + } catch (error) { + next(error); + } +}); + +module.exports = router diff --git a/server/routers/dry-wash/arm-orders.js b/server/routers/dry-wash/arm-orders.js new file mode 100644 index 0000000..2f4be72 --- /dev/null +++ b/server/routers/dry-wash/arm-orders.js @@ -0,0 +1,9 @@ +const router = require('express').Router() + +router.get('/orders', (req, res) => { + res + .status(200) + .send(require(`./json/arm-orders/success.json`)) +}) + +module.exports = router diff --git a/server/routers/dry-wash/arm.js b/server/routers/dry-wash/arm.js deleted file mode 100644 index e0a00e8..0000000 --- a/server/routers/dry-wash/arm.js +++ /dev/null @@ -1,37 +0,0 @@ -const router = require('express').Router() -const {MasterModel} = require('./model/master') - -router.post('/master', async (req, res,next) => { - - const {name, phone} = req.body - - if (!name || !phone ){ - throw new Error('Enter name and phone') - } - try { - const master = await MasterModel.create({name, phone}) - res.status(200).send({success: true, body: master}) - } catch (error) { - next(error) - } -}) - - - -router.get('/masters', async (req, res,next) => { - try { - const master = await MasterModel.find({}) - res.status(200).send({success: true, body: master}) - } catch (error) { - next(error) - } -}) - - -router.get('/orders', (req, res) => { - res - .status(200) - .send(require(`./json/arm-orders/success.json`)) -}) - -module.exports = router diff --git a/server/routers/dry-wash/index.js b/server/routers/dry-wash/index.js index e9f1aaf..202295b 100644 --- a/server/routers/dry-wash/index.js +++ b/server/routers/dry-wash/index.js @@ -1,8 +1,10 @@ const router = require('express').Router() -const armRouter = require('./arm') +const armMasterRouter = require('./arm-master') +const armOrdersRouter = require('./arm-orders') -router.use('/arm', armRouter) +router.use('/arm', armMasterRouter) +router.use('/arm', armOrdersRouter) module.exports = router diff --git a/server/routers/dry-wash/postman/dry-wash.postman_collection.json b/server/routers/dry-wash/postman/dry-wash.postman_collection.json index 685bf02..50ad33a 100644 --- a/server/routers/dry-wash/postman/dry-wash.postman_collection.json +++ b/server/routers/dry-wash/postman/dry-wash.postman_collection.json @@ -16,7 +16,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\n \"name\":\"Anton\",\n \"phone\": \"89172420577\"\n}", + "raw": "{\n \"name\":\"Anto234\",\n \"phone\": \"89172420577\"\n}", "options": { "raw": { "language": "json" @@ -42,13 +42,60 @@ "method": "GET", "header": [], "url": { - "raw": "{{host}}/arm/masters", + "raw": "{{host}}/arm/master-list", "host": [ "{{host}}" ], "path": [ "arm", - "masters" + "master-list" + ] + } + }, + "response": [] + }, + { + "name": "delete master", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{host}}/arm/masters/{{id}}", + "host": [ + "{{host}}" + ], + "path": [ + "arm", + "masters", + "{{id}}" + ] + } + }, + "response": [] + }, + { + "name": "update master", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"name\":\"Anto234\",\n \"phone\": \"89172420577\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}/arm/masters/{{id}}", + "host": [ + "{{host}}" + ], + "path": [ + "arm", + "masters", + "{{id}}" ] } }, @@ -57,4 +104,4 @@ ] } ] -} +} \ No newline at end of file -- 2.45.2