-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from ldhbenecia/feature/mogaco
[Feat] 모각코 CRUD 초안 설계 및 구현
- Loading branch information
Showing
18 changed files
with
263 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(BigInt.prototype as any).toJSON = function () { | ||
return this.toString(); | ||
}; |
11 changes: 11 additions & 0 deletions
11
app/backend/prisma/migrations/20231121065729_init/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
Warnings: | ||
- You are about to alter the column `date` on the `Mogaco` table. The data in that column could be lost. The data in that column will be cast from `DateTime(0)` to `DateTime`. | ||
- You are about to alter the column `deleted_at` on the `Mogaco` table. The data in that column could be lost. The data in that column will be cast from `DateTime(0)` to `DateTime`. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE `Mogaco` MODIFY `date` DATETIME NOT NULL, | ||
MODIFY `updated_at` DATETIME(3) NULL, | ||
MODIFY `deleted_at` DATETIME NULL; |
10 changes: 10 additions & 0 deletions
10
app/backend/prisma/migrations/20231121065750_morak_0_0_2/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
Warnings: | ||
- You are about to alter the column `date` on the `Mogaco` table. The data in that column could be lost. The data in that column will be cast from `DateTime(0)` to `DateTime`. | ||
- You are about to alter the column `deleted_at` on the `Mogaco` table. The data in that column could be lost. The data in that column will be cast from `DateTime(0)` to `DateTime`. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE `Mogaco` MODIFY `date` DATETIME NOT NULL, | ||
MODIFY `deleted_at` DATETIME NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { IsDateString, IsEnum, IsInt, IsNotEmpty, IsOptional } from 'class-validator'; | ||
import { MogacoStatus } from './mogaco-status.enum'; | ||
|
||
export class CreateMogacoDto { | ||
@IsNotEmpty() | ||
@IsInt() | ||
group_id: number; | ||
|
||
@IsNotEmpty() | ||
title: string; | ||
|
||
@IsNotEmpty() | ||
contents: string; | ||
|
||
@IsNotEmpty() | ||
@IsDateString() | ||
date: string; | ||
|
||
@IsNotEmpty() | ||
max_human_count: number; | ||
|
||
@IsNotEmpty() | ||
address: string; | ||
|
||
@IsOptional() | ||
@IsEnum(MogacoStatus, { message: 'Invalid status' }) | ||
status?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './create-mogaco.dto'; | ||
export * from './mogaco.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum MogacoStatus { | ||
RECRUITING = '모집 중', | ||
CLOSED = '모집 마감', | ||
COMPLETED = '종료', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class MogacoDto { | ||
id: bigint; | ||
group_id: bigint; | ||
title: string; | ||
contents: string; | ||
date: Date; | ||
max_human_count: number; | ||
address: string; | ||
status: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 { | ||
constructor(private readonly mogacoService: MogacoService) {} | ||
|
||
@Get('/') | ||
async getAllMogaco(): Promise<Mogaco[]> { | ||
return this.mogacoService.getAllMogaco(); | ||
} | ||
|
||
@Get('/:id') | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MogacoService } from './mogaco.service'; | ||
import { MogacoController } from './mogaco.controller'; | ||
import { MogacoRepository } from './mogaco.repository'; | ||
import { PrismaService } from 'libs/utils/prisma.service'; | ||
|
||
@Module({ | ||
controllers: [MogacoController], | ||
providers: [MogacoService, MogacoRepository, PrismaService], | ||
}) | ||
export class MogacoModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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 { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async getAllMogaco(): Promise<Mogaco[]> { | ||
return this.prisma.mogaco.findMany(); | ||
} | ||
|
||
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}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 { | ||
constructor(private mogacoRepository: MogacoRepository) {} | ||
|
||
async getAllMogaco(): Promise<Mogaco[]> { | ||
return this.mogacoRepository.getAllMogaco(); | ||
} | ||
|
||
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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/backend/src/mogaco/pipes/mogaco-status-validation.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { BadRequestException, PipeTransform } from '@nestjs/common'; | ||
import { MogacoStatus } from '../dto/mogaco-status.enum'; | ||
|
||
export class MogacoStatusValidationPipe implements PipeTransform { | ||
readonly StatusOptions = [MogacoStatus.RECRUITING, MogacoStatus.CLOSED, MogacoStatus.COMPLETED]; | ||
|
||
transform(value: any) { | ||
value = value.toUpperCase(); | ||
|
||
if (!this.isStatusValid(value)) { | ||
throw new BadRequestException(`${value} isn't in the status options`); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private isStatusValid(status: any) { | ||
const index = this.StatusOptions.indexOf(status); | ||
return index !== -1; | ||
} | ||
} |