Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions backend/src/base/location/location-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { LocationEntity } from './location.entity';
import { TreeRepository } from 'typeorm';
import { SelectQueryBuilder, TreeRepository } from 'typeorm';
import { DeepPartial } from 'typeorm/common/DeepPartial';

@Injectable()
Expand All @@ -11,22 +11,40 @@ export class LocationDbService {
private readonly repo: TreeRepository<LocationEntity>,
) {}

public async getCount() {
return this.repo.count();
private searchQueryBuilder(
query: SelectQueryBuilder<LocationEntity>,
searchTerm: string,
): SelectQueryBuilder<LocationEntity> {
return query.where('l.name ilike :searchTerm', {
searchTerm: `%${searchTerm}%`,
});
}

public async getCount(searchTerm?: string) {
let query = this.repo.createQueryBuilder('l');
if (searchTerm) {
query = this.searchQueryBuilder(query, searchTerm);
}
return query.getCount();
}

public async findAll(
offset?: number,
limit?: number,
sortCol?: string,
sortDir?: 'ASC' | 'DESC',
searchTerm?: string,
) {
let query = this.repo
.createQueryBuilder('l')
.leftJoinAndSelect('l.parent', 'p')
.limit(limit ?? 100)
.offset(offset ?? 0);

if (searchTerm) {
query = this.searchQueryBuilder(query, searchTerm);
}

if (sortCol) {
if (sortCol.startsWith('parent.')) {
query = query.orderBy(
Expand Down
7 changes: 5 additions & 2 deletions backend/src/base/location/location.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { LocationGetQueryDto } from './dto/location-get-query.dto';
import { LocationCreateDto } from './dto/location-create.dto';
import { LocationUpdateDto } from './dto/location-update.dto';
import { PaginationDto } from '../../shared/dto/pagination.dto';
import { SearchDto } from '../../shared/dto/search.dto';

@Controller('location')
export class LocationController {
Expand All @@ -23,8 +24,8 @@ export class LocationController {
responseType: CountDto,
roles: [Role.LocationView],
})
public getCount(): Promise<CountDto> {
return this.service.getCount();
public getCount(@Query() search: SearchDto): Promise<CountDto> {
return this.service.getCount(search.searchTerm);
}

@Endpoint(EndpointType.GET, {
Expand All @@ -36,12 +37,14 @@ export class LocationController {
public async getAll(
@Query() pagination: PaginationDto,
@Query() querys: LocationGetQueryDto,
@Query() search: SearchDto,
): Promise<LocationDto[]> {
return this.service.findAll(
pagination.offset,
pagination.limit,
querys.sortCol,
querys.sortDir,
search.searchTerm,
);
}

Expand Down
6 changes: 4 additions & 2 deletions backend/src/base/location/location.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export class LocationService {
limit?: number,
sortCol?: string,
sortDir?: 'ASC' | 'DESC',
searchTerm?: string,
) {
const locations = await this.dbService.findAll(
offset,
limit,
sortCol,
sortDir,
searchTerm,
);
return plainToInstance(LocationDto, locations);
}
Expand All @@ -44,8 +46,8 @@ export class LocationService {
}
}

public async getCount() {
const count = await this.dbService.getCount();
public async getCount(searchTerm?: string) {
const count = await this.dbService.getCount(searchTerm);
return plainToInstance(CountDto, { count });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ describe('DeviceGroupDbService', () => {
expect(repoMock.createQueryBuilder).toHaveBeenCalled();
expect(repoMock.createQueryBuilder().limit).toHaveBeenCalledWith(10);
expect(repoMock.createQueryBuilder().offset).toHaveBeenCalledWith(10);
expect(repoMock.createQueryBuilder().orderBy).toHaveBeenCalledWith('dg.name', 'ASC');
expect(repoMock.createQueryBuilder().orderBy).toHaveBeenCalledWith(
'dg.name',
'ASC',
);
expect(repoMock.createQueryBuilder().getMany).toHaveBeenCalled();
});
});
Expand Down
24 changes: 21 additions & 3 deletions backend/src/inventory/device-group/device-group-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { DeepPartial } from 'typeorm/common/DeepPartial';
import { DeviceGroupEntity } from './device-group.entity';

Expand All @@ -11,21 +11,39 @@ export class DeviceGroupDbService {
private readonly repo: Repository<DeviceGroupEntity>,
) {}

public async getCount() {
return this.repo.count();
private searchQueryBuilder(
query: SelectQueryBuilder<DeviceGroupEntity>,
searchTerm: string,
): SelectQueryBuilder<DeviceGroupEntity> {
return query.where('dg.name ilike :searchTerm', {
searchTerm: `%${searchTerm}%`,
});
}

public async getCount(searchTerm?: string) {
let query = this.repo.createQueryBuilder('dg');
if (searchTerm) {
query = this.searchQueryBuilder(query, searchTerm);
}
return query.getCount();
}

public async findAll(
offset?: number,
limit?: number,
sortCol?: string,
sortDir?: 'ASC' | 'DESC',
searchTerm?: string,
) {
let query = this.repo
.createQueryBuilder('dg')
.limit(limit ?? 100)
.offset(offset ?? 0);

if (searchTerm) {
query = this.searchQueryBuilder(query, searchTerm);
}

if (sortCol) {
query = query.orderBy(`dg.${sortCol}`, sortDir ?? 'ASC');
} else {
Expand Down
7 changes: 5 additions & 2 deletions backend/src/inventory/device-group/device-group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DeviceGroupUpdateDto } from './dto/device-group-update.dto';
import { DeviceGroupCreateDto } from './dto/device-group-create.dto';
import { DeviceGroupGetQueryDto } from './dto/device-group-get-query.dto';
import { PaginationDto } from '../../shared/dto/pagination.dto';
import { SearchDto } from '../../shared/dto/search.dto';

@Controller('device-group')
export class DeviceGroupController {
Expand All @@ -23,8 +24,8 @@ export class DeviceGroupController {
responseType: CountDto,
roles: [Role.DeviceTypeView],
})
public getCount(): Promise<CountDto> {
return this.service.getCount();
public getCount(@Query() search: SearchDto): Promise<CountDto> {
return this.service.getCount(search.searchTerm);
}

@Endpoint(EndpointType.GET, {
Expand All @@ -36,12 +37,14 @@ export class DeviceGroupController {
public async getAll(
@Query() pagination: PaginationDto,
@Query() querys: DeviceGroupGetQueryDto,
@Query() search: SearchDto,
): Promise<DeviceGroupDto[]> {
return this.service.findAll(
pagination.offset,
pagination.limit,
querys.sortCol,
querys.sortDir,
search.searchTerm,
);
}

Expand Down
6 changes: 4 additions & 2 deletions backend/src/inventory/device-group/device-group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export class DeviceGroupService {
limit?: number,
sortCol?: string,
sortDir?: 'ASC' | 'DESC',
searchTerm?: string,
) {
const entities = await this.dbService.findAll(
offset,
limit,
sortCol,
sortDir,
searchTerm,
);
return plainToInstance(DeviceGroupDto, entities);
}
Expand All @@ -43,8 +45,8 @@ export class DeviceGroupService {
}
}

public async getCount() {
const count = await this.dbService.getCount();
public async getCount(searchTerm?: string) {
const count = await this.dbService.getCount(searchTerm);
return plainToInstance(CountDto, { count });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ describe('DeviceTypeDbService', () => {
expect(repoMock.createQueryBuilder).toHaveBeenCalled();
expect(repoMock.createQueryBuilder().limit).toHaveBeenCalledWith(10);
expect(repoMock.createQueryBuilder().offset).toHaveBeenCalledWith(10);
expect(repoMock.createQueryBuilder().orderBy).toHaveBeenCalledWith('dt.name', 'ASC');
expect(repoMock.createQueryBuilder().orderBy).toHaveBeenCalledWith(
'dt.name',
'ASC',
);
expect(repoMock.createQueryBuilder().getMany).toHaveBeenCalled();
});
});
Expand Down
24 changes: 21 additions & 3 deletions backend/src/inventory/device-type/device-type-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { DeviceTypeEntity } from './device-type.entity';
import { DeepPartial } from 'typeorm/common/DeepPartial';

Expand All @@ -11,21 +11,39 @@ export class DeviceTypeDbService {
private readonly repo: Repository<DeviceTypeEntity>,
) {}

public async getCount() {
return this.repo.count();
private searchQueryBuilder(
query: SelectQueryBuilder<DeviceTypeEntity>,
searchTerm: string,
): SelectQueryBuilder<DeviceTypeEntity> {
return query.where('dt.name ilike :searchTerm', {
searchTerm: `%${searchTerm}%`,
});
}

public async getCount(searchTerm?: string) {
let query = this.repo.createQueryBuilder('dt');
if (searchTerm) {
query = this.searchQueryBuilder(query, searchTerm);
}
return query.getCount();
}

public async findAll(
offset?: number,
limit?: number,
sortCol?: string,
sortDir?: 'ASC' | 'DESC',
searchTerm?: string,
) {
let query = this.repo
.createQueryBuilder('dt')
.limit(limit ?? 100)
.offset(offset ?? 0);

if (searchTerm) {
query = this.searchQueryBuilder(query, searchTerm);
}

if (sortCol) {
query = query.orderBy(`dt.${sortCol}`, sortDir ?? 'ASC');
} else {
Expand Down
7 changes: 5 additions & 2 deletions backend/src/inventory/device-type/device-type.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DeviceTypeUpdateDto } from './dto/device-type-update.dto';
import { DeviceTypeGetQueryDto } from './dto/device-type-get-query.dto';
import { CountDto } from '../../shared/dto/count.dto';
import { PaginationDto } from '../../shared/dto/pagination.dto';
import { SearchDto } from '../../shared/dto/search.dto';

@Controller('device-type')
export class DeviceTypeController {
Expand All @@ -23,8 +24,8 @@ export class DeviceTypeController {
responseType: CountDto,
roles: [Role.DeviceTypeView],
})
public getCount(): Promise<CountDto> {
return this.service.getCount();
public getCount(@Query() search: SearchDto): Promise<CountDto> {
return this.service.getCount(search.searchTerm);
}

@Endpoint(EndpointType.GET, {
Expand All @@ -36,12 +37,14 @@ export class DeviceTypeController {
public async getAll(
@Query() pagination: PaginationDto,
@Query() querys: DeviceTypeGetQueryDto,
@Query() search: SearchDto,
): Promise<DeviceTypeDto[]> {
return this.service.findAll(
pagination.offset,
pagination.limit,
querys.sortCol,
querys.sortDir,
search.searchTerm,
);
}

Expand Down
6 changes: 4 additions & 2 deletions backend/src/inventory/device-type/device-type.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export class DeviceTypeService {
limit?: number,
sortCol?: string,
sortDir?: 'ASC' | 'DESC',
searchTerm?: string,
) {
const entities = await this.dbService.findAll(
offset,
limit,
sortCol,
sortDir,
searchTerm,
);
return plainToInstance(DeviceTypeDto, entities);
}
Expand All @@ -43,8 +45,8 @@ export class DeviceTypeService {
}
}

public async getCount() {
const count = await this.dbService.getCount();
public async getCount(searchTerm?: string) {
const count = await this.dbService.getCount(searchTerm);
return plainToInstance(CountDto, { count });
}

Expand Down
Loading