diff --git a/src/lecture/repositories/popular-lecture.repository.ts b/src/lecture/repositories/popular-lecture.repository.ts index 73b14d7..21dcfb8 100644 --- a/src/lecture/repositories/popular-lecture.repository.ts +++ b/src/lecture/repositories/popular-lecture.repository.ts @@ -1,4 +1,3 @@ -import { PrismaTransaction } from '@src/common/interface/common-interface'; import { Injectable } from '@nestjs/common'; import { Lecture, Reservation } from '@prisma/client'; import { PrismaService } from '@src/prisma/prisma.service'; @@ -7,24 +6,19 @@ import { PrismaService } from '@src/prisma/prisma.service'; export class PopularLectureRepository { constructor(private readonly prismaService: PrismaService) {} - async trxReadLectureReservationCount( - trasaction: PrismaTransaction, - lectureId: number, - ): Promise { - return await trasaction.reservation.count({ + async readLectureReservationCount(lectureId: number): Promise { + return await this.prismaService.reservation.count({ where: { lectureSchedule: { lectureId } }, }); } - async trxReadLectureLikesCount( - trasaction: PrismaTransaction, - lectureId: number, - ): Promise { - return await trasaction.likedLecture.count({ where: { lectureId } }); + async readLectureLikesCount(lectureId: number): Promise { + return await this.prismaService.likedLecture.count({ + where: { lectureId }, + }); } - async trxReadLectureWithUserId( - transaction: PrismaTransaction, + async readLectureWithUserId( lectureId: number, userId?: number, ): Promise { @@ -44,17 +38,14 @@ export class PopularLectureRepository { userId ? (include['likedLecture'] = { where: { userId } }) : false; - return await transaction.lecture.findFirst({ + return await this.prismaService.lecture.findFirst({ where: { id: lectureId, isActive: true }, include, }); } - async trxReadLecture( - transaction: PrismaTransaction, - lectureId: number, - ): Promise { - return await transaction.lecture.findFirst({ + async readLecture(lectureId: number): Promise { + return await this.prismaService.lecture.findFirst({ where: { id: lectureId, isActive: true }, include: { lecturer: true, diff --git a/src/lecture/services/popular-lecture.service.ts b/src/lecture/services/popular-lecture.service.ts index 0a67e84..6cb41f5 100644 --- a/src/lecture/services/popular-lecture.service.ts +++ b/src/lecture/services/popular-lecture.service.ts @@ -12,58 +12,48 @@ export class PopularLectureService { ) {} async readPopularLecture(userId?: number): Promise { - return await this.prismaService.$transaction( - async (trasaction: PrismaTransaction) => { - const popularScores = []; - const where = { isActive: true }; - userId - ? (where['lecturer'] = { blockedLecturer: { none: { userId } } }) - : false; + const popularScores = []; + const where = { isActive: true }; + userId + ? (where['lecturer'] = { blockedLecturer: { none: { userId } } }) + : false; - const lectures = await trasaction.lecture.findMany({ - where, - select: { id: true }, - }); + const lectures = await this.prismaService.lecture.findMany({ + where, + select: { id: true }, + }); - for (const lecture of lectures) { - const reservationCount = - await this.popularLectureRepository.trxReadLectureReservationCount( - trasaction, - lecture.id, - ); - const likesCount = - await this.popularLectureRepository.trxReadLectureLikesCount( - trasaction, - lecture.id, - ); - const popularScore = this.createPopularScore( - lecture.id, - reservationCount, - likesCount, - ); - popularScores.push(popularScore); - } + for (const lecture of lectures) { + const reservationCount = + await this.popularLectureRepository.readLectureReservationCount( + lecture.id, + ); + const likesCount = + await this.popularLectureRepository.readLectureLikesCount(lecture.id); + const popularScore = this.createPopularScore( + lecture.id, + reservationCount, + likesCount, + ); + popularScores.push(popularScore); + } - const sortedPopularScores = this.sortPopularScores(popularScores); + const sortedPopularScores = this.sortPopularScores(popularScores); - const topEightPopularScores = sortedPopularScores.slice(0, 8); + const topEightPopularScores = sortedPopularScores.slice(0, 8); - const popularLectures = []; + const popularLectures = []; - for (const popularLecture of topEightPopularScores) { - const lecture = - await this.popularLectureRepository.trxReadLectureWithUserId( - trasaction, - popularLecture.id, - userId, - ); + for (const popularLecture of topEightPopularScores) { + const lecture = await this.popularLectureRepository.readLectureWithUserId( + popularLecture.id, + userId, + ); - popularLectures.push(new LectureDto(lecture)); - } + popularLectures.push(new LectureDto(lecture)); + } - return popularLectures; - }, - ); + return popularLectures; } private createPopularScore( diff --git a/src/lecturer/repositories/popular-lecturer.repository.ts b/src/lecturer/repositories/popular-lecturer.repository.ts index 91908ae..573a718 100644 --- a/src/lecturer/repositories/popular-lecturer.repository.ts +++ b/src/lecturer/repositories/popular-lecturer.repository.ts @@ -7,27 +7,20 @@ import { Lecturer } from '@prisma/client'; export class PopularLecturerRepository { constructor(private readonly prismaService: PrismaService) {} - async trxReadLecturerReservationCount( - trasaction: PrismaTransaction, - lecturerId: number, - ): Promise { - return await trasaction.reservation.count({ + async readLecturerReservationCount(lecturerId: number): Promise { + return await this.prismaService.reservation.count({ where: { lectureSchedule: { lecture: { lecturerId } } }, }); } - async trxReadLecturerLikesCount( - trasaction: PrismaTransaction, - lecturerId: number, - ): Promise { - return await trasaction.likedLecturer.count({ where: { lecturerId } }); + async readLecturerLikesCount(lecturerId: number): Promise { + return await this.prismaService.likedLecturer.count({ + where: { lecturerId }, + }); } - async trxReadLecturerWithLecturerId( - transaction: PrismaTransaction, - lecturerId: number, - ): Promise { - return await transaction.lecturer.findFirst({ + async readLecturerWithLecturerId(lecturerId: number): Promise { + return await this.prismaService.lecturer.findFirst({ where: { id: lecturerId }, include: { lecturerProfileImageUrl: true }, }); diff --git a/src/lecturer/services/popular-lecturer.service.ts b/src/lecturer/services/popular-lecturer.service.ts index 0c5254a..4594a16 100644 --- a/src/lecturer/services/popular-lecturer.service.ts +++ b/src/lecturer/services/popular-lecturer.service.ts @@ -1,7 +1,6 @@ import { PrismaService } from '@src/prisma/prisma.service'; import { PopularLecturerRepository } from './../repositories/popular-lecturer.repository'; import { Injectable } from '@nestjs/common'; -import { PrismaTransaction } from '@src/common/interface/common-interface'; import { LecturerDto } from '@src/common/dtos/lecturer.dto'; @Injectable() @@ -12,63 +11,76 @@ export class PopularLecturerService { ) {} async readManyPopularLecturer(userId?: number): Promise { - return await this.prismaService.$transaction( - async (trasaction: PrismaTransaction) => { - const where = { deletedAt: null }; + const where = { deletedAt: null }; - userId ? (where['blockedLecturer'] = { none: { userId } }) : false; + userId ? (where['blockedLecturer'] = { none: { userId } }) : false; - const popularScores = []; - const lecturers = await trasaction.lecturer.findMany({ - where, - select: { id: true }, - }); + const popularScores = []; + const lecturers = await this.prismaService.lecturer.findMany({ + where, + select: { id: true }, + }); - for (const lecturer of lecturers) { - const reservationCount = - await this.popularLecturerRepository.trxReadLecturerReservationCount( - trasaction, - lecturer.id, - ); - const likesCount = - await this.popularLecturerRepository.trxReadLecturerLikesCount( - trasaction, - lecturer.id, - ); - const popularScore = { - id: lecturer.id, - reservationCount, - likesCount, - score: - Math.round((reservationCount * 0.6 + likesCount * 0.4) * 100) / - 100, - }; - popularScores.push(popularScore); - } + for (const lecturer of lecturers) { + const reservationCount = + await this.popularLecturerRepository.readLecturerReservationCount( + lecturer.id, + ); + const likesCount = + await this.popularLecturerRepository.readLecturerLikesCount( + lecturer.id, + ); + const popularScore = this.createPopularScore( + lecturer.id, + reservationCount, + likesCount, + ); - popularScores.sort((a, b) => { - if (a.score !== b.score) { - return b.score - a.score; - } else { - return b.reservationCount - a.reservationCount; - } - }); + popularScores.push(popularScore); + } - const topTenPopularScores = popularScores.slice(0, 10); - const popularLecturers = []; + const sortedPopularScores = this.sortPopularScores(popularScores); - for (const popularLecturer of topTenPopularScores) { - const lecturer = - await this.popularLecturerRepository.trxReadLecturerWithLecturerId( - trasaction, - popularLecturer.id, - ); + const topTenPopularScores = sortedPopularScores.slice(0, 10); + const popularLecturers = []; - popularLecturers.push(new LecturerDto(lecturer)); - } + for (const popularLecturer of topTenPopularScores) { + const lecturer = + await this.popularLecturerRepository.readLecturerWithLecturerId( + popularLecturer.id, + ); - return popularLecturers; - }, - ); + popularLecturers.push(new LecturerDto(lecturer)); + } + + return popularLecturers; + } + + private createPopularScore( + lecturerId: number, + reservationCount: number, + likesCount: number, + ) { + const popularScore = { + id: lecturerId, + reservationCount, + likesCount, + score: + Math.round((reservationCount * 0.6 + likesCount * 0.4) * 100) / 100, + }; + + return popularScore; + } + + private sortPopularScores(popularScores) { + popularScores.sort((a, b) => { + if (a.score !== b.score) { + return b.score - a.score; + } else { + return b.reservationCount - a.reservationCount; + } + }); + + return popularScores; } }