/
package_repo.go
98 lines (74 loc) · 2.23 KB
/
package_repo.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2023-present, Ukama Inc.
*/
package db
import (
"github.com/ukama/ukama/systems/common/sql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
uuid "github.com/ukama/ukama/systems/common/uuid"
)
type PackageRepo interface {
Add(_package *Package) error
Get(uuid uuid.UUID) (*Package, error)
GetDetails(uuid.UUID) (*Package, error)
Delete(uuid uuid.UUID) error
GetByOrg(orgId uuid.UUID) ([]Package, error)
Update(uuid uuid.UUID, pkg *Package) error
}
type packageRepo struct {
Db sql.Db
}
func NewPackageRepo(db sql.Db) *packageRepo {
return &packageRepo{
Db: db,
}
}
func (r *packageRepo) Add(_package *Package) error {
result := r.Db.GetGormDb().Create(_package)
return result.Error
}
func (p *packageRepo) Get(uuid uuid.UUID) (*Package, error) {
var _package Package
result := p.Db.GetGormDb().Preload("PackageRate").Where("uuid = ?", uuid).First(&_package)
if result.Error != nil {
return nil, result.Error
}
return &_package, nil
}
func (p *packageRepo) GetDetails(uuid uuid.UUID) (*Package, error) {
var _package Package
result := p.Db.GetGormDb().Preload(clause.Associations).Where("uuid = ?", uuid).First(&_package)
if result.Error != nil {
return nil, result.Error
}
return &_package, nil
}
func (p *packageRepo) GetByOrg(orgId uuid.UUID) ([]Package, error) {
var packages []Package
result := p.Db.GetGormDb().Preload("PackageRate").Where(&Package{OrgId: orgId}).Find(&packages)
if result.Error != nil {
return nil, result.Error
}
return packages, nil
}
func (r *packageRepo) Delete(uuid uuid.UUID) error {
p := &Package{}
result := r.Db.GetGormDb().Model(&Package{}).Where("uuid=?", uuid).Delete(p)
if result.Error != nil {
return result.Error
}
return nil
}
func (b *packageRepo) Update(uuid uuid.UUID, pkg *Package) error {
d := b.Db.GetGormDb().Clauses(clause.Returning{}).Where("uuid = ?", uuid).Updates(pkg)
if d.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
//https://stackoverflow.com/questions/65683156/updates-doesnt-seem-to-update-the-associations
return d.Error
}