-
Notifications
You must be signed in to change notification settings - Fork 4
/
createProduct.go
51 lines (44 loc) · 1.32 KB
/
createProduct.go
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
46
47
48
49
50
51
package service
import (
"log"
"github.com/sebajax/go-vertical-slice-architecture/internal/product"
"github.com/sebajax/go-vertical-slice-architecture/pkg/apperror"
)
// Product use cases (port injection)
type CreateProductService struct {
productRepository product.ProductRepository
}
// Create a new product service use case instance
func NewCreateProductService(repository product.ProductRepository) *CreateProductService {
// return the pointer to product service
return &CreateProductService{
productRepository: repository,
}
}
// Create a new product and store the product into the database
func (service *CreateProductService) CreateProduct(p *product.Product) (int64, error) {
_, check, err := service.productRepository.GetBySku(p.Sku)
// check if product sky does not exist and no database error ocurred
if err != nil {
// database error
log.Fatalln(err)
err := apperror.InternalServerError()
return 0, err
}
if check {
// product sku found
log.Println(p, product.ErrorSkuExists)
err := apperror.BadRequest(product.ErrorSkuExists)
return 0, err
}
// create the new product and return the id
id, err := service.productRepository.Save(p)
if err != nil {
// database error
log.Fatalln(err)
err := apperror.InternalServerError()
return 0, err
}
// product created successfuly
return id, nil
}