building search
This commit is contained in:
parent
2e59d0b907
commit
5fff92bf0c
@ -8,6 +8,7 @@ import {
|
||||
ParseIntPipe,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
@ -43,6 +44,8 @@ import {
|
||||
BuildingsCreateRoomRequestDto,
|
||||
BuildingsUpdateRoomRequestDto,
|
||||
} from './dto/buildings-create-room-request.dto';
|
||||
import { BuildingSearchRequestDto } from './dto/buildings-search-request.dto';
|
||||
import { BuildingSearchResponseDto } from './dto/building-search-response.dto';
|
||||
|
||||
@Controller({
|
||||
path: 'buildings',
|
||||
@ -75,6 +78,16 @@ export class AppBuildingController {
|
||||
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')
|
||||
@ApiParam({ name: 'id', description: 'Building ID' })
|
||||
@ApiOperation({ summary: 'Get building by ID' })
|
||||
|
@ -20,6 +20,8 @@ import {
|
||||
BuildingsUpdateRoomRequestDto,
|
||||
} from './dto/buildings-create-room-request.dto';
|
||||
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()
|
||||
export class AppBuildingService {
|
||||
@ -298,4 +300,12 @@ export class AppBuildingService {
|
||||
|
||||
return room;
|
||||
}
|
||||
|
||||
async searchForBuilding(
|
||||
{ searchTerm }: BuildingSearchRequestDto,
|
||||
{ sub }: User,
|
||||
) {
|
||||
const list = await this.buildingService.searchBuilding(searchTerm, sub);
|
||||
return list.map((building) => new BuildingSearchResponseDto(building));
|
||||
}
|
||||
}
|
||||
|
14
src/app-building/dto/building-search-response.dto.ts
Normal file
14
src/app-building/dto/building-search-response.dto.ts
Normal 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);
|
||||
}
|
||||
}
|
9
src/app-building/dto/buildings-search-request.dto.ts
Normal file
9
src/app-building/dto/buildings-search-request.dto.ts
Normal 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;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ILike, Repository } from 'typeorm';
|
||||
import { GroupService } from '../group/group.service';
|
||||
import { UserBuildingAccessControl } from './entities/acl.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(
|
||||
buildingId: number,
|
||||
floorNumber: number,
|
||||
|
Loading…
Reference in New Issue
Block a user