Explorar o código

Изменения manufacturer - методы полученния в контроллере и сервисе

portowyi %!s(int64=2) %!d(string=hai) anos
pai
achega
11e33552c4

+ 7 - 2
src/manufacturer/manufacturer.controller.ts

@@ -18,7 +18,12 @@ export class ManufacturerController {
   }
 
   @Get(':id')
-  findById(@Param('id') id: string): Promise<Manufacturer> {
-    return this.manufacturerService.fineById(id);
+  getById(@Param('id') id: string): Promise<Manufacturer> {
+    return this.manufacturerService.findById(id);
+  }
+
+  @Get('')
+  getAll(): Promise<Manufacturer[]> {
+    return this.manufacturerService.findAll();
   }
 }

+ 35 - 5
src/manufacturer/manufacturer.service.ts

@@ -27,10 +27,10 @@ export class ManufacturerService {
     } catch (err) {
       throw new HttpException(
         {
-          status: HttpStatus.INTERNAL_SERVER_ERROR,
-          error: 'This is a custom message',
+          status: HttpStatus.BAD_REQUEST,
+          error: err?.driverError?.detail,
         },
-        HttpStatus.FORBIDDEN,
+        HttpStatus.BAD_REQUEST,
         {
           cause: err,
         },
@@ -46,14 +46,44 @@ export class ManufacturerService {
       code,
     });
 
-    return await this.manufacturerRepository.save(manufacturer);
+    try {
+      return await this.manufacturerRepository.save(manufacturer);
+    } catch (err) {
+      throw new HttpException(
+        {
+          status: HttpStatus.BAD_REQUEST,
+          error: err?.driverError?.detail,
+        },
+        HttpStatus.BAD_REQUEST,
+        {
+          cause: err,
+        },
+      );
+    }
   }
 
-  async fineById(id: string): Promise<Manufacturer | null> {
+  async findById(id: string): Promise<Manufacturer | null> {
     return await this.manufacturerRepository.findOneOrFail({
       where: {
         id: id,
       },
     });
   }
+
+  async findAll(): Promise<Manufacturer[]> {
+    try {
+      return await this.manufacturerRepository.find();
+    } catch (err) {
+      throw new HttpException(
+        {
+          status: HttpStatus.BAD_REQUEST,
+          error: err?.driverError?.detail,
+        },
+        HttpStatus.BAD_REQUEST,
+        {
+          cause: err,
+        },
+      );
+    }
+  }
 }