Skip to content

Commit

Permalink
reduce the taken-lecture api response time
Browse files Browse the repository at this point in the history
  • Loading branch information
LarryKwon committed Aug 26, 2024
1 parent 97fe6e5 commit 23c9d57
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/common/entities/ECourse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ export namespace ECourse {
},
});
export type Details = Prisma.subject_courseGetPayload<typeof Details>;

export const DetailWithIsRead = Prisma.validator<Prisma.subject_courseArgs>()(
{
include: {
...Details.include,
subject_courseuser: true,
},
},
);
export type DetailWithIsRead = Prisma.subject_courseGetPayload<
typeof DetailWithIsRead
>;
}
11 changes: 11 additions & 0 deletions src/common/entities/ELecture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ export namespace ELecture {
});
export type Details = Prisma.subject_lectureGetPayload<typeof Details>;

export const DetailsWithCourse =
Prisma.validator<Prisma.subject_lectureArgs>()({
include: {
...Details.include,
course: true,
},
});
export type DetailsWithCourse = Prisma.subject_lectureGetPayload<
typeof DetailsWithCourse
>;

export function isDetails(
lecture: ELecture.Extended | ELecture.Details,
): lecture is ELecture.Details {
Expand Down
34 changes: 19 additions & 15 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,28 @@ export class UserService {
takenLecturesId,
query.order ?? DEFAULT_ORDER,
);
return Promise.all(
courses.map(async (course) => {
const representativeLecture = getRepresentativeLecture(course.lecture);
const professorRaw = course.subject_course_professors.map(
(x) => x.professor,
);
const result = toJsonCourseDetail(
course,
representativeLecture,
professorRaw,
);

return courses.map((course) => {
const representativeLecture = getRepresentativeLecture(course.lecture);
const professorRaw = course.subject_course_professors.map(
(x) => x.professor,
);
const result = toJsonCourseDetail(
course,
representativeLecture,
professorRaw,
);
const subjectCourseUser = course.subject_courseuser.filter(
(e) => e.user_profile_id === user.id && e.course_id === course.id,
)[0];
if (!subjectCourseUser || !course.latest_written_datetime)
return addIsRead(result, false);
else
return addIsRead(
result,
await this.courseRepository.isUserSpecificRead(course.id, user.id),
course.latest_written_datetime >
subjectCourseUser?.latest_read_datetime,
);
}),
);
});
}

async getUserLikedReviews(
Expand Down
4 changes: 2 additions & 2 deletions src/prisma/repositories/course.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export class CourseRepository {
async getUserTakenCourses(
takenLecturesId: number[],
order: string[],
): Promise<ECourse.Details[]> {
): Promise<ECourse.DetailWithIsRead[]> {
const orderFilter: { [key: string]: string }[] = [];
order.forEach((orderList) => {
const orderDict: { [key: string]: string } = {};
Expand All @@ -381,7 +381,7 @@ export class CourseRepository {
orderDict[orderBy[orderBy.length - 1]] = order;
orderFilter.push(orderDict);
});
return await this.prisma.subject_course.findMany({
return this.prisma.subject_course.findMany({
where: {
lecture: {
some: {
Expand Down
12 changes: 2 additions & 10 deletions src/prisma/repositories/lecture.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PrismaService } from '../prisma.service';
import { CourseRepository } from './course.repository';
import { FilterType } from '@src/common/types/types';
import SubjectClasstimeFilter = FilterType.SubjectClasstimeFilter;
import Details = ELecture.Details;

@Injectable()
export class LectureRepository {
Expand Down Expand Up @@ -194,16 +195,7 @@ export class LectureRepository {
},
include: {
lecture: {
include: {
subject_lecture_professors: {
include: {
professor: true,
},
},
subject_department: true,
subject_examtime: true,
subject_classtime: true,
},
include: Details.include,
},
},
})
Expand Down

0 comments on commit 23c9d57

Please sign in to comment.