File size: 1,364 Bytes
97dab2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Shipper } from '@bpm/data/models/shipper.entity';
import { CreateShipperDto } from '@bpm/data/dto/shipper/create-shipper.dto';
import { UpdateShipperDto } from '@bpm/data/dto/shipper/update-shipper.dto';

@Injectable()
export class ShipperService {
  constructor(
    @InjectRepository(Shipper)
    private readonly shipperRepo: Repository<Shipper>,
  ) {}

  async findAll(): Promise<Shipper[]> {
    return this.shipperRepo.find();
  }

  async findOne(id: number): Promise<Shipper> {
    const shipper = await this.shipperRepo.findOne({
      where: { ShipperID: id },
    });
    if (!shipper) {
      throw new NotFoundException(`Shipper with ID ${id} not found`);
    }
    return shipper;
  }

  async create(dto: CreateShipperDto): Promise<Shipper> {
    const shipper = this.shipperRepo.create(dto);
    return this.shipperRepo.save(shipper);
  }

  async update(id: number, dto: UpdateShipperDto): Promise<Shipper> {
    const shipper = await this.findOne(id);
    const updated = { ...shipper, ...dto };
    return this.shipperRepo.save(updated);
  }

  async remove(id: number): Promise<void> {
    const shipper = await this.findOne(id);
    await this.shipperRepo.remove(shipper);
  }
}