Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NextFunction, Request, Response } from "express";
import { Error as MongooseError } from "mongoose";

export function errorHandler(err: Error, _req: Request, res: Response, _next: NextFunction) {
if (err instanceof MongooseError.CastError) {
res.status(400).json({ error: `Invalid ID: ${err.value}` });
}
else {
res.status(500).json({ error: 'Internal server error' });
}
}
9 changes: 5 additions & 4 deletions api/routes/Alumni.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router, Request, Response } from 'express';
import Alumni from '../models/Alumni';
import { validate, createAlumniSchema, updateAlumniSchema, CreateAlumniInput, UpdateAlumniInput } from '../schema/AlumniSchema';

const router = Router();

Expand All @@ -25,19 +26,19 @@ router.get('/:id', async (req: Request, res: Response) => {
});

// Create a new alumni profile
router.post('/', async (req: Request, res: Response) => {
router.post('/', validate(createAlumniSchema), async (req: Request, res: Response) => {
try {
const alumni = await new Alumni(req.body).save();
const alumni = await new Alumni(req.body as CreateAlumniInput).save();
res.status(201).json(alumni);
} catch (err) {
res.status(500).json({ error: (err as Error).message });
}
});

// Update an existing alumni profile
router.put('/:id', async (req: Request, res: Response) => {
router.put('/:id', validate(updateAlumniSchema), async (req: Request, res: Response) => {
try {
const alumni = await Alumni.findByIdAndUpdate(req.params.id, req.body, {
const alumni = await Alumni.findByIdAndUpdate(req.params.id, req.body as UpdateAlumniInput, {
new: true,
runValidators: true,
});
Expand Down
58 changes: 58 additions & 0 deletions api/schema/AlumniSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { z } from "zod";
import { Request, Response } from "express";

export const createAlumniSchema = z.object({
bio: z.string().max(2600).optional(),
headline: z.string().max(220).optional(),
profilePhotoUrl: z.url("Must be a valid URL").optional(),
linkedInUrl: z.url("Must be a valid URL").regex(/linkedin\.com\/in/, "Must be a valid LinkedIn URL").optional(),
startYear: z.number().refine((y) => y.toString().length === 4, "Must be a 4-digit year"),
graduationYear: z.number().refine((y) => y.toString().length === 4, "Must be a 4-digit year"),
major: z.string().max(50).optional(),
experiences: z.array(
z.object({
company: z.string().min(1).max(100),
title: z.string().min(1).max(100),
startDate: z.date().optional(),
endDate: z.date().optional(),
isCurrent: z.boolean(),
description: z.string().max(2000)
})
).optional()
});

export type CreateAlumniInput = z.infer<typeof createAlumniSchema>;

export const updateAlumniSchema = z.object({
bio: z.string().max(2600).optional(),
headline: z.string().max(220).optional(),
profilePhotoUrl: z.url("Must be a valid URL").optional(),
linkedInUrl: z.url("Must be a valid URL").regex(/linkedin\.com\/in/, "Must be a valid LinkedIn URL").optional(),
startYear: z.number().refine((y) => y.toString().length === 4, "Must be a 4-digit year").optional(),
graduationYear: z.number().refine((y) => y.toString().length === 4, "Must be a 4-digit year").optional(),
major: z.string().max(50).optional(),
experiences: z.array(
z.object({
company: z.string().min(1).max(100).optional(),
title: z.string().min(1).max(100).optional(),
startDate: z.date().optional().optional(),
endDate: z.date().optional().optional(),
isCurrent: z.boolean().optional(),
description: z.string().max(2000).optional()
})
).optional()
})
.refine(data => Object.keys(data).length > 0, {
message: "At least one field must be provided for update",
});

export type UpdateAlumniInput = z.infer<typeof updateAlumniSchema>;

export const validate = (schema: z.ZodObject<any>) => (req: Request, res: Response, next: Function) => {
try {
schema.parse(req.body);
next();
} catch (err) {
res.status(400).json({ error: (err as Error).message });
}
};
2 changes: 2 additions & 0 deletions api/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import express from "express";
import mongoose from "mongoose";
import alumniRouter from "./routes/Alumni";
import { errorHandler } from "./middleware/errorHandler";

const app = express();
const PORT = 8081;

app.use(express.json());
app.use("/api", alumniRouter);
app.use(errorHandler);

const dbHost = process.env.DATABASE_HOST || "127.0.0.1";
mongoose
Expand Down
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"homepage": "https://github.com/SCE-Development/sce-linkedin#readme",
"dependencies": {
"express": "^5.2.1",
"mongoose": "^9.3.1"
"mongoose": "^9.3.1",
"zod": "^4.4.2"
},
"devDependencies": {
"@types/express": "^5.0.6",
Expand Down