Skip to content

Commit

Permalink
MERGE :: soyeonkim0227/Tips-API-006 -> main
Browse files Browse the repository at this point in the history
PR :: 팁 추천
  • Loading branch information
gurdl0525 committed Jun 5, 2024
2 parents 6324a1c + 191a121 commit 7169c2b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/core/tip/port/tip.in.port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ export interface ReadDetailTipUseCase {
}

export interface SearchTipUseCase {
searchTip(dto: SearchTipRequest,token: string): Promise<object[]>
}
searchTip(dto: SearchTipRequest, token: string): Promise<object[]>;
}

export interface ReadRecommendedTipUseCase {
readRecommendedTip(): Promise<object[]>;
}
7 changes: 6 additions & 1 deletion src/core/tip/port/tip.out.port.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ModifyTipRequest, SearchTipRequest } from 'src/presentation/tip/dto/tip.request';
import {
ModifyTipRequest,
SearchTipRequest,
} from 'src/presentation/tip/dto/tip.request';
import { Tip } from '../../../domain/tip/tip.entity';

export interface SaveTipPort {
Expand All @@ -13,4 +16,6 @@ export interface ReadTipPort {
findByIdOrFail(tipId: number): Promise<Tip>;

searchTip(dto: SearchTipRequest): Promise<object[]>;

readAllTipByRandom(): Promise<object[] | null>;
}
2 changes: 2 additions & 0 deletions src/core/tip/tip.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UserRepository } from '../../domain/user/user.repository';
import {
ModifyTipService,
ReadDetailTipService,
ReadRecommendedTipService,
SearchTipService,
WriteTipService,
} from './tip.service';
Expand All @@ -30,6 +31,7 @@ import { JwtModule } from '@nestjs/jwt';
{ provide: 'modifyTip', useClass: ModifyTipService },
{ provide: 'readDetailTip', useClass: ReadDetailTipService },
{ provide: 'searchTip', useClass: SearchTipService },
{ provide: 'readRecommendedTip', useClass: ReadRecommendedTipService },
],
})
export class TipModule {}
17 changes: 17 additions & 0 deletions src/core/tip/tip.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ModifyTipUseCase,
ReadDetailTipUseCase,
ReadRecommendedTipUseCase,
SearchTipUseCase,
WriteTipUseCase,
} from './port/tip.in.port';
Expand Down Expand Up @@ -91,3 +92,19 @@ export class SearchTipService implements SearchTipUseCase {
return data;
};
}

export class ReadRecommendedTipService implements ReadRecommendedTipUseCase {
constructor(
@Inject('tip out port')
private readonly readTipPort: ReadTipPort,
) {}

readRecommendedTip = async (): Promise<object[]> => {
const data = await this.readTipPort.readAllTipByRandom();

if (!data || data.length === 0)
throw new HttpException('There is No Content', 204);

return data;
};
}
4 changes: 2 additions & 2 deletions src/domain/post/post.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class PostRepository
searchPost = async (dto: PostSearchRequest): Promise<object[]> => {
const { keyword, type, major, sort } = dto;

const posts = await this.postEntity
const posts = this.postEntity
.createQueryBuilder('post')
.innerJoin('post.user', 'user', 'user.id = post.writer_id')
.select([
Expand All @@ -72,6 +72,6 @@ export class PostRepository
posts.andWhere('post.post_type = :post_type', { post_type: type });
}

return posts.getRawMany();
return await posts.getRawMany();
};
}
15 changes: 15 additions & 0 deletions src/domain/tip/tip.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,19 @@ export class TipRepository implements SaveTipPort, UpdateTipPort, ReadTipPort {
.groupBy('tip.id')
.getRawMany();
};

readAllTipByRandom = async (): Promise<object[] | null> => {
return await this.tipEntity
.createQueryBuilder('tip')
.select([
'tip.id AS id',
'tip.title AS title',
'tip.content AS content',
'tip.writer_id AS writer_id',
'tip.created_at AS created_at',
])
.orderBy('RAND()')
.limit(18)
.getRawMany();
};
}
15 changes: 14 additions & 1 deletion src/presentation/tip/tip.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Response } from 'express';
import {
ModifyTipUseCase,
ReadDetailTipUseCase,
ReadRecommendedTipUseCase,
SearchTipUseCase,
WriteTipUseCase,
} from '../../core/tip/port/tip.in.port';
Expand All @@ -35,8 +36,20 @@ export class TipController {
private readonly readDetailTipUseCase: ReadDetailTipUseCase,
@Inject('searchTip')
private readonly searchTipUseCase: SearchTipUseCase,
@Inject('readRecommendedTip')
private readonly readRecommendedTipUseCase: ReadRecommendedTipUseCase,
) {}

@Get('/recommend')
async readRecommended(@Res() res: Response): Promise<Response> {
return res
.status(200)
.json({
posts: await this.readRecommendedTipUseCase.readRecommendedTip(),
})
.send();
}

@Post()
async writeTip(
@Body() dto: WriteTipRequest,
Expand Down Expand Up @@ -83,7 +96,7 @@ export class TipController {
.sendStatus(200);
}

@Get('search?')
@Get('/search?')
async searchTip(
@Headers('Authorization') token: string,
@Query() dto: SearchTipRequest,
Expand Down

0 comments on commit 7169c2b

Please sign in to comment.