From 8ed8e72f57bf7cd8d22c86884dd5d922673e6fd6 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 2 Oct 2023 16:30:24 +0200 Subject: [PATCH] Add query for federal state --- .../school/controller/dto/school-query.params.ts | 10 ++++++++++ .../modules/school/controller/school.controller.ts | 9 +++++++-- .../school/domain/interface/school-repo.interface.ts | 3 ++- .../modules/school/domain/service/school.service.ts | 7 ++++--- apps/server/src/modules/school/domain/type/index.ts | 1 + .../src/modules/school/domain/type/school-query.ts | 5 +++++ apps/server/src/modules/school/domain/uc/school.uc.ts | 5 +++-- apps/server/src/modules/school/repo/school.repo.ts | 11 ++++++++--- .../src/modules/school/repo/scope/school.scope.ts | 10 ++++++++++ 9 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 apps/server/src/modules/school/controller/dto/school-query.params.ts create mode 100644 apps/server/src/modules/school/domain/type/school-query.ts create mode 100644 apps/server/src/modules/school/repo/scope/school.scope.ts diff --git a/apps/server/src/modules/school/controller/dto/school-query.params.ts b/apps/server/src/modules/school/controller/dto/school-query.params.ts new file mode 100644 index 00000000000..f28291901ff --- /dev/null +++ b/apps/server/src/modules/school/controller/dto/school-query.params.ts @@ -0,0 +1,10 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; +import { EntityId } from '@shared/domain'; +import { IsMongoId, IsOptional } from 'class-validator'; + +export class SchoolQueryParams { + @IsMongoId() + @IsOptional() + @ApiPropertyOptional() + federalStateId?: EntityId; +} diff --git a/apps/server/src/modules/school/controller/school.controller.ts b/apps/server/src/modules/school/controller/school.controller.ts index 2b1da892be0..8a8856fd3d9 100644 --- a/apps/server/src/modules/school/controller/school.controller.ts +++ b/apps/server/src/modules/school/controller/school.controller.ts @@ -4,6 +4,7 @@ import { PaginationParams } from '@shared/controller'; import { Authenticate } from '@src/modules/authentication/decorator/auth.decorator'; import { SchoolUc } from '../domain/uc/school.uc'; import { SchoolListResponse, SchoolResponse } from './dto'; +import { SchoolQueryParams } from './dto/school-query.params'; import { SchoolUrlParams } from './dto/school-url.params'; import { SchoolResponseMapper } from './mapper'; @@ -14,9 +15,13 @@ export class SchoolController { constructor(private readonly schoolUc: SchoolUc) {} @Get('/') - public async getAllSchools(@Query() pagination: PaginationParams): Promise { - const schools = await this.schoolUc.getAllSchools(pagination); + public async getAllSchools( + @Query() query: SchoolQueryParams, + @Query() pagination: PaginationParams + ): Promise { + const schools = await this.schoolUc.getAllSchools(query, pagination); + // TODO: Add pagination params to response const res = SchoolResponseMapper.mapToListResponse(schools); return res; diff --git a/apps/server/src/modules/school/domain/interface/school-repo.interface.ts b/apps/server/src/modules/school/domain/interface/school-repo.interface.ts index 905ac7d3b32..b7d9b15b279 100644 --- a/apps/server/src/modules/school/domain/interface/school-repo.interface.ts +++ b/apps/server/src/modules/school/domain/interface/school-repo.interface.ts @@ -1,8 +1,9 @@ import { EntityId, IFindOptions, SchoolEntity } from '@shared/domain'; import { School } from '../do/school'; +import { SchoolQuery } from '../type'; export interface SchoolRepo { - getAllSchools(options: IFindOptions): Promise; + getAllSchools(query: SchoolQuery, options: IFindOptions): Promise; getSchool(schoolId: EntityId): Promise; } diff --git a/apps/server/src/modules/school/domain/service/school.service.ts b/apps/server/src/modules/school/domain/service/school.service.ts index 2cc5949aba5..86a8ba0b3f8 100644 --- a/apps/server/src/modules/school/domain/service/school.service.ts +++ b/apps/server/src/modules/school/domain/service/school.service.ts @@ -1,14 +1,15 @@ import { Inject, Injectable } from '@nestjs/common'; import { EntityId, IPagination } from '@shared/domain'; import { SchoolRepo, SCHOOL_REPO } from '../interface'; -import { School } from '../do/school'; +import { School } from '../do'; +import { SchoolQuery } from '../type'; @Injectable() export class SchoolService { constructor(@Inject(SCHOOL_REPO) private readonly schoolRepo: SchoolRepo) {} - public async getAllSchools(pagination: IPagination): Promise { - const schools = await this.schoolRepo.getAllSchools({ pagination }); + public async getAllSchools(query: SchoolQuery, pagination: IPagination): Promise { + const schools = await this.schoolRepo.getAllSchools(query, { pagination }); return schools; } diff --git a/apps/server/src/modules/school/domain/type/index.ts b/apps/server/src/modules/school/domain/type/index.ts index 0da6560b9b1..12dac34a8d1 100644 --- a/apps/server/src/modules/school/domain/type/index.ts +++ b/apps/server/src/modules/school/domain/type/index.ts @@ -4,3 +4,4 @@ export * from './oauth-config'; export * from './oidc-config'; export * from './school-feature.enum'; export * from './school-purpose.enum'; +export * from './school-query'; diff --git a/apps/server/src/modules/school/domain/type/school-query.ts b/apps/server/src/modules/school/domain/type/school-query.ts new file mode 100644 index 00000000000..c10a8a9cec9 --- /dev/null +++ b/apps/server/src/modules/school/domain/type/school-query.ts @@ -0,0 +1,5 @@ +import { EntityId } from '@shared/domain'; + +export interface SchoolQuery { + federalStateId?: EntityId; +} diff --git a/apps/server/src/modules/school/domain/uc/school.uc.ts b/apps/server/src/modules/school/domain/uc/school.uc.ts index a6022444eca..1c9e88a125f 100644 --- a/apps/server/src/modules/school/domain/uc/school.uc.ts +++ b/apps/server/src/modules/school/domain/uc/school.uc.ts @@ -2,13 +2,14 @@ import { Injectable } from '@nestjs/common'; import { EntityId, IPagination } from '@shared/domain'; import { School } from '../do/school'; import { SchoolService } from '../service/school.service'; +import { SchoolQuery } from '../type/school-query'; @Injectable() export class SchoolUc { constructor(private readonly schoolService: SchoolService) {} - public async getAllSchools(pagination: IPagination): Promise { - const schools = await this.schoolService.getAllSchools(pagination); + public async getAllSchools(query: SchoolQuery, pagination: IPagination): Promise { + const schools = await this.schoolService.getAllSchools(query, pagination); return schools; } diff --git a/apps/server/src/modules/school/repo/school.repo.ts b/apps/server/src/modules/school/repo/school.repo.ts index a20cf6c3eb8..801af56f717 100644 --- a/apps/server/src/modules/school/repo/school.repo.ts +++ b/apps/server/src/modules/school/repo/school.repo.ts @@ -1,9 +1,10 @@ import { EntityName, FindOptions } from '@mikro-orm/core'; import { EntityId, IFindOptions, SchoolEntity, SortOrder } from '@shared/domain'; import { BaseRepo } from '@shared/repo'; -import { SchoolRepo } from '../domain'; +import { SchoolQuery, SchoolRepo } from '../domain'; import { School } from '../domain/do/school'; import { SchoolMapper } from './mapper/school.mapper'; +import { SchoolScope } from './scope/school.scope'; // TODO: How should the repo implentation be named? I'm undecided between "SchoolMongoRepo" and "SchoolMikroOrmRepo". // On the one hand we could have another repo for MongoDB but with mongoose, on the other hand we could have another repo with MikroORM but for a SQL database. @@ -14,10 +15,14 @@ export class SchoolMikroOrmRepo extends BaseRepo implements School return SchoolEntity; } - public async getAllSchools(options?: IFindOptions): Promise { + public async getAllSchools(query: SchoolQuery, options?: IFindOptions): Promise { + const scope = new SchoolScope(); + scope.allowEmptyQuery(true); + scope.byFederalState(query.federalStateId); + const findOptions = this.mapToMikroOrmOptions(options); - const entities = await this._em.find(SchoolEntity, {}, findOptions); + const entities = await this._em.find(SchoolEntity, scope.query, findOptions); const schools = SchoolMapper.mapToDos(entities); diff --git a/apps/server/src/modules/school/repo/scope/school.scope.ts b/apps/server/src/modules/school/repo/scope/school.scope.ts new file mode 100644 index 00000000000..26fc69cf834 --- /dev/null +++ b/apps/server/src/modules/school/repo/scope/school.scope.ts @@ -0,0 +1,10 @@ +import { EntityId, SchoolEntity } from '@shared/domain'; +import { Scope } from '@shared/repo'; + +export class SchoolScope extends Scope { + byFederalState(federalStateId?: EntityId) { + if (federalStateId) { + this.addQuery({ federalState: federalStateId }); + } + } +}