Skip to content

Commit

Permalink
Introduce more dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper committed Oct 17, 2023
1 parent a15e0de commit af114ee
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { County } from '@src/modules/school/domain';
import { FederalStateDto } from '@src/modules/school/domain/dto';
import { FederalStateResponse } from '../response';
import { CountyResponse } from '../response/county.response';

export class FederalStateMapper {
public static mapToResponse(federalState: FederalStateDto): FederalStateResponse {
const counties = federalState.counties && this.mapToCountyResponses(federalState.counties);

const res = new FederalStateResponse({
...federalState,
counties,
});

return res;
}

private static mapToCountyResponses(counties: County[]): CountyResponse[] {
const res = counties.map((county) => this.mapToCountyResponse(county));

return res;
}

private static mapToCountyResponse(county: County): CountyResponse {
const res = new CountyResponse({
name: county.name,
countyId: county.countyId,
antaresKey: county.antaresKey,
});

return res;
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
import { PaginationParams } from '@shared/controller';
import { SlimSchoolDto } from '@src/modules/school/domain/dto';
import { County, FederalState, School, SchoolYear, System } from '../../../domain';
import { SlimSchoolListResponse, SchoolResponse } from '../response';
import { CountyResponse } from '../response/county.response';
import { FederalStateResponse } from '../response/federal-state.response';
import { SchoolDto, SlimSchoolDto } from '@src/modules/school/domain/dto';
import { SchoolResponse, SlimSchoolListResponse } from '../response';
import { SlimSchoolResponse } from '../response/school-reduced.response';
import { SchoolYearResponse } from '../response/school-year.response';
import { SystemResponse } from '../response/system.response';
import { FederalStateMapper } from './federal-state.mapper';
import { SchoolYearMapper } from './school-year.mapper';
import { SystemMapper } from './system.mapper';

export class SchoolResponseMapper {
public static mapToResponse(school: School): SchoolResponse {
const schoolProps = school.getProps();

const federalState = this.mapToFederalStateResponse(schoolProps.federalState);
const currentYear = schoolProps.currentYear && this.mapToSchoolYearResponse(schoolProps.currentYear);
const features = schoolProps.features && Array.from(schoolProps.features);
const systems = schoolProps.systems?.map((system) => this.mapToSystemResponse(system));
public static mapToResponse(school: SchoolDto): SchoolResponse {
const federalState = FederalStateMapper.mapToResponse(school.federalState);
const currentYear = school.currentYear && SchoolYearMapper.mapToResponse(school.currentYear);
const features = school.features && Array.from(school.features);
const systems = school.systems?.map((system) => SystemMapper.mapToResponse(system));

// TODO: Do we want to access the props via getProps() here or do we want getters?
// I added getters for federalState and schoolYear because there are conditions with them below
// and then the code is a easier to read. But I wasn't really sure.
// Do we want any fixed criteria for when to add getters?
const res = new SchoolResponse({
id: school.id,
name: schoolProps.name,
officialSchoolNumber: schoolProps.officialSchoolNumber,
currentYear,
...school,
federalState,
county: schoolProps.county,
purpose: schoolProps.purpose,
currentYear,
features,
systems,
inMaintenance: school.isInMaintenance(),
isExternal: school.isExternal(),
logo_dataUrl: schoolProps.logo_dataUrl,
});

return res;
Expand All @@ -52,69 +41,4 @@ export class SchoolResponseMapper {

return res;
}

// TODO: Create own mappers for other DOs!
private static mapToFederalStateResponse(federalState: FederalState): FederalStateResponse {
const federalStateProps = federalState.getProps();

const counties = federalStateProps.counties && this.mapToCountyResponses(federalStateProps.counties);

const res = new FederalStateResponse({
id: federalState.id,
name: federalStateProps.name,
abbreviation: federalStateProps.abbreviation,
logoUrl: federalStateProps.logoUrl,
counties,
});

return res;
}

private static mapToCountyResponses(counties: County[]): CountyResponse[] {
const res = counties.map((county) => this.mapToCountyResponse(county));

return res;
}

private static mapToCountyResponse(county: County): CountyResponse {
const res = new CountyResponse({
name: county.name,
countyId: county.countyId,
antaresKey: county.antaresKey,
});

return res;
}

private static mapToSchoolYearResponse(schoolYear: SchoolYear): SchoolYearResponse {
const schoolYearProps = schoolYear.getProps();

const res = new SchoolYearResponse({
id: schoolYear.id,
name: schoolYearProps.name,
startDate: schoolYearProps.startDate,
endDate: schoolYearProps.endDate,
});

return res;
}

private static mapToSystemResponse(system: System) {
const systemProps = system.getProps();

const res = new SystemResponse({
id: system.id,
type: systemProps.type,
url: systemProps.url,
alias: systemProps.alias,
displayName: systemProps.displayName,
oauthConfig: systemProps.oauthConfig,
oidcConfig: systemProps.oidcConfig,
ldapConfig: systemProps.ldapConfig,
provisioningStrategy: systemProps.provisioningStrategy,
provisioningUrl: systemProps.provisioningUrl,
});

return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SchoolYearDto } from '@src/modules/school/domain';
import { SchoolYearResponse } from '../response';

export class SchoolYearMapper {
public static mapToResponse(schoolYear: SchoolYearDto): SchoolYearResponse {
const res = new SchoolYearResponse(schoolYear);

return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SystemDto } from '@src/modules/school/domain';
import { SystemResponse } from '../response';

export class SystemMapper {
public static mapToResponse(system: SystemDto): SystemResponse {
const res = new SystemResponse(system);

return res;
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './federal-state.response';
export * from './school.response';
export * from './school-list.response';
export * from './school-year.response';
export * from './system.response';
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export class SchoolController {

// TODO: Do we have a convention for the casing of routes?
@Get('/slim-list')
public async getAllSchools(
public async getSlimSchoolsList(
@Query() query: SchoolQueryParams,
@Query() pagination: PaginationParams
): Promise<SlimSchoolListResponse> {
// TODO: Think about consistent naming here: What comes first "Slim" or "List"?
const schools = await this.schoolUc.getListOfSlimSchools(query, pagination);

const res = SchoolResponseMapper.mapToSlimListResponse(schools, pagination);
Expand Down
21 changes: 21 additions & 0 deletions apps/server/src/modules/school/domain/dto/federal-state.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { County } from '../type';

export class FederalStateDto {
constructor({ id, name, abbreviation, logoUrl, counties }: FederalStateDto) {
this.id = id;
this.name = name;
this.abbreviation = abbreviation;
this.logoUrl = logoUrl;
this.counties = counties;
}

id: string;

name: string;

abbreviation: string;

logoUrl: string;

counties?: County[];
}
4 changes: 4 additions & 0 deletions apps/server/src/modules/school/domain/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './federal-state.dto';
export * from './school.dto';
export * from './school-year.dto';
export * from './slim-school.dto';
export * from './system.dto';
16 changes: 16 additions & 0 deletions apps/server/src/modules/school/domain/dto/school-year.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class SchoolYearDto {
constructor({ id, name, startDate, endDate }: SchoolYearDto) {
this.id = id;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
}

id: string;

name: string;

startDate: Date;

endDate: Date;
}
58 changes: 58 additions & 0 deletions apps/server/src/modules/school/domain/dto/school.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { County, SchoolFeature, SchoolPurpose } from '../type';
import { FederalStateDto } from './federal-state.dto';
import { SchoolYearDto } from './school-year.dto';
import { SystemDto } from './system.dto';

export class SchoolDto {
constructor({
id,
name,
officialSchoolNumber,
federalState,
currentYear,
purpose,
features,
county,
systems,
inMaintenance,
isExternal,
logo_dataUrl,
}: SchoolDto) {
this.id = id;
this.name = name;
this.officialSchoolNumber = officialSchoolNumber;
this.federalState = federalState;
this.currentYear = currentYear;
this.purpose = purpose;
this.features = features;
this.county = county;
this.systems = systems;
this.inMaintenance = inMaintenance;
this.isExternal = isExternal;
this.logo_dataUrl = logo_dataUrl;
}

id: string;

name: string;

officialSchoolNumber?: string;

currentYear?: SchoolYearDto;

federalState: FederalStateDto;

county?: County;

purpose?: SchoolPurpose;

features?: SchoolFeature[];

systems?: SystemDto[];

inMaintenance: boolean;

isExternal: boolean;

logo_dataUrl?: string;
}
48 changes: 48 additions & 0 deletions apps/server/src/modules/school/domain/dto/system.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { SystemProvisioningStrategy } from '@shared/domain/interface/system-provisioning.strategy';
import { LdapConfig, OauthConfig, OidcConfig } from '../type';

export class SystemDto {
constructor({
id,
type,
url,
alias,
displayName,
oauthConfig,
oidcConfig,
ldapConfig,
provisioningStrategy,
provisioningUrl,
}: SystemDto) {
this.id = id;
this.type = type;
this.url = url;
this.alias = alias;
this.displayName = displayName;
this.oauthConfig = oauthConfig;
this.oidcConfig = oidcConfig;
this.ldapConfig = ldapConfig;
this.provisioningStrategy = provisioningStrategy;
this.provisioningUrl = provisioningUrl;
}

id: string;

type: string;

url?: string;

alias?: string;

displayName?: string;

oauthConfig?: OauthConfig;

oidcConfig?: OidcConfig;

ldapConfig?: LdapConfig;

provisioningStrategy?: SystemProvisioningStrategy;

provisioningUrl?: string;
}
1 change: 1 addition & 0 deletions apps/server/src/modules/school/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './do';
export * from './dto';
export * from './interface';
export * from './service';
export * from './uc';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FederalState } from '../do';
import { FederalStateDto } from '../dto';

export class FederalStateMapper {
public static mapToDto(federalState: FederalState): FederalStateDto {
const federalStateProps = federalState.getProps();

const dto = new FederalStateDto({
id: federalState.id,
name: federalStateProps.name,
abbreviation: federalStateProps.abbreviation,
logoUrl: federalStateProps.logoUrl,
counties: federalStateProps.counties,
});

return dto;
}
}
17 changes: 17 additions & 0 deletions apps/server/src/modules/school/domain/mapper/school-year.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SchoolYear } from '../do';
import { SchoolYearDto } from '../dto';

export class SchoolYearMapper {
public static mapToDto(schoolYear: SchoolYear): SchoolYearDto {
const schoolYearProps = schoolYear.getProps();

const dto = new SchoolYearDto({
id: schoolYear.id,
name: schoolYearProps.name,
startDate: schoolYearProps.startDate,
endDate: schoolYearProps.endDate,
});

return dto;
}
}
Loading

0 comments on commit af114ee

Please sign in to comment.