All files / place place.query.repository.ts

100% Statements 59/59
100% Branches 25/25
100% Functions 15/15
100% Lines 57/57

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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 21112x 12x 12x       12x         12x   12x     29x       1x   1x         1x 1x 1x       1x           3x           3x 1x       3x 1x   2x   3x   3x 3x 3x 3x       3x       3x           3x 1x       3x 2x   1x   3x   3x 3x 3x 3x       3x       1x           1x           2x 2x           2x   2x             1x                           1x             1x 1x       1x       1x                           1x             1x 1x         1x       1x   1x           1x                     1x          
import { InjectRepository } from '@nestjs/typeorm';
import { PLACE_TYPE } from 'src/commons/enum/place-type-enum';
import { CursorPaginationHelper } from 'src/commons/helpers/cursor.helper';
import { ApiCourseGetPlaceCustomizeRequestQueryDto } from 'src/course/dto/api-course-get-place-customize-request-query.dto';
import { ApiCourseGetRecommendRequestQueryDto } from 'src/course/dto/api-course-get-recommend-request-query.dto';
import { ApiCoursePostRecommendRequestBodyDto } from 'src/course/dto/api-course-post-recommend-request-body.dto';
import { PlaceEntity } from 'src/entities/place.entity';
import { ApiPlaceGetCultureRequestQueryDto } from 'src/place/dto/api-place-get-culture-request-query.dto';
import { ApiPlaceGetExhibitionRequestQueryDto } from 'src/place/dto/api-place-get-exhibition-request-query.dto';
import { ApiPlaceGetPopupRequestQueryDto } from 'src/place/dto/api-place-get-popup-request-query.dto';
import { ApiSearchGetRequestQueryDto } from 'src/search/dto/api-search-get-request-query.dto';
import { Brackets, FindOptionsOrder, In, LessThan, Like, MoreThan, Repository } from 'typeorm';
 
export class PlaceQueryRepository {
  constructor(
    @InjectRepository(PlaceEntity)
    private repository: Repository<PlaceEntity>,
  ) {}
 
  async findList(dto: ApiPlaceGetCultureRequestQueryDto): Promise<PlaceEntity[]> {
    const now = new Date();
 
    const q = await this.repository
      .createQueryBuilder('place')
      .select('place')
      .where('place.place_type IN (:...types)', { types: ['전시', '팝업'] })
      .andWhere('place.end_date > :now', { now });
    q.orderBy('start_date', 'DESC');
    q.limit(dto.size);
    return q.getMany();
  }
 
  async findOne(uuid: string): Promise<PlaceEntity> {
    return this.repository.findOne({
      where: { uuid },
    });
  }
 
  async findExhibitionList(dto: ApiPlaceGetExhibitionRequestQueryDto) {
    const qb = this.repository
      .createQueryBuilder('place')
      .where('place.place_type = :type', { type: '전시' })
      .andWhere('place.end_date > NOW()');
 
    // 커서 적용
    if (dto.next_page) {
      CursorPaginationHelper.applyCursor(qb, dto.order, dto.next_page);
    }
 
    // 정렬 & 페이징
    if (dto.order === 'latest') {
      qb.orderBy('place.start_date', 'DESC').addOrderBy('place.id', 'DESC');
    } else {
      qb.orderBy('place.end_date', 'ASC').addOrderBy('place.id', 'ASC');
    }
    qb.take(dto.size + 1);
 
    const results = await qb.getMany();
    const hasNext = results.length > dto.size;
    const pageItems = hasNext ? results.slice(0, dto.size) : results;
    const nextCursor = hasNext
      ? CursorPaginationHelper.generateCursor(pageItems[pageItems.length - 1], dto.order)
      : null;
 
    return { items: pageItems, nextCursor };
  }
 
  async findPopupList(dto: ApiPlaceGetPopupRequestQueryDto) {
    const qb = this.repository
      .createQueryBuilder('place')
      .where('place.place_type = :type', { type: '팝업' })
      .andWhere('place.end_date > NOW()');
 
    // 커서 적용
    if (dto.next_page) {
      CursorPaginationHelper.applyCursor(qb, dto.order, dto.next_page);
    }
 
    // 정렬 & 페이징
    if (dto.order === 'latest') {
      qb.orderBy('place.start_date', 'DESC').addOrderBy('place.id', 'DESC');
    } else {
      qb.orderBy('place.end_date', 'ASC').addOrderBy('place.id', 'ASC');
    }
    qb.take(dto.size + 1);
 
    const results = await qb.getMany();
    const hasNext = results.length > dto.size;
    const pageItems = hasNext ? results.slice(0, dto.size) : results;
    const nextCursor = hasNext
      ? CursorPaginationHelper.generateCursor(pageItems[pageItems.length - 1], dto.order)
      : null;
 
    return { items: pageItems, nextCursor };
  }
 
