mirror of
https://github.com/yusufipk/RememberMe.git
synced 2026-09-11 10:56:07 +00:00
create(middleware) validateobjectid middleware has been added
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
module.exports = function (req, res, next) {
|
||||
if (!mongoose.Types.ObjectId.isValid(req.params.id))
|
||||
return res.status(404).send("Invalid ID");
|
||||
next();
|
||||
};
|
||||
@@ -98,6 +98,15 @@ const personSchema = new mongoose.Schema({
|
||||
message: "Max tag size is 6!",
|
||||
},
|
||||
},
|
||||
createdAt: {
|
||||
type: String,
|
||||
maxlength: 25,
|
||||
},
|
||||
editedAt: {
|
||||
type: Date,
|
||||
default: Date.now(),
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
const Person = mongoose.model("Person", personSchema);
|
||||
@@ -125,6 +134,7 @@ function validatePerson(value) {
|
||||
nextcontact: Joi.string().min(3).max(50),
|
||||
notes: Joi.string().min(3).max(500),
|
||||
tags: Joi.array().max(6),
|
||||
createdAt: Joi.string().max(25),
|
||||
});
|
||||
return schema.validate(value);
|
||||
}
|
||||
|
||||
+8
-6
@@ -1,4 +1,5 @@
|
||||
const { Person, validate } = require("../models/person-model");
|
||||
const validateObjectId = require("../middleware/validateObjectId");
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
@@ -7,11 +8,9 @@ router.get("/", async (req, res) => {
|
||||
res.send(people);
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
// validate id
|
||||
|
||||
router.get("/:id", validateObjectId, async (req, res) => {
|
||||
const person = await Person.findById(req.params.id);
|
||||
if (person.length === 0)
|
||||
if (!person || person.length === 0)
|
||||
return res.status(404).send("Person does not exist!");
|
||||
|
||||
res.send(person);
|
||||
@@ -43,6 +42,8 @@ router.post("/", async (req, res) => {
|
||||
nextcontact,
|
||||
notes,
|
||||
tags,
|
||||
createdAt,
|
||||
editedAt,
|
||||
} = req.body;
|
||||
|
||||
const person = new Person({
|
||||
@@ -58,14 +59,15 @@ router.post("/", async (req, res) => {
|
||||
nextcontact,
|
||||
notes,
|
||||
tags,
|
||||
createdAt,
|
||||
editedAt,
|
||||
});
|
||||
|
||||
await person.save();
|
||||
res.send(person);
|
||||
});
|
||||
|
||||
router.put("/:id", async (req, res) => {
|
||||
// validate id
|
||||
router.put("/:id", validateObjectId, async (req, res) => {
|
||||
const person = await Person.findById(req.params.id);
|
||||
if (!person) return res.status(404).send("Person does not exist!");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user