Skip to content

Commit

Permalink
Add query for federal state
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper committed Oct 2, 2023
1 parent a758bb6 commit 8ed8e72
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -14,9 +15,13 @@ export class SchoolController {
constructor(private readonly schoolUc: SchoolUc) {}

@Get('/')
public async getAllSchools(@Query() pagination: PaginationParams): Promise<SchoolListResponse> {
const schools = await this.schoolUc.getAllSchools(pagination);
public async getAllSchools(
@Query() query: SchoolQueryParams,
@Query() pagination: PaginationParams
): Promise<SchoolListResponse> {
const schools = await this.schoolUc.getAllSchools(query, pagination);

// TODO: Add pagination params to response
const res = SchoolResponseMapper.mapToListResponse(schools);

return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SchoolEntity>): Promise<School[]>;
getAllSchools(query: SchoolQuery, options: IFindOptions<SchoolEntity>): Promise<School[]>;

getSchool(schoolId: EntityId): Promise<School>;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<School[]> {
const schools = await this.schoolRepo.getAllSchools({ pagination });
public async getAllSchools(query: SchoolQuery, pagination: IPagination): Promise<School[]> {
const schools = await this.schoolRepo.getAllSchools(query, { pagination });

return schools;
}
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/modules/school/domain/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions apps/server/src/modules/school/domain/type/school-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EntityId } from '@shared/domain';

export interface SchoolQuery {
federalStateId?: EntityId;
}
5 changes: 3 additions & 2 deletions apps/server/src/modules/school/domain/uc/school.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<School[]> {
const schools = await this.schoolService.getAllSchools(pagination);
public async getAllSchools(query: SchoolQuery, pagination: IPagination): Promise<School[]> {
const schools = await this.schoolService.getAllSchools(query, pagination);

return schools;
}
Expand Down
11 changes: 8 additions & 3 deletions apps/server/src/modules/school/repo/school.repo.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,10 +15,14 @@ export class SchoolMikroOrmRepo extends BaseRepo<SchoolEntity> implements School
return SchoolEntity;
}

public async getAllSchools(options?: IFindOptions<SchoolEntity>): Promise<School[]> {
public async getAllSchools(query: SchoolQuery, options?: IFindOptions<SchoolEntity>): Promise<School[]> {
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);

Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/modules/school/repo/scope/school.scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EntityId, SchoolEntity } from '@shared/domain';
import { Scope } from '@shared/repo';

export class SchoolScope extends Scope<SchoolEntity> {
byFederalState(federalStateId?: EntityId) {
if (federalStateId) {
this.addQuery({ federalState: federalStateId });
}
}
}

0 comments on commit 8ed8e72

Please sign in to comment.