Skip to content

Commit

Permalink
Merge pull request #58 from ti-broish/towns-code
Browse files Browse the repository at this point in the history
Номенклатурни кодове на градовете вместо вътрешни идентификатори
  • Loading branch information
hkdobrev authored Jun 22, 2021
2 parents 38d9399 + 92d1210 commit a360e26
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/sections/api/town-exists.constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export class IsTownExistsConstraint implements ValidatorConstraintInterface {
@Inject(TownsRepository) private readonly repo: TownsRepository,
) {}

async validate(townId?: number): Promise<boolean> {
if (!townId) {
async validate(townCode?: number): Promise<boolean> {
if (!townCode) {
return true;
}

if (typeof townId !== 'number' || townId <= 0) {
if (typeof townCode !== 'number' || townCode <= 0) {
return false;
}

return !!(await this.repo.findOne(townId));
return !!(await this.repo.findOneByCode(townCode));
}

defaultMessage?(validationArguments?: ValidationArguments): string {
return `Town with ID "${validationArguments.value}" does not exist!`;
return `Town with code "${validationArguments.value}" does not exist!`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sections/api/town.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IsTownExists } from './town-exists.constraint';
@Exclude()
export class TownDto {
@ApiProperty()
@Expose({ groups: ['read', 'create'] })
@Expose({ name: 'code', groups: ['read', 'create'] })
@IsTownExists({ groups: ['create'] })
@IsNumber({}, { groups: ['create'] })
@Min(1, { groups: ['create'] })
Expand Down
4 changes: 2 additions & 2 deletions src/sections/entities/sections.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class SectionsRepository {
}

findByTownAndCityRegion(
townId: number,
townCode: number,
cityRegionCode?: string,
): Promise<Section[]> {
return this.repo.find({
Expand All @@ -50,7 +50,7 @@ export class SectionsRepository {
},
},
where: (qb: SelectQueryBuilder<Section>): void => {
qb.where({ town: townId });
qb.andWhere('town.code = :townCode', { townCode });

if (cityRegionCode) {
qb.innerJoinAndSelect(
Expand Down
8 changes: 4 additions & 4 deletions src/sections/entities/towns.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export class TownsRepository {
private repo: Repository<Town>,
) {}

findOne(id: number): Promise<Town> {
return this.repo.findOne(id);
findOneByCode(code: number): Promise<Town> {
return this.repo.findOne({ code });
}

findOneOrFail(id: number): Promise<Town> {
return this.repo.findOneOrFail(id);
findOneByCodeOrFail(code: number): Promise<Town> {
return this.repo.findOneOrFail({ code });
}

filter(
Expand Down
2 changes: 1 addition & 1 deletion src/violations/api/violations-filters.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ViolationsFilters extends PageDTO {
author: string;

@Optional()
town: string;
town: number;

@Optional()
organization: number;
Expand Down
2 changes: 1 addition & 1 deletion src/violations/entities/violations.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ViolationsRepository {
}

if (filters.town) {
qb.andWhere('town.id = :town', { town: filters.town });
qb.andWhere('town.code = :town', { town: filters.town });
}

if (filters.author || filters.organization) {
Expand Down

0 comments on commit a360e26

Please sign in to comment.