Skip to content

Commit

Permalink
fix:: constructor를 이용해 DTO내에서 초기화하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Devheun committed Jul 20, 2024
1 parent 4af158b commit bf9c0fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
58 changes: 12 additions & 46 deletions src/course/course.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { CourseRepository } from './course.repository';
import { CourseEntity } from 'src/entities/course.entity';
import { CourseDetailEntity } from 'src/entities/course-detail.entity';
Expand All @@ -23,28 +27,11 @@ export class CourseService {
relations: ['courseDetails'],
});

const courseInformations = {
id: course.id,
professorName: course.professorName,
category: course.category,
college: course.college,
courseName: course.courseName,
courseCode: course.courseCode,
credit: course.credit,
major: course.major,
hasExchangeSeat: course.hasExchangeSeat,
year: course.year,
semester: course.semester,
syllabus: course.syllabus,
totalRate: course.totalRate,
details: course.courseDetails.map((detail) => ({
day: detail.day,
startTime: detail.startTime,
endTime: detail.endTime,
classroom: detail.classroom,
})),
};
return new CommonCourseResponseDto(courseInformations);
if (!course) {
throw new NotFoundException('Course not found!');
}

return new CommonCourseResponseDto(course);
}

async getCourseWithCourseDetails(courseId: number): Promise<CourseEntity> {
Expand Down Expand Up @@ -341,30 +328,9 @@ export class CourseService {
async mappingCourseDetailsToCourses(
courses: CourseEntity[],
): Promise<PaginatedCoursesDto> {
const courseInformations = courses.map((course) => ({
id: course.id,
professorName: course.professorName,
category: course.category,
college: course.college,
courseName: course.courseName,
courseCode: course.courseCode,
credit: course.credit,
major: course.major,
hasExchangeSeat: course.hasExchangeSeat,
year: course.year,
semester: course.semester,
syllabus: course.syllabus,
totalRate: course.totalRate,
details: course.courseDetails.map((detail) => ({
day: detail.day,
startTime: detail.startTime,
endTime: detail.endTime,
classroom: detail.classroom,
})),
}));
const response = courseInformations.map(
const courseInformations = courses.map(
(course) => new CommonCourseResponseDto(course),
);
return new PaginatedCoursesDto(response);
return new PaginatedCoursesDto(courseInformations);
}
}
23 changes: 21 additions & 2 deletions src/course/dto/common-course-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { CourseEntity } from 'src/entities/course.entity';

export class CommonCourseResponseDto {
@ApiProperty({ description: 'ID' })
Expand Down Expand Up @@ -48,7 +49,25 @@ export class CommonCourseResponseDto {
classroom: string;
}[];

constructor(commonCourseResponseDto: CommonCourseResponseDto) {
Object.assign(this, commonCourseResponseDto);
constructor(course: CourseEntity) {
this.id = course.id;
this.professorName = course.professorName;
this.category = course.category;
this.college = course.college;
this.courseName = course.courseName;
this.courseCode = course.courseCode;
this.credit = course.credit;
this.major = course.major;
this.hasExchangeSeat = course.hasExchangeSeat;
this.year = course.year;
this.semester = course.semester;
this.syllabus = course.syllabus;
this.totalRate = course.totalRate;
this.details = course.courseDetails.map((detail) => ({
day: detail.day,
startTime: detail.startTime,
endTime: detail.endTime,
classroom: detail.classroom,
}));
}
}

0 comments on commit bf9c0fc

Please sign in to comment.