-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathblog-controller.js
More file actions
105 lines (90 loc) · 3.23 KB
/
blog-controller.js
File metadata and controls
105 lines (90 loc) · 3.23 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
const mongoose = require("mongoose");
const Blog = require("../model/Blog");
const User = require("../model/User");
const ApiResponse = require("../utils/ApiResponse");
const ApiError = require("../utils/ApiError");
const getAllBlogs = async (req, res, next) => {
try {
console.log("Hello")
const blogs = await Blog.find();
console.log("Blogs", blogs);
if (!blogs || blogs.length === 0) {
return res.status(404).json(new ApiError(404, "No blogs found"));
}
return res.status(200).json(new ApiResponse(200, { blogs }, "Blogs found"));
} catch (e) {
return res.status(500).json(new ApiError(500, e.message));
}
};
const addBlog = async (req, res, next) => {
console.log("Inside add blog");
const { title, desc, img, user } = req.body;
console.log(req.body)
const currentDate = new Date();
try {
const existingUser = await User.findById(user);
if (!existingUser) {
return res.status(400).json(new ApiError(400, "Unauthorized"));
}
const blog = await Blog.create({ title, desc, img, user, date: currentDate });
existingUser.blogs.push(blog._id);
await existingUser.save();
return res.status(201).json(new ApiResponse(201, { blog }, "Blog created successfully"));
} catch (e) {
console.log("error", e)
return res.status(500).json(new ApiError(500, e.message));
}
};
const updateBlog = async (req, res, next) => {
const blogId = req.params.id;
const { title, desc } = req.body;
try {
const blog = await Blog.findByIdAndUpdate(blogId, { title, desc }, { new: true });
if (!blog) {
return res.status(404).json(new ApiError(404, "Blog not found"));
}
return res.status(200).json(new ApiResponse(200, { blog }, "Blog updated successfully"));
} catch (e) {
return res.status(500).json(new ApiError(500, e.message));
}
};
const getById = async (req, res, next) => {
const id = req.params.id;
try {
const blog = await Blog.findById(id);
if (!blog) {
return res.status(404).json(new ApiError(404, "Blog not found"));
}
return res.status(200).json(new ApiResponse(200, { blog }, "Blog retrieved successfully"));
} catch (e) {
return res.status(500).json(new ApiError(500, e.message));
}
};
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(new ApiError(404, "Blog not found"));
}
const user = blog.user;
user.blogs.pull(blog);
await user.save();
return res.status(200).json(new ApiResponse(200, null, "Blog deleted successfully"));
} catch (e) {
return res.status(500).json(new ApiError(500, e.message));
}
};
const getByUserId = async (req, res, next) => {
const userId = req.params.id;
try {
const userBlogs = await User.findById(userId).populate("blogs");
if (!userBlogs) {
return res.status(404).json(new ApiError(404, "No blog found for this user"));
}
return res.status(200).json(new ApiResponse(200, { user: userBlogs }, "Blogs retrieved successfully"));
} catch (e) {
return res.status(500).json(new ApiError(500, e.message));
}
};
module.exports = { getAllBlogs, addBlog, updateBlog, getById, deleteBlog, getByUserId };