microservices/assignment-service/routes/InstructorRouter.js

129 lines
4.2 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");
2025-05-02 12:41:41 -07:00
const multer = require('multer');
const FormData = require('form-data');
2025-04-24 11:36:30 -07:00
2025-05-01 16:07:55 -07:00
const DB_ASSIGNMENT_SERVICE_URL =
process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
2025-04-29 12:01:10 -07:00
console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL);
2025-04-25 11:07:46 -07:00
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
2025-05-02 12:41:41 -07:00
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.');
}
await axios.post('https://target-api.com/endpoint', {
filename: file.originalname,
mimetype: file.mimetype,
content: file.buffer.toString('base64')
}, {
headers: {
'Content-Type': 'application/json'
}
});
2025-05-01 16:07:55 -07:00
console.log("Creating a new assignment with data:", req.body);
2025-05-02 12:41:41 -07:00
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);
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;
console.log("Fetching assignments for instructorId:", instructorId);
// const instructorId = req.user.userid; // Assuming req.user contains the authenticated user
2025-05-01 16:07:55 -07:00
const response = await axios.get(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/instructor/${instructorId}`
);
2025-04-29 12:01:10 -07:00
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
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",
// 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.put(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`,
req.body
);
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-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;