Skip to content

Commit

Permalink
chore(be): standardize the return format of 'get' apis (#1507)
Browse files Browse the repository at this point in the history
* chore(be): seperate total field from get-problems apis

* chore(be): resolve path conflict problem

* docs(be): add get-problem-count api docs

* chore(be): remove finished field from returned object of get-finished-contests

* Revert "chore(be): remove finished field from returned object of get-finished-contests"

This reverts commit 4ae342a.

* Revert "docs(be): add get-problem-count api docs"

This reverts commit cb29f05.

* Revert "chore(be): resolve path conflict problem"

This reverts commit 37b12dc.

* Revert "chore(be): seperate total field from get-problems apis"

This reverts commit da674f8.

* chore(be): replace field name(problem module)

* chore(be): replace field name(group module)

* chore(be): replace field name(notice module)

* chore(be): replace field name(submission module)

* chore(be): replace field name(workbook module)

* chore(be): replace field name(contest module)

* fix(be): add where condition

* fix(be): fix lint warning

* refactor(be): remove unused condition

* fix(be): unpack config condition

* test(be): add notice-total

* fix(be): match options for count total

---------

Co-authored-by: Jiyun Park <[email protected]>
  • Loading branch information
Jaehyeon1020 and cho-to authored May 23, 2024
1 parent 599e975 commit 650cab3
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 92 deletions.
22 changes: 10 additions & 12 deletions apps/backend/apps/client/src/contest/contest.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('ContestService', () => {
groupId,
user01Id
)
expect(contests).to.have.lengthOf(takeNum)
expect(contests.data).to.have.lengthOf(takeNum)
})

it('should return a contest array which starts with id 9', async () => {
Expand All @@ -228,7 +228,7 @@ describe('ContestService', () => {
groupId,
user01Id
)
expect(contests[0].id).to.equals(9)
expect(contests.data[0].id).to.equals(9)
})

it('a contest should contain following fields', async () => {
Expand All @@ -238,12 +238,12 @@ describe('ContestService', () => {
groupId,
user01Id
)
expect(contests[0]).to.have.property('title')
expect(contests[0]).to.have.property('startTime')
expect(contests[0]).to.have.property('endTime')
expect(contests[0]).to.have.property('participants')
expect(contests[0].group).to.have.property('id')
expect(contests[0].group).to.have.property('groupName')
expect(contests.data[0]).to.have.property('title')
expect(contests.data[0]).to.have.property('startTime')
expect(contests.data[0]).to.have.property('endTime')
expect(contests.data[0]).to.have.property('participants')
expect(contests.data[0].group).to.have.property('id')
expect(contests.data[0].group).to.have.property('groupName')
})

it("shold return contests whose title contains '낮'", async () => {
Expand All @@ -255,7 +255,7 @@ describe('ContestService', () => {
user01Id,
keyword
)
expect(contests.map((contest) => contest.title)).to.deep.equals([
expect(contests.data.map((contest) => contest.title)).to.deep.equals([
'소프트의 낮'
])
})
Expand All @@ -268,9 +268,7 @@ describe('ContestService', () => {
10,
groupId
)
const contestIds = contests.finished
.map((c) => c.id)
.sort((a, b) => a - b)
const contestIds = contests.data.map((c) => c.id).sort((a, b) => a - b)
const finishedContestIds = [6, 7, 8, 9, 10, 11, 12, 13]
expect(contestIds).to.deep.equal(finishedContestIds)
})
Expand Down
34 changes: 32 additions & 2 deletions apps/backend/apps/client/src/contest/contest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,23 @@ export class ContestService {
orderBy: [{ endTime: 'desc' }, { id: 'desc' }]
})

return this.renameToParticipants(contests)
const total = await this.prisma.contest.count({
where: {
groupId,
endTime: {
lte: now
},
id: {
in: registeredContestIds
},
title: {
contains: search
},
isVisible: true
}
})

return { data: this.renameToParticipants(contests), total }
}

async getFinishedContestsByGroupId(
Expand Down Expand Up @@ -239,7 +255,21 @@ export class ContestService {
select: contestSelectOption,
orderBy: [{ endTime: 'desc' }, { id: 'desc' }]
})
return { finished: this.renameToParticipants(finished) }

const total = await this.prisma.contest.count({
where: {
endTime: {
lte: now
},
groupId,
isVisible: true,
title: {
contains: search
}
}
})

return { data: this.renameToParticipants(finished), total }
}

// TODO: participants 대신 _count.contestRecord 그대로 사용하는 것 고려해보기
Expand Down
35 changes: 19 additions & 16 deletions apps/backend/apps/client/src/group/group.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,25 @@ describe('GroupService', () => {
const cursor = null
const res = await service.getGroups(cursor, take)

expect(res).to.deep.equal([
{
id: 3,
groupName: 'Example Private Group 2',
description:
'This is an example private group just for testing. Check if this group is not shown to users not registered to this group.',
memberNum: 1
},
{
id: 4,
groupName: 'Example Private Group 3',
description:
'This is an example private group just for testing. Check if this group is not shown to users not registered to this group.',
memberNum: 2
}
])
expect(res).to.deep.equal({
data: [
{
id: 3,
groupName: 'Example Private Group 2',
description:
'This is an example private group just for testing. Check if this group is not shown to users not registered to this group.',
memberNum: 1
},
{
id: 4,
groupName: 'Example Private Group 3',
description:
'This is an example private group just for testing. Check if this group is not shown to users not registered to this group.',
memberNum: 2
}
],
total: 4
})
})
})

