Compare commits

...

2 Commits

5 changed files with 151 additions and 43 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,8 +1,10 @@
const router = require('express').Router() 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 module.exports = router

View File

@ -16,7 +16,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n \"name\":\"Anton\",\n \"phone\": \"89172420577\"\n}", "raw": "{\n \"name\":\"Anto234\",\n \"phone\": \"89172420577\"\n}",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -42,13 +42,60 @@
"method": "GET", "method": "GET",
"header": [], "header": [],
"url": { "url": {
"raw": "{{host}}/arm/masters", "raw": "{{host}}/arm/master-list",
"host": [ "host": [
"{{host}}" "{{host}}"
], ],
"path": [ "path": [
"arm", "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 @@
] ]
} }
] ]
} }