import { Injectable } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import { IProjectRepository } from 'src/Domain/Project/Repository/IProjectRepository' ; import { Project } from 'src/Domain/Project/Project.entity' ; import { MAX_ITEMS_PER_PAGE } from 'src/Application/Common/Pagination' ; @ Injectable ( ) export class ProjectRepository implements IProjectRepository { constructor ( @ InjectRepository ( Project ) private readonly repository ) { } public save ( project ) { return this . repository . save ( project ) ; } public findOneByName ( name ) { return this . repository . createQueryBuilder ( 'project' ) . where ( 'LOWER(project.name) = LOWER(:name)' , { name } ) . getOne ( ) ; } public findOneById ( id ) { return this . repository . createQueryBuilder ( 'project' ) . select ( [ 'project.id' , 'project.name' , 'project.invoiceUnit' , 'project.dayDuration' , 'customer.id' , 'customer.name' ] ) . innerJoin ( 'project.customer' , 'customer' ) . where ( 'project.id = :id' , { id } ) . getOne ( ) ; } public findProjects ( page = 1 , customerId ? ) { const query = this . repository . createQueryBuilder ( 'project' ) . select ( [ 'project.id' , 'project.name' , 'project.dayDuration' , 'project.invoiceUnit' , 'customer.id' , 'customer.name' ] ) . innerJoin ( 'project.customer' , 'customer' ) . orderBy ( 'project.name' , 'ASC' ) . addOrderBy ( 'customer.name' , 'ASC' ) . limit ( MAX_ITEMS_PER_PAGE ) . offset ( ( page - 1 ) * MAX_ITEMS_PER_PAGE ) ; if ( customerId ) { query . andWhere ( 'customer.id = :customerId' , { customerId } ) ; } return query . getManyAndCount ( ) ; } }