Skip to content

Commit

Permalink
refactor: 코스 리스트의 Response객체 통일 #92
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 13, 2024
1 parent 6af91b3 commit 9fd1523
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
21 changes: 9 additions & 12 deletions backend/src/course/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. 작성자명 등 ... 검색 조건 추가
Expand All @@ -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),
Expand All @@ -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. 그룹 기능 추가
Expand All @@ -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) {
Expand All @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion backend/src/course/dto/PagedCourseResponse.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 9fd1523

Please sign in to comment.