Skip to content

Commit 74b36fc

Browse files
committed
Update endpoints to check for duplicated names
1 parent ef9771b commit 74b36fc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

course-matrix/backend/src/controllers/timetablesController.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Request, Response } from "express";
22
import asyncHandler from "../middleware/asyncHandler";
33
import { supabase } from "../db/setupDb";
4+
import { maybeCoerceBoolean } from "openai/core";
45

56
export default {
67
/**
@@ -25,6 +26,26 @@ export default {
2526
.json({ error: "timetable title and semester are required" });
2627
}
2728

29+
// Check if a timetable with the same title already exist for this user
30+
const { data: existingTimetable, error: existingTimetableError } =
31+
await supabase
32+
.schema("timetable")
33+
.from("timetables")
34+
.select("id")
35+
.eq("user_id", user_id)
36+
.eq("timetable_title", timetable_title)
37+
.maybeSingle();
38+
39+
if (existingTimetableError) {
40+
return res.status(400).json({ error: existingTimetableError.message });
41+
}
42+
43+
if (existingTimetable) {
44+
return res
45+
.status(400)
46+
.json({ error: "A timetable with this title already exist" });
47+
}
48+
2849
//Create query to insert the user_id and timetable_title into the db
2950
let insertTimetable = supabase
3051
.schema("timetable")
@@ -122,6 +143,25 @@ export default {
122143
.status(400)
123144
.json({ error: "Timetable not found or unauthorized" });
124145

146+
// Check for duplicate timetable title
147+
if (timetable_title) {
148+
const { data: existingTimetable, error: existingTimetableError } =
149+
await supabase
150+
.schema("timetable")
151+
.from("timetables")
152+
.select("*")
153+
.eq("user_id", user_id)
154+
.eq("timetable_title", timetable_title)
155+
.neq("id", id)
156+
.maybeSingle();
157+
158+
if (existingTimetable) {
159+
return res
160+
.status(400)
161+
.json({ error: "A timetable this title already exist" });
162+
}
163+
}
164+
125165
let updateData: any = {};
126166
if (timetable_title) updateData.timetable_title = timetable_title;
127167
if (semester) updateData.semester = semester;

0 commit comments

Comments
 (0)