added back-end call for edit

This commit is contained in:
JBB0807 2025-05-05 22:21:24 -07:00
parent ad50756440
commit 5a79b9970b
5 changed files with 209 additions and 63 deletions

View file

@ -4,27 +4,6 @@ const axios = require("axios");
const multer = require("multer");
const FormData = require("form-data");
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
// const {
// COMMON_BUCKET,
// AWS_ACCESS_KEY_ID,
// AWS_SECRET_ACCESS_KEY,
// AWS_ENDPOINT_URL_S3,
// AWS_REGION,
// } = process.env;
// Log environment variables for debugging
// console.log('--- ENV START ---');
// console.log('COMMON_BUCKET: ', COMMON_BUCKET);
// console.log('AWS_ACCESS_KEY_ID: ', AWS_ACCESS_KEY_ID ? '(found)' : '(NOT SET)');
// console.log('AWS_SECRET_ACCESS_KEY:', AWS_SECRET_ACCESS_KEY ? '(found)' : '(NOT SET)');
// console.log('AWS_ENDPOINT_URL_S3: ', AWS_ENDPOINT_URL_S3);
// console.log('AWS_REGION: ', AWS_REGION);
// console.log('--- ENV END ---');
const DB_ASSIGNMENT_SERVICE_URL =
process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
@ -33,14 +12,6 @@ const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL);
console.log("DEPLOY_API_URL:", DEPLOY_API_URL);
// const s3 = new AWS.S3({
// endpoint: AWS_ENDPOINT_URL_S3,
// region: AWS_REGION,
// credentials: new AWS.Credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY),
// s3ForcePathStyle: true
// });
// Use memory storage to keep file in RAM
const upload = multer({ storage: multer.memoryStorage() });
@ -65,17 +36,6 @@ intructorRouter.post(
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
// Upload the file to the S3 bucket
// console.log('Uploading notebook to S3');
// const key = `${assignmentData.appname}/notebooks/${Date.now()}-notebook.ipynb`;
// console.log('S3 key:', key);
// await s3.putObject({
// Bucket: COMMON_BUCKET,
// Key: key,
// Body: file.buffer,
// ContentType: 'application/json'
// }).promise();
// 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`, {
@ -126,12 +86,11 @@ intructorRouter.get(
// if (req.isAuthenticated()) {
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
// console.log("Fetching assignments for instructorId:", instructorId);
const response = await axios.get(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/instructor/${instructorId}`
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
// 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 });
@ -145,17 +104,23 @@ intructorRouter.get(
// This endpoint is for instructors to update an assignment
intructorRouter.put(
"/update/:id",
upload.none(), // No file upload for this endpoint
// passport.authenticate("jwt", { session: false }),
async (req, res) => {
try {
const assignmentId = req.params.id;
console.log("Updating assignment with ID:", assignmentId);
console.log("Request body:", req.body);
const response = await axios.put(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`,
req.body
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
res.status(response.status).json(response.data);
} catch (error) {
console.error("Error updating assignment:", error.message);
res.status(error.response?.status || 500).json({ error: error.message });
}
}