microservices/assignment-service/routes/InstructorRouter.js

147 lines
5.1 KiB
JavaScript
Raw Normal View History

2025-04-24 11:36:30 -07:00
const intructorRouter = require("express").Router();
const passport = require("passport");
const axios = require("axios");
const multer = require("multer");
const FormData = require("form-data");
2025-05-02 12:41:41 -07:00
2025-05-01 16:07:55 -07:00
const DB_ASSIGNMENT_SERVICE_URL =
process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
2025-04-29 12:01:10 -07:00
console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL);
console.log("DEPLOY_API_URL:", DEPLOY_API_URL);
2025-05-02 12:41:41 -07:00
// Use memory storage to keep file in RAM
const upload = multer({ storage: multer.memoryStorage() });
2025-04-25 11:07:46 -07:00
// This endpoint is for instructors to create a new assignment
intructorRouter.post(
"/create",
upload.single("file"),
// passport.authenticate("jwt", { session: false }),
2025-04-29 12:01:10 -07:00
async (req, res) => {
2025-05-01 16:07:55 -07:00
try {
2025-05-02 12:41:41 -07:00
const file = req.file;
const assignmentData = req.body;
if (!file) {
return res.status(400).send("No file uploaded.");
2025-05-02 12:41:41 -07:00
}
2025-05-01 16:07:55 -07:00
console.log("Creating a new assignment with data:", req.body);
const response = await axios.post(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments`,
req.body
);
2025-05-01 16:07:55 -07:00
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
// call upload api to upload the file to S3
console.log("Uploading file to:", `${DEPLOY_API_URL}/${assignmentData.appname}/upload`);
const uploadResponse = await axios.post(`${DEPLOY_API_URL}/${assignmentData.appname}/upload`, {
"appName": assignmentData.appname,
"notebookName": file.originalname,
"fileContentBase64": file.buffer.toString('base64'),
});
console.log('Response from DEPLOY_API_URL:', uploadResponse.data);
// Deploy a new Battlesnake API
console.log('Deploying a new Battlesnake API');
console.log("DEPLOY_API_URL:", DEPLOY_API_URL, assignmentData.appname);
const deployResponse = await axios.post(`${DEPLOY_API_URL}/deploy`, {
"appName": assignmentData.appname
});
console.log('Response from DEPLOY_API_URL:', deployResponse.data);
2025-05-01 16:07:55 -07:00
res.status(response.status).json(response.data);
} catch (error) {
console.error("Error creating assignment:", error.message);
res.status(error.response?.status || 500).json({ error: error.message });
}
2025-04-24 11:36:30 -07:00
}
2025-05-01 16:07:55 -07:00
);
2025-04-24 11:36:30 -07:00
2025-04-29 12:01:10 -07:00
// This endpoint is for instructors to get details of a specific assignment
intructorRouter.get("/details/:id", async (req, res) => {
2025-04-24 11:36:30 -07:00
try {
2025-04-29 12:01:10 -07:00
const assignmentId = req.params.id;
console.log("Fetching details for assignmentId:", assignmentId);
2025-05-01 16:07:55 -07:00
const response = await axios.get(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`
);
2025-04-29 12:01:10 -07:00
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
2025-04-24 11:36:30 -07:00
res.status(response.status).json(response.data);
} catch (error) {
2025-04-29 12:01:10 -07:00
console.error("Error fetching assignment details:", error.message);
2025-04-24 11:36:30 -07:00
res.status(error.response?.status || 500).json({ error: error.message });
}
});
2025-04-29 12:01:10 -07:00
// This endpoint is for instructors to get a list of assignments they have created
2025-05-01 16:07:55 -07:00
intructorRouter.get(
"/list/:id",
// passport.authenticate("jwt", { session: false }),
async (req, res) => {
// if (req.isAuthenticated()) {
2025-04-29 12:01:10 -07:00
try {
const instructorId = req.params.id;
2025-05-05 22:21:24 -07:00
// console.log("Fetching assignments for instructorId:", instructorId);
2025-05-01 16:07:55 -07:00
const response = await axios.get(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/instructor/${instructorId}`
);
2025-05-05 22:21:24 -07:00
// console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
2025-04-29 12:01:10 -07:00
res.status(response.status).json(response.data);
} catch (error) {
res.status(error.response?.status || 500).json({ error: error.message });
}
2025-05-01 16:07:55 -07:00
// } else {
// return res.status(401).json({ error: "Not authenticated" });
// }
}
);
2025-04-29 12:01:10 -07:00
2025-04-25 11:07:46 -07:00
// This endpoint is for instructors to update an assignment
2025-05-01 16:07:55 -07:00
intructorRouter.put(
"/update/:id",
2025-05-05 22:21:24 -07:00
upload.none(), // No file upload for this endpoint
2025-05-01 16:07:55 -07:00
// passport.authenticate("jwt", { session: false }),
2025-04-29 12:01:10 -07:00
async (req, res) => {
2025-05-01 16:07:55 -07:00
try {
const assignmentId = req.params.id;
2025-05-05 22:21:24 -07:00
console.log("Updating assignment with ID:", assignmentId);
console.log("Request body:", req.body);
2025-05-01 16:07:55 -07:00
const response = await axios.put(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`,
req.body
);
2025-05-05 22:21:24 -07:00
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
2025-05-01 16:07:55 -07:00
res.status(response.status).json(response.data);
} catch (error) {
2025-05-05 22:21:24 -07:00
console.error("Error updating assignment:", error.message);
2025-05-01 16:07:55 -07:00
res.status(error.response?.status || 500).json({ error: error.message });
}
2025-04-24 11:36:30 -07:00
}
2025-05-01 16:07:55 -07:00
);
2025-04-24 11:36:30 -07:00
2025-04-25 11:07:46 -07:00
// This endpoint is for instructors to delete an assignment
2025-05-01 16:07:55 -07:00
intructorRouter.delete(
"/delete/:id",
// passport.authenticate("jwt", { session: false }),
2025-04-29 12:01:10 -07:00
async (req, res) => {
2025-05-01 16:07:55 -07:00
try {
const assignmentId = req.params.id;
const response = await axios.delete(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`
);
res.status(response.status).json(response.data);
} catch (error) {
res.status(error.response?.status || 500).json({ error: error.message });
}
2025-04-24 11:36:30 -07:00
}
2025-05-01 16:07:55 -07:00
);
2025-04-24 11:36:30 -07:00
2025-05-01 16:07:55 -07:00
module.exports = intructorRouter;