diff --git a/cmd/app/server.go b/cmd/app/server.go index 6c711fe..f00f008 100644 --- a/cmd/app/server.go +++ b/cmd/app/server.go @@ -146,7 +146,7 @@ func (s *Server) incidentClientHandler(houstonGroup *gin.RouterGroup) { func (s *Server) productsHandler(houstonGroup *gin.RouterGroup) { productsHandler := handler.NewProductHandler(s.gin) - houstonGroup.POST("/products", s.authService.IfAdmin(productsHandler.HandleCreateProduct)) + houstonGroup.POST("/product", s.authService.IfAdmin(productsHandler.HandleCreateProduct)) houstonGroup.PUT("/product/:id", s.authService.IfAdmin(productsHandler.HandleUpdateProduct)) houstonGroup.DELETE("/product/:id", s.authService.IfAdmin(productsHandler.HandleDeleteProductByID)) houstonGroup.GET("/products", productsHandler.HandleGetAllProducts) diff --git a/service/products/product_service_impl.go b/service/products/product_service_impl.go index 52657d0..6a72859 100644 --- a/service/products/product_service_impl.go +++ b/service/products/product_service_impl.go @@ -2,6 +2,9 @@ package products import ( "errors" + "fmt" + "gorm.io/gorm" + "houston/logger" productModel "houston/model/product" ) @@ -9,12 +12,15 @@ type productServiceImpl struct { repo productModel.ProductRepository } +var processingError = errors.New("error while processing the request") + func (service *productServiceImpl) CreateProduct(productName string) (uint, error) { - existingProducts, err := service.repo.GetProductByName(productName) - if err != nil { - return 0, errors.New("error while processing the request") + existingProduct, err := service.repo.GetProductByName(productName) + if err != nil && !errors.Is(gorm.ErrRecordNotFound, err) { + logger.Error(fmt.Sprintf("error while processing the create product request. %+v", err)) + return 0, processingError } - if existingProducts != nil { + if existingProduct != nil { return 0, errors.New("product already exists") } return service.repo.InsertProduct(productName) @@ -23,7 +29,12 @@ func (service *productServiceImpl) CreateProduct(productName string) (uint, erro func (service *productServiceImpl) UpdateProduct(productDTO *productModel.ProductDTO) error { entity, err := service.repo.GetProductById(productDTO.ProductID) if err != nil { - return err + if errors.Is(gorm.ErrRecordNotFound, err) { + return fmt.Errorf("no product found with ID %d", productDTO.ProductID) + } else { + logger.Error(fmt.Sprintf("error while processing the update product request. %+v", err)) + return processingError + } } entity.Name = productDTO.ProductName return service.repo.UpdateProduct(entity) @@ -64,6 +75,15 @@ func (service *productServiceImpl) GetProductByName(productName string) (*produc }, nil } func (service *productServiceImpl) DeleteProductByID(productID uint) error { + _, err := service.repo.GetProductById(productID) + if err != nil { + if errors.Is(gorm.ErrRecordNotFound, err) { + return fmt.Errorf("no product found with ID %d", productID) + } else { + logger.Error(fmt.Sprintf("error while processing the delete product request. %+v", err)) + return processingError + } + } return service.repo.DeleteProductByID(productID) } func (service *productServiceImpl) DeleteProductByName(productName string) error {