Expand Down
14 changes: 13 additions & 1 deletion apps/backend/apps/client/src/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,19 @@ export class GroupService {
}
})

return groups
const total = await this.prisma.group.count({
where: {
NOT: {
id: 1
},
config: {
path: ['showOnList'],
equals: true
}
}
})

return { data: groups, total }
}

async getJoinedGroups(userId: number) {
Expand Down
15 changes: 12 additions & 3 deletions apps/backend/apps/client/src/notice/notice.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const noticeId = 2
const userId = 1
const groupId = 1
const username = 'manager'
const totalNotice = 24

const notice = {
id: noticeId,
Expand Down Expand Up @@ -55,7 +56,8 @@ const db = {
findUnique: stub().resolves(notice),
findUniqueOrThrow: stub().resolves(notice),
findFirst: stub(),
findFirstOrThrow: stub()
findFirstOrThrow: stub(),
count: stub().resolves(24)
},
group: {
findUnique: stub().resolves(group)
Expand Down Expand Up @@ -121,7 +123,10 @@ describe('NoticeService', () => {
take: 3,
groupId: group.id
})
expect(getNoticesByGroupId).to.deep.equal(userNotices)
expect(getNoticesByGroupId).to.deep.equal({
data: userNotices,
total: totalNotice
})
})
})

Expand Down Expand Up @@ -165,7 +170,11 @@ describe('NoticeService', () => {
take: 3,
groupId: group.id
})
expect(getFixedNoticesByGroupId).to.deep.equal(userNotices)

expect(getFixedNoticesByGroupId).to.deep.equal({
data: userNotices,
total: totalNotice
})
})
})

Expand Down
16 changes: 15 additions & 1 deletion apps/backend/apps/client/src/notice/notice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,26 @@ export class NoticeService {
orderBy: { id: 'desc' }
})

