222 lines
4.5 KiB
TypeScript
222 lines
4.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { GroupService } from '../group/group.service';
|
|
import { Building } from './entities/building.entity';
|
|
import { Floor } from './entities/floor.entity';
|
|
import { Room } from './entities/room.entity';
|
|
|
|
@Injectable()
|
|
export class BuildingService {
|
|
constructor(
|
|
@InjectRepository(Building)
|
|
private readonly buildingRepository: Repository<Building>,
|
|
@InjectRepository(Floor)
|
|
private readonly floorRepository: Repository<Floor>,
|
|
@InjectRepository(Room)
|
|
private readonly roomRepository: Repository<Room>,
|
|
private readonly groupService: GroupService,
|
|
) {}
|
|
|
|
async getStorageBuilding(storageId: number) {
|
|
return this.buildingRepository.findOne({
|
|
where: {
|
|
floors: {
|
|
rooms: {
|
|
storages: {
|
|
id: storageId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async getBuildingsByUserSub(sub: string, relations = []) {
|
|
return this.buildingRepository.find({
|
|
where: {
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getBuildingByIdAndUserSub(id: number, sub: string, relations = []) {
|
|
return this.buildingRepository.findOne({
|
|
where: {
|
|
id,
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getFloorByBuildingAndUserSub(
|
|
buildingId: number,
|
|
floorNumber: number,
|
|
sub: string,
|
|
relations = [],
|
|
) {
|
|
return this.floorRepository.findOne({
|
|
where: {
|
|
number: floorNumber,
|
|
building: {
|
|
id: buildingId,
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getRoomByBuildingAndUserSub(
|
|
buildingId: number,
|
|
roomId: number,
|
|
sub: string,
|
|
relations = [],
|
|
) {
|
|
return this.roomRepository.findOne({
|
|
where: {
|
|
id: roomId,
|
|
building: {
|
|
id: buildingId,
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getRoomByIdAndUserSub(roomId: number, sub: string, relations = []) {
|
|
return this.roomRepository.findOne({
|
|
where: {
|
|
id: roomId,
|
|
building: {
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getRoomByBuilding(buildingId: number, roomId: number, relations = []) {
|
|
return this.roomRepository.findOne({
|
|
where: {
|
|
id: roomId,
|
|
building: {
|
|
id: buildingId,
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getRoomsOnFloorByBuildingAndUserSub(
|
|
buildingId: number,
|
|
floorNumber: number,
|
|
sub: string,
|
|
relations = [],
|
|
) {
|
|
return this.roomRepository.find({
|
|
where: {
|
|
floor: {
|
|
number: floorNumber,
|
|
building: {
|
|
id: buildingId,
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getRoomsByBuildingAndUserSub(
|
|
buildingId: number,
|
|
sub: string,
|
|
relations = [],
|
|
) {
|
|
return this.roomRepository.find({
|
|
where: {
|
|
floor: {
|
|
building: {
|
|
id: buildingId,
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getFloorsByBuildingAndUserSub(
|
|
buildingId: number,
|
|
sub: string,
|
|
relations = [],
|
|
) {
|
|
return this.floorRepository.find({
|
|
where: {
|
|
building: {
|
|
id: buildingId,
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async saveBuilding(data: Partial<Building>) {
|
|
const newBuilding = new Building();
|
|
Object.assign(newBuilding, data);
|
|
return this.buildingRepository.save(newBuilding);
|
|
}
|
|
|
|
async saveFloor(data: Partial<Floor>) {
|
|
const newFloor = new Floor();
|
|
Object.assign(newFloor, data);
|
|
return this.floorRepository.save(newFloor);
|
|
}
|
|
|
|
async saveRoom(data: Partial<Room>) {
|
|
const newRoom = new Room();
|
|
Object.assign(newRoom, data);
|
|
return this.roomRepository.save(newRoom);
|
|
}
|
|
|
|
async deleteRoom(room: Room) {
|
|
return this.roomRepository.remove(room);
|
|
}
|
|
}
|