All files / src/projects projects.service.ts

52% Statements 13/25
0% Branches 0/1
16.66% Functions 1/6
47.82% Lines 11/23

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 516x 6x 6x 6x 6x 6x     6x     6x                         6x                 6x                       6x          
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from '../core-entities';
import { CreateProjectDto, UpdateProjectDto } from './dto/project.dto';
import { Trace } from '../utils';
 
@Injectable()
export class ProjectsService {
  constructor(
    @InjectRepository(Project)
    private projectsRepository: Repository<Project>,
  ) {}
 
  async create(createProjectDto: CreateProjectDto): Promise<Project> {
    const project = this.projectsRepository.create(createProjectDto);
    return this.projectsRepository.save(project);
  }
 
  async findAll(): Promise<Project[]> {
    return this.projectsRepository.find();
  }
 
  @Trace()
  async findOne(id: string): Promise<Project> {
    const project = await this.projectsRepository.findOneBy({ id });
    Iif (!project) {
      throw new NotFoundException(`Project with ID "${id}" not found`);
    }
    return project;
  }
 
  @Trace()
  async update(
    id: string,
    updateProjectDto: UpdateProjectDto,
  ): Promise<Project> {
    const project = await this.findOne(id); // findOne will throw NotFoundException if not found
    // Merge the existing entity with the new data.
    // This approach ensures that only provided fields are updated and @UpdateDateColumn works as expected.
    this.projectsRepository.merge(project, updateProjectDto);
    return this.projectsRepository.save(project);
  }
 
  @Trace()
  async remove(id: string): Promise<void> {
    const project = await this.findOne(id); // findOne will throw NotFoundException if not found
    await this.projectsRepository.remove(project);
  }
}