From 9fd1523d64b92cd254de68d266d01bfe1f9e9e78 Mon Sep 17 00:00:00 2001 From: koomchang Date: Wed, 13 Nov 2024 14:21:13 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BD=94=EC=8A=A4=20=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=9D=98=20Response=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=ED=86=B5=EC=9D=BC=20#92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/course/course.service.ts | 21 ++++++++----------- backend/src/course/dto/PagedCourseResponse.ts | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/backend/src/course/course.service.ts b/backend/src/course/course.service.ts index 1c628fa6..94e93f5b 100644 --- a/backend/src/course/course.service.ts +++ b/backend/src/course/course.service.ts @@ -10,16 +10,15 @@ import { CourseNotFoundException } from './exception/CourseNotFoundException'; import { UpdateCourseInfoRequest } from './dto/UpdateCourseInfoRequest'; import { SetPlacesOfCourseRequest } from './dto/AddPlaceToCourseRequest'; import { PlaceRepository } from '../place/place.repository'; -import { UserRepository } from '../user/user.repository'; import { InvalidPlaceToCourseException } from './exception/InvalidPlaceToCourseException'; -import { OwnCourseListResponse } from './dto/OwnCourseListResponse'; +import { PagedCourseResponse } from './dto/PagedCourseResponse'; +import { User } from '../user/entity/user.entity'; @Injectable() export class CourseService { constructor( private readonly courseRepository: CourseRepository, private readonly placeRepository: PlaceRepository, - private readonly userRepository: UserRepository, ) {} // Todo. 작성자명 등 ... 검색 조건 추가 @@ -28,7 +27,7 @@ export class CourseService { page: number = 1, pageSize: number = 10, ) { - const [maps, totalCount] = query + const [searchedCourses, totalCount] = query ? await Promise.all([ this.courseRepository.searchByTitleQuery(query, page, pageSize), this.courseRepository.countByTitleAndIsPublic(query), @@ -38,11 +37,10 @@ export class CourseService { this.courseRepository.countAllPublic(), ]); - return { - courses: await Promise.all(maps.map(CourseListResponse.from)), - totalPages: Math.ceil(totalCount / pageSize), - currentPage: page, - }; + const courses = await Promise.all( + searchedCourses.map(CourseListResponse.from), + ); + return new PagedCourseResponse(courses, totalCount, page, pageSize); } // Todo. 그룹 기능 추가 @@ -53,8 +51,7 @@ export class CourseService { ]); const courses = await Promise.all(ownCourses.map(CourseListResponse.from)); - const totalPages = Math.ceil(totalCount / pageSize); - return new OwnCourseListResponse(courses, totalPages, page); + return new PagedCourseResponse(courses, totalCount, page, pageSize); } async getCourseById(id: number) { @@ -72,7 +69,7 @@ export class CourseService { } async createCourse(userId: number, createCourseForm: CreateCourseRequest) { - const user = await this.userRepository.findById(userId); + const user = { id: userId } as User; const map = createCourseForm.toEntity(user); return { id: (await this.courseRepository.save(map)).id }; diff --git a/backend/src/course/dto/PagedCourseResponse.ts b/backend/src/course/dto/PagedCourseResponse.ts index 87a10b69..ee24d9c5 100644 --- a/backend/src/course/dto/PagedCourseResponse.ts +++ b/backend/src/course/dto/PagedCourseResponse.ts @@ -1,7 +1,7 @@ import { CourseListResponse } from './CourseListResponse'; import { PaginationResponse } from '../../common/dto/PaginationResponse'; -export class OwnCourseListResponse extends PaginationResponse { +export class PagedCourseResponse extends PaginationResponse { constructor( readonly courses: CourseListResponse[], totalCount: number,