Skip to content

Commit

Permalink
feat: 이벤트 CRUD (#14)
Browse files Browse the repository at this point in the history
* feat: Group 속성에 `events` 추가

* feat: Event CRUD

* feat: Group.members > `any`

* feat: [GET] /group/member 수정

* feat: groupService > addEvent, deleteEvent

* feat: Event CRUD

* feat: excel 서비스 common/excel로 이전

* lint: format @trivago/prettier-plugin-sort-imports

* refactor: /common /excel 분리

* fix: eventService Update

* fix: member.controller.spec.ts module dependency

* fix: eslint.yml

* feat: member/excel 엔드포인트 변경
  • Loading branch information
w8385 authored Apr 22, 2024
1 parent 7a2c4bc commit 5c3e7be
Show file tree
Hide file tree
Showing 42 changed files with 408 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
continue-on-error: true

- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v2
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: eslint-results.sarif
wait-for-processing: true
16 changes: 15 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
{
"printWidth": 80,
"tabWidth": 2,
"trailingComma": "all",
"singleQuote": true,
"semi": true,
"plugins": [
"@trivago/prettier-plugin-sort-imports"
],
"importOrderParserPlugins": [
"typescript",
"decorators-legacy"
],
"importOrder": [
"^@core/(.*)$",
"^@server/(.*)$",
"^@ui/(.*)$",
"^[./]"
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
}
}
9 changes: 5 additions & 4 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { UserModule } from './user/user.module';
import { GroupModule } from './group/group.module';
import { AuthModule } from './auth/auth.module';
import { MemberModule } from './member/member.module';
import { ConfigModule } from '@nestjs/config';

import { AuthModule } from './auth/auth.module';
import { EventModule } from './event/event.module';
import { GroupModule } from './group/group.module';
import { MemberModule } from './member/member.module';
import { UserModule } from './user/user.module';

@Module({
imports: [
Expand Down
3 changes: 2 additions & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';

import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

@Module({
controllers: [AuthController],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

export const configSwagger = (app: INestApplication<any>) => {
export const swaggerConfig = (app: INestApplication<any>) => {
const config = new DocumentBuilder()
.setTitle('sometime API')
.addTag('Group', '모임 관련 API')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiBody, ApiConsumes } from '@nestjs/swagger';
import { UseInterceptors, applyDecorators } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { applyDecorators, UseInterceptors } from '@nestjs/common';
import { ApiBody, ApiConsumes } from '@nestjs/swagger';

export function ApiFile(fieldName: string = 'file') {
return applyDecorators(
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';

import { databaseProviders } from './database.providers';

@Module({
Expand Down
2 changes: 1 addition & 1 deletion src/database/database.providers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as mongoose from 'mongoose';
import { ConfigService } from '@nestjs/config';
import * as mongoose from 'mongoose';

export const databaseProviders = [
{
Expand Down
34 changes: 33 additions & 1 deletion src/event/dto/create-event.dto.ts
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
export class CreateEventDto {}
import { ApiProperty } from '@nestjs/swagger';

export class CreateEventDto {
@ApiProperty({
description: '이벤트 이름',
example: '2024년 1학기 모임',
})
name: string;

@ApiProperty({
description: '이벤트 설명',
example: '2024년 1학기 모임입니다.',
})
description: string;

@ApiProperty({
description: '이벤트 시작일',
example: new Date().toISOString(),
})
startDate: Date;

@ApiProperty({
description: '이벤트 종료일',
example: new Date(),
})
endDate: Date;

@ApiProperty({
description: '이벤트 참가비',
example: 10000,
})
fee: number;
}
1 change: 1 addition & 0 deletions src/event/dto/update-event.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PartialType } from '@nestjs/swagger';

import { CreateEventDto } from './create-event.dto';

export class UpdateEventDto extends PartialType(CreateEventDto) {}
9 changes: 8 additions & 1 deletion src/event/entities/event.entity.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export class Event {}
export class Event {
name: string;
description: string;
startDate: Date;
endDate: Date;
fee: number;
attendees: string[];
}
55 changes: 40 additions & 15 deletions src/event/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
Patch,
Post,
} from '@nestjs/common';
import { EventService } from './event.service';
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';

import { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
import { ApiParam, ApiTags } from '@nestjs/swagger';
import { EventService } from './event.service';

@ApiTags('Event')
@Controller('group/:groupId/event')
Expand All @@ -23,20 +24,35 @@ export class EventController {
constructor(private readonly eventService: EventService) {}

@Post()
create(@Body() createEventDto: CreateEventDto) {
return this.eventService.create(createEventDto);
@ApiOperation({
summary: '이벤트 생성',
description: '모임의 이벤트를 생성합니다.',
})
create(
@Param('groupId') groupId: string,
@Body() createEventDto: CreateEventDto,
) {
return this.eventService.create(groupId, createEventDto);
}

@Get()
findAll() {
return this.eventService.findAll();
@ApiOperation({
summary: '모임 모든 이벤트 조회',
description: '모임의 모든 이벤트를 조회합니다.',
})
findAll(@Param('groupId') groupId: string) {
return this.eventService.findAll(groupId);
}

@Get(':eventId')
@ApiOperation({
summary: '이벤트 상세 조회',
description: '특정 이벤트를 조회합니다.',
})
@ApiParam({
name: 'memberId',
name: 'eventId',
required: true,
description: '모임 회원 ID',
description: '이벤트 ID',
})
findOne(
@Param('groupId') groupId: string,
Expand All @@ -46,25 +62,34 @@ export class EventController {
}

@Patch(':eventId')
@ApiOperation({
summary: '이벤트 수정',
description: '특정 이벤트를 수정합니다.',
})
@ApiParam({
name: 'memberId',
name: 'eventId',
required: true,
description: '모임 회원 ID',
description: '이벤트 ID',
})
update(
@Param('groupId') groupId: string,
@Param('eventId') eventId: string,
@Body() updateEventDto: UpdateEventDto,
) {
return this.eventService.update(+eventId, updateEventDto);
return this.eventService.update(groupId, eventId, updateEventDto);
}

@Delete(':eventId')
@ApiOperation({
summary: '이벤트 삭제',
description: '특정 이벤트를 삭제합니다.',
})
@ApiParam({
name: 'memberId',
name: 'eventId',
required: true,
description: '모임 회원 ID',
description: '이벤트 ID',
})
remove(@Param('eventId') eventId: string) {
return this.eventService.remove(+eventId);
remove(@Param('groupId') groupId: string, @Param('eventId') eventId: string) {
return this.eventService.remove(groupId, eventId);
}
}
10 changes: 8 additions & 2 deletions src/event/event.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Module } from '@nestjs/common';
import { EventService } from './event.service';

import { DatabaseModule } from '../database/database.module';
import { GroupModule } from '../group/group.module';
import { EventController } from './event.controller';
import { eventProviders } from './event.providers';
import { EventRepository } from './event.repository';
import { EventService } from './event.service';

@Module({
imports: [DatabaseModule, GroupModule],
controllers: [EventController],
providers: [EventService],
providers: [EventService, ...eventProviders, EventRepository],
})
export class EventModule {}
12 changes: 12 additions & 0 deletions src/event/event.providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Connection } from 'mongoose';

import { EventSchema } from './schemas/event.schema';

export const eventProviders = [
{
provide: 'EVENT_MODEL',
useFactory: (connection: Connection) =>
connection.model('Event', EventSchema),
inject: ['MONGODB_CONNECTION'],
},
];
35 changes: 35 additions & 0 deletions src/event/event.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Inject, Injectable } from '@nestjs/common';
import { Model } from 'mongoose';

import { CreateEventDto } from './dto/create-event.dto';
import { Event } from './interfaces/event.interface';

@Injectable()
export class EventRepository {
constructor(
@Inject('EVENT_MODEL')
private readonly eventModel: Model<Event>,
) {}

async create(createEventDto: CreateEventDto): Promise<Event> {
return await this.eventModel.create(createEventDto);
}

findAll(): Promise<Event[]> {
return this.eventModel.find().exec();
}

async findOne(eventId: string): Promise<Event> {
return await this.eventModel.findById(eventId).exec();
}

update(eventId: string, event: Event): Promise<Event> {
return this.eventModel
.findByIdAndUpdate(eventId, event, { new: true })
.exec();
}

delete(eventId: string) {
this.eventModel.findByIdAndDelete(eventId);
}
}
58 changes: 47 additions & 11 deletions src/event/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
import { Injectable } from '@nestjs/common';

import { GroupService } from '../group/group.service';
import { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
import { EventRepository } from './event.repository';

@Injectable()
export class EventService {
create(createEventDto: CreateEventDto) {
return createEventDto;
constructor(
private readonly eventRepository: EventRepository,
private readonly groupService: GroupService,
) {}

async create(groupId: string, createEventDto: CreateEventDto) {
const createdEvent = await this.eventRepository.create(createEventDto);
await this.groupService.addEvent(groupId, createdEvent.id);

return createdEvent;
}

findAll() {
return `This action returns all event`;
async findAll(groupId: string) {
const group = await this.groupService.findOne(groupId);

const events = [];
for (const eventId of group.events) {
events.push(await this.eventRepository.findOne(eventId));
}

return events;
}

findOne(groupId: string, eventId: string) {
async findOne(groupId: string, eventId: string) {
const group = await this.groupService.findOne(groupId);
const event = await this.eventRepository.findOne(eventId);

return {
groupId,
eventId,
group,
event,
};
}

update(eventId: number, updateEventDto: UpdateEventDto) {
return `This action updates a #${eventId} event` + updateEventDto;
async update(
groupId: string,
eventId: string,
updateEventDto: UpdateEventDto,
) {
const group = await this.groupService.findOne(groupId);
const event = await this.eventRepository.findOne(eventId);

for (const key in updateEventDto) {
event[key] = updateEventDto[key];
}
await this.eventRepository.update(eventId, event);

return {
group,
event,
};
}

remove(eventId: number) {
return `This action removes a #${eventId} event`;
async remove(groupId: string, eventId: string) {
return await this.groupService.deleteEvent(groupId, eventId);
}
}
10 changes: 10 additions & 0 deletions src/event/interfaces/event.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Document } from 'mongoose';

export interface Event extends Document {
name: string;
description: string;
startDate: Date;
endDate: Date;
fee: number;
attendees: string[];
}
Loading

0 comments on commit 5c3e7be

Please sign in to comment.