-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add student favourtie course endpoint
- Loading branch information
1 parent
5363e6b
commit 2c4c77f
Showing
10 changed files
with
233 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
apps/api/src/studentFavouritedCourses/api/studentFavouritedCourses.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Body, Controller, Delete, Post, Query } from "@nestjs/common"; | ||
import { Validate } from "nestjs-typebox"; | ||
import { BaseResponse, nullResponse, UUIDSchema } from "src/common"; | ||
import { CurrentUser } from "src/common/decorators/user.decorator"; | ||
import { StudentFavouritedCoursesService } from "../studentFavouritedCourses.service"; | ||
import { | ||
createFavouritedCourseSchema, | ||
CreateFavouritedCourseSchema, | ||
} from "../schemas/createFavouritedCourse.schema"; | ||
|
||
@Controller("studentFavouritedCourses") | ||
export class StudentFavouritedCoursesController { | ||
constructor( | ||
private readonly studentFavouritedCoursesService: StudentFavouritedCoursesService, | ||
) {} | ||
|
||
@Post() | ||
@Validate({ | ||
request: [{ type: "body", schema: createFavouritedCourseSchema }], | ||
}) | ||
async createFavouritedCourse( | ||
@Body() data: CreateFavouritedCourseSchema, | ||
@CurrentUser() currentUser: { userId: string }, | ||
): Promise<BaseResponse<{ message: string }>> { | ||
await this.studentFavouritedCoursesService.createFavouritedCourseForUser( | ||
data.courseId, | ||
currentUser.userId, | ||
); | ||
|
||
return new BaseResponse({ | ||
message: "Favourite course created successfully", | ||
}); | ||
} | ||
|
||
@Delete() | ||
@Validate({ | ||
response: nullResponse(), | ||
request: [{ type: "query", name: "id", schema: UUIDSchema }], | ||
}) | ||
async deleteFavouritedCourseForUser( | ||
@Query("id") id: string, | ||
@CurrentUser() currentUser: { userId: string }, | ||
): Promise<null> { | ||
console.log(id, currentUser.userId); | ||
|
||
await this.studentFavouritedCoursesService.deleteFavouritedCourseForUser( | ||
id, | ||
currentUser.userId, | ||
); | ||
|
||
return null; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/api/src/studentFavouritedCourses/schemas/course.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Type, Static } from "@sinclair/typebox"; | ||
import { UUIDSchema } from "src/common"; | ||
|
||
export const allCoursesSchema = Type.Array( | ||
Type.Object({ | ||
id: UUIDSchema, | ||
title: Type.String(), | ||
imageUrl: Type.Union([Type.String(), Type.Null()]), | ||
author: Type.String(), | ||
category: Type.String(), | ||
courseLessonCount: Type.Number(), | ||
enrolledParticipantCount: Type.Number(), | ||
}), | ||
); | ||
|
||
export type AllCoursesResponse = Static<typeof allCoursesSchema>; |
10 changes: 10 additions & 0 deletions
10
apps/api/src/studentFavouritedCourses/schemas/createFavouritedCourse.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { UUIDSchema } from "src/common"; | ||
|
||
export const createFavouritedCourseSchema = Type.Object({ | ||
courseId: UUIDSchema, | ||
}); | ||
|
||
export type CreateFavouritedCourseSchema = Static< | ||
typeof createFavouritedCourseSchema | ||
>; |
12 changes: 12 additions & 0 deletions
12
apps/api/src/studentFavouritedCourses/studentFavouritedCourses.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { StudentFavouritedCoursesService } from "./studentFavouritedCourses.service"; | ||
import { StudentFavouritedCoursesController } from "./api/studentFavouritedCourses.controller"; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [StudentFavouritedCoursesController], | ||
providers: [StudentFavouritedCoursesService], | ||
exports: [], | ||
}) | ||
export class StudentFavouritedCoursesModule {} |
61 changes: 61 additions & 0 deletions
61
apps/api/src/studentFavouritedCourses/studentFavouritedCourses.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
ConflictException, | ||
Inject, | ||
Injectable, | ||
NotFoundException, | ||
} from "@nestjs/common"; | ||
import { and, eq } from "drizzle-orm"; | ||
import { DatabasePg } from "src/common"; | ||
import { courses, studentFavouritedCourses } from "src/storage/schema"; | ||
import { Status } from "src/storage/schema/utils"; | ||
|
||
@Injectable() | ||
export class StudentFavouritedCoursesService { | ||
constructor(@Inject("DB") private readonly db: DatabasePg) {} | ||
|
||
async createFavouritedCourseForUser(courseId: string, userId: string) { | ||
const [course] = await this.db | ||
.select() | ||
.from(courses) | ||
.where( | ||
and(eq(courses.id, courseId), eq(courses.state, Status.published.key)), | ||
); | ||
if (!course) { | ||
throw new NotFoundException("Course not found"); | ||
} | ||
|
||
const [existingRecord] = await this.db | ||
.select() | ||
.from(studentFavouritedCourses) | ||
.where( | ||
and( | ||
eq(studentFavouritedCourses.courseId, courseId), | ||
eq(studentFavouritedCourses.studentId, userId), | ||
), | ||
); | ||
if (existingRecord) { | ||
throw new ConflictException("Favourite course already exists"); | ||
} | ||
|
||
await this.db | ||
.insert(studentFavouritedCourses) | ||
.values({ courseId: courseId, studentId: userId }) | ||
.returning(); | ||
} | ||
|
||
async deleteFavouritedCourseForUser(id: string, userId: string) { | ||
const [deletedFavouritedCourse] = await this.db | ||
.delete(studentFavouritedCourses) | ||
.where( | ||
and( | ||
eq(studentFavouritedCourses.id, id), | ||
eq(studentFavouritedCourses.studentId, userId), | ||
), | ||
) | ||
.returning(); | ||
|
||
if (!deletedFavouritedCourse) { | ||
throw new NotFoundException("Favourite course not found"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters