All files / course course.query.repository.ts

100% Statements 20/20
100% Branches 2/2
100% Functions 9/9
100% Lines 18/18

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 8011x   11x 11x   11x   11x     20x   20x       1x       1x       2x 2x   2x                   5x           7x                 1x           9x                 4x         4x              
import { InjectRepository } from '@nestjs/typeorm';
import { ApiCourseGetMyHistoryRequestQueryDto } from 'src/course/dto/api-course-get-my-history-request-query.dto';
import { CourseDetailEntity } from 'src/entities/course.detail.entity';
import { CourseEntity } from 'src/entities/course.entity';
import { UserDto } from 'src/user/dto/user.dto';
import { In, LessThan, Repository } from 'typeorm';
 
export class CourseQueryRepository {
  constructor(
    @InjectRepository(CourseEntity)
    private repository: Repository<CourseEntity>,
    @InjectRepository(CourseDetailEntity)
    private detailRepository: Repository<CourseDetailEntity>,
  ) {}
 
  async saveCourse(courseEntity: CourseEntity): Promise<CourseEntity> {
    return this.repository.save(courseEntity);
  }
 
  async saveCourseDetail(courseDetailEntity: CourseDetailEntity): Promise<CourseDetailEntity> {
    return this.detailRepository.save(courseDetailEntity);
  }
 
  async findUserHistoryCourse(uuid: string): Promise<CourseDetailEntity[]> {
    const sevenDaysAgo = new Date();
    sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
 
    return this.detailRepository
      .createQueryBuilder('courseDetail')
      .innerJoin('courseDetail.course', 'course')
      .where('course.user_uuid = :uuid', { uuid })
      .andWhere('course.created_at >= :sevenDaysAgo', { sevenDaysAgo })
      .select('courseDetail.place_uuid')
      .getMany();
  }
 
  async findCourse(uuid: string): Promise<CourseEntity> {
    return this.repository.findOne({
      where: { uuid },
    });
  }
 
  async findPlace(courseUuid: string): Promise<CourseDetailEntity[]> {
    return this.detailRepository
      .createQueryBuilder('courseDetail')
      .leftJoinAndSelect('courseDetail.place', 'place')
      .where('courseDetail.course_uuid = :courseUuid', { courseUuid })
      .select(['courseDetail', 'place'])
      .getMany();
  }
 
  async findList(uuids: string[]): Promise<CourseEntity[]> {
    return this.repository.find({
      where: { uuid: In(uuids) },
    });
  }
 
  async findOne(uuid: string): Promise<CourseEntity> {
    return this.repository.findOne({
      where: { uuid },
    });
  }
 
  async findMyCourse(
    dto: ApiCourseGetMyHistoryRequestQueryDto,
    user: UserDto,
  ): Promise<CourseEntity[]> {
    const whereConditions = {
      user_uuid: user.uuid,
      ...(dto.last_id > 0 ? { id: LessThan(dto.last_id) } : {}),
    };
 
    return this.repository.find({
      where: whereConditions,
      order: { created_at: 'DESC' },
      take: dto.size,
    });
  }
}