Skip to content

Commit

Permalink
[BE#501] 친구 목록 페이지네이션 추가 (#504)
Browse files Browse the repository at this point in the history
* feat: 친구 목록 페이지네이션 추가

* fix: default value 설정
  • Loading branch information
victolee0 authored Jan 23, 2024
1 parent 34088c5 commit 6cb19bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
4 changes: 3 additions & 1 deletion BE/src/mates/mates.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ export class MatesController {
@User('id') user_id: number,
@Query('datetime') datetime: string,
@Query('timezone') timezone: string,
@Query('page') page: string = '0',
): Promise<object> {
return this.matesService.getMates(user_id, datetime, timezone);
const page_num = parseInt(page);
return this.matesService.getMates(user_id, datetime, timezone, page_num);
}

@Get('/followers')
Expand Down
16 changes: 4 additions & 12 deletions BE/src/mates/mates.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('MatesService', () => {
follower_id: { id: 1 } as UsersModel,
following_id: { id: 3 } as UsersModel,
is_fixed: false,
is_blocked: false,
});
const result = await service.addMate(user, '어린콩3');
expect(result).toStrictEqual({
Expand All @@ -94,18 +95,6 @@ describe('MatesService', () => {
});
});

it('친구는 최대 10명까지 추가할 수 있다.', async () => {
jest.spyOn(usersRepository, 'findOne').mockResolvedValueOnce({
id: 3,
nickname: '어린콩3',
image_url: null,
} as UsersModel);
jest.spyOn(repository, 'count').mockResolvedValueOnce(10);
expect(service.addMate(user, '어린콩3')).rejects.toThrow(
BadRequestException,
);
});

it('자신을 친구 추가 할 수 없다.', async () => {
jest.spyOn(usersRepository, 'findOne').mockResolvedValueOnce({
id: 1,
Expand Down Expand Up @@ -136,6 +125,7 @@ describe('MatesService', () => {
follower_id: { id: 1 } as UsersModel,
following_id: { id: 3 } as UsersModel,
is_fixed: false,
is_blocked: false,
});
expect(service.addMate(user, '어린콩2')).rejects.toThrow(
BadRequestException,
Expand Down Expand Up @@ -180,6 +170,7 @@ describe('MatesService', () => {
follower_id: { id: 1 } as UsersModel,
following_id: { id: 2 } as UsersModel,
is_fixed: false,
is_blocked: false,
},
]);
jest
Expand Down Expand Up @@ -276,6 +267,7 @@ describe('MatesService', () => {
follower_id: { id: 1 } as UsersModel,
following_id: { id: 2 } as UsersModel,
is_fixed: false,
is_blocked: false,
});
const result = await service.findMate(user, '어린콩2');
expect(result).toStrictEqual({
Expand Down
23 changes: 12 additions & 11 deletions BE/src/mates/mates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class MatesService {
user_id: number,
datetime: string,
timezone: string,
page: number = 0,
): Promise<object[]> {
if (!user_id || !datetime || !timezone) {
throw new BadRequestException('인자의 형식이 잘못되었습니다.');
Expand All @@ -110,6 +111,7 @@ export class MatesService {
nowUserTime,
offset,
user_id,
page,
);
return Promise.all(
studyTimeByFollowing.map(async (record) => {
Expand All @@ -134,7 +136,9 @@ export class MatesService {
followerDate: string,
followerTimezone: string,
followerId: number,
page: number,
) {
const COUNT_PER_PAGE = 9;
return this.userRepository.query(
`
SELECT u.id, u.nickname, u.image_url, COALESCE(SUM(s.learning_time), 0) AS total_time
Expand All @@ -144,8 +148,15 @@ export class MatesService {
WHERE m.follower_id = ?
GROUP BY u.id, m.is_fixed
ORDER BY m.is_fixed DESC, u.is_studying DESC, total_time DESC
LIMIT ? OFFSET ?
`,
[followerDate, followerTimezone, followerId],
[
followerDate,
followerTimezone,
followerId,
COUNT_PER_PAGE,
page * COUNT_PER_PAGE,
],
);
}

Expand Down Expand Up @@ -178,16 +189,6 @@ export class MatesService {
throw new NotFoundException('해당 유저는 존재하지 않습니다.');
}

const matesCount = await this.matesRepository.count({
where: { follower_id: user },
});

if (matesCount >= MATES_MAXIMUM) {
throw new BadRequestException(
`친구는 최대 ${MATES_MAXIMUM}명까지 추가할 수 있습니다.`,
);
}

const isExist = await this.matesRepository.findOne({
where: { follower_id: user, following_id: following },
});
Expand Down

0 comments on commit 6cb19bb

Please sign in to comment.