Skip to content

Commit

Permalink
Refactor routes and utils/KV modules
Browse files Browse the repository at this point in the history
  • Loading branch information
m7medVision committed Jan 16, 2024
1 parent a7506ae commit a984a99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export const handler: Handlers<Props> = {
async GET(_req, ctx) {
const courses = await getCourses(cache);
const session = getCookies(_req.headers)["sessionId"];
const total = getNumberOfCourses(courses.courses);
if (!session) {
return ctx.render({
completed: [],
total: getNumberOfCourses(courses.courses),
total: total,
courses: courses.courses,
});
}
const completed = (await getStudent(session)).completedCourses;
const total = getNumberOfCourses(courses.courses);
return ctx.render({
completed,
total,
Expand Down
8 changes: 4 additions & 4 deletions utils/KV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ export const createStudent = async (): Promise<student> => {
if (res.ok) {
return student
} else {
throw new Error("Failed to create student")
return { sessionId: "", completedCourses: [] } as student
}
}
export const getStudent = async (sessionId: string): Promise<student> => {
const student = await kv.get(["student", sessionId])
if (student) {
return student.value as student
} else {
throw new Error("Failed to get student")
return { sessionId: "", completedCourses: [] } as student
}
}
export const updateStudent = async (sessionId: string, student: student): Promise<Result> => {
const res = await kv.set(["student", sessionId], student)
if (res.ok) {
return res
} else {
throw new Error("Failed to update student")
return {ok: false} as Result
}
}
export const addCompletedCourse = async (sessionId: string, course: string) : Promise<Result> => {
Expand All @@ -41,6 +41,6 @@ export const addCompletedCourse = async (sessionId: string, course: string) : Pr
const res = await updateStudent(sessionId, student)
return res
} catch {
return { ok: false }
return { ok: false } as Result
}
}

0 comments on commit a984a99

Please sign in to comment.