Working Database setupgit add .
This commit is contained in:
195
src/routes/grades.routes.ts
Normal file
195
src/routes/grades.routes.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
import { Router } from 'express';
|
||||
import { getGradesForUser, getFetchHistory, getFinalGrades } from '../services/grades.service';
|
||||
import { syncUserGrades } from '../services/sync.service';
|
||||
import { prisma } from '../db';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Get all grades for a user
|
||||
router.get('/users/:username/grades', async (req, res) => {
|
||||
try {
|
||||
const { username } = req.params;
|
||||
const user = await getGradesForUser(username);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
user: {
|
||||
username: user.username,
|
||||
createdAt: user.createdAt,
|
||||
classes: user.classes,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get classes for a user
|
||||
router.get('/users/:username/classes', async (req, res) => {
|
||||
try {
|
||||
const { username } = req.params;
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { username },
|
||||
include: {
|
||||
classes: {
|
||||
select: {
|
||||
id: true,
|
||||
className: true,
|
||||
teacher: true,
|
||||
period: true,
|
||||
category: true,
|
||||
createdAt: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'User not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
username: user.username,
|
||||
classes: user.classes,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get assignments for a specific class
|
||||
router.get('/users/:username/classes/:category/assignments', async (req, res) => {
|
||||
try {
|
||||
const { username, category } = req.params;
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { username },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'User not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Find all classes with this category (there might be multiple)
|
||||
const classes = await prisma.class.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
category: category,
|
||||
},
|
||||
include: {
|
||||
assignments: {
|
||||
orderBy: { createdAt: 'desc' },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (classes.length === 0) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'No classes found for this category',
|
||||
});
|
||||
}
|
||||
|
||||
// Return all classes with this category
|
||||
res.json({
|
||||
success: true,
|
||||
category,
|
||||
classes: classes.map(cls => ({
|
||||
className: cls.className,
|
||||
teacher: cls.teacher,
|
||||
period: cls.period,
|
||||
category: cls.category,
|
||||
assignments: cls.assignments,
|
||||
})),
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get fetch history for a user
|
||||
router.get('/users/:username/history', async (req, res) => {
|
||||
try {
|
||||
const { username } = req.params;
|
||||
const history = await getFetchHistory(username);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
username,
|
||||
count: history.length,
|
||||
history,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get final grades for a user
|
||||
router.get('/users/:username/final-grades', async (req, res) => {
|
||||
try {
|
||||
const { username } = req.params;
|
||||
const finalGrades = await getFinalGrades(username);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
username,
|
||||
count: finalGrades.length,
|
||||
finalGrades,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Manual sync for one user
|
||||
router.post('/sync/manual', async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Username and password are required',
|
||||
});
|
||||
}
|
||||
|
||||
const result = await syncUserGrades(username, password);
|
||||
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
59
src/routes/stats.routes.ts
Normal file
59
src/routes/stats.routes.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Router } from 'express';
|
||||
import { getOverallStats, getUserStats, getClassStats } from '../services/stats.service';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Overall system stats
|
||||
router.get('/overall', async (req, res) => {
|
||||
try {
|
||||
const stats = await getOverallStats();
|
||||
res.json({
|
||||
success: true,
|
||||
stats,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// User-specific stats
|
||||
router.get('/users/:username', async (req, res) => {
|
||||
try {
|
||||
const { username } = req.params;
|
||||
const stats = await getUserStats(username);
|
||||
res.json({
|
||||
success: true,
|
||||
stats,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Class-specific stats
|
||||
router.get('/users/:username/classes/:category', async (req, res) => {
|
||||
try {
|
||||
const { username, category } = req.params;
|
||||
const stats = await getClassStats(username, category);
|
||||
res.json({
|
||||
success: true,
|
||||
stats,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
90
src/routes/users.routes.ts
Normal file
90
src/routes/users.routes.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { Router } from 'express';
|
||||
import { prisma } from '../db';
|
||||
import { getAllUsers, deleteUser } from '../services/grades.service';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Register a new user
|
||||
router.post('/register', async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Username and password are required',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if user exists
|
||||
const existing = await prisma.user.findUnique({
|
||||
where: { username },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return res.status(409).json({
|
||||
success: false,
|
||||
error: 'Username already exists',
|
||||
});
|
||||
}
|
||||
|
||||
// Create user
|
||||
const user = await prisma.user.create({
|
||||
data: { username, password },
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
user,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get all users
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const users = await getAllUsers();
|
||||
res.json({
|
||||
success: true,
|
||||
count: users.length,
|
||||
users,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Delete user
|
||||
router.delete('/:username', async (req, res) => {
|
||||
try {
|
||||
const { username } = req.params;
|
||||
const result = await deleteUser(username);
|
||||
res.json({
|
||||
success: true,
|
||||
...result,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: errorMsg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user