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,
|
2025-05-05 15:07:15 -07:00
|
|
|
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-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-05-01 16:07: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);
|
|
|
|
|
const assignments = await prisma.assignments.findMany({
|
|
|
|
|
where: { instructorid: parseInt(instructorId) },
|
2025-05-05 22:21:24 -07:00
|
|
|
orderBy: { assignmentid: 'asc' },
|
2025-04-24 11:36:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (assignments.length === 0) {
|
2025-04-29 12:01:10 -07:00
|
|
|
return res
|
|
|
|
|
.status(404)
|
|
|
|
|
.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 });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Read Assignment
|
2025-05-05 18:49:15 -07:00
|
|
|
app.get("/assignments/:qrNumber", async (req, res) => {
|
2025-04-24 11:36:03 -07:00
|
|
|
try {
|
2025-05-05 23:37:54 -07:00
|
|
|
console.log("Fetching assignment with QR Code Number:", req.params.qrNumber);
|
|
|
|
|
|
2025-05-06 10:43:48 -07:00
|
|
|
const assignment = await prisma.assignments.findUnique({
|
2025-05-05 18:49:15 -07:00
|
|
|
where: { qrcodenumber: parseInt(req.params.qrNumber) },
|
2025-04-24 11:36:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!assignment) {
|
2025-05-05 23:37:54 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-05-05 23:37:54 -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;
|
2025-05-06 10:43:48 -07:00
|
|
|
const assignment = await prisma.assignments.findUnique({
|
2025-05-05 22:21:24 -07:00
|
|
|
where: { appname: appName },
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-06 10:43:48 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-05-06 10:43:48 -07:00
|
|
|
console.log("Assignment found:", assignment);
|
|
|
|
|
res.json(assignment);
|
2025-05-05 22:21:24 -07:00
|
|
|
} catch (err) {
|
2025-05-06 10:43:48 -07:00
|
|
|
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;
|
2025-05-01 16:07:55 -07:00
|
|
|
const assignment = await convertToAssignment(req);
|
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) {
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
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-04-29 12:01:10 -07:00
|
|
|
await prisma.assignments.delete({
|
|
|
|
|
where: { assignmentid: parseInt(id) },
|
2025-04-24 11:36:03 -07:00
|
|
|
});
|
|
|
|
|
|
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}`);
|
|
|
|
|
});
|