-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating tagSchema model and tag controller and handling errors and a…
…voiding duplicate tag in the backend
- Loading branch information
1 parent
76bc2bd
commit 844d597
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const Tag = require("../models/tagSchema"); | ||
const slugify = require("slugify"); | ||
const {errorHandler} = require("../helpers/databaseErrorHandler") | ||
|
||
exports.create = (req,res)=>{ | ||
const {name} = req.body | ||
let slug = slugify(name).toLowerCase() | ||
|
||
let tag = new Tag({name,slug}) | ||
|
||
tag.save((err,data)=>{ | ||
if (err){ | ||
return res.status(400).json({ | ||
error:errorHandler(err) | ||
}) | ||
} | ||
res.json(data) | ||
}) | ||
}; | ||
|
||
exports.list = (req,res)=>{ | ||
Tag.find({}).exec((err,data)=>{ | ||
if (err){ | ||
return res.status(400).json({ | ||
error:errorHandler(err) | ||
}) | ||
} | ||
res.json(data) | ||
}); | ||
}; | ||
|
||
|
||
exports.read =(req,res)=>{ | ||
const slug = req.params.slug.toLowerCase() | ||
|
||
Tag.findOne({slug}).exec((err,tag)=>{ | ||
if (err){ | ||
return res.status(400).json({ | ||
error:errorHandler(err) | ||
}) | ||
} | ||
res.json(tag) | ||
}); | ||
}; | ||
|
||
|
||
|
||
exports.remove =(req,res)=>{ | ||
const slug = req.params.slug.toLowerCase() | ||
|
||
Tag.findOneAndRemove({slug}).exec((err,data)=>{ | ||
if (err){ | ||
return res.status(400).json({ | ||
error:errorHandler(err) | ||
}) | ||
} | ||
res.json({ | ||
message: "Tag Deleted" | ||
}) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const TagSchema = new mongoose.Schema( | ||
{ | ||
name: { | ||
type: String, | ||
trim: true, | ||
required: true, | ||
max: 32 | ||
}, | ||
slug: { | ||
type: String, | ||
unique: true, | ||
index: true | ||
} | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
module.exports = mongoose.model("Tag", TagSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const express = require('express') | ||
const router = express.Router(); | ||
const {create,list,read,remove} = require("../controllers/tag.js") | ||
const {requireLogin,adminAuthenticationMiddleware} = require("../controllers/userAuthentication.js") | ||
|
||
const {runValidation} = require("../validators") | ||
const {createTagValidator} = require("../validators/Tag") | ||
|
||
router.post('/tag',createTagValidator,runValidation,requireLogin,adminAuthenticationMiddleware,create); | ||
router.get('/taglists',list) | ||
router.get('/tag/:slug',read) | ||
router.delete('/tag/:slug',requireLogin,adminAuthenticationMiddleware,remove) | ||
|
||
module.exports = router; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const {check} = require("express-validator"); | ||
|
||
exports.createTagValidator = [ | ||
check('name').not().isEmpty().withMessage("hold up!! Name is required") | ||
]; |