All files / apps/properties-service/src/properties properties.service.ts

100% Statements 19/19
100% Branches 8/8
100% Functions 4/4
100% Lines 16/16

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 443x 3x 3x       3x     3x     6x       1x       2x   2x 2x   2x             2x       2x 2x 1x   1x      
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { FindOptionsWhere, Repository } from 'typeorm';
import { CreatePropertyDto } from './dto/create-property.dto';
import { PaginatedResult } from './dto/paginated-result';
import { QueryPropertiesDto } from './dto/query-properties.dto';
import { Property } from './property.entity';
 
@Injectable()
export class PropertiesService {
  constructor(
    @InjectRepository(Property)
    private readonly repository: Repository<Property>,
  ) {}
 
  create(dto: CreatePropertyDto): Promise<Property> {
    return this.repository.save(this.repository.create(dto));
  }
 
  async findAll(query: QueryPropertiesDto): Promise<PaginatedResult<Property>> {
    const { page, limit, city } = query;
 
    const where: FindOptionsWhere<Property> = {};
    if (city) where.city = city;
 
    const [data, total] = await this.repository.findAndCount({
      where,
      order: { createdAt: 'DESC' },
      skip: (page - 1) * limit,
      take: limit,
    });
 
    return { data, meta: { page, limit, total } };
  }
 
  async findOne(id: string): Promise<Property> {
    const property = await this.repository.findOneBy({ id });
    if (!property) {
      throw new NotFoundException(`property ${id} not found`);
    }
    return property;
  }
}