INFRA-2911 | updated error messages for product APIs (#376)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user