feat: basic api setup and boilerplate

This commit is contained in:
ARJUN S THAMPI
2026-03-12 14:15:44 +05:30
commit 521a1fea79
24 changed files with 684 additions and 0 deletions
@@ -0,0 +1,76 @@
import prisma from "../prisma/client.js";
import {generateToken} from "../utils/jwt.js";
import {hashPassword, comparePassword} from "../utils/password.js";
/**
* REGISTER
* POST /api/auth/register
*/
export async function register(req, res) {
const {username, password, role} = req.body;
if (!username || !password) {
return res.status(400).json({error: "Username and password required"});
}
const existingUser = await prisma.user.findUnique({
where: {username},
});
if (existingUser) {
return res.status(409).json({error: "Username already exists"});
}
const hashedPassword = await hashPassword(password);
const user = await prisma.user.create({
data: {
username,
password: hashedPassword,
role: role || "admin",
},
});
res.status(201).json({
message: "User registered successfully",
user: {
id: user.id,
username: user.username,
role: user.role,
},
});
}
/**
* LOGIN
* POST /api/auth/login
*/
export async function login(req, res) {
const {username, password} = req.body;
if (!username || !password) {
return res.status(400).json({error: "Username and password required"});
}
const user = await prisma.user.findUnique({
where: {username},
});
if (!user) {
return res.status(401).json({error: "Invalid credentials"});
}
const isValid = await comparePassword(password, user.password);
if (!isValid) {
return res.status(401).json({error: "Invalid credentials"});
}
const token = generateToken({
userId: user.id,
username: user.username,
role: user.role,
});
res.json({token});
}
+110
View File
@@ -0,0 +1,110 @@
import prisma from "../prisma/client.js";
/* CREATE BLOG */
export async function createBlog(req, res) {
const {title, writer, image, content, isActive} = req.body;
try {
const blog = await prisma.blog.create({
data: {
title,
writer,
image,
content,
isActive,
},
});
res.json(blog);
} catch (error) {
res.status(500).json({error: "Blog creation failed"});
}
}
/* GET ALL BLOGS (Public) */
export async function getBlogs(req, res) {
try {
const blogs = await prisma.blog.findMany({
where: {isActive: true},
orderBy: {createdAt: "desc"},
});
res.json(blogs);
} catch (error) {
res.status(500).json({error: error.message});
}
}
/* GET ALL BLOGS (Admin) */
export async function getAllBlogs(req, res) {
try {
const blogs = await prisma.blog.findMany({
orderBy: {createdAt: "desc"},
});
res.json(blogs);
} catch (error) {
res.status(500).json({error: error.message});
}
}
/* GET SINGLE BLOG */
export async function getBlog(req, res) {
try {
const id = Number(req.params.id);
const blog = await prisma.blog.findUnique({
where: {id},
});
if (!blog) {
return res.status(404).json({error: "Blog not found"});
}
res.json(blog);
} catch (error) {
res.status(500).json({error: error.message});
}
}
/* UPDATE BLOG */
export async function updateBlog(req, res) {
try {
const {title, writer, image, content} = req.body;
const blog = await prisma.blog.update({
where: {id: Number(req.params.id)},
data: {
title,
writer,
image,
content,
},
});
res.json(blog);
} catch (error) {
res.status(500).json({error: error.message});
}
}
/* DELETE BLOG */
export async function deleteBlog(req, res) {
try {
const id = Number(req.params.id);
await prisma.blog.delete({
where: {id},
});
res.json({message: "Blog deleted successfully"});
} catch (error) {
res.status(500).json({error: error.message});
}
}
@@ -0,0 +1,66 @@
import prisma from "../prisma/client.js";
export const getAllDepartments = async (req, res) => {
try {
const departments = await prisma.department.findMany({
orderBy: {name: "asc"},
});
const response = departments.map((dep) => ({
departmentId: dep.departmentId,
Department: dep.name,
para1: dep.para1 ?? "",
para2: dep.para2 ?? "",
para3: dep.para3 ?? "",
facilities: dep.facilities ?? "",
services: dep.services ?? "",
}));
return res.status(200).json({
success: true,
data: response,
});
} catch (error) {
console.error(error);
return res.status(500).json({
success: false,
message: "Failed to fetch departments",
});
}
};
export async function createDepartment(req, res) {
try {
const {departmentId, name, para1, para2, para3, facilities, services} =
req.body;
if (!departmentId || !name) {
return res
.status(400)
.json({error: "departmentId and name are required"});
}
const department = await prisma.department.create({
data: {
departmentId,
name,
para1,
para2,
para3,
facilities,
services,
},
});
res.status(201).json({
message: "Department created successfully",
data: department,
});
} catch (error) {
if (error.code === "P2002") {
return res.status(409).json({error: "Department already exists"});
}
res.status(500).json({error: "Failed to create department"});
}
}
@@ -0,0 +1,15 @@
import multer from "multer";
import path from "path";
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads/blog");
},
filename: function (req, file, cb) {
const fileName = Date.now() + path.extname(file.originalname);
cb(null, fileName);
},
});
export const upload = multer({storage});