  async countPopup(): Promise<number> {
    return this.repository.count({
      where: { place_type: '팝업', end_date: MoreThan(new Date()) },
    });
  }
 
  async countExhibition(): Promise<number> {
    return this.repository.count({
      where: { place_type: '전시', end_date: MoreThan(new Date()) },
    });
  }
 
  async getSearchPlace(dto: ApiSearchGetRequestQueryDto): Promise<PlaceEntity[]> {
    const { search, last_id: lastId, size, place_type: placeType } = dto;
    const where = {
      place_name: Like(`%${search}%`),
      ...(lastId > 0 ? { id: LessThan(lastId) } : {}),
      place_type: In(placeType === 'culture' ? ['전시회', '팝업'] : ['음식점', '카페', '술집']),
    };
    const order = (
      placeType === 'culture' ? { start_date: 'DESC' } : { review_count: 'DESC' }
    ) as FindOptionsOrder<PlaceEntity>;
    return this.repository.find({ where, order, take: size });
  }
 
  async old_findSubwayPlaceList(
    customs,
    dto: ApiCoursePostRecommendRequestBodyDto,
  ): Promise<PlaceEntity[]> {
    return this.repository
      .createQueryBuilder('p')
      .innerJoinAndSelect('p.subways', 's')
      .where('s.line = :line', { line: dto.line })
      .andWhere('s.name = :name', { name: dto.subway })
      .andWhere('s.place_type IN (:...types)', { types: customs })
      .andWhere('s.kakao_rating = :rating', { rating: 1 })
      .getMany();
  }
 
  async findSubwayPlaceList(
    dto: ApiCourseGetRecommendRequestQueryDto,
    subwayStationName: string,
  ): Promise<PlaceEntity[]> {
    const qb = this.repository
      .createQueryBuilder('p')
      .innerJoinAndSelect('p.subways', 's')
      .andWhere('s.name = :name', { name: subwayStationName })
      .andWhere('s.place_type IN (:...types)', { types: ['음식점', '카페', '술집'] })
      .andWhere('s.kakao_rating = :rating', { rating: 1 });
 
    if (dto.theme_uuid) {
      qb.innerJoinAndSelect('p.placeThemes', 'pt').andWhere('pt.theme_uuid = :theme_uuid', {
        theme_uuid: dto.theme_uuid,
      });
    }
    return qb.getMany();
  }
 
  async findSubwayCultureList(dto: ApiCoursePostRecommendRequestBodyDto): Promise<PlaceEntity[]> {
    return this.repository
      .createQueryBuilder('p')
      .innerJoinAndSelect('p.subways', 's')
      .where('s.line = :line', { line: dto.line })
      .andWhere('s.name = :name', { name: dto.subway })
      .andWhere('s.place_type IN (:...types)', { types: ['전시', '팝업'] })
      .andWhere('p.end_date > :now', { now: new Date() })
      .getMany();
  }
 
  async findSubwayPlacesCustomizeList(
    dto: ApiCourseGetPlaceCustomizeRequestQueryDto,
    subwayStationName: string,
  ): Promise<PlaceEntity[]> {
    const qb = this.repository
      .createQueryBuilder('p')
      .innerJoinAndSelect('p.subways', 's')
      .andWhere('s.name = :name', { name: subwayStationName })
      .andWhere('s.place_type = :type', { type: PLACE_TYPE[dto.place_type] })
      .andWhere('s.kakao_rating = :rating', { rating: 1 });
 
    if (dto.theme_uuid && dto.place_type !== 'SHOPPING' && dto.place_type !== 'ENTERTAINMENT') {
      qb.innerJoinAndSelect('p.placeThemes', 'pt').andWhere('pt.theme_uuid = :theme_uuid', {
        theme_uuid: dto.theme_uuid,
      });
    }
 
    return qb.getMany();
  }
 
  async findSubwayPlacesCustomizeCultureList(subwayStationName: string): Promise<PlaceEntity[]> {
    const now = new Date();
 
    return this.repository
      .createQueryBuilder('p')
      .innerJoinAndSelect('p.subways', 's')
      .where('s.name = :name', { name: subwayStationName })
      .andWhere(
        new Brackets((qb) => {
          qb.where('s.place_type = :type1', { type1: '전시' }).orWhere('s.place_type = :type2', {
            type2: '팝업',
          });
        }),
      )
      .andWhere('p.end_date > :now', { now })
      .andWhere('s.kakao_rating >= :rating', { rating: 1 })
      .getMany();
  }
 
  async findPlacesWithUuids(uuids: string[]): Promise<PlaceEntity[]> {
    return this.repository.find({
      where: { uuid: In(uuids) },
    });
  }
}