From 8391055effc73b823a016d7eb5ada0a78c4a0984 Mon Sep 17 00:00:00 2001 From: yena <50291995+nyj001012@users.noreply.github.com> Date: Sat, 28 Sep 2024 10:50:59 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20histories=20api=20=EB=AA=85=EC=84=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#48)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: histories swagger api 구조 작성 * refactor: :truck: histories 폴더 구조 변경 * feat: histories dto 추가 * feat: swagger 명세 작성 * refactor: * feat: Request, Reponse용 스키마 구별 * feat: * [autofix.ci] apply automated fixes * Update backend/src/histories/schema/histories.schema.ts Co-authored-by: scarf * Update backend/src/histories/dto/histories.dto.ts Co-authored-by: scarf * feat: 결과 스키마 구체화 * feat: * feat: zodToOpneAPI를 이용하여 조회 결과를 스키마로 작성 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: scarf --- backend/src/app.module.ts | 2 +- .../src/controller/histories.controller.ts | 12 ----- backend/src/histories/dto/histories.dto.ts | 4 ++ backend/src/histories/histories.controller.ts | 27 ++++++++++++ .../module => histories}/histories.module.ts | 6 +-- .../histories.providers.ts | 0 .../histories.service.ts | 0 .../src/histories/schema/histories.schema.ts | 44 +++++++++++++++++++ 8 files changed, 79 insertions(+), 16 deletions(-) delete mode 100644 backend/src/controller/histories.controller.ts create mode 100644 backend/src/histories/dto/histories.dto.ts create mode 100644 backend/src/histories/histories.controller.ts rename backend/src/{repository/module => histories}/histories.module.ts (54%) rename backend/src/{repository/providers => histories}/histories.providers.ts (100%) rename backend/src/{service => histories}/histories.service.ts (100%) create mode 100644 backend/src/histories/schema/histories.schema.ts diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 4fc6281..ff56137 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; // Add this line import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { HistoriesModule } from './repository/module/histories.module'; +import { HistoriesModule } from './histories/histories.module'; import * as dotenv from 'dotenv'; dotenv.config(); diff --git a/backend/src/controller/histories.controller.ts b/backend/src/controller/histories.controller.ts deleted file mode 100644 index 109df36..0000000 --- a/backend/src/controller/histories.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; -import { HistoriesService } from 'src/service/histories.service'; - -@Controller('histories') -export class HistoriesController { - constructor(private historiesService: HistoriesService) {} - - @Get() - getHistories() { - return this.historiesService.getHistories(); - } -} diff --git a/backend/src/histories/dto/histories.dto.ts b/backend/src/histories/dto/histories.dto.ts new file mode 100644 index 0000000..9197c5a --- /dev/null +++ b/backend/src/histories/dto/histories.dto.ts @@ -0,0 +1,4 @@ +import { createZodDto } from '@anatine/zod-nestjs'; +import { getHistoriesRequestSchema } from '../schema/histories.schema'; + +export class getHistoriesDto extends createZodDto(getHistoriesRequestSchema) {} diff --git a/backend/src/histories/histories.controller.ts b/backend/src/histories/histories.controller.ts new file mode 100644 index 0000000..ce2b9fa --- /dev/null +++ b/backend/src/histories/histories.controller.ts @@ -0,0 +1,27 @@ +import { Controller, Get, Query } from '@nestjs/common'; +import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { HistoriesService } from 'src/histories/histories.service'; +import { getHistoriesDto } from 'src/histories/dto/histories.dto'; // Adjust the import path as necessary +import { getHistoriesResponseSchema } from './schema/histories.schema'; +import { zodToOpenAPI } from 'nestjs-zod'; + +@Controller('histories') +export class HistoriesController { + constructor(private historiesService: HistoriesService) {} + + @Get() + @ApiOperation({ + summary: '대출 기록 조회', + description: + '현재까지의 대출 기록을 최신순으로 가져온다. 사서라면 모든 사용자의 기록을, 사서가 아니라면 본인의 기록만 볼 수 있다.', + tags: ['histories'], + }) + @ApiResponse({ + status: 200, + description: '대출 기록을 반환한다.', + schema: zodToOpenAPI(getHistoriesResponseSchema), + }) + getHistories(@Query() query: getHistoriesDto) { + return this.historiesService.getHistories(); + } +} diff --git a/backend/src/repository/module/histories.module.ts b/backend/src/histories/histories.module.ts similarity index 54% rename from backend/src/repository/module/histories.module.ts rename to backend/src/histories/histories.module.ts index 9362380..cf8df13 100644 --- a/backend/src/repository/module/histories.module.ts +++ b/backend/src/histories/histories.module.ts @@ -1,9 +1,9 @@ import { Module } from '@nestjs/common'; -import { HistoriesService } from 'src/service/histories.service'; -import { historiesProviders } from '../providers/histories.providers'; // Import the 'historiesProviders' variable from the correct file +import { HistoriesService } from 'src/histories/histories.service'; +import { historiesProviders } from './histories.providers'; // Import the 'historiesProviders' variable from the correct file import { TypeOrmModule } from '@nestjs/typeorm'; import { VHistories } from 'src/entities/VHistories'; -import { HistoriesController } from 'src/controller/histories.controller'; +import { HistoriesController } from 'src/histories/histories.controller'; @Module({ imports: [TypeOrmModule.forFeature([VHistories])], diff --git a/backend/src/repository/providers/histories.providers.ts b/backend/src/histories/histories.providers.ts similarity index 100% rename from backend/src/repository/providers/histories.providers.ts rename to backend/src/histories/histories.providers.ts diff --git a/backend/src/service/histories.service.ts b/backend/src/histories/histories.service.ts similarity index 100% rename from backend/src/service/histories.service.ts rename to backend/src/histories/histories.service.ts diff --git a/backend/src/histories/schema/histories.schema.ts b/backend/src/histories/schema/histories.schema.ts new file mode 100644 index 0000000..d24b9d6 --- /dev/null +++ b/backend/src/histories/schema/histories.schema.ts @@ -0,0 +1,44 @@ +import { z } from 'zod'; + +export const getHistoriesRequestSchema = z.object({ + who: z + .enum(['all', 'my']) + .describe( + '모든 사용자의 기록을 볼 것인지, 본인의 기록만 볼 것인지 결정하는 필터', + ), + query: z.string().optional().describe('검색어'), + type: z + .enum(['user', 'title', 'callsign', 'bookId']) + .optional() + .describe('어떤 값들로 검색하고 싶은지 결정하는 필터'), + page: z.number().optional().describe('페이지 번호'), + limit: z.number().optional().describe('한 페이지에 보여줄 항목 수'), +}); + +export const getHistoriesResponseSchema = z + .object({ + items: z.array( + z.object({ + id: z.number().int(), + lendingCondition: z.string(), + login: z.string(), + returningCondition: z.string(), + penaltyDays: z.number().int(), + callSign: z.string(), + title: z.string(), + bookInfoId: z.number().int(), + createdAt: z.string(), + }), + ), + meta: z.object({ + totalItems: z.number().positive().describe('전체 대출 기록 수'), + itemCount: z.number().positive().describe('현재 페이지의 대출 기록 수'), + itemsPerPage: z + .number() + .positive() + .describe('한 페이지에 보여줄 대출 기록 수'), + totalPages: z.number().positive().describe('전체 페이지 수'), + currentPage: z.number().positive().describe('현재 페이지 번호'), + }), + }) + .describe('대출 기록 조회 응답');