/
repo.go
129 lines (118 loc) · 3.63 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package entity
import (
"database/sql"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/yulpa/yulmails/api/utils"
)
// Entity represents an entity
type Entity struct {
Id int `json:"id,omitempty"`
// Name is the entity address
Name string `json:"name"`
Created string `json:"created,omitempty"`
Description string `json:"description,omitempty"`
ConservationID int `json:"conservation_id"`
AbuseID int `json:"abuse_id"`
}
// EntityRepo is the interface to implements in order to manage
// entity address
type EntityRepo interface {
ListEntity() ([]*Entity, error)
GetEntity(id int) (*Entity, error)
DeleteEntity(id int) error
InsertEntity(*Entity) error
}
type entityRepo struct{ d *sql.DB }
// NewEntityRepo returns a struct that implements entity repo
// with a database connection
func NewEntityRepo(db *sql.DB) *entityRepo {
return &entityRepo{d: db}
}
// ListEntity will return a list of entities from the database
func (a *entityRepo) ListEntity() ([]*Entity, error) {
query := "SELECT id, name, created, description, conservation_id, abuse_id FROM entity;"
entities := make([]*Entity, 0)
res, err := a.d.Query(query)
if err != nil {
return entities, errors.Wrapf(err, "unable to query db: %s", query)
}
defer res.Close()
for res.Next() {
var (
id int
name string
created string
description string
conservationID int
abuseID int
)
if err := res.Scan(&id, &name, &created, &description, &conservationID, &abuseID); err != nil {
return entities, errors.Wrap(err, "unable to extract result")
}
entities = append(entities, &Entity{
Id: id,
Name: name,
Created: created,
Description: description,
ConservationID: conservationID,
AbuseID: abuseID,
})
}
return entities, nil
}
// GetEntity returns an entity selected from the DB with its ID
func (a *entityRepo) GetEntity(id int) (*Entity, error) {
query := fmt.Sprintf("SELECT name, created, description, conservation_id, abuse_id FROM entity WHERE id = %d", id)
var (
name string
created string
description string
conservationID int
abuseID int
)
err := a.d.QueryRow(query).Scan(&name, &created, &description, &conservationID, &abuseID)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, errors.Wrapf(err, "unable to select entity adress: %d", id)
default:
return &Entity{
Name: name,
Created: created,
Id: id,
Description: description,
ConservationID: conservationID,
AbuseID: abuseID,
}, nil
}
}
// DeleteEntity removes an entity selected from the DB with its ID
func (a *entityRepo) DeleteEntity(id int) error {
// first we assert that the record exists
ab, err := a.GetEntity(id)
if err != nil {
return errors.Wrapf(err, "unable to fetch entity: %d", id)
}
if ab == nil {
return utils.NotFound
}
query := fmt.Sprintf("DELETE FROM entity WHERE id = %d", id)
if _, err := a.d.Exec(query); err != nil {
return errors.Wrapf(err, "unable to delete entity: %d", id)
}
return nil
}
// InsertEntity creates and returns an entity
func (a *entityRepo) InsertEntity(ab *Entity) error {
ab.Created = time.Now().Format(time.RFC3339)
query := fmt.Sprintf("INSERT INTO entity(created, name, description, conservation_id, abuse_id) VALUES( '%s', '%s', '%s', %d, %d) RETURNING id;", ab.Created, ab.Name, ab.Description, ab.ConservationID, ab.AbuseID)
var id int
if err := a.d.QueryRow(query).Scan(&id); err != nil {
return errors.Wrapf(err, "unable to insert entity adress: %d", id)
}
ab.Id = id
return nil
}