Skip to content

Commit

Permalink
✨ 모각코 CRUD 초안 설계
Browse files Browse the repository at this point in the history
모각코 CRUD 초안 설계입니다.
사용자 인증에 대한 로직이 필요합니다.
  • Loading branch information
ldhbenecia committed Nov 21, 2023
1 parent 7aa7466 commit 83f5e1b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
25 changes: 23 additions & 2 deletions app/backend/src/mogaco/mogaco.controller.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,7 +15,25 @@ export class MogacoController {
}

@Get('/:id')
async getMogacoById(@Param('id', ParseIntPipe) id: number): Promise<Mogaco> {
async getMogacoById(@Param('id', ParseIntPipe) id: number): Promise<MogacoDto> {
return this.mogacoService.getMogacoById(id);
}

@Post('/')
async createMogaco(@Body() createMogacoDto: CreateMogacoDto): Promise<Mogaco> {
return this.mogacoService.createMogaco(createMogacoDto);
}

@Delete('/:id')
async deleteMogaco(@Param('id', ParseIntPipe) id: number): Promise<void> {
return this.mogacoService.deleteMogaco(id);
}

@Patch('/:id/status')
updateMogacoStatus(
@Param('id', ParseIntPipe) id: number,
@Body('status', MogacoStatusValidationPipe) status: MogacoStatus,
): Promise<Mogaco> {
return this.mogacoService.updateMogacoStatus(id, status);
}
}
70 changes: 67 additions & 3 deletions app/backend/src/mogaco/mogaco.repository.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,9 +12,71 @@ export class MogacoRepository {
return this.prisma.mogaco.findMany();
}

async getMogacoById(id: number): Promise<Mogaco> {
return this.prisma.mogaco.findUnique({
async getMogacoById(id: number): Promise<MogacoDto> {
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<Mogaco> {
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<void> {
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<Mogaco> {
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}`);
}
}
}
18 changes: 17 additions & 1 deletion app/backend/src/mogaco/mogaco.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,7 +12,21 @@ export class MogacoService {
return this.mogacoRepository.getAllMogaco();
}

async getMogacoById(id: number): Promise<Mogaco> {
async getMogacoById(id: number): Promise<MogacoDto> {
return this.mogacoRepository.getMogacoById(id);
}

async createMogaco(createMogaco: CreateMogacoDto): Promise<Mogaco> {
return this.mogacoRepository.createMogaco(createMogaco);
}

async deleteMogaco(id: number): Promise<void> {
return this.mogacoRepository.deleteMogaco(id);
}

async updateMogacoStatus(id: number, status: MogacoStatus): Promise<Mogaco> {
const mogaco = await this.getMogacoById(id);
mogaco.status = status;
return this.mogacoRepository.updateMogacoStatus(mogaco);
}
}

0 comments on commit 83f5e1b

Please sign in to comment.