blog-app/server/index.js
2025-01-22 18:21:52 +05:30

338 lines
7.6 KiB
JavaScript

const express = require("express");
const cors = require("cors");
const { PrismaClient } = require("@prisma/client");
const app = express();
const prisma = new PrismaClient();
const PORT = process.env.PORT || 4000;
// Middleware
app.use(cors());
app.use(express.json());
/* Blog Crud */
// Create a New Blog
app.post("/blogs", async (req, res) => {
const { slug, title, body, categoryIDs, tagIDs } = req.body;
try {
const newBlog = await prisma.blog.create({
data: { slug, title, body, categoryIDs, tagIDs },
});
res.status(201).json(newBlog);
} catch (err) {
console.error("Error while creating a New Blog:", err);
res
.status(400)
.json({ error: "Error while creating a New Blog", details: err.message });
}
});
// Get all blogs
app.get("/blogs", async (req, res) => {
try {
const blogs = await prisma.blog.findMany({
include: { comments: true, categories: true, tags: true },
});
res.status(200).json(blogs);
} catch (error) {
console.log("Error while fetching a blogs:", err);
res
.status(400)
.json({ error: "Error fetching blogs", details: err.message });
}
});
// Get blog by slug
app.get("/blogs/:slug", async (req, res) => {
const { slug } = req.params;
try {
const blog = await prisma.blog.findUnique({
where: { slug },
include: { comments: true, categories: true, tags: true },
});
if (blog) res.status(200).json(blog);
else res.status(404).json({ error: "Blog not found" });
} catch (error) {
console.log("Error while blog by slug:", err);
res
.status(400)
.json({ error: "Error fetching blog", details: err.message });
}
});
// Update a blog
app.put("/blogs/:slug", async (req, res) => {
const { slug } = req.params;
const { title, body, categoryIDs, tagIDs } = req.body;
try {
const updatedBlog = await prisma.blog.update({
where: { slug },
data: { title, body, categoryIDs, tagIDs },
});
res.status(200).json(updatedBlog);
} catch (error) {
console.log("Error while uploading a Blog:", err);
res
.status(400)
.json({ error: "Error updating blog", details: err.message });
}
});
// Delete a blog
app.delete("/blogs/:slug", async (req, res) => {
const { slug } = req.params;
try {
await prisma.blog.delete({
where: { slug },
});
res.status(204).send();
} catch (error) {
console.log("Error while deleting a Blog:", err);
res
.status(400)
.json({ error: "Error deleting blog", details: err.message });
}
});
/* Category CRUD */
// Create a New Category
app.post("/categories", async (req, res) => {
const { name } = req.body;
try {
const newCategory = await prisma.category.create({
data: { name },
});
res.status(201).json(newCategory);
} catch (err) {
console.error("Error while creating a New Category:", err);
res.status(400).json({
error: "Error while creating a New Category",
details: err.message,
});
}
});
// Get all categories
app.get("/categories", async (req, res) => {
try {
const categories = await prisma.category.findMany();
res.status(200).json(categories);
} catch (err) {
console.error("Error while fetching categories:", err);
res.status(400).json({
error: "Error fetching categories",
details: err.message,
});
}
});
// Update a category
app.put("/categories/:id", async (req, res) => {
const { id } = req.params;
const { name } = req.body;
try {
const updatedCategory = await prisma.category.update({
where: { id },
data: { name },
});
res.status(200).json(updatedCategory);
} catch (err) {
console.log("Error while updating a Category:", err);
res
.status(400)
.json({ error: "Error updating category", details: err.message });
}
});
// Delete a category
app.delete("/categories/:id", async (req, res) => {
const { id } = req.params;
try {
await prisma.category.delete({
where: { id },
});
res.status(204).send();
} catch (err) {
console.log("Error while deleting a Category:", err);
res
.status(400)
.json({ error: "Error deleting category", details: err.message });
}
});
/* Comments */
// Post a comment
app.post("/comments", async (req, res) => {
const { comment, blogId } = req.body;
try {
const newComment = await prisma.comment.create({
data: { comment, blogId },
});
res.status(201).json(newComment);
} catch (err) {
console.error("Error while creating a New Comment:", err);
res.status(400).json({
error: "Error while creating a New Comment",
details: err.message,
});
}
});
// Get comments
app.get("/comments", async (req, res) => {
try {
const comments = await prisma.comment.findMany({
include: { blog: true },
});
res.status(200).json(comments);
} catch (err) {
console.error("Error while fetching comments:", err);
res.status(400).json({
error: "Error fetching comments",
details: err.message,
});
}
});
// Update a comment
app.put("/comments/:id", async (req, res) => {
const { id } = req.params;
const { comment } = req.body;
try {
const updatedComment = await prisma.comment.update({
where: { id },
data: { comment },
});
res.status(200).json(updatedComment);
} catch (err) {
console.error("Error while updating a comment:", err);
res.status(400).json({
error: "Error updating comment",
details: err.message,
});
}
});
// Delete a comment
app.delete("/comments/:id", async (req, res) => {
const { id } = req.params;
try {
await prisma.comment.delete({
where: { id },
});
res.status(204).send();
} catch (err) {
console.error("Error while deleting a comment:", err);
res.status(400).json({
error: "Error deleting comment",
details: err.message,
});
}
});
/* Blogs Tags */
// Create a new tag
app.post("/blogs/tag", async (req, res) => {
const { name } = req.body;
try {
const newTag = await prisma.tag.create({
data: { name },
});
res.status(201).json(newTag);
} catch (err) {
console.error("Error while creating a New Tag:", err);
res.status(400).json({
error: "Error while creating a New Tag",
details: err.message,
});
}
});
// Fetch all the tags
app.get("/blogs/tags", async (req, res) => {
try {
const tags = await prisma.tag.findMany({
include: { blogs: true },
});
res.status(200).json(tags);
} catch (err) {
console.error("Error while fetching tags:", err);
res.status(400).json({
error: "Error fetching tags",
details: err.message,
});
}
});
// Update tags
app.put("/blogs/tags/:id", async (req, res) => {
const { id } = req.params;
const { name, blogIDs } = req.body;
try {
const updatedTag = await prisma.tag.update({
where: { id },
data: { name, blogIDs },
});
res.status(200).json(updatedTag);
} catch (err) {
console.error("Error while updating tag:", err);
res.status(400).json({ error: "Error updating tag", details: err.message });
}
});
// Delete a tag
app.delete("/blogs/tags/:id", async (req, res) => {
const { id } = req.params;
try {
await prisma.tag.delete({
where: { id },
});
res.status(204).send();
} catch (err) {
console.error("Error while deleting tag:", err);
res
.status(400)
.json({ error: "Error deleting tag", details: err.message });
}
});
// start a server
app.listen(PORT, () => {
console.log(`Server is running at ${PORT}`);
});