-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
53 lines (43 loc) · 1.58 KB
/
service.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
52
53
package product
import "golang-restful-api/helper"
type Service interface {
GetProductById(param *getProductByIDRequest) (*Product, error)
GetProducts(params *getProductsRequest) (*ProductList, error)
InsertProduct(params *getAddProductRequest) (int64, error)
UpdateProduct(params *updateProductRequest) (int64, error)
DeleteProduct(params *deleteProductRequest) (int64, error)
GetBestSellers() (*ProductTopResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) GetProductById(param *getProductByIDRequest) (*Product, error) {
// Business Logic
return s.repo.GetProductById(param.ProductID)
}
func (s *service) GetProducts(params *getProductsRequest) (*ProductList, error) {
products, err := s.repo.GetProducts(params)
helper.Catch(err)
totalProducts, err := s.repo.GetTotalProducts()
helper.Catch(err)
return &ProductList{Data: products, TotalRecords: totalProducts}, nil
}
func (s *service) InsertProduct(params *getAddProductRequest) (int64, error) {
return s.repo.InsertProduct(params)
}
func (s *service) UpdateProduct(params *updateProductRequest) (int64, error) {
return s.repo.UpdateProduct(params)
}
func (s *service) DeleteProduct(params *deleteProductRequest) (int64, error) {
return s.repo.DeleteProduct(params)
}
func (s *service) GetBestSellers() (*ProductTopResponse, error) {
products, err := s.repo.GetBestSellers()
helper.Catch(err)
totalVentas, err := s.repo.GetTotalVentas()
helper.Catch(err)
return &ProductTopResponse{Data: products, TotalVentas: totalVentas}, nil
}