A
This commit is contained in:
parent
aff58d8c08
commit
6d4e324a93
7 changed files with 1959 additions and 0 deletions
20
assignment-db-service/Dockerfile
Normal file
20
assignment-db-service/Dockerfile
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# syntax=docker/dockerfile:1
|
||||||
|
|
||||||
|
# Use the official Node.js image as the base image
|
||||||
|
ARG NODE_VERSION=22.13.1
|
||||||
|
FROM node:${NODE_VERSION}-slim AS base
|
||||||
|
|
||||||
|
# Set the working directory
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN apt-get update -y && apt-get install -y openssl && npm install && npx prisma generate
|
||||||
|
|
||||||
|
# Copy the application source code
|
||||||
|
COPY --link . .
|
||||||
|
|
||||||
|
# Expose the application port
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
# Define the command to run the application
|
||||||
|
CMD ["node", "app.js"]
|
||||||
130
assignment-db-service/app.js
Normal file
130
assignment-db-service/app.js
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
const express = require('express');
|
||||||
|
const { PrismaClient } = require('@prisma/client');
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
const port = process.env.NODE_PORT || 3000;
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
|
||||||
|
// Create Assignment
|
||||||
|
app.post('/assignments', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const {
|
||||||
|
CampID,
|
||||||
|
ProgramID,
|
||||||
|
StudentName,
|
||||||
|
SnakeGameId,
|
||||||
|
OriginalFile,
|
||||||
|
EditableFile,
|
||||||
|
AssignmentUrl,
|
||||||
|
Password,
|
||||||
|
InstructorID,
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
|
const hashedPassword = await bcrypt.hash(Password, 10);
|
||||||
|
|
||||||
|
const newAssignment = await prisma.assignment.create({
|
||||||
|
data: {
|
||||||
|
CampID,
|
||||||
|
ProgramID,
|
||||||
|
StudentName,
|
||||||
|
SnakeGameId,
|
||||||
|
OriginalFile,
|
||||||
|
EditableFile,
|
||||||
|
AssignmentUrl,
|
||||||
|
PasswordHash: hashedPassword,
|
||||||
|
InstructorID,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ message: 'Assignment created successfully', assignment: newAssignment });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error creating assignment:', err.message);
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get Assignments by InstructorID
|
||||||
|
app.get('/assignments/instructor/:instructorId', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { instructorId } = req.params;
|
||||||
|
|
||||||
|
const assignments = await prisma.assignment.findMany({
|
||||||
|
where: { InstructorID: parseInt(instructorId) },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (assignments.length === 0) {
|
||||||
|
return res.status(404).json({ message: 'No assignments found for this instructor' });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(assignments);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching assignments:', err.message);
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Read Assignment
|
||||||
|
app.get('/assignments/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const assignment = await prisma.assignment.findUnique({
|
||||||
|
where: { AssignmentID: parseInt(req.params.id) },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!assignment) {
|
||||||
|
return res.status(404).json({ message: 'Assignment not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(assignment);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching assignment:', err.message);
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update Assignment
|
||||||
|
app.put('/assignments/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const data = req.body;
|
||||||
|
|
||||||
|
if (data.Password) {
|
||||||
|
data.PasswordHash = await bcrypt.hash(data.Password, 10);
|
||||||
|
delete data.Password;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedAssignment = await prisma.assignment.update({
|
||||||
|
where: { AssignmentID: parseInt(id) },
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ message: 'Assignment updated successfully', assignment: updatedAssignment });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error updating assignment:', err.message);
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete Assignment
|
||||||
|
app.delete('/assignments/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
await prisma.assignment.delete({
|
||||||
|
where: { AssignmentID: parseInt(id) },
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ message: 'Assignment deleted successfully' });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error deleting assignment:', err.message);
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server running at http://localhost:${port}`);
|
||||||
|
});
|
||||||
32
assignment-db-service/fly.toml
Normal file
32
assignment-db-service/fly.toml
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
# fly.toml app configuration file generated for db-assignment-service on 2025-04-24T11:13:50-07:00
|
||||||
|
#
|
||||||
|
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
|
||||||
|
#
|
||||||
|
|
||||||
|
app = 'db-assignment-service'
|
||||||
|
primary_region = 'sea'
|
||||||
|
|
||||||
|
[build]
|
||||||
|
|
||||||
|
[http_service]
|
||||||
|
internal_port = 3000
|
||||||
|
force_https = true
|
||||||
|
auto_stop_machines = 'stop'
|
||||||
|
auto_start_machines = true
|
||||||
|
min_machines_running = 0
|
||||||
|
processes = ['app']
|
||||||
|
|
||||||
|
[[services]]
|
||||||
|
protocol = 'tcp'
|
||||||
|
internal_port = 3000
|
||||||
|
ports = []
|
||||||
|
|
||||||
|
[services.concurrency]
|
||||||
|
type = 'requests'
|
||||||
|
hard_limit = 1000
|
||||||
|
soft_limit = 500
|
||||||
|
|
||||||
|
[[vm]]
|
||||||
|
memory = '1gb'
|
||||||
|
cpu_kind = 'shared'
|
||||||
|
cpus = 1
|
||||||
1738
assignment-db-service/package-lock.json
generated
Normal file
1738
assignment-db-service/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
13
assignment-db-service/package.json
Normal file
13
assignment-db-service/package.json
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"@prisma/client": "^6.1.0",
|
||||||
|
"bcrypt": "^5.1.1",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"nodemon": "^3.1.9",
|
||||||
|
"prisma": "^6.1.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"dev": "nodemon app.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
22
assignment-db-service/prisma/schema.prisma
Normal file
22
assignment-db-service/prisma/schema.prisma
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
binaryTargets = ["native", "windows"]
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "postgresql"
|
||||||
|
url = env("DATABASE_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
model Assignment {
|
||||||
|
AssignmentID Int @id @default(autoincrement())
|
||||||
|
CampID Int?
|
||||||
|
ProgramID Int?
|
||||||
|
StudentName String
|
||||||
|
SnakeGameId String?
|
||||||
|
OriginalFile String?
|
||||||
|
EditableFile String?
|
||||||
|
AssignmentUrl String?
|
||||||
|
PasswordHash String // store bcrypt hash
|
||||||
|
InstructorID Int?
|
||||||
|
}
|
||||||
4
assignment-db-service/readme.md
Normal file
4
assignment-db-service/readme.md
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# database microservice
|
||||||
|
- store database tables
|
||||||
|
- issue and re-issue auth tokens (jwt)
|
||||||
|
- handle password reset into db
|
||||||
Loading…
Add table
Add a link
Reference in a new issue