-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathblog-controller.js
More file actions
172 lines (131 loc) · 3.93 KB
/
blog-controller.js
File metadata and controls
172 lines (131 loc) · 3.93 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
const mongoose = require("mongoose");
const { findByIdAndRemove } = require("../model/Blog");
const Blog = require("../model/Blog");
const User = require("../model/User");
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 currentDate = new Date();
try {
console.log(user);
const existingUser = await User.findById(user);
console.log(existingUser);
if (!existingUser) {
return res.status(400).json({ message: "Unauthorized - User not found" });
}
const blog = new Blog({
title,
desc,
img,
user,
date: currentDate
});
const savedBlog = await blog.save();
existingUser.blogs.push(savedBlog._id);
await existingUser.save();
return res.status(201).json({ message: "Blog created", blog: savedBlog });
} catch (err) {
console.error("Error while creating blog:", err);
return res.status(500).json({ message: "Server error", error: err });
}
};
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;
console.log(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} ;