Skip to content

Commit

Permalink
feat: getMyEvents 라우터 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokahi committed Feb 27, 2022
1 parent c74aa14 commit 5aa25cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/server/routes/events/getMyEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {defineSchema} from '../../libs/schema';
import {defineRoute} from '../../libs/route';
import EventService from '../../../service/EventService';
import {EventResponseScheme} from '../../../entity/schemes';

const schema = defineSchema({
summary: '내가 쓴 행사를 다 가져옵니다.',
description: '행사 id 역순으로 가져옵니다.',

response: [EventResponseScheme]
});

export default defineRoute('get', '/myevents', schema, async (req, res) => {
const userId = req.requireUserId();
const eventInformation = await EventService.getMyEvents(userId);
return res.json(await Promise.all(eventInformation.map(e => e.toResponse(userId))))
});
10 changes: 10 additions & 0 deletions src/service/EventService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Event from '../entity/Event';
import {log} from '../common/utils/log';
import {preview} from '../server/libs/json';
import User from '../entity/User';
import Comment from '../entity/Comment';
import EventLike from '../entity/EventLike';
import EventNotification from '../entity/EventNotification';
Expand Down Expand Up @@ -31,6 +32,15 @@ class EventService {
return event;
}

async getMyEvents(userId: number): Promise<Event[]> {
const user = await User.findOneOrFail(userId);
if (user == null) {

This comment has been minimized.

Copy link
@potados99

potados99 Feb 27, 2022

Member

널 체크 아주 좋아요!

근데 findOneOrFail로 가져오면 null인 경우는 없답니다(없으면 예외 발생하고 404 응답이 나가요)

사용자가 없을때 예외를 던지지 않고 조용히 지나가기를 원하신다면 findOneOrFail을 findOne으로 바꿔주시면 되구, 지금 이대로 두고 싶으시면 딱히 수정을 안해도 되어용(물론 if문은 의미가 없을거예용)

This comment has been minimized.

Copy link
@seokahi

seokahi Oct 1, 2022

Author Collaborator

넹 감사합니다~!!!

return [];
}

return await Event.find({where: {user}, order: {id: 'DESC'}});
}

async getEvents(): Promise<Event[]> {
return await Event.find({order: {id: 'DESC'}});
}
Expand Down

0 comments on commit 5aa25cd

Please sign in to comment.