-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten.go
119 lines (94 loc) · 2.87 KB
/
shorten.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
package shorten
import (
"context"
"fmt"
"time"
"github.com/pavelmemory/jobtome/internal"
"github.com/pavelmemory/jobtome/internal/storage"
)
type Entity struct {
ID int64
URL string
Hash string
CreatedAt time.Time
}
type Repo struct{}
func (p Repo) Persist(ctx context.Context, run storage.Runner, entry Entity) (int64, error) {
const query = `
INSERT INTO shorten(id, url, hash, created_at)
VALUES (NULL, $1, $2, $3)`
res := run.Exec(ctx, query, entry.URL, entry.Hash, time.Now().Unix())
if err := storage.ConvertError(res.Err()); err != nil {
return 0, fmt.Errorf("exec: %w", err)
}
return res.ID(), nil
}
func (p Repo) Retrieve(ctx context.Context, run storage.Runner, id int64) (Entity, error) {
const query = `
SELECT url, hash, created_at
FROM shorten
WHERE id = $1`
entity := Entity{ID: id}
var createdAt int64
res := run.QuerySingle(ctx, query, id)
if err := storage.ConvertError(res.Scan(&entity.URL, &entity.Hash, &createdAt)); err != nil {
return Entity{}, fmt.Errorf("retrieve single: %w", err)
}
entity.CreatedAt = time.Unix(createdAt, 0)
return entity, nil
}
// Pager is a lightweight paging abstraction that we could afford to use only with small databases like SQLLite.
type Pager struct {
Limit int64
Offset int64
}
func (Repo) List(ctx context.Context, run storage.Runner, pager Pager) ([]Entity, error) {
const query = `
SELECT id, url, hash, created_at
FROM shorten
ORDER BY id
LIMIT $1
OFFSET $2`
var entities []Entity
res, err := run.Query(ctx, query, pager.Limit, pager.Offset)
if err := storage.ConvertError(err); err != nil {
return nil, fmt.Errorf("retrieve multiple: %w", err)
}
defer res.Close() // TODO: proper handling of closing error
for res.Next() {
var entity Entity
var createdAt int64
err := res.Scan(&entity.ID, &entity.URL, &entity.Hash, &createdAt)
if err := storage.ConvertError(err); err != nil {
return nil, fmt.Errorf("scan retrieved: %w", err)
}
entity.CreatedAt = time.Unix(createdAt, 0)
entities = append(entities, entity)
}
return entities, nil
}
func (Repo) Delete(ctx context.Context, run storage.Runner, id int64) error {
const query = `DELETE FROM shorten WHERE id = $1`
res := run.Exec(ctx, query, id)
if err := storage.ConvertError(res.Err()); err != nil {
return fmt.Errorf("exec delete: %w", err)
}
if res.Affected() == 1 {
return nil
}
return internal.ErrNotFound
}
func (Repo) ByHash(ctx context.Context, run storage.Runner, hash string) (Entity, error) {
const query = `
SELECT id, url, created_at
FROM shorten
WHERE hash = $1`
entity := Entity{Hash: hash}
var createdAt int64
res := run.QuerySingle(ctx, query, hash)
if err := storage.ConvertError(res.Scan(&entity.ID, &entity.URL, &createdAt)); err != nil {
return Entity{}, fmt.Errorf("retrieve single: %w", err)
}
entity.CreatedAt = time.Unix(createdAt, 0)
return entity, nil
}