Skip to content

Commit

Permalink
feat: add order by list character
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Oct 9, 2024
1 parent 8908575 commit c654bf8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/modules/story-event/dto/query-approved-character.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { CharacterSortType } from '../story-event.enum';

export class QueryApprovedCharacterParamDto {
@ApiPropertyOptional()
user_id: string;

@ApiPropertyOptional({
enum: [
CharacterSortType.Created_At_Asc,
CharacterSortType.Created_At_Desc,
CharacterSortType.User_Collect_Asc,
CharacterSortType.User_Collect_Desc,
],
})
order_by: CharacterSortType;

@ApiPropertyOptional()
limit: number;

@ApiPropertyOptional()
offset: number;
}
7 changes: 7 additions & 0 deletions src/modules/story-event/story-event.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export enum SubmissionType {
Manga = 'manga',
Artwork = 'artwork',
}

export enum CharacterSortType {
Created_At_Asc = 'Created_At_Asc',
Created_At_Desc = 'Created_At_Desc',
User_Collect_Asc = 'User_Collect_Asc',
User_Collect_Desc = 'User_Collect_Desc',
}
13 changes: 10 additions & 3 deletions src/modules/story-event/story-event.graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class StoryEventGraphql {
);
}

async queryApprovedCharacters(variables: any) {
async queryApprovedCharacters(variables: any, orderBy: string[]) {
const headers = {
'x-hasura-admin-secret': this.configSvc.get<string>(
'graphql.adminSecret'
Expand All @@ -223,8 +223,15 @@ export class StoryEventGraphql {
return this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
'',
`query story_character($user_id: bpchar = "") {
story_character(where: {status: {_eq: "Approved"}}, order_by: {is_default_character: desc}) {
`query story_character($user_id: bpchar = "", $limit: Int = 10, $offset: Int = 0) {
story_character_aggregate(where: {status: {_eq: "Approved"}}) {
aggregate {
count
}
}
story_character(where: {status: {_eq: "Approved"}}, order_by: {${orderBy.join(
','
)}}, limit: $limit, offset: $offset) {
id
avatar_url
descripton_url
Expand Down
31 changes: 28 additions & 3 deletions src/modules/story-event/story-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SubmitMangaRequestDto,
} from './dto/submit-manga.dto';
import {
CharacterSortType,
StoryCharacterStatus,
SubmissionStatus,
SubmissionType,
Expand Down Expand Up @@ -395,9 +396,33 @@ export class StoryEventService {
}

async queryCharacter(data: QueryApprovedCharacterParamDto) {
return this.storyEventGraphql.queryApprovedCharacters({
user_id: data.user_id,
});
const { user_id, limit, offset, order_by } = data;
const orderBy = ['is_default_character: desc'];
switch (order_by) {
case CharacterSortType.Created_At_Asc:
orderBy.push('created_at: asc');
break;
case CharacterSortType.Created_At_Desc:
orderBy.push('created_at: desc');
break;
case CharacterSortType.User_Collect_Asc:
orderBy.push('user_collect_characters_aggregate: {count: asc}');
break;
case CharacterSortType.User_Collect_Desc:
orderBy.push('user_collect_characters_aggregate: {count: desc}');
break;
default:
break;
}

return this.storyEventGraphql.queryApprovedCharacters(
{
user_id,
limit,
offset,
},
orderBy
);
}

async queryCollectedCharacter() {
Expand Down

0 comments on commit c654bf8

Please sign in to comment.