-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.go
126 lines (115 loc) · 3.83 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
package conservation
import (
"database/sql"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/yulpa/yulmails/api/utils"
)
// Conservation represea conservation rule
type Conservation struct {
ID int `json:"id,omitempty"`
Created string `json:"created,omitempty"`
// Sent is the number of day to keep the email in DB
Sent int `json:"sent"`
// Unsent is the number of day to keep unsent email in DB
Unsent int `json:"unsent"`
// KeepEmailContent will keep the body in DB
KeepEmailContent bool `json:"keep_email_content"`
}
// ConservationRepo is the interface to implements in order to manage
// conservation address
type ConservationRepo interface {
ListConservation() ([]*Conservation, error)
GetConservation(id int) (*Conservation, error)
DeleteConservation(id int) error
InsertConservation(*Conservation) error
}
type conservationRepo struct{ d *sql.DB }
// NewConservationRepo returns a struct that implements conservation repo
// with a database connection
func NewConservationRepo(db *sql.DB) *conservationRepo {
return &conservationRepo{d: db}
}
// ListConservation will return a list of conservations from the database
func (a *conservationRepo) ListConservation() ([]*Conservation, error) {
query := "SELECT id, created, sent, unsent, keep_email_content from conservation;"
conservations := make([]*Conservation, 0)
res, err := a.d.Query(query)
if err != nil {
return conservations, errors.Wrapf(err, "unable to query db: %s", query)
}
defer res.Close()
for res.Next() {
var (
id int
created string
sent int
unsent int
keepEmailContent bool
)
if err := res.Scan(&id, &created, &sent, &unsent, &keepEmailContent); err != nil {
return conservations, errors.Wrap(err, "unable to extract result")
}
conservations = append(conservations, &Conservation{
ID: id,
Created: created,
Sent: sent,
Unsent: unsent,
KeepEmailContent: keepEmailContent,
})
}
return conservations, nil
}
// GetConservation returns an entity selected from the DB with its ID
func (a *conservationRepo) GetConservation(id int) (*Conservation, error) {
query := fmt.Sprintf("SELECT created, sent, unsent, keep_email_content FROM conservation WHERE id = %d", id)
var (
created string
sent int
unsent int
keepEmailContent bool
)
err := a.d.QueryRow(query).Scan(&created, &sent, &unsent, &keepEmailContent)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, errors.Wrapf(err, "unable to select conservation rule: %d", id)
default:
return &Conservation{
Created: created,
ID: id,
Sent: sent,
Unsent: unsent,
KeepEmailContent: keepEmailContent,
}, nil
}
}
// DeleteConservation removes an entity selected from the DB with its ID
func (a *conservationRepo) DeleteConservation(id int) error {
// first we assert that the record exists
ab, err := a.GetConservation(id)
if err != nil {
return errors.Wrapf(err, "unable to fetch conservation rule: %d", id)
}
if ab == nil {
return utils.NotFound
}
query := fmt.Sprintf("DELETE FROM conservation WHERE id = %d", id)
if _, err := a.d.Exec(query); err != nil {
return errors.Wrapf(err, "unable to delete conservation: %d", id)
}
return nil
}
// InsertConservation creates and returns an entity
func (a *conservationRepo) InsertConservation(ab *Conservation) error {
ab.Created = time.Now().Format(time.RFC3339)
query := fmt.Sprintf("INSERT INTO conservation(created, sent, unsent, keep_email_content) VALUES( '%s', %d, %d, %t) RETURNING id;", ab.Created, ab.Sent, ab.Unsent, ab.KeepEmailContent)
var id int
if err := a.d.QueryRow(query).Scan(&id); err != nil {
return errors.Wrapf(err, "unable to insert conservation rule: %d", id)
}
ab.ID = id
return nil
}