From f519b9ebc8e9bc4646481c2026f7d7a9d13f0ac7 Mon Sep 17 00:00:00 2001 From: BuddyLongLegs <=> Date: Tue, 11 Oct 2022 19:51:59 +0530 Subject: [PATCH] Added User and Admin Routes --- adminRoutes.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++-- app.js | 28 ++++++++++++++++++++++ model.js | 9 +++---- package.json | 3 ++- userRoutes.js | 38 ++++++++++++++++-------------- 5 files changed, 118 insertions(+), 24 deletions(-) diff --git a/adminRoutes.js b/adminRoutes.js index 92d9956..225906d 100644 --- a/adminRoutes.js +++ b/adminRoutes.js @@ -1,6 +1,66 @@ const express = require('express'); +const mongoose = require('mongoose'); const router = express.Router(); - const {Post, Tag} = require("./model"); -module.exports = router; \ No newline at end of file +module.exports = router; + +async function newTag(req, res) { + let data = {}; + data.tags = req.tags; + if(req.body.name!==undefined && req.body.name!==""){ + let exists = await Tag.exists({name: req.body.name}); + if(exists) return res.status(401).json({"error": "exists"}); + let tag = new Tag({name: req.body.name}); + tag.save(); + data.newTag = tag; + data.tags.push({"name":tag.name}); + return res.status(201).json(data); + } + return res.status(401).json({"error":"invalid"}); +} + +async function newPost(req, res){ + let data = {}; + data.tags = req.tags; + + let title = req.body.title, + desc = req.body.desc, + links = req.body.link, + pnames = req.body.peoplename, + plinks = req.body.peoplelink, + tag = req.body.tag; + if ((title!==undefined && title!="")&& + (desc!==undefined && desc!="")&& + (tag!==undefined && tag!="")) + { + let tagobj = await Tag.findOne({name: tag}); + // console.log(tagobj) + let post = new Post({ + title: title, + desc: desc, + tag: tagobj._id + }); + // console.log(post) + for(let i=0; i{ + console.log("connected to db"); +}); + +app.use(async (req, res, next)=>{ + req.tags = await Tag.find({}, {name: 1, _id:0}); + return next(); +}) +let adminRoutes = require("./adminRoutes"); +app.use("/admin", adminRoutes); + +let userroutes = require("./userRoutes"); +app.use("/", userroutes); + +app.listen(process.env.PORT||8080); \ No newline at end of file diff --git a/model.js b/model.js index 320fc99..c001469 100644 --- a/model.js +++ b/model.js @@ -10,7 +10,7 @@ const TagSchema = new mongoose.Schema({ } }) -const Tag = mongoose.Model("Tag", TagSchema); +const Tag = mongoose.model("Tag", TagSchema); const PostSchema = new mongoose.Schema({ title: String, @@ -28,13 +28,14 @@ const PostSchema = new mongoose.Schema({ }], tag:{ type: mongoose.Schema.Types.ObjectId, - ref: "Tag" + ref: "Tag", + required: true } -}) +},{ timestamps: true }) PostSchema.plugin(mongoosePaginate); -const Post = mongoose.Model("Post", PostSchema); +const Post = mongoose.model("Post", PostSchema); module.exports.Post = Post; module.exports.Tag = Tag; \ No newline at end of file diff --git a/package.json b/package.json index fe4babd..3cd93aa 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,9 @@ "name": "webdproject", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "app.js", "scripts": { + "start": "nodemon app.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { diff --git a/userRoutes.js b/userRoutes.js index 970784a..268aded 100644 --- a/userRoutes.js +++ b/userRoutes.js @@ -6,34 +6,38 @@ const {Post, Tag} = require("./model"); const DEFAULT_QUERY_PAGE = 1; const DEFAULT_QUERY_PERPAGE = 10; -async function getAllTags(req, res) { - const tags = Tag.find({}, {name: 1, _id:0}); - return res.status(200).json(tags); -} +// async function getAllTags(req, res) { +// const tags = Tag.find({}, {name: 1, _id:0}); +// return res.status(200).json(tags); +// } async function getPaginatedPosts(req, res){ - var page = req.query.page ? Math.max(1, Number(req.query.page)) : DEFAULT_QUERY_PAGE; - var perpage = Number(req.query.perpage) || DEFAULT_QUERY_PERPAGE; - + let data ={}; + data.tags = req.tags; const query = { - "tag.name" : req.params.tag + // "tag.name" : req.params.tag, }; - + req.params.tag = req.params.tag.replace(/\+/g, " ") let options = { - page: page, - limit: perpage, populate: { - path: "tag" + path: "tag", + select : "name -_id", + match: { + "name": req.params.tag + } }, - select:"title desc links people tag.name" + select:"title desc links people tag updatedAt", + order:{ + "updatedAt": -1 + } } - - let posts = await Post.paginate(query, options); - return res.status(200).json(posts); + data.posts = await Post.find(query).select(options.select).populate(options.populate).sort(options.order); + data.posts = data.posts.filter((post)=>{return post.tag!==null;}) + return res.status(200).json(data); } -router.get("/tags", getAllTags); +// router.get("/tags", getAllTags); router.get("/posts/:tag", getPaginatedPosts); module.exports = router; \ No newline at end of file