Skip to content

Commit

Permalink
Merge pull request #92 from boostcampwm2023/feat/90-diaries-list-api
Browse files Browse the repository at this point in the history
[Feat] 전체 일기 목록 API 구현 및 e2e 테스트 작성
  • Loading branch information
JoonSoo-Kim authored Nov 22, 2023
2 parents 4316dc7 + 5909f95 commit 0092f47
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
41 changes: 40 additions & 1 deletion BE/src/diaries/diaries.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import {
DeleteDiaryDto,
DiaryUuidDto,
ReadDiaryDto,
ReadDiariesResponseDto,
UpdateDiaryDto,
} from "./diaries.dto";
import { Diary } from "./diaries.entity";
import { AuthGuard } from "@nestjs/passport";
import { IdGuard } from "src/auth/guard/auth.id-guard";
import { GetUser } from "src/auth/get-user.decorator";
import { User } from "src/users/users.entity";

@Controller("diaries")
@UseGuards(AuthGuard())
Expand Down Expand Up @@ -59,12 +62,48 @@ export class DiariesController {
y: parseFloat(coordinateArray[1]),
z: parseFloat(coordinateArray[2]),
},
shape_uuid: diary.shape.uuid,
shapeUuid: diary.shape.uuid,
};

return response;
}

@Get()
async readDiariesByUser(
@GetUser() user: User,
): Promise<ReadDiariesResponseDto[]> {
const diaryList = await this.diariesService.readDiariesByUser(user);
let readDiaryResponseDtoList: ReadDiariesResponseDto[] = [];
diaryList.map((diary) => {
const coordinateArray = diary.point.split(",");
const response = {
userId: diary.user.userId,
uuid: diary.uuid,
title: diary.title,
content: diary.content,
date: diary.date,
tags: [],
emotion: {
positive: diary.positiveRatio,
neutral: diary.neutralRatio,
negative: diary.negativeRatio,
sentiment: diary.sentiment,
},
coordinate: {
x: parseFloat(coordinateArray[0]),
y: parseFloat(coordinateArray[1]),
z: parseFloat(coordinateArray[2]),
},
shapeUuid: diary.shape.uuid,
};

const readDiaryResponseDto: ReadDiariesResponseDto = response;
readDiaryResponseDtoList.push(readDiaryResponseDto);
});

return readDiaryResponseDtoList;
}

@Put()
@UseGuards(IdGuard)
modifyDiary(@Body() updateDiaryDto: UpdateDiaryDto): Promise<Diary> {
Expand Down
7 changes: 4 additions & 3 deletions BE/src/diaries/diaries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ export class DiaryUuidDto {
uuid: string;
}

export class ReadDiaryResponseDto {
export class ReadDiariesResponseDto {
userId: string;
uuid: string;
title: string;
content: string;
date: Date;
tags: string[];
emotion: {
position: number;
positive: number;
neutral: number;
negative: number;
sentiment: sentimentStatus;
Expand All @@ -93,5 +94,5 @@ export class ReadDiaryResponseDto {
y: number;
z: number;
};
shape_uuid: string;
shapeUuid: string;
}
8 changes: 8 additions & 0 deletions BE/src/diaries/diaries.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export class DiariesRepository {
return diary;
}

async readDiariesByUser(user): Promise<Diary[]> {
const diaryList = await Diary.find({
where: { user: user.id },
});

return diaryList;
}

async updateDiary(
updateDiaryDto: UpdateDiaryDto,
encodedContent: string,
Expand Down
13 changes: 13 additions & 0 deletions BE/src/diaries/diaries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ export class DiariesService {
return diary;
}

async readDiariesByUser(user): Promise<Diary[]> {
let diaryList: Diary[] =
await this.diariesRepository.readDiariesByUser(user);

diaryList.map((diary) => {
diary.content = atob(diary.content);
// Mysql DB에서 가져온 UST 날짜 데이터를 KST로 변경
diary.date.setHours(diary.date.getHours() + 9);
});

return diaryList;
}

async modifyDiary(updateDiaryDto: UpdateDiaryDto): Promise<Diary> {
const encodedContent = btoa(updateDiaryDto.content);
return this.diariesRepository.updateDiary(updateDiaryDto, encodedContent);
Expand Down
55 changes: 55 additions & 0 deletions BE/test/diaries.read-list.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import * as request from "supertest";
import { AppModule } from "../src/app.module";
import { ValidationPipe } from "@nestjs/common";

describe("AppController (e2e)", () => {
let app: INestApplication;
let accessToken: string;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
app.enableCors();
app.useGlobalPipes(new ValidationPipe());

await app.init();

const signInPost = await request(app.getHttpServer())
.post("/auth/signin")
.send({
userId: "commonUser",
password: process.env.COMMON_USER_PASS,
});

accessToken = signInPost.body.accessToken;
});

afterEach(async () => {
await app.close();
});

it("정상 요청 시 200 OK 응답", async () => {
const postResponse = await request(app.getHttpServer())
.get("/diaries")
.set("Authorization", `Bearer ${accessToken}`)
.expect(200);
});

it("정상 요청 시 200 OK 응답", async () => {
const postResponse = await request(app.getHttpServer())
.get("/diaries")
.set("Authorization", `Bearer ${accessToken}`)
.expect(200);
});

it("액세스 토큰 없이 요청 시 401 Unauthorized 응답", async () => {
const postResponse = await request(app.getHttpServer())
.get("/diaries")
.expect(401);
});
});

0 comments on commit 0092f47

Please sign in to comment.