Skip to content

Commit

Permalink
[BE#487] 친구 고정 기능 (#488)
Browse files Browse the repository at this point in the history
* feat: 친구 고정 기능 추가

- entity에 고정 칼럼 생성
- controller에 고정 기능 추가
- service에 고정 기능 추가

* docs: swagger api 문서 수정

* test: 테스트 코드 수정

* fix: 고정 함수 수정

* fix: 고정된 친구를 먼저 불러오도록 수정

* refactor: 변수 이름 is_fixed로 변경
  • Loading branch information
victolee0 authored Jan 11, 2024
1 parent fe8b64e commit a601139
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
34 changes: 34 additions & 0 deletions BE/src/mates/mates.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Body,
UseGuards,
Query,
Patch,
} from '@nestjs/common';
import {
ApiBearerAuth,
Expand Down Expand Up @@ -154,4 +155,37 @@ export class MatesController {
message: '성공적으로 삭제되었습니다.',
};
}

@Patch('fixation')
@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@ApiOperation({ summary: '친구 목록에서 고정/고정 해제' })
@ApiBody({
schema: {
properties: {
following_id: {
type: 'number',
description: '친구의 id',
example: '1',
},
fixation: {
type: 'boolean',
description: '고정/고정 해제 여부',
example: 'true',
},
},
},
})
async fixationMate(
@User('id') id: number,
@Body('following_id') following_id: number,
@Body('is_fixed') is_fixed: boolean,
): Promise<StatusMessageDto> {
await this.matesService.fixationMate(id, following_id, is_fixed);

return {
statusCode: 200,
message: '수정 완료',
};
}
}
11 changes: 10 additions & 1 deletion BE/src/mates/mates.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import {
Entity,
PrimaryGeneratedColumn,
ManyToOne,
JoinColumn,
Column,
} from 'typeorm';
import { UsersModel } from 'src/users/entity/users.entity';

@Entity()
Expand All @@ -19,4 +25,7 @@ export class Mates {
})
@JoinColumn({ name: 'following_id' })
following_id: UsersModel;

@Column({ type: 'boolean', default: false })
is_fixed: boolean;
}
9 changes: 7 additions & 2 deletions BE/src/mates/mates.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ describe('MatesService', () => {
id: 2,
follower_id: { id: 1 },
following_id: { id: 3 },
fixation: false,
} as never);
jest.spyOn(repository, 'save').mockResolvedValueOnce({
id: 2,
follower_id: { id: 1 } as UsersModel,
following_id: { id: 3 } as UsersModel,
is_fixed: false,
});
const result = await service.addMate(user, '어린콩3');
expect(result).toStrictEqual({
Expand Down Expand Up @@ -133,6 +135,7 @@ describe('MatesService', () => {
id: 1,
follower_id: { id: 1 } as UsersModel,
following_id: { id: 3 } as UsersModel,
is_fixed: false,
});
expect(service.addMate(user, '어린콩2')).rejects.toThrow(
BadRequestException,
Expand Down Expand Up @@ -176,6 +179,7 @@ describe('MatesService', () => {
id: 1,
follower_id: { id: 1 } as UsersModel,
following_id: { id: 2 } as UsersModel,
is_fixed: false,
},
]);
jest
Expand All @@ -198,7 +202,7 @@ describe('MatesService', () => {
jest
.spyOn(redisService, 'hget')
.mockResolvedValueOnce('2023-11-29 16:00:00');
const result = await service.getMates(1, '2023-11-29', '09:00');
const result = await service.getMates(1, '2023-11-29 00:00:00', ' 09:00');
expect(result).toStrictEqual([
{
id: 2,
Expand All @@ -211,7 +215,7 @@ describe('MatesService', () => {
});
it('친구가 없는 유저는 빈 배열을 가져온다.', async () => {
jest.spyOn(service, 'getMatesStudyTime').mockResolvedValueOnce([]);
const result = await service.getMates(3, '2023-11-29', '09:00');
const result = await service.getMates(3, '2023-11-29 00:00:00', '09:00');
expect(result).toStrictEqual([]);
});
});
Expand Down Expand Up @@ -271,6 +275,7 @@ describe('MatesService', () => {
id: 1,
follower_id: { id: 1 } as UsersModel,
following_id: { id: 2 } as UsersModel,
is_fixed: false,
});
const result = await service.findMate(user, '어린콩2');
expect(result).toStrictEqual({
Expand Down
22 changes: 20 additions & 2 deletions BE/src/mates/mates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class MatesService {
LEFT JOIN mates m ON m.following_id = u.id
LEFT JOIN study_logs s ON s.user_id = u.id AND s.date = DATE(CONVERT_TZ(?, ?, u.timezone))
WHERE m.follower_id = ?
GROUP BY u.id
ORDER BY total_time DESC
GROUP BY u.id, m.fixation
ORDER BY m.fixation DESC, total_time DESC
`,
[followerDate, followerTimezone, followerId],
);
Expand Down Expand Up @@ -221,6 +221,24 @@ export class MatesService {
}
}

async fixationMate(
id,
following_id: number,
is_fixed: boolean,
): Promise<void> {
const result = await this.matesRepository.update(
{
follower_id: { id: id },
following_id: { id: following_id },
},
{ is_fixed: is_fixed },
);

if (!result) {
throw new NotFoundException('해당 친구 관계는 존재하지 않습니다.');
}
}

entityToDto(mate: Mates): MatesDto {
const { id, follower_id, following_id } = mate;
const mateDto = {
Expand Down

0 comments on commit a601139

Please sign in to comment.