diff --git a/src/response/response.service.ts b/src/response/response.service.ts index 6ad43c8..52382aa 100644 --- a/src/response/response.service.ts +++ b/src/response/response.service.ts @@ -104,11 +104,22 @@ export class ResponseService { return new Conflict_SelectResponseDto(); } - const tutoringService = new TutoringService(this.tutoringModel); - const tutoring = await tutoringService.create(selectResponseDto); + const tutoringService = new TutoringService( + this.requestModel, + this.tutoringModel, + ); + + const tempTutoring = await tutoringService.create(selectResponseDto); + let tutoringId; + if ('id' in tempTutoring.data) { + tutoringId = tempTutoring.data.id; + } + + const tutoring = await this.tutoringModel.get({ id: tutoringId }); + request.status = 'selected'; request.selectedTeacherId = selectResponseDto.teacherId; - request.tutoringId = tutoring.id; + request.tutoringId = tutoringId; await this.requestModel.update(request); return new Success_SelectResponseDto({ tutoringId: tutoring.id }); diff --git a/src/tutoring/dto/create-tutoring.dto.ts b/src/tutoring/dto/create-tutoring.dto.ts index 9f43e42..0308325 100644 --- a/src/tutoring/dto/create-tutoring.dto.ts +++ b/src/tutoring/dto/create-tutoring.dto.ts @@ -1,4 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; +import { ResponseDto } from '../../responseDto'; +import { Tutoring } from '../entities/tutoring.interface'; export class CreateTutoringDto { @ApiProperty({ @@ -20,9 +22,9 @@ export class CreateTutoringDto { teacherId: string; } -export class Success_CreateTutoringDto { +export class Success_CreateTutoringDto extends ResponseDto { @ApiProperty({ - default: 'Create tutoring successfully.', + default: '과외를 성공적으로 생성했습니다.', }) message: string; @@ -40,13 +42,34 @@ export class Success_CreateTutoringDto { default: { id: 'test-tutoring-id', studentId: 'test-student-id', - requestId: 'test-request-id', teacherId: 'test-teacher-id', - matchedAt: '2021-01-01T00:00:00.000Z', - createdAt: '2021-01-01T00:00:00.000Z', - endedAt: null, status: 'ongoing', }, }) - data: object; + data: Tutoring; + + constructor(data: Tutoring) { + super(201, '과외를 성공적으로 생성했습니다.', false, data); + } +} + +export class NotFound_CreateTutoringDto extends ResponseDto { + @ApiProperty({ + default: '리소스를 찾을 수 없습니다.', + }) + message: string; + + @ApiProperty({ + default: true, + }) + error: boolean; + + @ApiProperty({ + default: 404, + }) + statusCode: number; + + constructor(message: string) { + super(404, message, true); + } } diff --git a/src/tutoring/entities/tutoring.interface.ts b/src/tutoring/entities/tutoring.interface.ts index fc5a9d0..c5a3701 100644 --- a/src/tutoring/entities/tutoring.interface.ts +++ b/src/tutoring/entities/tutoring.interface.ts @@ -3,11 +3,11 @@ export interface TutoringKey { } export interface Tutoring extends TutoringKey { - requestId: string; + requestId?: string; studentId: string; teacherId: string; - matchedAt: string; - startedAt: string; - endedAt: string; + matchedAt?: string; + startedAt?: string; + endedAt?: string; status: string; } diff --git a/src/tutoring/tutoring.controller.ts b/src/tutoring/tutoring.controller.ts index fc40a9f..81e8e65 100644 --- a/src/tutoring/tutoring.controller.ts +++ b/src/tutoring/tutoring.controller.ts @@ -11,10 +11,10 @@ import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; export class TutoringController { constructor(private readonly tutoringService: TutoringService) {} - @Post() + @Post('create') @ApiResponse({ status: 201, - description: '과외 생성 완료', + description: '과외를 성공적으로 생성했습니다.', type: Success_CreateTutoringDto, }) @ApiOperation({ diff --git a/src/tutoring/tutoring.module.ts b/src/tutoring/tutoring.module.ts index 68c0f39..11afa1e 100644 --- a/src/tutoring/tutoring.module.ts +++ b/src/tutoring/tutoring.module.ts @@ -2,14 +2,22 @@ import { Module } from '@nestjs/common'; import { TutoringService } from './tutoring.service'; import { TutoringController } from './tutoring.controller'; import { DynamooseModule } from 'nestjs-dynamoose'; -import { UserSchema } from '../user/entities/user.schema'; +import { TutoringSchema } from './entities/tutoring.schema'; +import { RequestSchema } from '../request/entities/request.schema'; @Module({ imports: [ DynamooseModule.forFeature([ + { + name: 'Request', + schema: RequestSchema, + options: { + tableName: 'Requests', + }, + }, { name: 'Tutoring', - schema: UserSchema, + schema: TutoringSchema, options: { tableName: 'Tutorings', }, diff --git a/src/tutoring/tutoring.service.ts b/src/tutoring/tutoring.service.ts index c79122e..14c14fa 100644 --- a/src/tutoring/tutoring.service.ts +++ b/src/tutoring/tutoring.service.ts @@ -1,17 +1,31 @@ import { Injectable } from '@nestjs/common'; -import { CreateTutoringDto } from './dto/create-tutoring.dto'; +import { + CreateTutoringDto, + NotFound_CreateTutoringDto, + Success_CreateTutoringDto, +} from './dto/create-tutoring.dto'; import { InjectModel, Model } from 'nestjs-dynamoose'; import { Tutoring, TutoringKey } from './entities/tutoring.interface'; import { v4 as uuid } from 'uuid'; +import { Request, RequestKey } from '../request/entities/request.interface'; @Injectable() export class TutoringService { constructor( + @InjectModel('Request') + private requestModel: Model, @InjectModel('Tutoring') private tutoringModel: Model, ) {} async create(createTutoringDto: CreateTutoringDto) { + const request = await this.requestModel.get({ + id: createTutoringDto.requestId, + }); + if (request === undefined) { + return new NotFound_CreateTutoringDto('과외 요청을 찾을 수 없습니다'); + } + const tutoring = { id: uuid(), ...createTutoringDto, @@ -20,6 +34,8 @@ export class TutoringService { endedAt: '', status: 'matched', }; - return this.tutoringModel.create(tutoring); + await this.tutoringModel.create(tutoring); + + return new Success_CreateTutoringDto(tutoring); } }