Skip to content

Commit

Permalink
feat: read course (#77)
Browse files Browse the repository at this point in the history
* feat: add read course

* fix: add course filtering
  • Loading branch information
ddungiii authored Feb 28, 2024
1 parent 22f5581 commit 9291fcc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/modules/courses/courses.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Controller, Get, Param, Post, Query } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import { GetUser } from 'src/common/decorators/get-user.decorator';
import { Public } from 'src/common/decorators/skip-auth.decorator';
Expand Down Expand Up @@ -50,4 +50,12 @@ export class CourseController {
) {
return await this.CoursesService.getReviewsByCourseId(query, id, user);
}

@Post(':id/read')
async readCourse(
@Param('id') id: number,
@GetUser() user: session_userprofile,
) {
await this.CoursesService.readCourse(user.id, id);
}
}
13 changes: 9 additions & 4 deletions src/modules/courses/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import { ECourse } from 'src/common/entities/ECourse';
import { ICourse } from 'src/common/interfaces';
import { CourseQueryDto } from 'src/common/interfaces/dto/course/course.request.dto';
import { CourseReviewQueryDto } from 'src/common/interfaces/dto/course/course.review.request.dto';
import { toJsonLecture } from 'src/common/interfaces/serializer/lecture.serializer';
import { toJsonReview } from 'src/common/interfaces/serializer/review.serializer';
Expand All @@ -15,7 +16,7 @@ export class CoursesService {
constructor(private readonly CourseRepository: CourseRepository) {}

public async getCourseByFilter(
query: any,
query: CourseQueryDto,
user: session_userprofile,
): Promise<(CourseResponseDtoNested & { userspecific_is_read: boolean })[]> {
const queryResult = await this.CourseRepository.filterByRequest(query);
Expand All @@ -32,9 +33,9 @@ export class CoursesService {
);

if (user) {
const latestReadDatetime = course.subject_courseuser.find(
(x) => (x.user_profile_id = user.id),
)?.latest_read_datetime;
const latestReadDatetime = course.subject_courseuser.find((x) => {
return x.user_profile_id === user.id && x.course_id === course.id;
})?.latest_read_datetime;
const latestWrittenDatetime = course.latest_written_datetime;
return Object.assign(result, {
userspecific_is_read:
Expand Down Expand Up @@ -150,4 +151,8 @@ export class CoursesService {
return professor.professor.professor_name_en;
}
}

async readCourse(userId: number, courseId: number) {
await this.CourseRepository.readCourse(userId, courseId);
}
}
20 changes: 20 additions & 0 deletions src/prisma/repositories/course.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,24 @@ export class CourseRepository {
});
return candidate;
}

async readCourse(userId: number, courseId: number) {
const now = new Date();
return await this.prisma.subject_courseuser.upsert({
create: {
latest_read_datetime: now,
user_profile_id: userId,
course_id: courseId,
},
update: {
latest_read_datetime: now,
},
where: {
course_id_user_profile_id: {
course_id: courseId,
user_profile_id: userId,
},
},
});
}
}

0 comments on commit 9291fcc

Please sign in to comment.