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:
@@ -11,35 +11,10 @@ mongoose
|
||||
.then(() => console.log("Connected to MongoDB..."))
|
||||
.catch((err) => console.log("Could not connect to MongoDB", err));
|
||||
|
||||
//temp
|
||||
// const { Person } = require("./models/person-model");
|
||||
const people = require("./routes/person");
|
||||
|
||||
// async function createPerson() {
|
||||
// const per = new Person({
|
||||
// name: "Mike",
|
||||
// place: "Discord Eng Group",
|
||||
// contact: {
|
||||
// phone: "12345",
|
||||
// email: "[email protected]",
|
||||
// socialmedia: {
|
||||
// instagram: "mike123",
|
||||
// youtube: "mike123",
|
||||
// twitter: "mike123",
|
||||
// },
|
||||
// },
|
||||
// birth: "10.05.1998",
|
||||
// age: 22,
|
||||
// likes: ["football", "video games"],
|
||||
// dislikes: ["cats", "spiders"],
|
||||
// occupation: "student",
|
||||
// lastseen: "12.07.2021",
|
||||
// nextcontact: "15.07.2021",
|
||||
// notes: "He is a nice guy.",
|
||||
// tags: ["student", "programmer", "father", "mother", "brother", "doctor"],
|
||||
// });
|
||||
// const result = await per.save();
|
||||
// console.log(result);
|
||||
// }
|
||||
app.use(express.json());
|
||||
app.use("/api/person", people);
|
||||
|
||||
const port = process.env.port || 3002;
|
||||
app.listen(port, () => console.log(`Listening on port ${port}...`));
|
||||
|
||||
+6
-12
@@ -21,13 +21,11 @@ const personSchema = new mongoose.Schema({
|
||||
type: String,
|
||||
minlength: 5,
|
||||
maxlength: 20,
|
||||
unique: true,
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
minlength: 5,
|
||||
maxlength: 255,
|
||||
unique: true,
|
||||
},
|
||||
socialmedia: {
|
||||
type: new mongoose.Schema({
|
||||
@@ -41,13 +39,11 @@ const personSchema = new mongoose.Schema({
|
||||
type: String,
|
||||
minlength: 3,
|
||||
maxlength: 15,
|
||||
unique: true,
|
||||
},
|
||||
twitter: {
|
||||
type: String,
|
||||
minlength: 3,
|
||||
maxlength: 15,
|
||||
unique: true,
|
||||
},
|
||||
}),
|
||||
},
|
||||
@@ -55,7 +51,6 @@ const personSchema = new mongoose.Schema({
|
||||
type: String,
|
||||
minlength: 5,
|
||||
maxlength: 25,
|
||||
unique: true,
|
||||
},
|
||||
}),
|
||||
},
|
||||
@@ -98,7 +93,6 @@ const personSchema = new mongoose.Schema({
|
||||
|
||||
validate: {
|
||||
validator: function (value) {
|
||||
console.log(value.length);
|
||||
return value.length <= 6;
|
||||
},
|
||||
message: "Max tag size is 6!",
|
||||
@@ -113,14 +107,14 @@ function validatePerson(value) {
|
||||
name: Joi.string().min(3).max(50).required(),
|
||||
place: Joi.string().min(3).max(50).required(true),
|
||||
contact: Joi.object({
|
||||
phone: Joi.string().min(5).max(20).unique(),
|
||||
email: Joi.string().email().min(5).max(255).unique(),
|
||||
phone: Joi.string().min(5).max(20),
|
||||
email: Joi.string().email().min(5).max(255),
|
||||
socialmedia: Joi.object({
|
||||
instagram: Joi.string().min(3).max(15).unique(),
|
||||
youtube: Joi.string().min(3).max(15).unique(),
|
||||
twitter: Joi.string().min(3).max(15).unique(),
|
||||
instagram: Joi.string().min(3).max(15),
|
||||
youtube: Joi.string().min(3).max(15),
|
||||
twitter: Joi.string().min(3).max(15),
|
||||
}),
|
||||
website: Joi.string().min(5).max(25).unique(),
|
||||
website: Joi.string().min(5).max(25),
|
||||
}),
|
||||
birth: Joi.string().max(25),
|
||||
age: Joi.number().max(100),
|
||||
|
||||
@@ -7,5 +7,8 @@
|
||||
"devDependencies": {
|
||||
"jest": "^27.0.6",
|
||||
"supertest": "^6.1.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest --watchAll --verbose --coverage"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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