diff --git a/BE/src/shapes/shapes.controller.ts b/BE/src/shapes/shapes.controller.ts index 84cdff4..6e6d4a9 100644 --- a/BE/src/shapes/shapes.controller.ts +++ b/BE/src/shapes/shapes.controller.ts @@ -23,4 +23,24 @@ export class ShapesController { ): Promise { return this.shapesService.getShapeFileByUuid(uuid, user); } + + @Get("/") + @UseGuards(JwtAuthGuard) + async getShapesByUser(@GetUser() user: User): Promise { + const shapeLists = await this.shapesService.getShapesByUser(user); + const shapeUuidList = shapeLists[0]; + const shapeFileList = shapeLists[1]; + + let result = {}; + + for (let i = 0; i < shapeUuidList.length; i++) { + const response = { + uuid: shapeUuidList[i], + svg: shapeFileList[i], + }; + result[`shape${i + 1}`] = response; + } + + return result; + } } diff --git a/BE/src/shapes/shapes.repository.ts b/BE/src/shapes/shapes.repository.ts index 16e083f..1b1dd94 100644 --- a/BE/src/shapes/shapes.repository.ts +++ b/BE/src/shapes/shapes.repository.ts @@ -31,4 +31,15 @@ export class ShapesRepository { } return found; } + + async getShapesByUser(user: User): Promise { + const shapeList: Shape[] = await Shape.find({ + where: { user: { userId: user.userId } }, + }); + if (!shapeList) { + throw new NotFoundException("존재하지 않는 사용자입니다."); + } + + return shapeList; + } } diff --git a/BE/src/shapes/shapes.service.ts b/BE/src/shapes/shapes.service.ts index 2d38474..4c88bbb 100644 --- a/BE/src/shapes/shapes.service.ts +++ b/BE/src/shapes/shapes.service.ts @@ -28,4 +28,30 @@ export class ShapesService { return getShapeFromS3(shape.shapePath); } + + async getShapesByUser(user: User): Promise { + let shapeList = []; + const defaultShapeList = await this.getDefaultShapeFiles(); + + if (user.userId !== "commonUser") { + shapeList = defaultShapeList.concat( + await this.shapesRepository.getShapesByUser(user), + ); + } else { + shapeList = defaultShapeList; + } + + const shapeUuidList = shapeList.map((shape) => { + return shape.uuid; + }); + + // getShapeFileByUuid가 async라서 Promise.all로 await을 걸지 않으면 Promise이 반환 + const shapeFileList = await Promise.all( + shapeUuidList.map((uuid) => { + return this.getShapeFileByUuid(uuid, user); + }), + ); + + return [shapeUuidList, shapeFileList]; + } } diff --git a/BE/test/int/shape.read-list.int-spec.ts b/BE/test/int/shape.read-list.int-spec.ts new file mode 100644 index 0000000..0cbc476 --- /dev/null +++ b/BE/test/int/shape.read-list.int-spec.ts @@ -0,0 +1,95 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { INestApplication } from "@nestjs/common"; +import * as request from "supertest"; +import { ValidationPipe } from "@nestjs/common"; +import { typeORMTestConfig } from "src/configs/typeorm.test.config"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { RedisModule } from "@liaoliaots/nestjs-redis"; +import { ShapesModule } from "src/shapes/shapes.module"; + +describe("[전체 모양 조회] /shapes GET 통합 테스트", () => { + let app: INestApplication; + let accessToken: string; + + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [ + TypeOrmModule.forRoot(typeORMTestConfig), + RedisModule.forRoot({ + readyLog: true, + config: { + host: "223.130.129.145", + port: 6379, + }, + }), + ShapesModule, + ], + }).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; + }); + + afterAll(async () => { + await app.close(); + }); + + it("정상 요청 시 200 OK 응답", async () => { + const postResponse = await request(app.getHttpServer()) + .get("/shapes") + .set("Authorization", `Bearer ${accessToken}`) + .expect(200); + + expect(postResponse.body).toEqual([ + "0c99bbc6-e404-464b-a310-5bf0fa0f0fa7", + "d4ffa969-a299-4e15-a700-dccd6ef89f25", + "acbb06cb-a6bf-4f3b-9f30-f59ad5c03c90", + ]); + }); + + it("일반 유저 정상 요청 시 200 OK 응답", async () => { + const signInPost = await request(app.getHttpServer()) + .post("/auth/signin") + .send({ + userId: "userId", + password: "password", + }); + + accessToken = signInPost.body.accessToken; + + const postResponse = await request(app.getHttpServer()) + .get("/shapes") + .set("Authorization", `Bearer ${accessToken}`) + .expect(200); + + expect(postResponse.body).toEqual([ + "0c99bbc6-e404-464b-a310-5bf0fa0f0fa7", + "d4ffa969-a299-4e15-a700-dccd6ef89f25", + "acbb06cb-a6bf-4f3b-9f30-f59ad5c03c90", + ]); + }); + + it("액세스 토큰 없이 요청 시 401 Unauthorized 응답", async () => { + const postResponse = await request(app.getHttpServer()) + .get("/shapes") + .expect(401); + + expect(postResponse.body).toEqual({ + error: "Unauthorized", + message: "비로그인 상태의 요청입니다.", + statusCode: 401, + }); + }); +});