-
Notifications
You must be signed in to change notification settings - Fork 2
/
repository.go
92 lines (71 loc) · 2.06 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
package apiv1
import (
"context"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/soldatov-s/go-garage/providers/pq"
"github.com/soldatov-s/go-garage/x/sqlx/miniorm"
"github.com/soldatov-s/go-garage/x/stringsx"
)
const productionTestTable = "production.test"
type RepositoryDeps struct {
Conn *pq.Enity
}
type Repository struct {
conn *pq.Enity
orm *miniorm.ORM
}
func NewRepository(deps *RepositoryDeps) (*Repository, error) {
r := &Repository{
conn: deps.Conn,
}
ormDeps := &miniorm.ORMDeps{
Conn: deps.Conn,
Target: productionTestTable,
Data: &Enity{},
PredefinedFields: miniorm.NewPredefinedFields(),
}
var err error
r.orm, err = miniorm.NewORM(ormDeps)
if err != nil {
return nil, errors.Wrap(err, "new miniorm")
}
return r, nil
}
func (r *Repository) GetByID(ctx context.Context, id int64) (*Enity, error) {
data := &Enity{}
if err := r.orm.SelectByID(ctx, id, data); err != nil {
return nil, errors.Wrap(err, "select by id")
}
return data, nil
}
func (r *Repository) GetByCode(ctx context.Context, code string) (*Enity, error) {
logger := zerolog.Ctx(ctx)
data := &Enity{}
if err := r.conn.GetConn().GetContext(ctx, data,
stringsx.JoinStrings(" ", "select * from", productionTestTable, "where code=$1"), code); err != nil {
return nil, errors.Wrap(err, "get from db")
}
logger.Debug().Msgf("data %+v", data)
return data, nil
}
func (r *Repository) HardDeleteByID(ctx context.Context, id int64) error {
if err := r.orm.HardDeleteByID(ctx, id); err != nil {
return errors.Wrap(err, "hard delete by id")
}
return nil
}
func (r *Repository) SoftDeleteByID(ctx context.Context, id int64) error {
if err := r.orm.SoftDeleteByID(ctx, id); err != nil {
return errors.Wrap(err, "soft delete by id")
}
return nil
}
func (r *Repository) CreateTest(ctx context.Context, data *Enity) (*Enity, error) {
data.CreatedAt = NewNullTime()
data.UpdatedAt = data.CreatedAt
if err := r.orm.InsertInto(ctx, data, data); err != nil {
return nil, errors.Wrap(err, "insert into")
}
return data, nil
}