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); + } }