Skip to content

Commit 210043e

Browse files
committed
Add isEnrolled handler
1 parent 9f8b5cf commit 210043e

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

backend/internal/handlers/courses.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ func CreateCourse(c *fiber.Ctx) error {
196196
})
197197
}
198198

199+
// Returns a true if user is enrolled in course
200+
func IsEnrolled(c *fiber.Ctx) error {
201+
course_id := c.Params("course_id")
202+
user_id := c.Params("user_id")
203+
204+
var count int64
205+
206+
if err := database.DB.Table("enrollment").Where("account_id = ? AND course_id = ?", user_id, course_id).Count(&count).Error; err != nil {
207+
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Error retrieving courses"})
208+
}
209+
210+
if count > 0 {
211+
return c.JSON(fiber.Map{
212+
"message": "User is enrolled in this course",
213+
"isEnrolled": true,
214+
})
215+
} else {
216+
return c.JSON(fiber.Map{
217+
"message": "User is not enrolled in this course",
218+
"isEnrolled": false,
219+
})
220+
}
221+
}
222+
199223
// Enroll user into course
200224
func Enroll(c *fiber.Ctx) error {
201225
user_id := c.Params("user_id")

backend/internal/router/route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func SetupRoutes(app *fiber.App) {
9191
app.Post("/delete-file/:creator_id/:content_id", handlers.DeleteFile)
9292
app.Post("/edit-content/:creator_id/:content_id", handlers.EditContent)
9393
app.Post("/edit-thumbnail/:creator_id/:course_id", handlers.EditThumbnail)
94+
app.Get("/is-enrolled/:user_id/:course_id", handlers.IsEnrolled)
9495
app.Post("/enroll/:user_id/:course_id", handlers.Enroll)
9596
app.Delete("/unenroll/:user_id/:course_id", handlers.Unenroll)
9697
}

frontend/src/components/pages/Course.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function Course() {
1010
const [userInfo, setUserInfo] = useState(null);
1111
const [error, setError] = useState(null);
1212
const [file, setFile] = useState(null);
13-
const [isEnrolled, setIsEnrolled] = useState(false);
13+
const [isEnrolled, setIsEnrolled] = useState(null);
1414
const [newContentName, setNewContentName] = useState({
1515
title: "",
1616
});
@@ -212,12 +212,25 @@ export function Course() {
212212
.catch((error) => setError(error.message));
213213
}
214214

215+
async function fetchIsEnrolled() {
216+
await fetch(`http://localhost:4000/is-enrolled/${userId}/${courseID}`)
217+
.then((response) => {
218+
if (!response.ok) {
219+
throw new Error('Network response was not ok');
220+
}
221+
return response.json();
222+
})
223+
.then((data) => setIsEnrolled(data.isEnrolled))
224+
.catch((error) => setError(error.message));
225+
}
226+
215227
fetchCourse();
216228
fetchUser();
229+
fetchIsEnrolled();
217230
}, [courseID]);
218231

219232
if (error) return <p>Error: {error}</p>;
220-
if (!courseInfo || !userInfo) return <p>Loading...</p>;
233+
if (!courseInfo || !userInfo || isEnrolled === null) return <p>Loading...</p>;
221234

222235
var moduleList = [];
223236
//Push all modules into moduleList

0 commit comments

Comments
 (0)