microservices/assignment-db-service/app.js

300 lines
8.1 KiB
JavaScript
Raw Permalink Normal View History

2025-04-29 12:01:10 -07:00
const express = require("express");
const { PrismaClient } = require("@prisma/client");
const bcrypt = require("bcrypt");
2025-04-24 11:36:03 -07:00
const app = express();
const prisma = new PrismaClient();
const port = process.env.NODE_PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
2025-05-01 16:07:55 -07:00
async function encryptPassword(password) {
if (!password) {
return null;
}
return new Promise((resolve, reject) => {
bcrypt.hash(password, 10, (err, hash) => {
if (err) {
reject(err);
} else {
resolve(hash);
}
});
});
}
2025-04-29 12:01:10 -07:00
//function to conver req.body to assignment
2025-05-01 16:07:55 -07:00
async function convertToAssignment(req) {
2025-05-05 22:21:24 -07:00
console.log("Converting request body to assignment object...");
2025-04-29 12:01:10 -07:00
const {
campid,
programid,
studentname,
snakegameid,
appname,
qrcodenumber,
description,
2025-04-29 12:01:10 -07:00
assignmenturl,
password,
instructorid
} = req.body;
2025-05-05 22:21:24 -07:00
console.log("Request body fields:", {
campid,
programid,
studentname,
snakegameid,
appname,
qrcodenumber,
description,
assignmenturl,
password,
instructorid
});
2025-05-01 16:07:55 -07:00
const hashPassword = await encryptPassword(req.body.password);
2025-05-05 22:21:24 -07:00
console.log("Password hash generated:", hashPassword);
const assignment = {
campid: campid ? parseInt(campid) : null,
programid: programid ? parseInt(programid) : null,
studentname: studentname || null,
snakegameid: snakegameid || null,
appname: appname || null,
qrcodenumber: qrcodenumber ? parseInt(qrcodenumber) : null,
description: description || null,
assignmenturl: assignmenturl || null,
passwordhash: hashPassword || null,
instructorid: instructorid ? parseInt(instructorid) : null,
};
console.log("Converted assignment object:", assignment);
return assignment;
2025-08-25 14:23:55 -07:00
}
2025-04-29 12:01:10 -07:00
2025-04-24 11:36:03 -07:00
// Create Assignment
2025-04-29 12:01:10 -07:00
app.post("/assignments", async (req, res) => {
2025-04-24 11:36:03 -07:00
try {
2025-04-29 12:01:10 -07:00
console.log("Request body:", req.body);
2025-05-01 16:07:55 -07:00
const assignment = await convertToAssignment(req);
2025-04-29 12:01:10 -07:00
const newAssignment = await prisma.assignments.create({
2025-05-01 16:07:55 -07:00
data: assignment,
2025-04-24 11:36:03 -07:00
});
2025-08-25 14:23:55 -07:00
2025-04-29 12:01:10 -07:00
console.log("Assignment created successfully:", newAssignment);
res.json({
message: "Assignment created successfully",
assignment: newAssignment,
});
2025-04-24 11:36:03 -07:00
} catch (err) {
2025-04-29 12:01:10 -07:00
console.error("Error creating assignment:", err.message);
2025-04-24 11:36:03 -07:00
res.status(500).json({ error: err.message });
}
});
// Get Assignments by InstructorID
2025-04-29 12:01:10 -07:00
app.get("/assignments/instructor/:instructorId", async (req, res) => {
2025-04-24 11:36:03 -07:00
try {
const { instructorId } = req.params;
2025-04-29 12:01:10 -07:00
console.log("InstructorID:", instructorId);
2025-08-25 14:23:55 -07:00
// changes below
// const whereClause = { instructorid: parseInt(instructorId) };
const whereClause = {}
// changes above
2025-04-29 12:01:10 -07:00
const assignments = await prisma.assignments.findMany({
2025-08-25 14:23:55 -07:00
// where: { instructorid: parseInt(instructorId) },
where: whereClause,
// orderBy: { assignmentid: 'asc' }, // commnented out to return in the chronological order
2025-04-24 11:36:03 -07:00
});
if (assignments.length === 0) {
2025-04-29 12:01:10 -07:00
return res
2025-08-25 14:23:55 -07:00
.status(204)
2025-04-29 12:01:10 -07:00
.json({ message: "No assignments found for this instructor" });
2025-04-24 11:36:03 -07:00
}
res.json(assignments);
} catch (err) {
2025-04-29 12:01:10 -07:00
console.error("Error fetching assignments:", err.message);
2025-04-24 11:36:03 -07:00
res.status(500).json({ error: err.message });
}
});
2025-08-25 14:23:55 -07:00
// // Get Assignments by optional InstructorID (if not provided, return all assignments)
// app.get("/assignments/instructor/:instructorId?", async (req, res) => {
// try {
// // force all queries to return all assignments even if instructorId is provided
// // const { instructorId } = req.params;
// const instructorId = null;
// const whereClause = instructorId
// ? { instructorid: parseInt(instructorId) }
// : {};
// const assignments = await prisma.assignments.findMany({
// where: whereClause,
// orderBy: { assignmentid: 'asc' },
// });
// if (assignments.length === 0) {
// return res.status(204).json({
// message: instructorId
// ? "No assignments found for this instructor"
// : "No assignments found",
// });
// }
// res.json(assignments);
// } catch (err) {
// console.error("Error fetching assignments:", err.message);
// res.status(500).json({ error: err.message });
// }
// });
2025-05-06 13:28:01 -07:00
//Get assignment by assignmentid
app.get("/assignments/:id", async (req, res) => {
try {
const { id } = req.params;
console.log("Fetching assignment with ID:", id);
const assignment = await prisma.assignments.findUnique({
where: { assignmentid: parseInt(id) },
});
if (!assignment) {
console.log("No assignment found for ID:", id);
return res.status(404).json({ message: "Assignment not found" });
}
console.log("Assignment found:", assignment);
res.json(assignment);
} catch (err) {
console.error("Error fetching assignment:", err.message);
res.status(500).json({ error: err.message });
}
});
// Get Assignment by QR Code Number
app.get("/assignments/qr/:qrNumber", async (req, res) => {
2025-04-24 11:36:03 -07:00
try {
console.log("Fetching assignment with QR Code Number:", req.params.qrNumber);
const assignment = await prisma.assignments.findUnique({
where: { qrcodenumber: parseInt(req.params.qrNumber) },
2025-04-24 11:36:03 -07:00
});
if (!assignment) {
console.log("No assignment found for QR Code Number:", req.params.qrNumber);
2025-04-29 12:01:10 -07:00
return res.status(404).json({ message: "Assignment not found" });
2025-04-24 11:36:03 -07:00
}
console.log("Assignment found:", assignment);
2025-04-24 11:36:03 -07:00
res.json(assignment);
} catch (err) {
2025-04-29 12:01:10 -07:00
console.error("Error fetching assignment:", err.message);
2025-04-24 11:36:03 -07:00
res.status(500).json({ error: err.message });
}
});
2025-05-05 22:21:24 -07:00
//get assignment by appname
app.get("/assignments/appname/:appName", async (req, res) => {
try {
const { appName } = req.params;
const assignment = await prisma.assignments.findUnique({
2025-05-05 22:21:24 -07:00
where: { appname: appName },
});
if (!assignment) {
console.log("No assignment found for app name:", req.params.qrNumber);
return res.status(404).json({ message: "Assignment not found" });
2025-05-05 22:21:24 -07:00
}
console.log("Assignment found:", assignment);
res.json(assignment);
2025-05-05 22:21:24 -07:00
} catch (err) {
console.error("Error fetching assignment:", err.message);
2025-05-05 22:21:24 -07:00
res.status(500).json({ error: err.message });
}
});
2025-04-24 11:36:03 -07:00
// Update Assignment
2025-04-29 12:01:10 -07:00
app.put("/assignments/:id", async (req, res) => {
2025-04-24 11:36:03 -07:00
try {
const { id } = req.params;
console.log("Updating assignment with ID:", id);
2025-05-01 16:07:55 -07:00
const assignment = await convertToAssignment(req);
console.log("Converted assignment object for update:", assignment);
2025-04-24 11:36:03 -07:00
2025-05-05 22:21:24 -07:00
const existingAssignment = await prisma.assignments.findUnique({
where: { assignmentid: parseInt(id) },
});
if (!existingAssignment) {
console.log("No existing assignment found for ID:", id);
2025-05-05 22:21:24 -07:00
return res.status(404).json({ message: "Assignment not found" });
}
// Update only the fields that are provided in the request body
Object.keys(assignment).forEach((key) => {
if (assignment[key]) {
existingAssignment[key] = assignment[key];
}
});
console.log("Existing Assignment before update:", existingAssignment);
2025-04-29 12:01:10 -07:00
const updatedAssignment = await prisma.assignments.update({
where: { assignmentid: parseInt(id) },
2025-05-05 22:21:24 -07:00
data: existingAssignment,
2025-04-24 11:36:03 -07:00
});
console.log("Assignment updated successfully:", updatedAssignment);
2025-04-29 12:01:10 -07:00
res.json({
message: "Assignment updated successfully",
assignment: updatedAssignment,
});
2025-04-24 11:36:03 -07:00
} catch (err) {
2025-04-29 12:01:10 -07:00
console.error("Error updating assignment:", err.message);
2025-04-24 11:36:03 -07:00
res.status(500).json({ error: err.message });
}
});
// Delete Assignment
2025-04-29 12:01:10 -07:00
app.delete("/assignments/:id", async (req, res) => {
2025-04-24 11:36:03 -07:00
try {
const { id } = req.params;
2025-05-08 00:14:10 -07:00
console.log("Deleting assignment with ID:", id);
2025-04-24 11:36:03 -07:00
2025-04-29 12:01:10 -07:00
await prisma.assignments.delete({
where: { assignmentid: parseInt(id) },
2025-04-24 11:36:03 -07:00
});
2025-05-08 00:14:10 -07:00
console.log("Assignment deleted successfully:", id);
2025-04-29 12:01:10 -07:00
res.json({ message: "Assignment deleted successfully" });
2025-04-24 11:36:03 -07:00
} catch (err) {
2025-04-29 12:01:10 -07:00
console.error("Error deleting assignment:", err.message);
2025-04-24 11:36:03 -07:00
res.status(500).json({ error: err.message });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
2025-05-06 13:28:01 -07:00