Skip to content

Commit

Permalink
Merge pull request #50 from gsainfoteam/49-psw
Browse files Browse the repository at this point in the history
Mapper를 대신하는 DTO
  • Loading branch information
siwonpada authored Aug 30, 2024
2 parents 65f20b1 + 9e60b8b commit 489c8ee
Show file tree
Hide file tree
Showing 20 changed files with 575 additions and 789 deletions.
96 changes: 62 additions & 34 deletions src/lecture/dto/res/lectureRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import {
Prisma,
Professor,
LectureCode,
LectureSection,
Semester,
} from '@prisma/client';
import { Professor, LectureCode, Semester, Prisma } from '@prisma/client';
import { Exclude, Expose } from 'class-transformer';
import { ExpandedLecture } from 'src/lecture/types/expandedLecture.type';

class ProfessorResDto implements Professor {
@ApiProperty({
Expand All @@ -21,21 +17,25 @@ class ProfessorResDto implements Professor {
name: string;
}

class LectureCodeResDto implements LectureCode {
@ApiProperty({
example: '1',
description: '강의코드',
})
code: string;

@ApiProperty({
example: 1,
description: '강의 id',
})
lectureId: number;
}
class LectureSectionResDto
implements
Prisma.LectureSectionGetPayload<{
include: {
LectureSectionProfessor: {
include: {
Professor: true;
};
};
};
}>
{
@Exclude()
LectureSectionProfessor: Prisma.LectureSectionProfessorGetPayload<{
include: {
Professor: true;
};
}>[];

class LectureSectionResDto implements LectureSection {
@ApiProperty({
example: 1,
description: '강의 section id',
Expand Down Expand Up @@ -82,18 +82,34 @@ class LectureSectionResDto implements LectureSection {
description: '교수 정보',
type: [ProfessorResDto],
})
Professor: ProfessorResDto[];
@Expose()
get professor(): ProfessorResDto[] {
return this.LectureSectionProfessor.map((professor) => ({
id: professor.Professor.id,
name: professor.Professor.name,
}));
}

constructor(partial: Partial<LectureSectionResDto>) {
Object.assign(this, partial);
}
}

export class ExpandedLectureResDto
implements
Prisma.LectureGetPayload<{
include: {
LectureCode: true;
LectureSection: true;
export class ExpandedLectureResDto implements ExpandedLecture {
@Exclude()
LectureCode: LectureCode[];

@Exclude()
LectureSection: Prisma.LectureSectionGetPayload<{
include: {
LectureSectionProfessor: {
include: {
Professor: true;
};
};
}>
{
};
}>[];

@ApiProperty({
example: 1,
description: '강의 Id',
Expand All @@ -106,14 +122,26 @@ export class ExpandedLectureResDto
name: string;

@ApiProperty({
description: '강의 코드 정보',
type: [LectureCodeResDto],
example: ['EC2202, MM2203'],
description: '강의 코드',
})
LectureCode: LectureCodeResDto[];
@Expose()
get lectureCode(): string[] {
return this.LectureCode.map((code) => code.code);
}

@ApiProperty({
description: '강의 section 정보',
type: [LectureSectionResDto],
})
LectureSection: LectureSectionResDto[];
@Expose()
get lectureSection(): LectureSectionResDto[] {
return this.LectureSection.map(
(section) => new LectureSectionResDto(section),
);
}

constructor(partial: Partial<ExpandedLectureResDto>) {
Object.assign(this, partial);
}
}
91 changes: 49 additions & 42 deletions src/lecture/lecture.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ClassSerializerInterceptor,
Controller,
Delete,
Get,
Expand All @@ -7,6 +8,7 @@ import {
Post,
Query,
UseGuards,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
Expand All @@ -24,49 +26,11 @@ import { IdPGuard } from 'src/user/guard/idp.guard';

@ApiTags('lecture')
@UsePipes(new ValidationPipe({ transform: true }))
@UseInterceptors(ClassSerializerInterceptor)
@Controller('lecture')
export class LectureController {
constructor(private readonly lectureService: LectureService) {}

@ApiOperation({
summary: '강의 북마크',
description: '특정 강의 분반을 북마크합니다.',
})
@ApiOAuth2(['email', 'profile', 'openid'], 'oauth2')
@UseGuards(IdPGuard)
@Post('bookmark')
async addBookMark(
@Query() query: BookMarkQueryDto,
@GetUser() user: User,
): Promise<BookMark> {
return this.lectureService.addBookMark(query, user);
}

@ApiOperation({
summary: '강의 북마크 취소',
description: '특정 강의 분반의 북마크를 취소합니다.',
})
@ApiOAuth2(['email', 'profile', 'openid'], 'oauth2')
@UseGuards(IdPGuard)
@Delete('bookmark')
async deleteBookMark(
@Query() query: BookMarkQueryDto,
@GetUser() user: User,
): Promise<BookMark> {
return this.lectureService.deleteBookMark(query, user);
}

@ApiOperation({
summary: '북마크한 강의 조회',
description: '북마크한 강의를 모두 조회합니다.',
})
@ApiOAuth2(['email', 'profile', 'openid'], 'oauth2')
@UseGuards(IdPGuard)
@Get('bookmark')
async getBookMark(@GetUser() user: User): Promise<BookMark[]> {
return this.lectureService.getBookMark(user!);
}

@ApiOperation({
summary: '강의 전체 조회',
description:
Expand All @@ -77,7 +41,9 @@ export class LectureController {
async getAll(
@Query() query: GetAllQueryDto,
): Promise<ExpandedLectureResDto[]> {
return this.lectureService.getAll(query);
return (await this.lectureService.getAll(query)).map((lecture) => {
return new ExpandedLectureResDto(lecture);
});
}

@ApiOperation({
Expand All @@ -89,7 +55,9 @@ export class LectureController {
async search(
@Query() query: SearchLectureQueryDto,
): Promise<ExpandedLectureResDto[]> {
return this.lectureService.search(query);
return (await this.lectureService.search(query)).map((lecture) => {
return new ExpandedLectureResDto(lecture);
});
}

@ApiOperation({
Expand All @@ -111,6 +79,45 @@ export class LectureController {
async getOne(
@Param('id', new ParseIntPipe()) id: number,
): Promise<ExpandedLectureResDto> {
return this.lectureService.getOne(id);
return new ExpandedLectureResDto(await this.lectureService.getOne(id));
}

@ApiOperation({
summary: '북마크한 강의 조회',
description: '북마크한 강의를 모두 조회합니다.',
})
@ApiOAuth2(['email', 'profile', 'openid'], 'oauth2')
@UseGuards(IdPGuard)
@Get('bookmark')
async getBookMark(@GetUser() user: User): Promise<BookMark[]> {
return this.lectureService.getBookMark(user!);
}

@ApiOperation({
summary: '강의 북마크',
description: '특정 강의 분반을 북마크합니다.',
})
@ApiOAuth2(['email', 'profile', 'openid'], 'oauth2')
@UseGuards(IdPGuard)
@Post('bookmark')
async addBookMark(
@Query() query: BookMarkQueryDto,
@GetUser() user: User,
): Promise<BookMark> {
return this.lectureService.addBookMark(query, user);
}

@ApiOperation({
summary: '강의 북마크 취소',
description: '특정 강의 분반의 북마크를 취소합니다.',
})
@ApiOAuth2(['email', 'profile', 'openid'], 'oauth2')
@UseGuards(IdPGuard)
@Delete('bookmark')
async deleteBookMark(
@Query() query: BookMarkQueryDto,
@GetUser() user: User,
): Promise<BookMark> {
return this.lectureService.deleteBookMark(query, user);
}
}
28 changes: 0 additions & 28 deletions src/lecture/lecture.mapper.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/lecture/lecture.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { LectureService } from './lecture.service';
import { PrismaModule } from 'src/prisma/prisma.module';
import { LectureRepository } from './lecture.repository';
import { UserModule } from 'src/user/user.module';
import { LectureMapper } from './lecture.mapper';

@Module({
imports: [PrismaModule, UserModule],
controllers: [LectureController],
providers: [LectureService, LectureRepository, LectureMapper],
providers: [LectureService, LectureRepository],
})
export class LectureModule {}
29 changes: 9 additions & 20 deletions src/lecture/lecture.service.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import { Injectable } from '@nestjs/common';
import { ExpandedLectureResDto } from './dto/res/lectureRes.dto';
import { LectureRepository } from './lecture.repository';
import { EvaluationQueryDto } from './dto/req/evaluationReq.dto';
import { EvaluationResDto } from './dto/res/evaluationRes.dto';
import { SearchLectureQueryDto } from './dto/req/searchReq.dto';
import { GetAllQueryDto } from './dto/req/getAllReq.dto';
import { BookMarkQueryDto } from './dto/req/bookmarkReq.dto';
import { User } from '@prisma/client';
import { LectureMapper } from './lecture.mapper';
import { ExpandedLecture } from './types/expandedLecture.type';

@Injectable()
export class LectureService {
constructor(
private readonly lectureRepository: LectureRepository,
private readonly lectureMapper: LectureMapper,
) {}

async getAll(query: GetAllQueryDto): Promise<ExpandedLectureResDto[]> {
const lectures = await this.lectureRepository.getAll(query);
return lectures.map((lecture) =>
this.lectureMapper.expandedLectureToExpandedLectureResDto(lecture),
);
constructor(private readonly lectureRepository: LectureRepository) {}

async getAll(query: GetAllQueryDto): Promise<ExpandedLecture[]> {
return this.lectureRepository.getAll(query);
}

async getOne(id: number): Promise<ExpandedLectureResDto> {
const lecture = await this.lectureRepository.getOne(id);
return this.lectureMapper.expandedLectureToExpandedLectureResDto(lecture);
async getOne(id: number): Promise<ExpandedLecture> {
return this.lectureRepository.getOne(id);
}

async getEvaluation(query: EvaluationQueryDto): Promise<EvaluationResDto> {
return this.lectureRepository.getEvaluation(query);
}

async search(query: SearchLectureQueryDto): Promise<ExpandedLectureResDto[]> {
const lectures = await this.lectureRepository.search(query);
return lectures.map((lecture) =>
this.lectureMapper.expandedLectureToExpandedLectureResDto(lecture),
);
async search(query: SearchLectureQueryDto): Promise<ExpandedLecture[]> {
return this.lectureRepository.search(query);
}

async addBookMark(query: BookMarkQueryDto, user: User) {
Expand Down
Loading

0 comments on commit 489c8ee

Please sign in to comment.