Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 이벤트 CRUD #14

Merged
merged 13 commits into from
Apr 22, 2024
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[];
}
42 changes: 19 additions & 23 deletions src/event/event.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
} from '@nestjs/common';
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common';
import { EventService } from './event.service';
import { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
Expand All @@ -23,20 +15,23 @@ export class EventController {
constructor(private readonly eventService: EventService) {}

@Post()
create(@Body() createEventDto: CreateEventDto) {
return this.eventService.create(createEventDto);
create(
@Param('groupId') groupId: string,
@Body() createEventDto: CreateEventDto,
) {
return this.eventService.create(groupId, createEventDto);
}

@Get()
findAll() {
return this.eventService.findAll();
findAll(@Param('groupId') groupId: string) {
return this.eventService.findAll(groupId);
}

@Get(':eventId')
@ApiParam({
name: 'memberId',
name: 'eventId',
required: true,
description: '모임 회원 ID',
description: '이벤트 ID',
})
findOne(
@Param('groupId') groupId: string,
Expand All @@ -47,24 +42,25 @@ export class EventController {

@Patch(':eventId')
@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')
@Patch(':eventId')
@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);
}
}
7 changes: 6 additions & 1 deletion src/event/event.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Module } from '@nestjs/common';
import { EventService } from './event.service';
import { EventController } from './event.controller';
import { DatabaseModule } from '../database/database.module';
import { EventRepository } from './event.repository';
import { eventProviders } from './event.providers';
import { GroupModule } from '../group/group.module';

@Module({
imports: [DatabaseModule, GroupModule],
controllers: [EventController],
providers: [EventService],
providers: [EventService, ...eventProviders, EventRepository],
})
export class EventModule {}
11 changes: 11 additions & 0 deletions src/event/event.providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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'],
},
];
34 changes: 34 additions & 0 deletions src/event/event.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Inject, Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { Event } from './interfaces/event.interface';
import { CreateEventDto } from './dto/create-event.dto';

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

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

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

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

update(eventId: string, updateEventDto: CreateEventDto): Promise<Event> {
return this.eventModel
.findByIdAndUpdate(eventId, updateEventDto, { new: true })
Fixed Show fixed Hide fixed
.exec();
}

delete(eventId: string) {
this.eventModel.findByIdAndDelete(eventId);
}
}
36 changes: 28 additions & 8 deletions src/event/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
import { Injectable } from '@nestjs/common';
import { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
import { EventRepository } from './event.repository';
import { GroupService } from '../group/group.service';

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

create(groupId: string, createEventDto: CreateEventDto) {
return { groupId, event: this.eventRepository.create(createEventDto) };
}

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

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

update(eventId: number, updateEventDto: UpdateEventDto) {
return `This action updates a #${eventId} event` + updateEventDto;
update(groupId: string, eventId: string, updateEventDto: UpdateEventDto) {
return {
groupId,
eventId,
event: this.eventRepository.update(eventId, updateEventDto),
};
}

remove(eventId: number) {
return `This action removes a #${eventId} event`;
remove(groupId: string, eventId: string) {
const group = this.groupService.findOne(groupId);
Fixed Show fixed Hide fixed
return this.eventRepository.delete(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[];
}
10 changes: 10 additions & 0 deletions src/event/schemas/event.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Schema } from 'mongoose';

export const EventSchema = new Schema({
name: String,
description: String,
startDate: Date,
endDate: Date,
fee: Number,
attendees: { type: [String], default: [] },
});
1 change: 1 addition & 0 deletions src/group/entities/group.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export class Group {
},
];
members: string[];
events: string[];
}
1 change: 1 addition & 0 deletions src/group/interfaces/group.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface Group extends Document {
},
];
members: string[];
events: string[];
}
1 change: 1 addition & 0 deletions src/group/schemas/group.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export const GroupSchema = new Schema({
default: [],
},
members: { type: [String], default: [] },
events: { type: [String], default: [] },
});
Loading