All files / subway subway.query.repository.ts

100% Statements 22/22
100% Branches 0/0
100% Functions 10/10
100% Lines 20/20

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 1069x 9x 9x 9x   9x   9x     20x   20x   20x       1x                         1x   1x                                   1x             1x           1x                       1x       1x       1x           1x           1x                  
import { InjectRepository } from '@nestjs/typeorm';
import { SubwayEntity } from 'src/entities/subway.entity';
import { SubwayLineEntity } from 'src/entities/subway_line.entity';
import { SubwayStationEntity } from 'src/entities/subway_station.entity';
import { StationInfo } from 'src/subway/interfaces/subway.interfaces';
import { Repository } from 'typeorm';
 
export class SubwayQueryRepository {
  constructor(
    @InjectRepository(SubwayEntity)
    private repository: Repository<SubwayEntity>,
    @InjectRepository(SubwayStationEntity)
    private subwayStationRepository: Repository<SubwayStationEntity>,
    @InjectRepository(SubwayLineEntity)
    private subwayLineRepository: Repository<SubwayLineEntity>,
  ) {}
 
  async groupByCustoms(stationInfo: StationInfo) {
    return this.repository
      .createQueryBuilder('subway')
      .select('subway.place_type', 'type')
      .addSelect('COUNT(subway.id)', 'count')
      .where('subway.name = :name AND subway.line = :line', {
        name: stationInfo.station,
        line: stationInfo.line,
      })
      .groupBy('subway.place_type')
      .getRawMany();
  }
 
  async findSubwayCurrentCulture(stationInfo: StationInfo) {
    const now = new Date();
 
    return this.repository
      .createQueryBuilder('subway')
      .leftJoinAndSelect('place', 'p', 'p.uuid = subway.place_uuid')
      .select('subway.place_type', 'type')
      .addSelect('COUNT(subway.id)', 'count')
      .where('subway.name = :name AND subway.line = :line', {
        name: stationInfo.station,
        line: stationInfo.line,
      })
      .andWhere('subway.place_type IN (:...types)', {
        types: ['팝업', '전시'],
      })
      .andWhere('p.end_date > :now', { now })
      .groupBy('subway.place_type')
      .getRawMany();
  }
 
  async subwayStationList(lineUuid: string): Promise<SubwayStationEntity[]> {
    return this.subwayStationRepository.find({
      where: { line_uuid: lineUuid },
      order: { id: 'ASC' },
    });
  }
 
  async findSubwayLine(): Promise<SubwayLineEntity[]> {
    return this.subwayLineRepository.find({
      order: { id: 'ASC' },
    });
  }
 
  async findSubway(subway: string): Promise<SubwayStationEntity[]> {
    return this.subwayStationRepository.find({
      where: { name: subway },
    });
  }
 
  async findLineAndStation(
    lineUuid: string,
    stationUuid: string,
  ): Promise<{
    line: string;
    station: string;
  }> {
    const result = await this.subwayStationRepository.findOne({
      where: { uuid: stationUuid, line_uuid: lineUuid },
    });
 
    return { line: result.line, station: result.name };
  }
 
  async findSubwayStationUuid(subwayUuid: string): Promise<SubwayStationEntity> {
    return this.subwayStationRepository.findOne({
      where: { uuid: subwayUuid },
    });
  }
 
  async findSubwayStationName(subwayName: string): Promise<SubwayStationEntity> {
    return this.subwayStationRepository.findOne({
      where: { name: subwayName },
    });
  }
 
  async findAllLinesForStation(subwayUuid: string): Promise<SubwayStationEntity[]> {
    return this.subwayStationRepository
      .createQueryBuilder('subway')
      .where('subway.uuid = :subwayUuid', { subwayUuid })
      .orWhere('subway.name = (SELECT name FROM subway_station WHERE uuid = :subwayUuid)', {
        subwayUuid,
      })
      .getMany();
  }
}