Skip to content

Commit a99aecf

Browse files
authored
Merge pull request #26 from hack4impact-calpoly/day-schema-endpoints
Day schema endpoints
2 parents 9aea48f + b4098b3 commit a99aecf

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

src/app/api/day/[dayId]/route.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import connectDB from "@/database/db";
2+
import { NextResponse } from "next/server";
3+
import Day from "@/database/models/daySchema";
4+
5+
/**
6+
* gets 1 day from the database based on the dayId
7+
* returns the day in the database as a JSON response
8+
* if an error occurs, returns a JSON response with an error message and status code 500
9+
*/
10+
11+
type IParams = {
12+
params: {
13+
dayId: string;
14+
};
15+
};
16+
17+
export async function GET(request: Request, { params }: IParams): Promise<NextResponse> {
18+
// Attempt to connect to the database
19+
await connectDB();
20+
21+
const { dayId } = params;
22+
23+
try {
24+
const day = await Day.findOne({ dayId: dayId });
25+
// check if day exists
26+
if (!day) {
27+
return NextResponse.json(
28+
{
29+
message: "Day not found.",
30+
},
31+
{ status: 404 },
32+
);
33+
}
34+
// if day exists, return it
35+
return NextResponse.json(
36+
{
37+
day: day,
38+
},
39+
{ status: 200 },
40+
);
41+
} catch (err) {
42+
console.error("Error fetching day:", err);
43+
return NextResponse.json(
44+
{
45+
message: "Failed to fetch program.",
46+
error: err instanceof Error ? err.message : "An unknown error occurred.",
47+
},
48+
{ status: 500 },
49+
);
50+
}
51+
}

src/app/api/day/route.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import connectDB from "@/database/db";
2+
import { NextResponse } from "next/server";
3+
import Day from "@/database/models/daySchema";
4+
5+
/**
6+
* gets all days from the database
7+
* returns all days in the database as a JSON response
8+
* if an error occurs, returns a JSON response with an error message and status code 500
9+
*/
10+
11+
export async function GET(): Promise<NextResponse> {
12+
// Attempt to connect to the database
13+
await connectDB();
14+
15+
try {
16+
const days = await Day.find();
17+
return NextResponse.json(
18+
{
19+
days: days,
20+
},
21+
{ status: 200 },
22+
);
23+
} catch (err) {
24+
console.error("Error fetching days:", err);
25+
return NextResponse.json(
26+
{
27+
message: "Failed to fetch days.",
28+
error: err instanceof Error ? err.message : "An unknown error occurred.",
29+
},
30+
{ status: 500 },
31+
);
32+
}
33+
}
34+
35+
/*
36+
* creates a new program in the database
37+
* request must require the following fields:
38+
* name: string;
39+
date: Date;
40+
startTime: string;
41+
endTime: string;
42+
programId: string;
43+
dayId: string;
44+
*/
45+
export async function POST(request: Request): Promise<NextResponse> {
46+
await connectDB();
47+
48+
try {
49+
const body = await request.json();
50+
51+
// ensure required fields are present
52+
53+
// "dayOfWeek" is not required because it will be derived from the "date" field
54+
const requiredFields = ["name", "date", "startTime", "endTime", "programId", "dayId"];
55+
56+
for (const field of requiredFields) {
57+
if (!body[field]) {
58+
return NextResponse.json({ message: `Missing required field: ${field}` }, { status: 400 });
59+
}
60+
}
61+
62+
const dayDate = new Date(body.date);
63+
64+
// convert the date to a day of the week string (e.g., "Monday", "Tuesday", etc.)
65+
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
66+
const dayName = days[dayDate.getDay()];
67+
68+
const newDay = new Day({
69+
name: body.name,
70+
dayOfWeek: dayName,
71+
date: new Date(body.date),
72+
startTime: body.startTime,
73+
endTime: body.endTime,
74+
programId: body.programId,
75+
dayId: body.dayId,
76+
});
77+
78+
const saved = await newDay.save();
79+
80+
return NextResponse.json(
81+
{
82+
day: saved,
83+
},
84+
{ status: 201 },
85+
);
86+
} catch (err) {
87+
console.error("Error creating day:", err);
88+
return NextResponse.json(
89+
{
90+
message: "Failed to create day.",
91+
error: err instanceof Error ? err.message : "An unknown error occurred.",
92+
},
93+
{ status: 500 },
94+
);
95+
}
96+
}

src/database/models/daySchema.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import mongoose, { Schema } from "mongoose";
2+
3+
/*
4+
Use the "date" for the "dayOfWeek" field to determine the day of the week.
5+
*/
6+
7+
type day = {
8+
name: string;
9+
dayOfWeek: string;
10+
date: Date;
11+
startTime: string;
12+
endTime: string;
13+
programId: string;
14+
dayId: string;
15+
};
16+
17+
const daySchema = new Schema<day>({
18+
name: { type: String, required: true },
19+
dayOfWeek: { type: String, required: true },
20+
date: { type: Date, required: true },
21+
startTime: { type: String, required: true },
22+
endTime: { type: String, required: true },
23+
programId: { type: String, required: true },
24+
dayId: { type: String, required: true },
25+
});
26+
27+
/*
28+
"event" term has changed to "day"
29+
MongoDB still has this as event_data, but from now on
30+
events will be referred to as days in the codebase.
31+
*/
32+
33+
const Day = mongoose.models["event_data"] || mongoose.model("event_data", daySchema, "event_data");
34+
35+
export default Day;

0 commit comments

Comments
 (0)