Skip to content

Commit

Permalink
feat: 필터링할 때 페이징도 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
seokahi committed Nov 24, 2022
1 parent da5f6ec commit 93b5893
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/server/routes/events/getCategorybyFiltering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import {EventResponseScheme} from '../../../entity/schemes';

const schema = defineSchema({
summary: '카테고리 별로 필터링해서 가져옵니다',
description: 'categoryId => 선택없음:0 동아리/소모임:1 학생회:2 간식나눔:3 대회/공모전:4 스터디:5 구인:6 기타:7 진행중인 이벤트: 8 | eventStatus => true 일 경우 해당 카테고리 중애서 진행 중인 이벤트만 가져옴, false 일 경우 진행 중인 이벤트와 상관없이 해당 카테고리 이벤트 다 가져옴',
description: `categoryId => 선택없음:0 동아리/소모임:1 학생회:2 간식나눔:3 대회/공모전:4 스터디:5 구인:6 기타:7 진행중인 이벤트: 8 `+
`eventStatus => true 일 경우 해당 카테고리 중애서 진행 중인 이벤트만 가져옴, false 일 경우 진행 중인 이벤트와 상관없이 해당 카테고리 이벤트 다 가져옴`,

query: {
categoryId: stringAsInt,
eventStatus: stringAsBoolean,
pageNum: stringAsInt.optional(),
pageSize:stringAsInt.optional()
},

response: EventResponseScheme,
});

export default defineRoute('get', '/events-by-category', schema, async (req, res) => {
const {userId} = req;
const {categoryId , eventStatus} = req.query;
const {categoryId , eventStatus,pageNum,pageSize} = req.query;

const eventCategoryInformation = await EventService.getCategorybyFiltering(userId,categoryId,eventStatus);
const eventCategoryInformation = await EventService.getCategorybyFiltering(userId,categoryId,eventStatus,pageNum,pageSize);
return res.json(eventCategoryInformation);
});
33 changes: 20 additions & 13 deletions src/service/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ class EventService {

}

async getCategorybyFiltering(userId?:number,categoryId?:number,eventStatus?:boolean): Promise<Event[]> {

if(categoryId == undefined || eventStatus == undefined) {
categoryId = 0; // 기본 값 : 선택없음 카테고리
eventStatus = false;
async getCategorybyFiltering(userId?:number,categoryId?:number,eventStatus?:boolean,pageNum?:number, pageSize?:number): Promise<Event[]> {
if(eventStatus == undefined) eventStatus = false; // 비어있으면 전체 가져옴

if(pageNum == undefined || pageSize == undefined) { // 하나라도 비어있으면
pageNum = 0;
pageSize = 0; // 전체 가져오는걸로!
}

let category =""
if(categoryId == 0) category = "선택없음";
else if(categoryId == 1) category = "동아리/소모임";
Expand All @@ -90,15 +91,17 @@ class EventService {
else if (categoryId == 8) category="기타";
else category = "선택없음";

if(userId == null) { // 로그인 X
if(eventStatus == true) // 진행 중인 이벤트만
{ return await Event.find({where: {endAt: MoreThanOrEqual(new Date()),category:category},order: {id: 'DESC'}});}
else //모든 이벤트 다
{ return await Event.find({where:{category:category}}); }
if(userId == undefined) { // 로그인 X
if(eventStatus == true) {
return await Event.find({where: {endAt: MoreThanOrEqual(new Date()),category:category},order: {id: 'DESC'},skip: pageSize * pageNum,take: pageSize});
} else {
return await Event.find({where:{category:category},skip: pageSize * pageNum,take: pageSize});
}

}

else { // 로그인 한 사용자
return await this.getEventsWithoutBlockedUserbyFiltering(userId, category, eventStatus); // 로그인 한 사람은 blocking user 빼고
return await this.getEventsWithoutBlockedUserbyFiltering(userId, category, eventStatus,pageNum,pageSize); // 로그인 한 사람은 blocking user 빼고
}
}

Expand Down Expand Up @@ -172,7 +175,7 @@ class EventService {
}

//차단한 사용자 제외하고 필터링
private async getEventsWithoutBlockedUserbyFiltering(requestorId: number, category:string, eventStatus:boolean ): Promise<Event[]> {
private async getEventsWithoutBlockedUserbyFiltering(requestorId: number, category:string, eventStatus:boolean,pageNum:number, pageSize:number): Promise<Event[]> {
if(eventStatus == true) { // 진행중인 이벤트만 가져옴
return await Event.createQueryBuilder('event')
/** relations 필드 가져오는 부분 */
Expand All @@ -190,6 +193,8 @@ class EventService {
)`, {requestorId})
.andWhere(`event.endAt >= :date`,{date :new Date()})
.andWhere(`event.category = :category`,{category:category})
.take(pageSize)
.skip(pageSize * pageNum) // 페이징 적용
.orderBy('event.id', 'DESC')
.getMany() // group by 안해도 얘가 잘 처리해줌 ^~^
}
Expand All @@ -209,6 +214,8 @@ class EventService {
WHERE block.blocking_user_id = :requestorId
)`, {requestorId})
.andWhere(`event.category = :category`,{category:category})
.take(pageSize)
.skip(pageSize * pageNum) // 페이징 적용
.orderBy('event.id', 'DESC')
.getMany() // group by 안해도 얘가 잘 처리해줌 ^~^
}
Expand Down

0 comments on commit 93b5893

Please sign in to comment.