return notices.map((notice) => {
const data = notices.map((notice) => {
return {
...notice,
createdBy: notice.createdBy?.username
}
})

const total = await this.prisma.notice.count({
where: {
groupId,
isVisible: true,
isFixed: fixed,
title: {
contains: search,
mode: 'insensitive'
}
}
})

return { data, total }
}

async getNoticeByID(id: number, groupId = OPEN_SPACE_ID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Exclude, Expose, Type } from 'class-transformer'
export class ProblemsResponseDto {
@Expose()
@Type(() => Problem)
problems: Problem[]
data: Problem[]

@Expose()
total: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Exclude, Expose, Transform, Type } from 'class-transformer'
export class RelatedProblemsResponseDto {
@Expose()
@Type(() => Problem)
problems: Problem[]
data: Problem[]

@Expose()
total: number
Expand Down
10 changes: 5 additions & 5 deletions apps/backend/apps/client/src/problem/problem.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('ProblemService', () => {
// then
expect(result).to.deep.equal(
plainToInstance(ProblemsResponseDto, {
problems: [
data: [
{
...mockProblems[0],
submissionCount: 10,
Expand Down Expand Up @@ -275,7 +275,7 @@ describe('ContestProblemService', () => {
// then
expect(result).to.deep.equal(
plainToInstance(RelatedProblemsResponseDto, {
problems: mockContestProblems,
data: mockContestProblems,
total: 2
})
)
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('ContestProblemService', () => {
// then
expect(result).to.deep.equal(
plainToInstance(RelatedProblemsResponseDto, {
problems: mockContestProblems,
data: mockContestProblems,
total: 2
})
)
Expand Down Expand Up @@ -491,7 +491,7 @@ describe('WorkbookProblemService', () => {
// then
expect(result).to.deep.equal(
plainToInstance(RelatedProblemsResponseDto, {
problems: mockWorkbookProblems,
data: mockWorkbookProblems,
total: 2
})
)
Expand All @@ -513,7 +513,7 @@ describe('WorkbookProblemService', () => {
// then
expect(result).to.deep.equal(
plainToInstance(RelatedProblemsResponseDto, {
problems: mockWorkbookProblems,
data: mockWorkbookProblems,
total: 2
})
)
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/apps/client/src/problem/problem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ProblemService {
)

return plainToInstance(ProblemsResponseDto, {
problems: await Promise.all(problems),
data: await Promise.all(problems),
total
})
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export class ContestProblemService {
await this.problemRepository.getContestProblemTotalCount(contestId)

return plainToInstance(RelatedProblemsResponseDto, {
problems: data,
data,
total
})
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export class WorkbookProblemService {
await this.problemRepository.getWorkbookProblemTotalCount(workbookId)

return plainToInstance(RelatedProblemsResponseDto, {
problems: data,
data,
total
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const db = {
findMany: stub(),
findFirstOrThrow: stub(),
create: stub(),
update: stub()
update: stub(),
count: stub().resolves(1)
},
submissionResult: {
create: stub()
Expand Down Expand Up @@ -309,7 +310,7 @@ describe('SubmissionService', () => {

expect(
await service.getSubmissions({ problemId: problems[0].id })
).to.be.deep.equal(submissions)
).to.be.deep.equal({ data: submissions, total: 1 })
})

it('should throw not found error', async () => {
Expand Down
16 changes: 12 additions & 4 deletions apps/backend/apps/client/src/submission/submission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export class SubmissionService implements OnModuleInit {
groupId?: number
cursor?: number | null
take?: number
}): Promise<Partial<Submission>[]> {
}) {
const paginator = this.prisma.getPaginator(cursor)

await this.prisma.problem.findFirstOrThrow({
Expand Down Expand Up @@ -486,7 +486,9 @@ export class SubmissionService implements OnModuleInit {
orderBy: [{ id: 'desc' }, { createTime: 'desc' }]
})

return submissions
const total = await this.prisma.submission.count({ where: { problemId } })

return { data: submissions, total }
}

@Span()
Expand Down Expand Up @@ -638,7 +640,7 @@ export class SubmissionService implements OnModuleInit {
groupId?: number
cursor?: number | null
take?: number
}): Promise<Partial<Submission>[]> {
}) {
const paginator = this.prisma.getPaginator(cursor)

const isAdmin = await this.prisma.user.findFirst({
Expand Down Expand Up @@ -670,7 +672,7 @@ export class SubmissionService implements OnModuleInit {
}
})

return await this.prisma.submission.findMany({
const submissions = await this.prisma.submission.findMany({
...paginator,
take,
where: {
Expand All @@ -692,5 +694,11 @@ export class SubmissionService implements OnModuleInit {
},
orderBy: [{ id: 'desc' }, { createTime: 'desc' }]
})

const total = await this.prisma.submission.count({
where: { problemId, contestId }
})

return { data: submissions, total }
}
}
Loading

0 comments on commit 650cab3

Please sign in to comment.