All files / search-sort search-sort.service.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 5/5
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69          18x 18x   18x         18x           18x   34x 34x                   8x       8x                   7x         7x 6x   1x               1x            
/**
 * Search sort service.
 * @packageDocumentation
 */
 
import { Injectable, Inject, forwardRef } from '@nestjs/common';
import { DBService } from '../db/db.service';
import { NBSearchSort } from './search-sort.interface';
import { ServiceException } from '../service.exception';
 
/**
 * Search sort table name.
 */
export const searchSortTableName = 'NB_SEARCH_SORT';
 
/**
 * Search sort table service.
 */
@Injectable()
export class SearchSortService {
  constructor(
    @Inject(forwardRef(() => DBService))
    private readonly dbService: DBService,
  ) {}
 
  /**
   * Determine whether or not a search sort option exists.
   *
   * @param sortID The sort option ID.
   * @returns Whether or not the search sort option exists.
   */
  public async sortOptionExists(sortID: number): Promise<boolean> {
    const sortOption = await this.dbService.getByID<NBSearchSort>(
      searchSortTableName,
      sortID,
    );
    return !!sortOption;
  }
 
  /**
   * Get a search sort option.
   *
   * @param sortID The sort option ID.
   * @returns The search sort option.
   */
  public async getSortOption(sortID: number): Promise<NBSearchSort> {
    const sortOption = await this.dbService.getByID<NBSearchSort>(
      searchSortTableName,
      sortID,
    );
 
    if (sortOption) {
      return sortOption;
    } else {
      throw new ServiceException('Search sort option does not exist');
    }
  }
 
  /**
   * Get all search sort options.
   */
  public async getSortOptions(): Promise<NBSearchSort[]> {
    return this.dbService.list<NBSearchSort>(searchSortTableName, {
      fieldName: 'id',
      sortOrder: 'ASC',
    });
  }
}