-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepository.go
100 lines (87 loc) · 2.56 KB
/
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
package main
import (
"github.com/jinzhu/gorm"
errs "github.com/pkg/errors"
)
// FruitRepository interface represents the repository methods
type FruitRepository interface {
ListFruits() ([]Fruit, error)
ShowFruit(id int) (*Fruit, error)
CreateFruit(fruit Fruit) (*Fruit, error)
UpdateFruit(id int, fruit Fruit) (*Fruit, error)
DeleteFruit(id int) (*Fruit, error)
}
// DB wraps the gorm.DB struct
type DB struct {
*gorm.DB
}
// Fruit represents the Fruit table in database
type Fruit struct {
ID int `gorm:"AUTO_INCREMENT"`
Name string
Stock int
}
// NewFruitsRepository returns a new DB object after establishing connection to
// the database
func NewFruitsRepository(database string, connectionString string) (*DB, error) {
db, err := gorm.Open(database, connectionString)
if err != nil {
return nil, err
}
// Ensure database is reachable
if err = db.DB().Ping(); err != nil {
return nil, err
}
return &DB{db}, nil
}
// ListFruits returns the list of fruits in the database.
func (db *DB) ListFruits() ([]Fruit, error) {
var fruits []Fruit
db.Find(&fruits)
if db.Error != nil {
return nil, db.Error
}
return fruits, nil
}
// ShowFruit returns the Fruit object with ID 'id' in the database.
func (db *DB) ShowFruit(id int) (*Fruit, error) {
var fruit Fruit
db.First(&fruit, id)
if db.Error != nil {
return nil, db.Error
}
return &fruit, nil
}
// CreateFruit creates a new Fruit object with the given name and stock in the
// database.
func (db *DB) CreateFruit(fruit Fruit) (*Fruit, error) {
if err := db.Create(&fruit).Error; err != nil {
return nil, errs.Wrap(err, "failed to create")
}
return &fruit, nil
}
// UpdateFruit updates the fruit object represented by the 'id'. Returns the
// updated fruit object, or error on failure
func (db *DB) UpdateFruit(id int, fruit Fruit) (*Fruit, error) {
var newFruit Fruit
if err := db.First(&newFruit, id).Error; err != nil {
return nil, errs.Wrapf(err, "failed to find object with ID: %d", id)
}
newFruit.Name = fruit.Name
newFruit.Stock = fruit.Stock
if err := db.Save(newFruit).Error; err != nil {
return nil, errs.Wrap(err, "failed to save updated object")
}
return &newFruit, nil
}
// DeleteFruit soft deletes the Fruit object represented by the 'id' in the database
func (db *DB) DeleteFruit(id int) (*Fruit, error) {
var fruit Fruit
if err := db.First(&fruit, id).Error; err != nil {
return nil, errs.Wrapf(err, "failed to find fruit with ID:%d", id)
}
if err := db.Delete(&fruit).Error; err != nil {
return nil, errs.Wrapf(err, "failed to delete fruit with ID:%d", id)
}
return &fruit, nil
}