mirror of
https://github.com/yusufipk/RememberMe.git
synced 2026-09-11 10:56:07 +00:00
create(person route) created REST api for person model
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
const { Person, validate } = require("../models/person-model");
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const people = await Person.find();
|
||||
res.send(people);
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
// validate id
|
||||
|
||||
const person = await Person.findById(req.params.id);
|
||||
if (person.length === 0)
|
||||
return res.status(404).send("Person does not exist!");
|
||||
|
||||
res.send(person);
|
||||
});
|
||||
|
||||
router.get("/get/:name", async (req, res) => {
|
||||
const p = new RegExp(req.params.name, "i");
|
||||
const person = await Person.find({ name: p });
|
||||
if (person.length === 0)
|
||||
return res.status(404).send("Person does not exist!");
|
||||
|
||||
res.send(person);
|
||||
});
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
const { error } = validate(req.body);
|
||||
if (error) return res.status(400).send(error.details[0].message);
|
||||
|
||||
const {
|
||||
name,
|
||||
place,
|
||||
contact,
|
||||
birth,
|
||||
age,
|
||||
likes,
|
||||
dislikes,
|
||||
occupation,
|
||||
lastseen,
|
||||
nextcontact,
|
||||
notes,
|
||||
tags,
|
||||
} = req.body;
|
||||
|
||||
const person = new Person({
|
||||
name,
|
||||
place,
|
||||
contact,
|
||||
birth,
|
||||
age,
|
||||
likes,
|
||||
dislikes,
|
||||
occupation,
|
||||
lastseen,
|
||||
nextcontact,
|
||||
notes,
|
||||
tags,
|
||||
});
|
||||
|
||||
await person.save();
|
||||
res.send(person);
|
||||
});
|
||||
|
||||
router.put("/:id", async (req, res) => {
|
||||
// validate id
|
||||
const person = await Person.findById(req.params.id);
|
||||
if (!person) return res.status(404).send("Person does not exist!");
|
||||
|
||||
const { error } = validate(req.body);
|
||||
if (error) return res.status(400).send(error.details[0].message);
|
||||
|
||||
const result = await Person.updateOne(
|
||||
{ _id: req.params.id },
|
||||
{
|
||||
$set: {
|
||||
name: req.body.name,
|
||||
place: req.body.place,
|
||||
contact: req.body.contact,
|
||||
birth: req.body.birth,
|
||||
age: req.body.age,
|
||||
likes: req.body.likes,
|
||||
dislikes: req.body.dislikes,
|
||||
occupation: req.body.occupation,
|
||||
lastseen: req.body.lastseen,
|
||||
nextcontact: req.body.nextcontact,
|
||||
notes: req.body.notes,
|
||||
tags: req.body.tags,
|
||||
},
|
||||
}
|
||||
);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
// validate id
|
||||
|
||||
const person = await Person.findById(req.params.id);
|
||||
if (!person) return res.status(404).send("Person does not exist!");
|
||||
|
||||
const result = await Person.deleteOne({ _id: req.params.id });
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user