-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
111 lines (96 loc) · 2.63 KB
/
type.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
package repository
import (
"fmt"
"github.com/alekseikovrigin/yandexbackendschool2023/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type TypeRepository interface {
Save(Type models.Type) (string, error)
Update(models.Type) error
Delete(models.Type) error
FindAll() []*models.Type
FindByID(TypeID uint) (*models.Type, error)
DeleteByID(TypeID string) error
FindByName(name string) (*models.Type, error)
FindByField(fieldName, fieldValue string) (*models.Type, error)
UpdateSingleField(Type models.Type, fieldName, fieldValue string) error
}
type TypeDatabase struct {
connection *gorm.DB
}
func NewTypeRepository() TypeRepository {
if DB == nil {
_, err = Connect()
if err != nil {
log.Error(err)
}
}
return &TypeDatabase{
connection: DB,
}
}
func (db TypeDatabase) DeleteByID(TypeID string) error {
Type := models.Type{}
Type.ID = TypeID
result := db.connection.Delete(&Type)
return result.Error
}
func (db TypeDatabase) Save(Type models.Type) (string, error) {
result := db.connection.Create(&Type)
if result.Error != nil {
return "", result.Error
}
return Type.ID, nil
}
func (db TypeDatabase) Update(Type models.Type) error {
result := db.connection.Save(&Type)
return result.Error
}
func (db TypeDatabase) Delete(Type models.Type) error {
result := db.connection.Delete(&Type)
return result.Error
}
func (db TypeDatabase) FindAll() []*models.Type {
var Types []*models.Type
db.connection.Preload(clause.Associations).Find(&Types)
return Types
}
func (db TypeDatabase) FindByID(TypeID uint) (*models.Type, error) {
var Type models.Type
result := db.connection.Preload(clause.Associations).Find(&Type, "id = ?", TypeID)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected > 0 {
return &Type, nil
}
return nil, nil
}
func (db TypeDatabase) FindByName(name string) (*models.Type, error) {
var Type models.Type
result := db.connection.Preload(clause.Associations).Find(&Type, "name = ?", name)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected > 0 {
return &Type, nil
}
return nil, nil
}
func (db TypeDatabase) FindByField(fieldName, fieldValue string) (*models.Type, error) {
var Type models.Type
result := db.connection.Preload(clause.Associations).Find(&Type, fmt.Sprintf("%s = ?", fieldName), fieldValue)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected > 0 {
return &Type, nil
}
return nil, nil
}
func (db TypeDatabase) UpdateSingleField(Type models.Type, fieldName, fieldValue string) error {
result := db.connection.Model(&Type).Update(fieldName, fieldValue)
return result.Error
}