building search

This commit is contained in:
Evert Prants 2023-01-27 18:26:32 +02:00
parent 2e59d0b907
commit 5fff92bf0c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 74 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import {
ParseIntPipe, ParseIntPipe,
Patch, Patch,
Post, Post,
Query,
UseGuards, UseGuards,
UseInterceptors, UseInterceptors,
} from '@nestjs/common'; } from '@nestjs/common';
@ -43,6 +44,8 @@ import {
BuildingsCreateRoomRequestDto, BuildingsCreateRoomRequestDto,
BuildingsUpdateRoomRequestDto, BuildingsUpdateRoomRequestDto,
} from './dto/buildings-create-room-request.dto'; } from './dto/buildings-create-room-request.dto';
import { BuildingSearchRequestDto } from './dto/buildings-search-request.dto';
import { BuildingSearchResponseDto } from './dto/building-search-response.dto';
@Controller({ @Controller({
path: 'buildings', path: 'buildings',
@ -75,6 +78,16 @@ export class AppBuildingController {
return this.service.createBuilding(user, body); return this.service.createBuilding(user, body);
} }
@Get('search')
@ApiOperation({ summary: 'Search for buildings' })
@ApiOkResponse({ isArray: true, type: BuildingSearchResponseDto })
async searchForBuildings(
@LoggedInUser() user: User,
@Query() query: BuildingSearchRequestDto,
): Promise<BuildingSearchResponseDto[]> {
return this.service.searchForBuilding(query, user);
}
@Get(':id') @Get(':id')
@ApiParam({ name: 'id', description: 'Building ID' }) @ApiParam({ name: 'id', description: 'Building ID' })
@ApiOperation({ summary: 'Get building by ID' }) @ApiOperation({ summary: 'Get building by ID' })

View File

@ -20,6 +20,8 @@ import {
BuildingsUpdateRoomRequestDto, BuildingsUpdateRoomRequestDto,
} from './dto/buildings-create-room-request.dto'; } from './dto/buildings-create-room-request.dto';
import { PlanRendererService } from './plan-renderer/plan-renderer.service'; import { PlanRendererService } from './plan-renderer/plan-renderer.service';
import { BuildingSearchRequestDto } from './dto/buildings-search-request.dto';
import { BuildingSearchResponseDto } from './dto/building-search-response.dto';
@Injectable() @Injectable()
export class AppBuildingService { export class AppBuildingService {
@ -298,4 +300,12 @@ export class AppBuildingService {
return room; return room;
} }
async searchForBuilding(
{ searchTerm }: BuildingSearchRequestDto,
{ sub }: User,
) {
const list = await this.buildingService.searchBuilding(searchTerm, sub);
return list.map((building) => new BuildingSearchResponseDto(building));
}
} }

View File

@ -0,0 +1,14 @@
import { PickType } from '@nestjs/swagger';
import { Building } from 'src/objects/building/entities/building.entity';
export class BuildingSearchResponseDto extends PickType(Building, [
'id',
'displayName',
'address',
'color',
]) {
constructor(obj: Partial<BuildingSearchResponseDto>) {
super();
Object.assign(this, obj);
}
}

View File

@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, MinLength } from 'class-validator';
export class BuildingSearchRequestDto {
@ApiProperty()
@IsString()
@MinLength(2)
searchTerm: string;
}

View File

@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm'; import { ILike, Repository } from 'typeorm';
import { GroupService } from '../group/group.service'; import { GroupService } from '../group/group.service';
import { UserBuildingAccessControl } from './entities/acl.entity'; import { UserBuildingAccessControl } from './entities/acl.entity';
import { Building } from './entities/building.entity'; import { Building } from './entities/building.entity';
@ -62,6 +62,33 @@ export class BuildingService {
}); });
} }
async searchBuilding(searchTerm: string, sub: string, relations = []) {
const search = ILike(`%${searchTerm}%`);
return this.buildingRepository.find({
where: [
{
displayName: search,
groups: {
members: {
sub,
},
},
},
{
address: search,
groups: {
members: {
sub,
},
},
},
],
relations,
take: 16,
select: ['id', 'displayName', 'address', 'color'],
});
}
async getFloorByBuildingAndUserSub( async getFloorByBuildingAndUserSub(
buildingId: number, buildingId: number,
floorNumber: number, floorNumber: number,