Skip to content

Commit

Permalink
⭐ Wrap tutoring APIs' response
Browse files Browse the repository at this point in the history
  • Loading branch information
w8385 committed Jul 13, 2023
1 parent 9de7921 commit 3c40c47
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
17 changes: 14 additions & 3 deletions src/response/response.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
37 changes: 30 additions & 7 deletions src/tutoring/dto/create-tutoring.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { ResponseDto } from '../../responseDto';
import { Tutoring } from '../entities/tutoring.interface';

export class CreateTutoringDto {
@ApiProperty({
Expand All @@ -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;

Expand All @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/tutoring/entities/tutoring.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/tutoring/tutoring.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
12 changes: 10 additions & 2 deletions src/tutoring/tutoring.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
20 changes: 18 additions & 2 deletions src/tutoring/tutoring.service.ts
Original file line number Diff line number Diff line change
@@ -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<Request, RequestKey>,
@InjectModel('Tutoring')
private tutoringModel: Model<Tutoring, TutoringKey>,
) {}

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,
Expand All @@ -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);
}
}

0 comments on commit 3c40c47

Please sign in to comment.