first commit #1
3
server/.gitignore
vendored
Normal file
3
server/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
node_modules
|
||||
# Keep environment variables out of version control
|
||||
.env
|
337
server/index.js
Normal file
337
server/index.js
Normal file
|
@ -0,0 +1,337 @@
|
|||
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}`);
|
||||
});
|
1116
server/package-lock.json
generated
Normal file
1116
server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
21
server/package.json
Normal file
21
server/package.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "server",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"@prisma/client": "^6.2.1",
|
||||
"body-parser": "^1.20.3",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"express": "^4.21.2",
|
||||
"mongodb": "^6.12.0",
|
||||
"prisma": "^6.2.1"
|
||||
}
|
||||
}
|
44
server/prisma/schema.prisma
Normal file
44
server/prisma/schema.prisma
Normal file
|
@ -0,0 +1,44 @@
|
|||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "mongodb"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
|
||||
model Blog {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
slug String @unique
|
||||
title String
|
||||
body String
|
||||
comments Comment[]
|
||||
|
||||
categoryIDs String[] @db.ObjectId
|
||||
categories Category[] @relation(fields: [categoryIDs], references: [id])
|
||||
|
||||
tagIDs String[] @db.ObjectId
|
||||
tags Tag[] @relation(fields: [categoryIDs], references: [id])
|
||||
}
|
||||
|
||||
model Comment {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
comment String
|
||||
blog Blog @relation(fields: [blogId], references: [id])
|
||||
blogId String @db.ObjectId
|
||||
}
|
||||
|
||||
model Category {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
name String
|
||||
blogIDs String[] @db.ObjectId
|
||||
blogs Blog[] @relation(fields: [blogIDs], references: [id])
|
||||
}
|
||||
|
||||
model Tag {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
name String
|
||||
blogIDs String[] @db.ObjectId
|
||||
blogs Blog[] @relation(fields: [blogIDs], references: [id])
|
||||
}
|
Loading…
Reference in a new issue