-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
130 lines (110 loc) · 3.36 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require("express")
const db = require("./db/db")
const passport = require("passport")
const authRouter = require("./routes/auth")
const blogRouter = require("./routes/blog/blog")
const BlogModel = require("./models/blog")
require("dotenv").config()
require("./authenticate/auth")
const PORT = process.env.PORT || 4000
const app = express()
app.use(express.json())
app.use(express.urlencoded({extended : true}))
db.connectToDb() // connect to mongo db
app.use("/", authRouter)
app.use("/blog", passport.authenticate('jwt', { session: false }), blogRouter)
// get all blog posts/articles
app.get("/",async(req,res)=>{
try{
const page = req.query.page || 1;
const author = req.query.author;
const title = req.query.title;
const tag = req.query.tag;
// order : read_count, reading_time and timestamp
const order= req.query.order || "timeStamp";
const limit = 20;
const skip = (page - 1) * limit;
let blogArticleArray
if(page && limit){
if(author){
blogArticleArray = await BlogModel.find({
author : author,
state : "publish"
}).skip(skip).limit(20).sort(order)
}else if(title){
blogArticleArray = await BlogModel.find({
title : title,
state : "publish"
}).skip(skip).limit(20).sort(order)
}else if(tag){
blogArticleArray = await BlogModel.find({
tag : tag,
state : "publish"
}).skip(skip).limit(20).sort(order)
}
else{
blogArticleArray = await BlogModel.find({
state : "publish"
}).skip(skip).limit(20).sort(order)
}
return res.status(200).json({
message : "blog articles successfully retrieved",
data : blogArticleArray
})
}
// else{
// return res.status(404).json({
// message : "search api by either author,title or tag and order outcome by either read_count,reading_time and timestamp",
// data : null
// })
// }
}catch(error){
return res.status(400).json({
message : "bad request",
data: null
})
}
})
// get a single blog
app.get("/:id",async(req,res)=>{
try{
const id = req.params.id;
const singleBlogPost = await BlogModel.findOne({
_id : id,
state: "publish"
})
if(!singleBlogPost){
return res.status(404).json({
message : "Article not found",
data : null
})
}
const read_Count_obj = {
read_count : singleBlogPost.read_count + 1
}
const updatedBlogPost = await BlogModel.findByIdAndUpdate(
id, read_Count_obj, {new: true}
)
return res.status(200).json({
message : "blog successfully retrieved",
data : updatedBlogPost
})
}catch(error){
return res.status(400).json({
message : "Bad request",
data : null
})
}
})
// global error middlewares
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).json({
data: null,
error: 'Server Error occured'
})
})
app.listen(PORT,()=>{
console.log(`localhost:${PORT}`)
})
module.exports = app