/
category.go
81 lines (72 loc) · 2.06 KB
/
category.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package services
import (
"github.com/gosimple/slug"
"github.com/iarsham/shop-api/internal/common"
"github.com/iarsham/shop-api/internal/db"
"github.com/iarsham/shop-api/internal/dto"
"github.com/iarsham/shop-api/internal/models"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type CategoryService struct {
db *gorm.DB
logs *common.Logger
}
func NewCategoryService(logs *common.Logger) *CategoryService {
return &CategoryService{
logs: logs,
db: db.GetDB(),
}
}
func (c *CategoryService) AllCategories() (*[]models.Category, error) {
var categories []models.Category
err := c.db.Preload("Products").Find(&categories).Error
if err != nil {
c.logs.Warn(err.Error())
return nil, err
}
return &categories, err
}
func (c *CategoryService) CreateCategory(req *dto.CategoryRequest) (*models.Category, error) {
var category models.Category
category.Title = req.Title
err := c.db.Create(&category).Error
if err != nil {
c.logs.Warn(err.Error())
return nil, err
}
return &category, nil
}
func (c *CategoryService) UpdateCategory(pk string, req *dto.CategoryRequest) (*models.Category, error) {
categoryObj, _ := c.CategoryByPK(pk)
newPK := slug.Make(req.Title)
if err := c.db.Model(categoryObj).Updates(map[string]interface{}{"title": req.Title, "slug": newPK}).Error; err != nil {
return nil, err
}
return categoryObj, nil
}
func (c *CategoryService) DeleteCategory(pk string) error {
category, _ := c.CategoryByPK(pk)
if err := c.db.Select(clause.Associations).Delete(&category).Error; err != nil {
c.logs.Warn(err.Error())
}
return nil
}
func (c *CategoryService) CategoryByPK(pk string) (*models.Category, bool) {
var category models.Category
err := c.db.Where("slug=?", pk).First(&category).Error
if err != nil {
c.logs.Warn(err.Error())
return nil, false
}
return &category, true
}
func (c *CategoryService) CategoryByTitle(title string) (*models.Category, bool) {
var category models.Category
err := c.db.Where("title=?", title).First(&category).Error
if err != nil {
c.logs.Warn(err.Error())
return nil, false
}
return &category, true
}