-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository.go
117 lines (102 loc) · 2.71 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"io"
"strconv"
"github.com/go-pg/pg/v9"
)
// Repository is the vehicle repository.
type Repository struct{ db *pg.DB }
// NewRepository creates a new Repository.
func NewRepository(options *pg.Options) *Repository {
return &Repository{db: pg.Connect(options)}
}
var _ io.Closer = (*Repository)(nil)
// Close closes this repository.
func (r *Repository) Close() error {
return r.db.Close()
}
// GetManufacturers returns all manufacturers.
func (r *Repository) GetManufacturers() ([]*Manufacturer, error) {
var entities []*Manufacturer
err := r.db.Model(&entities).Select()
if err != nil {
if err == pg.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return entities, err
}
// GetManufacturer returns the specified manufacturer.
func (r *Repository) GetManufacturer(id string) (*Manufacturer, error) {
manufacturer := new(Manufacturer)
err := r.db.Model(manufacturer).Where("id = ? ", id).First()
if err != nil {
if err == pg.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return manufacturer, nil
}
// GetVehicles returns all vehicles of the manufacturer.
func (r *Repository) GetVehicles(manufacturer *Manufacturer) ([]*Vehicle, error) {
var vehicles []*Vehicle
err := r.db.Model(&vehicles).
Column("id", "trade_name", "commercial_name", "allotment_date", "manufacturer_id").
Where("manufacturer_id = ?", manufacturer.ID).
Select()
if err != nil {
if err == pg.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return vehicles, nil
}
// GetVehicle tries to get the specified vehicle.
func (r *Repository) GetVehicle(manufacturer *Manufacturer, id string) (*Vehicle, error) {
vehicle := new(Vehicle)
err := r.db.Model(vehicle).
Relation("Manufacturer").
Relation("PowerSource").
Where("vehicle.manufacturer_id = ? AND vehicle.id = ?", manufacturer.ID, id).
First()
if err != nil {
if err == pg.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return vehicle, nil
}
// GetPowerSources gets all available power sources.
func (r *Repository) GetPowerSources() ([]*PowerSource, error) {
var entities []*PowerSource
err := r.db.Model(&entities).Select()
if err != nil {
if err == pg.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return entities, nil
}
// GetPowerSource gets the specified power source.
func (r *Repository) GetPowerSource(id string) (*PowerSource, error) {
nid, err := strconv.ParseInt(id, 10, 32)
if err != nil {
return nil, NewErrNotFoundF("id is bad '%v'", id)
}
powerSource := new(PowerSource)
err = r.db.Model(powerSource).
Where("id = ? ", nid).
First()
if err != nil {
if err == pg.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return powerSource, nil
}