Skip to content

Commit

Permalink
✨ 모각코 참가 및 참가자 정보 반환, Swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
ldhbenecia committed Nov 22, 2023
1 parent 6196cb4 commit 544c853
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
66 changes: 65 additions & 1 deletion app/backend/src/mogaco/mogaco.controller.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,106 @@
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, UseGuards } from '@nestjs/common';
import { MogacoService } from './mogaco.service';
import { Member, Mogaco } from '@prisma/client';
import { CreateMogacoDto, MogacoDto } from './dto';
import { CreateMogacoDto, MogacoDto, StatusDto } from './dto';
import { MogacoStatusValidationPipe } from './pipes/mogaco-status-validation.pipe';
import { MogacoStatus } from './dto/mogaco-status.enum';
import { GetUser } from 'libs/decorators/get-user.decorator';
import { AtGuard } from 'src/auth/guards/at.guard';
import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { MogacoWithMemberDto } from './dto/response-mogaco.dto';
import { ParticipantResponseDto } from './dto/response-participants.dto';

@ApiTags('Mogaco API')
@Controller('mogaco')
@UseGuards(AtGuard)
export class MogacoController {
constructor(private readonly mogacoService: MogacoService) {}

@Get('/')
@ApiOperation({
summary: '모든 모각코 조회',
description: '존재하는 모든 모각코를 조회합니다.\n 배열로 여러 개를 반환합니다.',
})
@ApiResponse({ status: 200, description: 'Successfully retrieved', type: [MogacoDto] })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async getAllMogaco(): Promise<Mogaco[]> {
return this.mogacoService.getAllMogaco();
}

@Get('/:id')
@ApiOperation({
summary: '특정 게시물 조회',
description: '특정 게시물의 Id 값으로 해당 게시물을 조회합니다.',
})
@ApiParam({ name: 'id', description: '조회할 모각코의 Id' })
@ApiResponse({ status: 200, description: 'Successfully retrieved', type: MogacoDto })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async getMogacoById(@Param('id', ParseIntPipe) id: number): Promise<MogacoDto> {
return this.mogacoService.getMogacoById(id);
}

@Post('/')
@ApiOperation({
summary: '모각코 개설',
description: '새로운 모각코를 개설합니다.',
})
@ApiBody({ type: CreateMogacoDto })
@ApiResponse({ status: 201, description: 'Successfully created', type: MogacoWithMemberDto })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async createMogaco(@Body() createMogacoDto: CreateMogacoDto, @GetUser() member: Member): Promise<Mogaco> {
return this.mogacoService.createMogaco(createMogacoDto, member);
}

@Delete('/:id')
@ApiOperation({
summary: '모각코 삭제',
description: '특정 모각코를 삭제합니다.',
})
@ApiParam({ name: 'id', description: '삭제할 모각코의 Id' })
@ApiResponse({ status: 200, description: 'Successfully deleted' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
async deleteMogaco(@Param('id', ParseIntPipe) id: number, @GetUser() member: Member): Promise<void> {
return this.mogacoService.deleteMogaco(id, member);
}

@Patch('/:id/status')
@ApiOperation({
summary: '모각코 상태 업데이트',
description: '특정 모각코의 상태를 업데이트합니다.',
})
@ApiParam({ name: 'id', description: '상태를 업데이트할 모각코의 Id' })
@ApiBody({ type: StatusDto })
@ApiResponse({ status: 200, description: 'Successfully Updated', type: MogacoWithMemberDto })
@ApiResponse({ status: 401, description: 'Unauthorized' })
updateMogacoStatus(
@Param('id', ParseIntPipe) id: number,
@Body('status', MogacoStatusValidationPipe) status: MogacoStatus,
): Promise<Mogaco> {
return this.mogacoService.updateMogacoStatus(id, status);
}

@Post('/:id/join')
@ApiOperation({
summary: '모각코 참가',
description: '특정 모각코에 참가합니다.',
})
@ApiParam({ name: 'id', description: '참가할 모각코의 Id' })
@ApiResponse({ status: 201, description: 'Successfully join' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async joinMogaco(@Param('id', ParseIntPipe) id: number, @GetUser() member: Member): Promise<void> {
return this.mogacoService.joinMogaco(id, member);
}

@Get('/:id/participants')
@ApiOperation({
summary: '참가자 목록 조회',
description: '특정 모각코에 참가한 모든 참가자 목록을 조회합니다.',
})
@ApiParam({ name: 'id', description: '참가자 목록을 조회할 모각코의 Id' })
@ApiResponse({ status: 200, description: 'Successfully retrieved', type: [ParticipantResponseDto] })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async getParticipants(@Param('id', ParseIntPipe) id: number): Promise<Member[]> {
return this.mogacoService.getParticipants(id);
}
}
44 changes: 44 additions & 0 deletions app/backend/src/mogaco/mogaco.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,48 @@ export class MogacoRepository {
throw new Error(`Failed to update Mogaco status: ${error.message}`);
}
}

async joinMogaco(id: number, member: Member): Promise<void> {
const mogaco = await this.prisma.mogaco.findUnique({
where: { id, deletedAt: null },
});

if (!mogaco) {
throw new NotFoundException(`Mogaco with id ${id} not found`);
}

const existingParticipant = await this.prisma.participant.findUnique({
where: {
postId_userId: {
postId: mogaco.id,
userId: member.id,
},
},
});

if (existingParticipant) {
throw new ForbiddenException(`Member with id ${member.id} is already participating in Mogaco with id ${id}`);
}

await this.prisma.participant.create({
data: {
postId: mogaco.id,
userId: member.id,
},
});
}

async getParticipants(id: number): Promise<Member[]> {
const participants = await this.prisma.participant.findMany({
where: {
postId: id,
},
include: {
member: true,
},
});

// 가져온 참가자 목록의 각 참가자의 member 속성을 통해 실제 멤버 객체로 매핑하여 반환
return participants.map((participant) => participant.member);
}
}
8 changes: 8 additions & 0 deletions app/backend/src/mogaco/mogaco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ export class MogacoService {
mogaco.status = status;
return this.mogacoRepository.updateMogacoStatus(mogaco);
}

async joinMogaco(id: number, member: Member): Promise<void> {
return await this.mogacoRepository.joinMogaco(id, member);
}

async getParticipants(id: number): Promise<Member[]> {
return this.mogacoRepository.getParticipants(id);
}
}

0 comments on commit 544c853

Please sign in to comment.