microservices/assignment-service/routes/StudentRouter.js

127 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-04-24 11:36:30 -07:00
const studentRouter = require("express").Router();
const passport = require("passport");
const axios = require("axios");
2025-05-02 12:41:41 -07:00
const bcrypt = require("bcrypt");
require("dotenv").config();
const DB_ASSIGNMENT_SERVICE_URL = process.env.DB_ASSIGNMENT_SERVICE_URL;
2025-05-07 15:36:59 -07:00
const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
2025-04-24 11:36:30 -07:00
2025-05-07 15:36:59 -07:00
studentRouter.post("/save", async (req, res) => {
//get the app name and code and save the latest jupiter file in s3 bucket
const { appName ,code } = req.body;
//convert the code to jupyter file format
const notebook = {
cells: [
{
cell_type: "code",
execution_count: null,
metadata: {},
outputs: [],
source: code.split('\n').map(line => line + '\n')
}
],
metadata: {
kernelspec: {
display_name: "Python 3",
language: "python",
name: "python3"
},
language_info: {
name: "python",
version: "3.x"
}
},
nbformat: 4,
nbformat_minor: 5
};
// Convert the notebook object to a JSON string and then to base64
const jsonString = JSON.stringify(notebook, null, 2);
const base64 = Buffer.from(jsonString, 'utf-8').toString('base64');
const notebookName = `${Date.now()}-notebook.ipynb`;
console.log("DEPLOY_API_URL:", DEPLOY_API_URL);
await fetch(`${DEPLOY_API_URL}/${appName}/upload`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ notebookName: notebookName, fileContentBase64: base64 })
})
.then((response) => {
if (!response.ok) throw new Error("Failed to save notebook");
return response.json();
})
.then((data) => {
console.log("Notebook saved successfully:", data);
res.status(200).json(data);
})
.catch((error) => {
console.error("Error saving notebook:", error.message);
res.status(500).json({ error: error.message });
});
});
2025-05-02 12:41:41 -07:00
2025-05-07 15:37:58 -07:00
studentRouter.post("/deploy", (req, res) => {
});
2025-05-02 12:41:41 -07:00
studentRouter.get("/assignment/:qrnum", (req, res) => {
const qrnum = req.params.qrnum;
console.log("Fetching details for qr number:", qrnum);
axios
2025-05-06 13:28:01 -07:00
.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/qr/${qrnum}`)
.then((response) => {
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
res.status(response.status).json(response.data);
})
.catch((error) => {
console.error("Error fetching assignment details:", error.message);
res.status(error.response?.status || 500).json({ error: error.message });
});
});
2025-05-02 15:06:00 -07:00
studentRouter.post("/verify", async (req, res) => {
2025-05-02 12:41:41 -07:00
try {
const qrNumber = req.body.qrNumber;
2025-05-02 12:41:41 -07:00
const password = req.body.password;
2025-05-02 15:06:00 -07:00
console.log("Received request to verify assignment.");
console.log("Request body:", req.body);
2025-05-02 12:41:41 -07:00
console.log(
"Accessing assignment with QR Number:",
qrNumber,
2025-05-02 12:41:41 -07:00
"and password:",
password
);
console.log(`Fetching from URL: ${DB_ASSIGNMENT_SERVICE_URL}/assignments/${qrNumber}`);
2025-05-02 12:41:41 -07:00
const response = await axios.get(
2025-05-06 13:28:01 -07:00
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/qr/${qrNumber}`
2025-05-02 12:41:41 -07:00
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
console.log("Password provided:", password);
console.log("Password hash from database:", response.data.passwordhash);
2025-05-02 12:41:41 -07:00
const isPasswordValid = await bcrypt.compare(
password,
response.data.passwordhash
2025-05-02 12:41:41 -07:00
);
2025-05-02 15:06:00 -07:00
console.log("Password validation result:", isPasswordValid);
2025-05-02 12:41:41 -07:00
if (!isPasswordValid || !response.data) {
2025-05-02 15:06:00 -07:00
console.log("Invalid id or password.");
2025-05-02 12:41:41 -07:00
return res.status(401).json({ error: "Invalid id and password" });
}
2025-05-02 15:06:00 -07:00
console.log("Verification successful. Sending response.");
2025-05-02 12:41:41 -07:00
res.status(response.status).json(response.data);
} catch (error) {
console.error("Error fetching assignment details:", error.message);
2025-05-02 15:06:00 -07:00
console.error("Error details:", error);
2025-05-02 12:41:41 -07:00
res.status(error.response?.status || 500).json({ error: error.message });
}
2025-04-24 11:36:30 -07:00
});
2025-05-02 12:41:41 -07:00
module.exports = studentRouter;