-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathblog-controller.js
More file actions
179 lines (142 loc) · 4.19 KB
/
Copy pathblog-controller.js
File metadata and controls
179 lines (142 loc) · 4.19 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const mongoose = require("mongoose");
const { findByIdAndRemove } = require("../model/Blog");
const Blog = require("../model/Blog");
const User = require("../model/User");
// const path = require('path')
const getAllBlogs = async(req,res,next) =>{
let blogs;
try{
blogs = await Blog.find();
}catch(e){
console.log(e);
}
if(!blogs){
return res.status(404).json({message : " No blogs found"});
}
return res.status(200).json({blogs});
}
// const addBlog = async(req,res,next) =>{
// const { title , desc , img , user } = req.body;
// let existingUser;
// try {
// existingUser = await User.findById(user);
// } catch (e) {
// return console.log(e);
// }
// if(!existingUser){
// return res.status(400).json({message: " Unautorized"});
// }
// const blog = new Blog({
// title ,desc , img , user
// });
// try {
// const session = await mongoose.startSession();
// session.startTransaction();
// await blog.save({session});
// existingUser.blogs.push(blog);
// await existingUser.save({session});
// await session.commitTransaction();
// } catch (e) {
// return res.status(500).json({message:e})
// }
// return res.status(200).json({blog});
// }
const addBlog = async(req,res,next) =>{
// const { title , desc , img , user } = req.body;
const title = req.body.title;
const desc = req.body.desc;
const imgUrl = req.file.path;
const user = req.body.user;
const currentDate = new Date();
let existingUser;
try {
existingUser = await User.findById(user);
} catch (e) {
return console.log(e);
}
if(!existingUser){
return res.status(400).json({message: " Unautorized"});
}
const blog = new Blog({
title ,desc , imgUrl , user, date: currentDate
});
try {
await blog.save();
} catch (e) {
return console.log(e);
}
try {
const session = await mongoose.startSession();
session.startTransaction();
await blog.save(session);
existingUser.blogs.push(blog);
await existingUser.save(session);
session.commitTransaction();
} catch (err) {
console.log(err);
return res.status(500).json({ message: err });
}
return res.status(200).json({blog});
}
const updateBlog = async(req,res,next) => {
const blogId = req.params.id;
const { title , desc } = req.body;
let blog;
try {
blog = await Blog.findByIdAndUpdate(blogId , {
title,
desc
});
} catch (e) {
return console.log(e);
}
if(!blog){
return res.status(500).json({message : "Unable to update"})
}
return res.status(200).json({blog});
}
const getById = async (req,res,next) =>{
const id = req.params.id;
let blog;
try{
blog = await Blog.findById(id);
}
catch(e){
return console.log(e);
}
if(!blog){
return res.status(500).json({ message : "not found"});
}
return res.status(200).json({blog});
}
const deleteBlog = async (req, res, next) => {
const id = req.params.id;
try {
const blog = await Blog.findByIdAndDelete(id).populate('user');
if (!blog) {
return res.status(404).json({ message: "Blog not found" });
}
// Remove the blog from the user's blogs array
const user = blog.user;
user.blogs.pull(blog);
await user.save();
return res.status(200).json({ message: "Successfully deleted" });
} catch (e) {
console.error(e);
return res.status(500).json({ message: "Unable to delete" });
}
}
const getByUserId = async (req, res, next) => {
const userId = req.params.id;
let userBlogs;
try {
userBlogs = await User.findById(userId).populate("blogs");
} catch (err) {
return console.log(err);
}
if (!userBlogs) {
return res.status(404).json({ message: "No Blog Found" });
}
return res.status(200).json({ user: userBlogs });
};
module.exports = { getAllBlogs ,addBlog , updateBlog , getById ,deleteBlog , getByUserId} ;