/
repository.go
93 lines (70 loc) · 2.08 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
package repository
import (
"context"
"database/sql"
"errors"
"fmt"
domain "github.com/TudorHulban/rest-articles/domain/article"
"github.com/TudorHulban/rest-articles/infra/db"
)
type Repository struct {
Db *sql.DB
}
func NewRepository(db *sql.DB) (*Repository, error) {
if db == nil {
return nil, errors.New("passed DB connection is nil")
}
return &Repository{
Db: db,
}, nil
}
func (repo *Repository) Create(ctx context.Context, item *domain.Article) (int64, error) {
stmt, errPrep := repo.Db.PrepareContext(ctx, `INSERT INTO articles(title, url, created_on) values(?,?,?)`)
if errPrep != nil {
return 0, db.HandleError(errPrep)
}
defer stmt.Close()
result, errDB := stmt.ExecContext(ctx, item.Title, item.URL, item.CreatedOn)
if errDB != nil {
return 0, db.HandleError(errDB)
}
return result.LastInsertId()
}
func (repo *Repository) Find(ctx context.Context, id int64) (*domain.Article, error) {
var item domain.Article
query := fmt.Sprintf("SELECT * FROM articles WHERE id = $1 AND deleted_on IS NULL")
_ = repo.Db.QueryRowContext(ctx, query, id).Scan(&item)
if item == nil {
return nil, db.ErrObjectNotFound{}
}
return &item, nil
}
func (repo *Repository) FindAll(ctx context.Context) (*domain.Articles, error) {
var items domain.Articles
query := fmt.Sprintf("SELECT * FROM articles WHERE deleted_on IS NULL")
rows, errDB := repo.Db.QueryContext(ctx, query)
if errDB != nil {
return nil, db.HandleError(errDB)
}
defer rows.Close()
for rows.Next() {
var item domain.Article
errScan := rows.Scan(&item)
if errScan != nil {
return nil, db.HandleError(errScan)
}
items = append(items, &item)
}
return &items, nil
}
func (repo *Repository) Update(ctx context.Context, item *domain.Article) error {
query := `UPDATE articles
SET title = :title,
url = :url,
created_on = :created_on,
updated_on = :updated_on,
deleted_on = :deleted_on
WHERE id = :id;`
_, errDB := repo.Db.ExecContext(ctx, query, item)
return db.HandleError(errDB)
}