Compare commits

..

No commits in common. "dev" and "main" have entirely different histories.
dev ... main

6 changed files with 0 additions and 2196 deletions

3
server/.gitignore vendored
View file

@ -1,3 +0,0 @@
node_modules
# Keep environment variables out of version control
.env

View file

@ -1,343 +0,0 @@
const express = require("express");
const cors = require("cors");
const { PrismaClient } = require("@prisma/client");
const swaggerUi = require("swagger-ui-express"); // Import Swagger UI
const swaggerDocument = require("./swagger.json");
// Serve Swagger UI
const app = express();
const prisma = new PrismaClient();
const PORT = process.env.PORT || 4000;
// Middleware
app.use(cors());
app.use(express.json());
// Swagger UI setup
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
/* 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}`);
});

1505
server/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,24 +0,0 @@
{
"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",
"joi": "^17.13.3",
"mongodb": "^6.12.0",
"prisma": "^6.2.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1"
}
}

View file

@ -1,44 +0,0 @@
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])
}

View file

@ -1,277 +0,0 @@
{
"openapi": "3.0.0",
"info": {
"title": "Blog API Documentation",
"version": "1.0.0",
"description": "API documentation for the Blog CRUD application"
},
"tags": [
{
"name": "Blog CRUD",
"description": "Operations related to managing blogs"
},
{
"name": "Category CRUD",
"description": "Operations related to managing categories"
}
],
"servers": [
{
"url": "http://localhost:4000"
}
],
"paths": {
"/blogs": {
"get": {
"tags": ["Blog CRUD"],
"summary": "Retrieve all blogs",
"responses": {
"200": {
"description": "A list of blogs",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Blog"
}
}
}
}
}
}
},
"post": {
"tags": ["Blog CRUD"],
"summary": "Create a new blog",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Blog"
}
}
}
},
"responses": {
"201": {
"description": "Blog created successfully"
}
}
}
},
"/blogs/{slug}": {
"get": {
"summary": "Retrieve a blog by slug",
"parameters": [
{
"in": "path",
"name": "slug",
"required": true,
"schema": {
"type": "string"
},
"description": "The slug of the blog"
}
],
"responses": {
"200": {
"description": "A single blog",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Blog"
}
}
}
},
"404": {
"description": "Blog not found"
}
}
},
"put": {
"summary": "Update a blog by slug",
"parameters": [
{
"in": "path",
"name": "slug",
"required": true,
"schema": {
"type": "string"
},
"description": "The slug of the blog to update"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Blog"
}
}
}
},
"responses": {
"200": {
"description": "Blog updated successfully"
}
}
},
"delete": {
"summary": "Delete a blog by slug",
"parameters": [
{
"in": "path",
"name": "slug",
"required": true,
"schema": {
"type": "string"
},
"description": "The slug of the blog to delete"
}
],
"responses": {
"204": {
"description": "Blog deleted successfully"
}
}
}
},
"/categories": {
"get": {
"summary": "Retrieve all categories",
"responses": {
"200": {
"description": "A list of categories",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Category"
}
}
}
}
}
}
},
"post": {
"summary": "Create a new category",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Category"
}
}
}
},
"responses": {
"201": {
"description": "Category created successfully"
}
}
}
},
"/categories/{id}": {
"put": {
"summary": "Update a category",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "integer"
},
"description": "The ID of the category to update"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Category"
}
}
}
},
"responses": {
"200": {
"description": "Category updated successfully"
}
}
},
"delete": {
"summary": "Delete a category",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "integer"
},
"description": "The ID of the category to delete"
}
],
"responses": {
"204": {
"description": "Category deleted successfully"
}
}
}
}
},
"components": {
"schemas": {
"Blog": {
"type": "object",
"required": ["slug", "title", "body"],
"properties": {
"slug": {
"type": "string",
"description": "Unique identifier for the blog"
},
"title": {
"type": "string",
"description": "The title of the blog"
},
"body": {
"type": "string",
"description": "The content of the blog"
},
"categoryIDs": {
"type": "array",
"items": {
"type": "integer"
}
},
"tagIDs": {
"type": "array",
"items": {
"type": "integer"
}
}
}
},
"Category": {
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"description": "The name of the category"
}
}
}
}
}
}