Skip to content

Commit

Permalink
Merge pull request #85 from boostcampwm2023/feat/81-diary-create-resp…
Browse files Browse the repository at this point in the history
…onse-handling

[Feat] 일기 생성 API에 대한 서버 응답 수정 및 e2e 테스트 작성
  • Loading branch information
JoonSoo-Kim authored Nov 21, 2023
2 parents cf1ffd8 + a153af5 commit 3642f51
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 133 deletions.
23 changes: 15 additions & 8 deletions BE/src/diaries/diaries.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Controller,
Delete,
Get,
HttpCode,
Param,
Post,
Put,
Expand All @@ -12,6 +13,7 @@ import { DiariesService } from "./diaries.service";
import {
CreateDiaryDto,
DeleteDiaryDto,
DiaryUuidDto,
ReadDiaryDto,
UpdateDiaryDto,
} from "./diaries.dto";
Expand All @@ -25,17 +27,22 @@ export class DiariesController {
constructor(private diariesService: DiariesService) {}

@Post()
async writeDiary(@Body() createDiaryDto: CreateDiaryDto): Promise<Diary> {
return this.diariesService.writeDiary(createDiaryDto);
@HttpCode(201)
async writeDiary(
@Body() createDiaryDto: CreateDiaryDto,
): Promise<DiaryUuidDto> {
const diary: Diary = await this.diariesService.writeDiary(createDiaryDto);
return { uuid: diary.uuid };
}

@Get("/:uuid")
@UseGuards(IdGuard)
async readDiary(@Param("uuid") uuid: string): Promise<String> {
async readDiary(@Param("uuid") uuid: string): Promise<Object> {
const readDiaryDto: ReadDiaryDto = { uuid };
const diary = await this.diariesService.readDiary(readDiaryDto);
const coordinateArray = diary.point.split(",");

const response = JSON.stringify({
const response = {
userId: diary.user.userId,
title: diary.title,
content: diary.content,
Expand All @@ -48,12 +55,12 @@ export class DiariesController {
sentiment: diary.sentiment,
},
coordinate: {
x: diary.point.split(",")[0],
y: diary.point.split(",")[1],
z: diary.point.split(",")[2],
x: parseFloat(coordinateArray[0]),
y: parseFloat(coordinateArray[1]),
z: parseFloat(coordinateArray[2]),
},
shape_uuid: diary.shape.uuid,
});
};

return response;
}
Expand Down
57 changes: 47 additions & 10 deletions BE/src/diaries/diaries.dto.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { IsString, IsDate, Matches, IsUUID, IsArray } from "class-validator";
import {
IsString,
IsDate,
Matches,
IsUUID,
IsArray,
IsNotEmpty,
IsDateString,
} from "class-validator";
import { sentimentStatus } from "src/utils/enum";

export class CreateDiaryDto {
@IsString()
@IsNotEmpty({ message: "제목은 비어있지 않아야 합니다." })
@IsString({ message: "제목은 문자열이어야 합니다." })
title: string;

@IsString()
@IsString({ message: "내용은 문자열이어야 합니다." })
content: string;

@IsString()
@Matches(RegExp(/^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$/), {
message: "적절하지 않은 포인트 양식입니다",
@IsNotEmpty({ message: "좌표는 비어있지 않아야 합니다." })
@IsString({ message: "좌표는 문자열이어야 합니다." })
@Matches(/^-?\d+(\.\d+)?,-?\d+(\.\d+)?,-?\d+(\.\d+)?$/, {
message: "적절하지 않은 좌표 양식입니다.",
})
point: string;

@IsDate()
@IsDateString()
@IsNotEmpty({ message: "날짜는 비어있지 않아야 합니다." })
date: Date;

@IsArray()
@IsArray({ message: "태그는 배열의 형태여야 합니다." })
tags: string[];

@IsUUID()
@IsUUID("4", { message: "모양 uuid 값이 uuid 양식이어야 합니다." })
@IsNotEmpty({ message: "모양 uuid는 비어있지 않아야 합니다." })
shapeUuid: string;
}

Expand All @@ -39,7 +52,7 @@ export class UpdateDiaryDto {
content: string;

@IsString()
@Matches(RegExp("^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$"), {
@Matches(/^-?\d+(\.\d+)?,-?\d+(\.\d+)?,-?\d+(\.\d+)?$/, {
message: "적절하지 않은 포인트 양식입니다",
})
point: string;
Expand All @@ -58,3 +71,27 @@ export class DeleteDiaryDto {
@IsUUID()
uuid: string;
}

export class DiaryUuidDto {
uuid: string;
}

export class ReadDiaryResponseDto {
userId: string;
title: string;
content: string;
date: Date;
tags: string[];
emotion: {
position: number;
neutral: number;
negative: number;
sentiment: sentimentStatus;
};
coordinate: {
x: number;
y: number;
z: number;
};
shape_uuid: string;
}
2 changes: 2 additions & 0 deletions BE/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ValidationPipe } from "@nestjs/common";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
app.useGlobalPipes(new ValidationPipe());
await app.listen(process.env.BE_PORT);
}
bootstrap();
4 changes: 3 additions & 1 deletion BE/src/users/users.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { IsString, Length, MaxLength, Matches } from "class-validator";
export class CreateUserDto {
@IsString()
@Length(4, 21)
@Matches(/^[A-Za-z0-9-]{5,20}$/)
userId: string;

@IsString()
@Matches(RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/), {
@Matches(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, {
message: "적절하지 않은 이메일 양식입니다.",
})
email: string;

@IsString()
@Length(4, 21)
@Matches(/^[A-Za-z0-9!@#$%^&*()+=-~]{5,20}$/)
password: string;

@IsString()
Expand Down
114 changes: 0 additions & 114 deletions BE/test/app.e2e-spec.ts

This file was deleted.

Loading

0 comments on commit 3642f51

Please sign in to comment.