Skip to content

Commit

Permalink
Merge pull request #17 from boostcampwm-2024/refactor/repository-reus…
Browse files Browse the repository at this point in the history
…e-rss

♻️ refactor: rss 도메인 repository 함수 재사용성 향상, 테스트 코드 일관성, 최적화
  • Loading branch information
Jo-Minseok authored Jan 14, 2025
2 parents 117b376 + dfd23dc commit 353d807
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 44 deletions.
4 changes: 4 additions & 0 deletions server/src/rss/dto/request/rss-management.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ export class RssManagementRequestDto {
})
@Type(() => Number)
id: number;

constructor(partial: Partial<RssManagementRequestDto>) {
Object.assign(this, partial);
}
}
16 changes: 8 additions & 8 deletions server/src/rss/dto/request/rss-register.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsString, IsUrl, Length } from 'class-validator';
import { RssInformation } from '../../entity/rss.entity';
import { Rss, RssInformation } from '../../entity/rss.entity';

export class RssRegisterRequestDto {
@ApiProperty({
Expand Down Expand Up @@ -65,12 +65,12 @@ export class RssRegisterRequestDto {
Object.assign(this, partial);
}

static from(rss: RssInformation) {
return new RssRegisterRequestDto({
blog: rss.name,
name: rss.userName,
email: rss.email,
rssUrl: rss.rssUrl,
});
toEntity() {
const rss = new Rss();
rss.name = this.blog;
rss.userName = this.name;
rss.email = this.email;
rss.rssUrl = this.rssUrl;
return rss;
}
}
4 changes: 2 additions & 2 deletions server/src/rss/dto/request/rss-reject.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class RejectRssRequestDto {
@IsString({ message: '거부 사유를 문자열로 작성해주세요.' })
description: string;

constructor(description: string) {
this.description = description;
constructor(partial: Partial<RejectRssRequestDto>) {
Object.assign(this, partial);
}
}
11 changes: 0 additions & 11 deletions server/src/rss/repository/rss.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ export class RssRepository extends Repository<Rss> {
constructor(private dataSource: DataSource) {
super(Rss, dataSource.createEntityManager());
}

async insertNewRss(rssRegisterDto: RssRegisterRequestDto) {
const { blog, name, email, rssUrl } = rssRegisterDto;
const rssObj = this.create({
name: blog,
userName: name,
email,
rssUrl,
});
await this.save(rssObj);
}
}

@Injectable()
Expand Down
2 changes: 1 addition & 1 deletion server/src/rss/service/feed-crawler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class FeedCrawlerService {

async saveRssFeeds(feeds: Partial<Feed>[], newRssAccept: RssAccept) {
feeds.forEach((feed) => (feed.blog = newRssAccept));
return await this.feedRepository.insert(feeds);
await this.feedRepository.insert(feeds);
}

private async fetchRss(rssUrl: string): Promise<Partial<Feed>[]> {
Expand Down
2 changes: 1 addition & 1 deletion server/src/rss/service/rss.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class RssService {
);
}

await this.rssRepository.insertNewRss(rssRegisterBodyDto);
await this.rssRepository.insert(rssRegisterBodyDto.toEntity());
}

async readAllRss() {
Expand Down
3 changes: 1 addition & 2 deletions server/test/rss/dto/rss-management.dto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { RssManagementRequestDto } from '../../../src/rss/dto/request/rss-manage
describe('RssManagementDto Test', () => {
it('Rss관리 API의 PathVariable이 정수가 아닐 경우', async () => {
// given
const dto = new RssManagementRequestDto();
dto.id = 'abc' as any;
const dto = new RssManagementRequestDto({ id: 'abc' as any });

// when
const errors = await validate(dto);
Expand Down
9 changes: 5 additions & 4 deletions server/test/rss/e2e/accept.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ describe('Rss Accept E2E Test', () => {
describe('정상적인 요청을 한다.', () => {
it('정상적으로 RSS를 승인한다.', async () => {
// given
const rssFixture = RssFixture.createRssFixture({
rssUrl: 'https://v2.velog.io/rss/@seok3765',
});
const rss = await rssRepository.save(rssFixture);
const rss = await rssRepository.save(
RssFixture.createRssFixture({
rssUrl: 'https://v2.velog.io/rss/@seok3765',
}),
);

// when
const response = await request(app.getHttpServer())
Expand Down
2 changes: 1 addition & 1 deletion server/test/rss/e2e/history/accept.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('GET /api/rss/history/accept E2E Test', () => {
rssAccepts.push(RssAcceptFixture.createRssAcceptFixture({}, i));
}
await Promise.all([
rssAcceptRepository.save(rssAccepts),
rssAcceptRepository.insert(rssAccepts),
redisService.sadd('auth:sid', 'test1234'),
]);
});
Expand Down
2 changes: 1 addition & 1 deletion server/test/rss/e2e/history/reject.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('GET /api/rss/history/reject E2E Test', () => {
rssAccepts.push(RssRejectFixture.createRssRejectFixture({}, i));
}
await Promise.all([
rssRejectRepository.save(rssAccepts),
rssRejectRepository.insert(rssAccepts),
redisService.sadd('auth:sid', 'test1234'),
]);
});
Expand Down
11 changes: 7 additions & 4 deletions server/test/rss/e2e/reject.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ describe('Rss Reject E2E Test', () => {
it('정상적으로 RSS를 거절한다.', async () => {
// given
const REJECT_REASON = '거절 사유';
const rssFixture = RssFixture.createRssFixture();
const rss = await rssRepository.save(rssFixture);
const rejectRssDto = new RejectRssRequestDto(REJECT_REASON);
const rss = await rssRepository.save(RssFixture.createRssFixture());
const rejectRssDto = new RejectRssRequestDto({
description: REJECT_REASON,
});

// when
const response = await request(app.getHttpServer())
Expand All @@ -59,7 +60,9 @@ describe('Rss Reject E2E Test', () => {
it('존재하지 않는 rss를 거절할 때', async () => {
// given
const REJECT_REASON = '거절 사유';
const rejectRssDto = new RejectRssRequestDto(REJECT_REASON);
const rejectRssDto = new RejectRssRequestDto({
description: REJECT_REASON,
});

// when
const response = await request(app.getHttpServer())
Expand Down
30 changes: 21 additions & 9 deletions server/test/rss/e2e/rss.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ describe('/api/rss E2E Test', () => {
describe('POST /api/rss E2E Test', () => {
it('정상적인 요청이 들어왔다면 올바른 응답을 한다.', async () => {
// given
const requestDto = RssRegisterRequestDto.from(
RssFixture.createRssFixture(),
);
const requestDto = new RssRegisterRequestDto({
blog: 'blog1',
name: 'name1',
email: '[email protected]',
rssUrl: 'https://example1.com/rss',
});

// when
const response = await request(app.getHttpServer())
Expand All @@ -43,9 +46,12 @@ describe('/api/rss E2E Test', () => {

it('이미 신청한 RSS를 또 신청한다면 거부를 한다.', async () => {
// given
const requestDto = RssRegisterRequestDto.from(
RssFixture.createRssFixture(),
);
const requestDto = new RssRegisterRequestDto({
blog: 'blog1',
name: 'name1',
email: '[email protected]',
rssUrl: 'https://example1.com/rss',
});
await request(app.getHttpServer()).post('/api/rss').send(requestDto);

// when
Expand All @@ -62,7 +68,12 @@ describe('/api/rss E2E Test', () => {
const acceptedRss = await rssAcceptRepository.save(
RssAcceptFixture.createRssAcceptFixture(),
);
const rssRegisterDto = RssRegisterRequestDto.from(acceptedRss);
const rssRegisterDto = new RssRegisterRequestDto({
blog: acceptedRss.name,
name: acceptedRss.userName,
email: acceptedRss.email,
rssUrl: acceptedRss.rssUrl,
});

// when
const response = await request(app.getHttpServer())
Expand All @@ -85,8 +96,9 @@ describe('/api/rss E2E Test', () => {

it('등록된 RSS가 존재할 경우 해당 데이터를 반환한다.', async () => {
// given
const rss = RssFixture.createRssFixture();
const expectedResult = await rssRepository.save(rss);
const expectedResult = await rssRepository.save(
RssFixture.createRssFixture(),
);

// when
const response = await request(app.getHttpServer()).get('/api/rss');
Expand Down

0 comments on commit 353d807

Please sign in to comment.