building search
This commit is contained in:
parent
2e59d0b907
commit
5fff92bf0c
@ -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' })
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
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 { 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,
|
||||||
|
Loading…
Reference in New Issue
Block a user