|
|
@@ -0,0 +1,59 @@
|
|
|
+import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
|
+import { InjectRepository } from '@nestjs/typeorm';
|
|
|
+import { Manufacturer } from './manufacturer.entity';
|
|
|
+import { ManufacturerRepository } from './manufacturer.repository';
|
|
|
+import { ManufacturerDto } from './dto/manufacturer.dto';
|
|
|
+
|
|
|
+@Injectable()
|
|
|
+export class ManufacturerService {
|
|
|
+ constructor(
|
|
|
+ @InjectRepository(Manufacturer)
|
|
|
+ private readonly manufacturerRepository: ManufacturerRepository,
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ async create(manufacturerDto: ManufacturerDto[]): Promise<void> {
|
|
|
+ const manufacturerArray: Manufacturer[] = manufacturerDto.map(
|
|
|
+ ({ id, name, code }) => {
|
|
|
+ return this.manufacturerRepository.create({
|
|
|
+ id,
|
|
|
+ name,
|
|
|
+ code,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ try {
|
|
|
+ await this.manufacturerRepository.save(manufacturerArray);
|
|
|
+ } catch (err) {
|
|
|
+ throw new HttpException(
|
|
|
+ {
|
|
|
+ status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
+ error: 'This is a custom message',
|
|
|
+ },
|
|
|
+ HttpStatus.FORBIDDEN,
|
|
|
+ {
|
|
|
+ cause: err,
|
|
|
+ },
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async createOne(manufacturerDto: ManufacturerDto): Promise<Manufacturer> {
|
|
|
+ const { id, name, code } = { ...manufacturerDto };
|
|
|
+ const manufacturer: Manufacturer = this.manufacturerRepository.create({
|
|
|
+ id,
|
|
|
+ name,
|
|
|
+ code,
|
|
|
+ });
|
|
|
+
|
|
|
+ return await this.manufacturerRepository.save(manufacturer);
|
|
|
+ }
|
|
|
+
|
|
|
+ async fineById(id: string): Promise<Manufacturer | null> {
|
|
|
+ return await this.manufacturerRepository.findOneOrFail({
|
|
|
+ where: {
|
|
|
+ id: id,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|