Skip to content

Commit

Permalink
refactor: event crud 될것가틈
Browse files Browse the repository at this point in the history
  • Loading branch information
potados99 committed Feb 7, 2022
1 parent 6797195 commit f427bb7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/server/routes/events/deleteEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import EventService from '../../../service/EventService';
import {stringAsInt} from '../../libs/zodTypes';
Expand All @@ -10,7 +9,7 @@ const schema = defineSchema({
},
});

export default defineRoute('delete', '/event/:eventId?', schema, async (req, res) => {
export default defineRoute('delete', '/event/:eventId', schema, async (req, res) => {
const {eventId} = req.params;

await EventService.deleteEvent(eventId);
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/events/getEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const schema = defineSchema({
},
});

export default defineRoute('get', '/event/:eventId?', schema, async (req, res) => {
export default defineRoute('get', '/event/:eventId', schema, async (req, res) => {
const {eventId} = req.params;

const eventInformation = await EventService.getEvent(eventId);
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/events/makeEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default defineRoute('post', '/event', schema, async (req, res) => {
const {userId} = req;
const {host, category, title, body, imageUuid, startAt, endAt} = req.body;

const Userstatus = await UserService.getUser(userId)
await EventService.makeEvent(Userstatus, host, category, title, body, imageUuid, startAt, endAt);
const user = await UserService.getUser(userId)
await EventService.makeEvent({user, host, category, title, body, imageUuid, startAt, endAt});

res.sendStatus(201); //success
});
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/events/updateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const schema = defineSchema({
params: {
eventId: stringAsInt,
},
query: {
body: {
host: z.string().optional(),
category: z.string().optional(),
title: z.string().optional(),
Expand All @@ -22,7 +22,7 @@ const schema = defineSchema({
export default defineRoute('patch', '/event/:eventId?', schema, async (req, res) => {
const {eventId} = req.params;

await EventService.patchEvent(eventId, req.query);
await EventService.patchEvent(eventId, req.body);

return res.send(`event ${eventId}를 업데이트: ${JSON.stringify(req.query)}`);
});
21 changes: 16 additions & 5 deletions src/service/EventService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import User from '../entity/User';
import Event from '../entity/Event';

type ModifyEventParams = {
user: User;
host: string;
category: string;
title: string;
body: string;
imageUuid: string;
startAt: Date;
endAt: Date
}

class EventService {
async makeEvent(user: User, host: string, category: string, title: string, body: string, imageUuid: string, startAt: Date, endAt: Date): Promise<Event> {
async makeEvent({user, host, category, title, body, imageUuid, startAt, endAt}: ModifyEventParams): Promise<Event> {
return await Event.create({
user: user,
host: host,
Expand All @@ -19,16 +30,16 @@ class EventService {
return await Event.findOne({where: {id: eventId}});
}

async patchEvent(eventId: number, req_query: Object): Promise<string> {
async patchEvent(eventId: number, body: Partial<ModifyEventParams>): Promise<string> {
const patchevent = await Event.update(
{id: eventId},
req_query
body
);
return patchevent.raw;
}

async deleteEvent(id: number): Promise<string> {
await Event.delete({id});
async deleteEvent(eventId: number): Promise<string> {
await Event.delete({id: eventId});
return;
}
}
Expand Down

0 comments on commit f427bb7

Please sign in to comment.