Skip to content

Commit

Permalink
Merge pull request #530 from connection-2023/refactor/#529
Browse files Browse the repository at this point in the history
Refactor/#529
  • Loading branch information
j-zzi authored Jun 18, 2024
2 parents bdeabf3 + 0060e47 commit 532b2a4
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 129 deletions.
29 changes: 10 additions & 19 deletions src/lecture/repositories/popular-lecture.repository.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<number> {
return await trasaction.reservation.count({
async readLectureReservationCount(lectureId: number): Promise<number> {
return await this.prismaService.reservation.count({
where: { lectureSchedule: { lectureId } },
});
}

async trxReadLectureLikesCount(
trasaction: PrismaTransaction,
lectureId: number,
): Promise<number> {
return await trasaction.likedLecture.count({ where: { lectureId } });
async readLectureLikesCount(lectureId: number): Promise<number> {
return await this.prismaService.likedLecture.count({
where: { lectureId },
});
}

async trxReadLectureWithUserId(
transaction: PrismaTransaction,
async readLectureWithUserId(
lectureId: number,
userId?: number,
): Promise<Lecture> {
Expand All @@ -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<Lecture> {
return await transaction.lecture.findFirst({
async readLecture(lectureId: number): Promise<Lecture> {
return await this.prismaService.lecture.findFirst({
where: { id: lectureId, isActive: true },
include: {
lecturer: true,
Expand Down
78 changes: 34 additions & 44 deletions src/lecture/services/popular-lecture.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,48 @@ export class PopularLectureService {
) {}

async readPopularLecture(userId?: number): Promise<LectureDto[]> {
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(
Expand Down
23 changes: 8 additions & 15 deletions src/lecturer/repositories/popular-lecturer.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@ import { Lecturer } from '@prisma/client';
export class PopularLecturerRepository {
constructor(private readonly prismaService: PrismaService) {}

async trxReadLecturerReservationCount(
trasaction: PrismaTransaction,
lecturerId: number,
): Promise<number> {
return await trasaction.reservation.count({
async readLecturerReservationCount(lecturerId: number): Promise<number> {
return await this.prismaService.reservation.count({
where: { lectureSchedule: { lecture: { lecturerId } } },
});
}

async trxReadLecturerLikesCount(
trasaction: PrismaTransaction,
lecturerId: number,
): Promise<number> {
return await trasaction.likedLecturer.count({ where: { lecturerId } });
async readLecturerLikesCount(lecturerId: number): Promise<number> {
return await this.prismaService.likedLecturer.count({
where: { lecturerId },
});
}

async trxReadLecturerWithLecturerId(
transaction: PrismaTransaction,
lecturerId: number,
): Promise<Lecturer> {
return await transaction.lecturer.findFirst({
async readLecturerWithLecturerId(lecturerId: number): Promise<Lecturer> {
return await this.prismaService.lecturer.findFirst({
where: { id: lecturerId },
include: { lecturerProfileImageUrl: true },
});
Expand Down
114 changes: 63 additions & 51 deletions src/lecturer/services/popular-lecturer.service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -12,63 +11,76 @@ export class PopularLecturerService {
) {}

async readManyPopularLecturer(userId?: number): Promise<LecturerDto[]> {
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;
}
}

0 comments on commit 532b2a4

Please sign in to comment.