-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.go
114 lines (103 loc) · 2.76 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
package abuse
import (
"database/sql"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/yulpa/yulmails/api/utils"
)
// Abuse represents an abuse address
type Abuse struct {
Id int `json:"id,omitempty"`
// Name is the abuse address
Name string `json:"name"`
Created string `json:"created,omitempty"`
}
// AbuseRepo is the interface to implements in order to manage
// abuse address
type AbuseRepo interface {
ListAbuse() ([]*Abuse, error)
GetAbuse(id int) (*Abuse, error)
DeleteAbuse(id int) error
InsertAbuse(*Abuse) error
}
type abuseRepo struct{ d *sql.DB }
// NewAbuseRepo returns a struct that implements abuse repo
// with a database connection
func NewAbuseRepo(db *sql.DB) *abuseRepo {
return &abuseRepo{d: db}
}
// ListAbuse will return a list of abuses from the database
func (a *abuseRepo) ListAbuse() ([]*Abuse, error) {
query := "SELECT * from abuse;"
abuses := make([]*Abuse, 0)
res, err := a.d.Query(query)
if err != nil {
return abuses, errors.Wrapf(err, "unable to query db: %s", query)
}
defer res.Close()
for res.Next() {
var (
id int
name string
created string
)
if err := res.Scan(&id, &name, &created); err != nil {
return abuses, errors.Wrap(err, "unable to extract result")
}
abuses = append(abuses, &Abuse{
Id: id,
Name: name,
Created: created,
})
}
return abuses, nil
}
// GetAbuse returns an entity selected from the DB with its ID
func (a *abuseRepo) GetAbuse(id int) (*Abuse, error) {
query := fmt.Sprintf("SELECT name, created FROM abuse WHERE id = %d", id)
var (
name string
created string
)
err := a.d.QueryRow(query).Scan(&name, &created)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, errors.Wrapf(err, "unable to select abuse adress: %d", id)
default:
return &Abuse{
Name: name,
Created: created,
Id: id,
}, nil
}
}
// DeleteAbuse removes an entity selected from the DB with its ID
func (a *abuseRepo) DeleteAbuse(id int) error {
// first we assert that the record exists
ab, err := a.GetAbuse(id)
if err != nil {
return errors.Wrapf(err, "unable to fetch abuse address: %d", id)
}
if ab == nil {
return utils.NotFound
}
query := fmt.Sprintf("DELETE FROM abuse WHERE id = %d", id)
if _, err := a.d.Exec(query); err != nil {
return errors.Wrapf(err, "unable to delete abuse: %d", id)
}
return nil
}
// InsertAbuse creates and returns an entity
func (a *abuseRepo) InsertAbuse(ab *Abuse) error {
ab.Created = time.Now().Format(time.RFC3339)
query := fmt.Sprintf("INSERT INTO abuse(created, name) VALUES( '%s', '%s' ) RETURNING id;", ab.Created, ab.Name)
var id int
if err := a.d.QueryRow(query).Scan(&id); err != nil {
return errors.Wrapf(err, "unable to insert abuse adress: %d", id)
}
ab.Id = id
return nil
}