added complete api functionality

This commit is contained in:
JBB0807 2025-04-29 12:01:10 -07:00
parent 956ede5619
commit 24bd570795
13 changed files with 205 additions and 107 deletions

View file

@ -1,2 +1,2 @@
#DB_ASSIGNMENT_SERVICE_URL = "http://localhost:3000/"
DB_ASSIGNMENT_SERVICE_URL = "http://db-assignment-service.internal:3000/"
#DB_ASSIGNMENT_SERVICE_URL = "http://localhost:3000"
DB_ASSIGNMENT_SERVICE_URL = "http://db-assignment-service.internal:3000"

View file

@ -1,3 +1,5 @@
DB_ASSIGNMENT_SERVICE_URL="http://localhost:3000/"
# DB_ASSIGNMENT_SERVICE_URL="http://db-assignment-service.internal:3000/"
#DB_ASSIGNMENT_SERVICE_URL="http://localhost:3000"
DB_ASSIGNMENT_SERVICE_URL="http://js-assignment-db-service:3200"
AUTH_SESSION_KEY="f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42"
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5173
PORT=8082

View file

@ -2,31 +2,61 @@ const intructorRouter = require("express").Router();
const passport = require("passport");
const axios = require("axios");
const { DB_ASSIGNMENT_SERVICE_URL } = process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
const DB_ASSIGNMENT_SERVICE_URL = process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL);
// This endpoint is for instructors to create a new assignment
intructorRouter.post("/create", passport.authenticate("jwt", { session: false }), async (req, res) => {
intructorRouter.post("/create",
// passport.authenticate("jwt", { session: false }),
async (req, res) => {
try {
console.log("Creating a new assignment with data:", req.body);
const response = await axios.post(`${DB_ASSIGNMENT_SERVICE_URL}/assignments`, req.body);
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 });
}
});
// This endpoint is for instructors to get details of a specific assignment
intructorRouter.get("/details/:id", async (req, res) => {
try {
const assignmentId = req.params.id;
console.log("Fetching details for assignmentId:", assignmentId);
const response = await axios.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`);
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 });
}
});
// This endpoint is for instructors to get a list of assignments they have created
intructorRouter.get("/list", passport.authenticate("jwt", { session: false }), async (req, res) => {
try {
const instructorId = req.user.id; // Assuming req.user contains the authenticated user
const response = await axios.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/instructor/${instructorId}`);
res.status(response.status).json(response.data);
} catch (error) {
res.status(error.response?.status || 500).json({ error: error.message });
}
intructorRouter.get("/list/:id", async (req, res) => {
// 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
const response = await axios.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/instructor/${instructorId}`);
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 });
}
// } else {
// return res.status(401).json({ error: "Not authenticated" });
// }
});
// This endpoint is for instructors to update an assignment
intructorRouter.put("/update/:id", passport.authenticate("jwt", { session: false }), async (req, res) => {
intructorRouter.put("/update/:id",
// passport.authenticate("jwt", { session: false }),
async (req, res) => {
try {
const assignmentId = req.params.id;
const response = await axios.put(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`, req.body);
@ -37,7 +67,9 @@ intructorRouter.put("/update/:id", passport.authenticate("jwt", { session: false
});
// This endpoint is for instructors to delete an assignment
intructorRouter.delete("/delete/:id", passport.authenticate("jwt", { session: false }), async (req, res) => {
intructorRouter.delete("/delete/:id",
// passport.authenticate("jwt", { session: false }),
async (req, res) => {
try {
const assignmentId = req.params.id;
const response = await axios.delete(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`);

View file

@ -1,10 +1,38 @@
require('dotenv').config();
const cors = require("cors");
const passport = require("passport");
const session = require("express-session");
const express = require("express");
const instructorRouter = require("./routes/InstructorRouter");
const studentRouter = require("./routes/StudentRouter");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// app.use(
// session({
// secret: process.env.AUTH_SESSION_KEY,
// resave: false,
// saveUninitialized: false,
// cookie: {
// maxAge: 24 * 60 * 60 * 1000, // 1 day
// },
// })
// );
// app.use(passport.initialize());
// app.use(passport.session());
// app.use(
// cors({
// origin: process.env.ACCEPTED_ORIGINS.split(","),
// methods: ["GET", "POST"],
// credentials: true,
// })
// )
app.use("/instructor", instructorRouter);
app.use("/student", studentRouter);