Skip to content

Commit

Permalink
✨ feat: 특정 모각밥 게시물에 신청한 리스트 가져오기 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanggwangseong committed Dec 6, 2024
1 parent 0a0868f commit 4897e7e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/controllers/participations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ export class ParticipationsController {
private readonly articlesService: ArticlesService,
) {}

@Get("articles/:articleId")
async getParticipationsByArticle(
@Param("articleId", new ParseIntPipe()) articleId: number,
@Query("cursor", new ParseIntPipe()) cursor: number,
@Query("limit", new ParseIntPipe()) limit: number,
) {
await this.articlesService.findById(articleId);

return await this.participationsService.getParticipationsByArticleId(
articleId,
cursor,
limit,
);
}

@Get()
async getParticipations(
@CurrentMemberDecorator("id") currentMemberId: number,
Expand Down
41 changes: 41 additions & 0 deletions src/services/participations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,47 @@ export class ParticipationsService {
private readonly configService: ConfigService,
) {}

async getParticipationsByArticleId(
articleId: number,
cursor: number,
limit: number,
) {
const query = this.participationsRepository
.createQueryBuilder("participation")
.where("participation.articleId = :articleId", {
articleId,
})
.orderBy("participation.id", "ASC")
.take(limit + 1);

if (cursor) {
query.andWhere("participation.id > :cursor", { cursor });
}

const participations = await query.getMany();
const hasNextPage = participations.length > limit;
const results = hasNextPage
? participations.slice(0, -1)
: participations;

const lastItem = results[results.length - 1];
const nextUrl =
lastItem && hasNextPage
? new URL(
`${this.configService.get(ENV_API_BASE_URL)}/participations/article/${articleId}?cursor=${lastItem.id}&limit=${limit}`,
)
: null;

return {
data: results,
cursor: {
after: lastItem?.id,
},
count: results.length,
next: nextUrl?.toString(),
};
}

async getParticipations(
currentMemberId: number,
cursor: number,
Expand Down

0 comments on commit 4897e7e

Please sign in to comment.