-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductService.go
44 lines (32 loc) · 1019 Bytes
/
ProductService.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
package services
import (
"github.com/noting59/Golang-Solid-Implementation/DTO"
"github.com/noting59/Golang-Solid-Implementation/interfaces"
"github.com/noting59/Golang-Solid-Implementation/models"
)
type ProductService struct {
interfaces.IProductRepository
interfaces.ICartRepository
}
func (service *ProductService) GetProduct(userId int, productId int) (DTO.ProductDTO, error) {
product, err := service.IProductRepository.GetById(productId)
if err != nil {
return DTO.ProductDTO{}, err
}
productDTO := DTO.ProductDTO{Name:product.Name, Id:product.Id, Price:product.Price, InCart:false}
cart, err := service.ICartRepository.CartByUserId(userId)
if err != nil {
return DTO.ProductDTO{}, err
}
if cart.ProductId == product.Id {
productDTO.InCart = true
}
return productDTO, nil
}
func (service *ProductService) GetById(id int) (models.ProductModel, error) {
res, err := service.IProductRepository.GetById(id)
if err != nil {
return models.ProductModel{}, err
}
return res, nil
}