Skip to content

Commit

Permalink
Added User and Admin Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
BuddyLongLegs committed Oct 11, 2022
1 parent e991717 commit f519b9e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 24 deletions.
64 changes: 62 additions & 2 deletions adminRoutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
const express = require('express');
const mongoose = require('mongoose');
const router = express.Router();

const {Post, Tag} = require("./model");

module.exports = router;
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<links.length || 0; i++){
post.links.push(links[i]);
}
for(let i=0; i<pnames.length ||0; i++){
post.people.push({
name: pnames[i],
link: plinks[i]
});
}
try{
await post.save();
}catch(e){
return res.status(400).json({"error": "Invalid Field Data"})
}
data.post = post;
return res.status(201).json(data);
}
}

router.post("/newtag", newTag);
router.post("/newpost", newPost);
module.exports = router;
28 changes: 28 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const {Tag} = require("./model");

const app=express();

app.use(bodyParser.urlencoded({extended:true}));
mongoose.connect("mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(()=>{
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);
9 changes: 5 additions & 4 deletions model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
38 changes: 21 additions & 17 deletions userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit f519b9e

Please sign in to comment.