-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_lang_repository.go
128 lines (104 loc) · 3.4 KB
/
text_lang_repository.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
package gorm
import (
"context"
"errors"
"gorm.io/gorm/clause"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/text_lang"
"github.com/yaruz/app/internal/pkg/apperror"
"gorm.io/gorm"
minipkg_gorm "github.com/minipkg/db/gorm"
"github.com/minipkg/selection_condition"
"github.com/yaruz/app/pkg/yarus_platform/yaruserror"
)
// TextLangRepository is a repository for the model entity
type TextLangRepository struct {
repository
}
var _ text_lang.Repository = (*TextLangRepository)(nil)
// New creates a new TextLangRepository
func NewTextLangRepository(repository *repository) (*TextLangRepository, error) {
return &TextLangRepository{repository: *repository}, nil
}
// Get reads the album with the specified ID from the database.
func (r *TextLangRepository) Get(ctx context.Context, id uint) (*text_lang.TextLang, error) {
entity := &text_lang.TextLang{}
err := r.db.DB().First(entity, id).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return entity, yaruserror.ErrNotFound
}
return nil, err
}
return entity, err
}
func (r *TextLangRepository) First(ctx context.Context, entity *text_lang.TextLang) (*text_lang.TextLang, error) {
err := r.db.DB().Where(entity).First(entity).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return entity, yaruserror.ErrNotFound
}
return nil, err
}
return entity, err
}
// Query retrieves the album records with the specified offset and limit from the database.
func (r *TextLangRepository) Query(ctx context.Context, cond *selection_condition.SelectionCondition) ([]text_lang.TextLang, error) {
items := []text_lang.TextLang{}
db := minipkg_gorm.Conditions(r.db.DB(), cond)
if db.Error != nil {
return nil, db.Error
}
err := db.Model(r.model).Find(&items).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return items, yaruserror.ErrNotFound
}
return nil, err
}
return items, err
}
func (r *TextLangRepository) Count(ctx context.Context, cond *selection_condition.SelectionCondition) (int64, error) {
var count int64
c := cond
c.Limit = 0
c.Offset = 0
db := minipkg_gorm.Conditions(r.db.DB(), cond)
if db.Error != nil {
return 0, db.Error
}
return count, db.Model(r.model).Count(&count).Error
}
// Create saves a new record in the database.
func (r *TextLangRepository) Create(ctx context.Context, entity *text_lang.TextLang) error {
if entity.ID > 0 {
return errors.New("entity is not new")
}
return r.db.DB().Create(entity).Error
}
// Update saves a changed record in the database.
func (r *TextLangRepository) Update(ctx context.Context, entity *text_lang.TextLang) error {
if entity.ID == 0 {
return errors.New("entity is new")
}
return r.Save(ctx, entity)
}
// Save update value in database, if the value doesn't have primary key, will insert it
func (r *TextLangRepository) Save(ctx context.Context, entity *text_lang.TextLang) error {
return r.db.DB().Save(entity).Error
}
func (r *TextLangRepository) Upsert(ctx context.Context, entity *text_lang.TextLang) error {
return r.db.DB().Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "code"}},
UpdateAll: true,
}).Create(entity).Error
}
// Delete (soft) deletes a record in the database.
func (r *TextLangRepository) Delete(ctx context.Context, id uint) error {
err := r.db.DB().Delete(r.model, id).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return apperror.ErrNotFound
}
}
return err
}