-
Notifications
You must be signed in to change notification settings - Fork 0
/
property_type2property_view_type_repository.go
112 lines (92 loc) · 3.84 KB
/
property_type2property_view_type_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
package gorm
import (
"context"
"errors"
"gorm.io/gorm"
minipkg_gorm "github.com/minipkg/db/gorm"
"github.com/minipkg/selection_condition"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_type2property_view_type"
"github.com/yaruz/app/pkg/yarus_platform/yaruserror"
"github.com/yaruz/app/internal/pkg/apperror"
)
// PropertyType2PropertyViewTypeRepository is a repository for the model entity
type PropertyType2PropertyViewTypeRepository struct {
repository
}
var _ property_type2property_view_type.Repository = (*PropertyType2PropertyViewTypeRepository)(nil)
// New creates a new PropertyType2PropertyViewTypeRepository
func NewPropertyType2PropertyViewTypeRepository(repository *repository) (*PropertyType2PropertyViewTypeRepository, error) {
return &PropertyType2PropertyViewTypeRepository{repository: *repository}, nil
}
// Get reads the album with the specified ID from the database.
func (r *PropertyType2PropertyViewTypeRepository) Get(ctx context.Context, id uint) (*property_type2property_view_type.PropertyType2PropertyViewType, error) {
entity := &property_type2property_view_type.PropertyType2PropertyViewType{}
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 *PropertyType2PropertyViewTypeRepository) First(ctx context.Context, entity *property_type2property_view_type.PropertyType2PropertyViewType) (*property_type2property_view_type.PropertyType2PropertyViewType, 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 *PropertyType2PropertyViewTypeRepository) Query(ctx context.Context, cond *selection_condition.SelectionCondition) ([]property_type2property_view_type.PropertyType2PropertyViewType, error) {
items := []property_type2property_view_type.PropertyType2PropertyViewType{}
db := minipkg_gorm.Conditions(r.db.DB(), cond)
if db.Error != nil {
return nil, db.Error
}
err := db.Find(&items).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return items, yaruserror.ErrNotFound
}
return nil, err
}
return items, err
}
func (r *PropertyType2PropertyViewTypeRepository) 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
}
err := db.Count(&count).Error
return count, err
}
// Create saves a new record in the database.
func (r *PropertyType2PropertyViewTypeRepository) Create(ctx context.Context, entity *property_type2property_view_type.PropertyType2PropertyViewType) error {
return r.db.DB().Create(entity).Error
}
// Update saves a changed Maintenance record in the database.
func (r *PropertyType2PropertyViewTypeRepository) Update(ctx context.Context, entity *property_type2property_view_type.PropertyType2PropertyViewType) error {
return r.Save(ctx, entity)
}
// Save update value in database, if the value doesn't have primary key, will insert it
func (r *PropertyType2PropertyViewTypeRepository) Save(ctx context.Context, entity *property_type2property_view_type.PropertyType2PropertyViewType) error {
return r.db.DB().Save(entity).Error
}
// Delete (soft) deletes a Maintenance record in the database.
func (r *PropertyType2PropertyViewTypeRepository) Delete(ctx context.Context, entity *property_type2property_view_type.PropertyType2PropertyViewType) error {
err := r.db.DB().Delete(r.model, entity).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return apperror.ErrNotFound
}
}
return err
}