/
categories.go
144 lines (120 loc) · 2.95 KB
/
categories.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package persistence
import (
"context"
"github.com/SOAT1StackGoLang/msvc-orders/internal/service/models"
kitlog "github.com/go-kit/log"
"github.com/google/uuid"
"go.uber.org/zap"
"gorm.io/gorm"
)
const categoriesTable = "lanchonete_categories"
type catPersistence struct {
db *gorm.DB
log kitlog.Logger
}
func (p *catPersistence) InsertCategory(ctx context.Context, in *models.Category) (*models.Category, error) {
var out *models.Category
cat := Category{
ID: in.ID,
CreatedAt: in.CreatedAt,
Name: in.Name,
}
if err := p.db.WithContext(ctx).Table(categoriesTable).
Create(&cat).Error; err != nil {
p.log.Log(
"db failed inserting category",
zap.Any("in_category", in),
zap.Error(err),
)
return nil, err
}
out = &models.Category{
ID: cat.ID,
CreatedAt: cat.CreatedAt,
Name: cat.Name,
}
if cat.UpdatedAt.Valid {
out.CreatedAt = cat.UpdatedAt.Time
}
return out, nil
}
func (p *catPersistence) GetCategoryByID(ctx context.Context, id uuid.UUID) (*models.Category, error) {
cat := Category{}
if err := p.db.WithContext(ctx).Table(categoriesTable).
Select("*").Where("id = ?", id).First(&cat).Error; err != nil {
_ = p.log.Log(
"db failed getting category",
zap.String("category_id", id.String()),
zap.Error(err),
)
return nil, err
}
out := &models.Category{
ID: cat.ID,
CreatedAt: cat.CreatedAt,
Name: cat.Name,
}
if cat.UpdatedAt.Valid {
out.CreatedAt = cat.UpdatedAt.Time
}
return out, nil
}
func (p *catPersistence) DeleteCategory(ctx context.Context, id uuid.UUID) error {
cat := Category{ID: id}
if err := p.db.WithContext(ctx).Table(categoriesTable).Delete(&cat).Error; err != nil {
p.log.Log(
"db failed deleting category",
zap.Any("category_id", id.String()),
zap.Error(err),
)
return err
}
return nil
}
func (p *catPersistence) ListCategories(ctx context.Context, limit int, offset int) (*models.CategoryList, error) {
var total int64
var savedCats []Category
var err error
// First, perform the count operation
if err = p.db.WithContext(ctx).Table(categoriesTable).
Count(&total).
Error; err != nil {
p.log.Log(
"failed counting categories",
zap.Error(err),
)
return nil, err
}
if err = p.db.WithContext(ctx).Table(categoriesTable).
Limit(limit).
Offset(offset).
Find(&savedCats).
Error; err != nil {
p.log.Log(
"failed listing categories",
zap.Error(err),
)
return nil, err
}
out := &models.CategoryList{}
outList := make([]*models.Category, 0)
for _, c := range savedCats {
out := &models.Category{
ID: c.ID,
CreatedAt: c.CreatedAt,
Name: c.Name,
}
if c.UpdatedAt.Valid {
out.CreatedAt = c.UpdatedAt.Time
}
outList = append(outList, out)
}
out.Categories = outList
out.Total = total
out.Limit = limit
out.Offset = offset
return out, err
}
func NewCategoriesPersistence(db *gorm.DB, log kitlog.Logger) CategoriesRepository {
return &catPersistence{db: db, log: log}
}