From 83f5e1bdd8d44a1ccfb322718b99830ffcff8cd3 Mon Sep 17 00:00:00 2001 From: DongHyeok Lim Date: Tue, 21 Nov 2023 17:22:48 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=EB=AA=A8=EA=B0=81=EC=BD=94=20CRUD?= =?UTF-8?q?=20=EC=B4=88=EC=95=88=20=EC=84=A4=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 모각코 CRUD 초안 설계입니다. 사용자 인증에 대한 로직이 필요합니다. --- app/backend/src/mogaco/mogaco.controller.ts | 25 +++++++- app/backend/src/mogaco/mogaco.repository.ts | 70 ++++++++++++++++++++- app/backend/src/mogaco/mogaco.service.ts | 18 +++++- 3 files changed, 107 insertions(+), 6 deletions(-) diff --git a/app/backend/src/mogaco/mogaco.controller.ts b/app/backend/src/mogaco/mogaco.controller.ts index 800b5c597..3907e0c96 100644 --- a/app/backend/src/mogaco/mogaco.controller.ts +++ b/app/backend/src/mogaco/mogaco.controller.ts @@ -1,6 +1,9 @@ -import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from '@nestjs/common'; import { MogacoService } from './mogaco.service'; import { Mogaco } from '@prisma/client'; +import { CreateMogacoDto, MogacoDto } from './dto'; +import { MogacoStatusValidationPipe } from './pipes/mogaco-status-validation.pipe'; +import { MogacoStatus } from './dto/mogaco-status.enum'; @Controller('mogaco') export class MogacoController { @@ -12,7 +15,25 @@ export class MogacoController { } @Get('/:id') - async getMogacoById(@Param('id', ParseIntPipe) id: number): Promise { + async getMogacoById(@Param('id', ParseIntPipe) id: number): Promise { return this.mogacoService.getMogacoById(id); } + + @Post('/') + async createMogaco(@Body() createMogacoDto: CreateMogacoDto): Promise { + return this.mogacoService.createMogaco(createMogacoDto); + } + + @Delete('/:id') + async deleteMogaco(@Param('id', ParseIntPipe) id: number): Promise { + return this.mogacoService.deleteMogaco(id); + } + + @Patch('/:id/status') + updateMogacoStatus( + @Param('id', ParseIntPipe) id: number, + @Body('status', MogacoStatusValidationPipe) status: MogacoStatus, + ): Promise { + return this.mogacoService.updateMogacoStatus(id, status); + } } diff --git a/app/backend/src/mogaco/mogaco.repository.ts b/app/backend/src/mogaco/mogaco.repository.ts index 54e47202f..2a16ed3e3 100644 --- a/app/backend/src/mogaco/mogaco.repository.ts +++ b/app/backend/src/mogaco/mogaco.repository.ts @@ -1,6 +1,8 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from '../../libs/utils/prisma.service'; import { Mogaco } from '@prisma/client'; +import { MogacoStatus } from './dto/mogaco-status.enum'; +import { CreateMogacoDto, MogacoDto } from './dto'; @Injectable() export class MogacoRepository { @@ -10,9 +12,71 @@ export class MogacoRepository { return this.prisma.mogaco.findMany(); } - async getMogacoById(id: number): Promise { - return this.prisma.mogaco.findUnique({ + async getMogacoById(id: number): Promise { + const mogaco = await this.prisma.mogaco.findUnique({ where: { id }, }); + + if (!mogaco) { + throw new NotFoundException(`Mogaco with id ${id} not found`); + } + + return { + id: mogaco.id, + group_id: mogaco.group_id, + title: mogaco.title, + contents: mogaco.contents, + date: mogaco.date, + max_human_count: mogaco.max_human_count, + address: mogaco.address, + status: mogaco.status, + }; + } + + async createMogaco(createMogacoDto: CreateMogacoDto): Promise { + try { + const { group_id, title, contents, max_human_count, address, date } = createMogacoDto; + + const mogaco = await this.prisma.mogaco.create({ + data: { + group_id, + title, + contents, + max_human_count, + address, + status: MogacoStatus.RECRUITING, + date: new Date(date), + }, + }); + + return mogaco; + } catch (error) { + throw new Error(`Failed to create Mogaco: ${error.message}`); + } + } + + async deleteMogaco(id: number): Promise { + const mogaco = await this.prisma.mogaco.findUnique({ + where: { id }, + }); + + if (!mogaco) { + throw new NotFoundException(`Mogaco with id ${id} not found`); + } + + await this.prisma.mogaco.delete({ + where: { id }, + }); + } + + async updateMogacoStatus(mogaco: MogacoDto): Promise { + try { + return await this.prisma.mogaco.update({ + where: { id: mogaco.id }, + data: { status: mogaco.status }, + }); + } catch (error) { + throw new Error(`Failed to update Mogaco status: ${error.message}`); + } } } diff --git a/app/backend/src/mogaco/mogaco.service.ts b/app/backend/src/mogaco/mogaco.service.ts index 9498947d1..a765e08ef 100644 --- a/app/backend/src/mogaco/mogaco.service.ts +++ b/app/backend/src/mogaco/mogaco.service.ts @@ -1,6 +1,8 @@ import { Injectable } from '@nestjs/common'; import { MogacoRepository } from './mogaco.repository'; import { Mogaco } from '@prisma/client'; +import { CreateMogacoDto, MogacoDto } from './dto'; +import { MogacoStatus } from './dto/mogaco-status.enum'; @Injectable() export class MogacoService { @@ -10,7 +12,21 @@ export class MogacoService { return this.mogacoRepository.getAllMogaco(); } - async getMogacoById(id: number): Promise { + async getMogacoById(id: number): Promise { return this.mogacoRepository.getMogacoById(id); } + + async createMogaco(createMogaco: CreateMogacoDto): Promise { + return this.mogacoRepository.createMogaco(createMogaco); + } + + async deleteMogaco(id: number): Promise { + return this.mogacoRepository.deleteMogaco(id); + } + + async updateMogacoStatus(id: number, status: MogacoStatus): Promise { + const mogaco = await this.getMogacoById(id); + mogaco.status = status; + return this.mogacoRepository.updateMogacoStatus(mogaco); + } }