Skip to content

Commit

Permalink
Merge pull request #101 from game-node-app/dev
Browse files Browse the repository at this point in the history
improved ordering for collection entries
  • Loading branch information
Lamarcke authored Sep 17, 2024
2 parents e44b968 + 07dc3b6 commit dfe36e9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server_swagger.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PaginationInterceptor } from "../../interceptor/pagination.interceptor"

import { CollectionEntriesPaginatedResponseDto } from "./dto/collection-entries-paginated-response.dto";
import { Public } from "../../auth/public.decorator";
import { FindCollectionEntriesForCollectionIdDto } from "./dto/find-collection-entries-for-collection-id.dto";

@Controller("collections-entries")
@ApiTags("collections-entries")
Expand Down Expand Up @@ -159,7 +160,7 @@ export class CollectionsEntriesController {
async findAllByCollectionId(
@Session() session: SessionContainer,
@Param("id") collectionId: string,
@Body() dto?: FindCollectionEntriesDto,
@Body() dto?: FindCollectionEntriesForCollectionIdDto,
): Promise<CollectionEntriesPaginatedResponseDto> {
return (await this.collectionsEntriesService.findAllByCollectionId(
session?.getUserId(),
Expand Down
43 changes: 37 additions & 6 deletions src/collections/collections-entries/collections-entries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
} from "@nestjs/common";
import { CollectionEntry } from "./entities/collection-entry.entity";
import { InjectRepository } from "@nestjs/typeorm";
import { DeepPartial, FindOptionsRelations, In, Repository } from "typeorm";
import {
DeepPartial,
FindManyOptions,
FindOptionsRelations,
In,
Repository,
} from "typeorm";
import { CreateUpdateCollectionEntryDto } from "./dto/create-update-collection-entry.dto";
import { ActivitiesQueueService } from "../../activities/activities-queue/activities-queue.service";
import { FindCollectionEntriesDto } from "./dto/find-collection-entries.dto";
Expand All @@ -19,6 +25,7 @@ import { getIconNamesForPlatformAbbreviations } from "../../game/game-repository
import { LevelService } from "../../level/level.service";
import { LevelIncreaseActivities } from "../../level/level.constants";
import { CollectionsService } from "../collections.service";
import { FindCollectionEntriesForCollectionIdDto } from "./dto/find-collection-entries-for-collection-id.dto";

@Injectable()
export class CollectionsEntriesService {
Expand Down Expand Up @@ -100,12 +107,15 @@ export class CollectionsEntriesService {
async findAllByCollectionId(
userId: string | undefined,
collectionId: string,
dto?: FindCollectionEntriesDto,
dto?: FindCollectionEntriesForCollectionIdDto,
) {
const findOptions = buildBaseFindOptions<CollectionEntry>(dto);
const baseFindOptions = buildBaseFindOptions<CollectionEntry>({
...dto,
orderBy: undefined,
});

return await this.collectionEntriesRepository.findAndCount({
...findOptions,
const findOptions: FindManyOptions<CollectionEntry> = {
...baseFindOptions,
where: {
collections: [
{
Expand All @@ -121,7 +131,28 @@ export class CollectionsEntriesService {
],
},
relations: this.relations,
});
};

if (dto?.orderBy) {
if (dto.orderBy.addedDate) {
findOptions.order = {
createdAt: dto.orderBy.addedDate,
};
} else if (dto.orderBy.releaseDate) {
findOptions.order = {
game: {
firstReleaseDate: dto.orderBy.releaseDate,
},
};
findOptions.relations = {
...this.relations,
// Needed for ordering
game: true,
};
}
}

return await this.collectionEntriesRepository.findAndCount(findOptions);
}

async findAllByUserIdWithPermissions(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { OmitType } from "@nestjs/swagger";
import { FindCollectionEntriesDto } from "./find-collection-entries.dto";

export class FindCollectionEntriesForCollectionIdOrderBy {
addedDate?: "ASC" | "DESC";
releaseDate?: "ASC" | "DESC";
}

export class FindCollectionEntriesForCollectionIdDto extends OmitType(
FindCollectionEntriesDto,
["orderBy"],
) {
orderBy?: FindCollectionEntriesForCollectionIdOrderBy;
}

0 comments on commit dfe36e9

Please sign in to comment.