-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
icons_requests_gorm_repository.go
74 lines (55 loc) · 1.66 KB
/
icons_requests_gorm_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
package adapters
import (
"errors"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"github.com/twofas/2fas-server/internal/api/icons/domain"
"github.com/twofas/2fas-server/internal/common/db"
)
type IconRequestCouldNotBeFound struct {
IconRequestId string
}
func (e IconRequestCouldNotBeFound) Error() string {
return fmt.Sprintf("Icon request could not be found: %s", e.IconRequestId)
}
type IconRequestMysqlRepository struct {
db *gorm.DB
}
func NewIconRequestMysqlRepository(db *gorm.DB) *IconRequestMysqlRepository {
return &IconRequestMysqlRepository{db: db}
}
func (r *IconRequestMysqlRepository) Save(iconRequest *domain.IconRequest) error {
if err := r.db.Create(iconRequest).Error; err != nil {
return err
}
return nil
}
func (r *IconRequestMysqlRepository) Update(iconRequest *domain.IconRequest) error {
if err := r.db.Updates(iconRequest).Error; err != nil {
return err
}
return nil
}
func (r *IconRequestMysqlRepository) Delete(iconRequest *domain.IconRequest) error {
if err := r.db.Delete(iconRequest).Error; err != nil {
return db.WrapError(err)
}
return nil
}
func (r *IconRequestMysqlRepository) FindById(id uuid.UUID) (*domain.IconRequest, error) {
iconRequest := &domain.IconRequest{}
result := r.db.First(&iconRequest, "id = ?", id.String())
if err := result.Error; err != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, IconRequestCouldNotBeFound{IconRequestId: id.String()}
}
return nil, db.WrapError(err)
}
return iconRequest, nil
}
func (r *IconRequestMysqlRepository) FindAll() []*domain.IconRequest {
var iconsRequests []*domain.IconRequest
r.db.Find(&iconsRequests)
return iconsRequests
}