-
Notifications
You must be signed in to change notification settings - Fork 5
/
category_test.go
157 lines (137 loc) · 2.86 KB
/
category_test.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package goerd_test
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/covrom/goerd"
"github.com/covrom/goerd/schema"
"github.com/google/uuid"
)
type Category struct {
ID uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt sql.NullTime
ParentID *uuid.UUID
Name string
IsDisabled bool
}
type Categorys struct {
sc *schema.Schema
t *schema.Table
cols []string
fields func(Category) []interface{}
pfields func(*Category) []interface{}
}
func NewCategorys(sc *schema.Schema) (*Categorys, error) {
tname := "categories"
t, err := sc.FindTableByName(tname)
if err != nil {
return nil, err
}
cs := &Categorys{
sc: sc,
t: t,
cols: []string{
"id",
"created_at",
"updated_at",
"deleted_at",
"parent_id",
"name",
"is_disabled",
},
fields: func(p Category) []interface{} {
return []interface{}{
p.ID,
p.CreatedAt,
p.UpdatedAt,
p.DeletedAt,
p.ParentID,
p.Name,
p.IsDisabled,
}
},
pfields: func(p *Category) []interface{} {
return []interface{}{
&p.ID,
&p.CreatedAt,
&p.UpdatedAt,
&p.DeletedAt,
&p.ParentID,
&p.Name,
&p.IsDisabled,
}
},
}
for _, c := range cs.cols {
if _, err := t.FindColumnByName(c); err != nil {
return nil, err
}
}
return cs, nil
}
func (d *Categorys) Table() string {
return d.t.Name
}
func (d *Categorys) Columns() []string {
return d.cols
}
// nolint hugeParam
func (d *Categorys) Fields(p Category) []interface{} {
return d.fields(p)
}
func (d *Categorys) PFields(p *Category) []interface{} {
return d.pfields(p)
}
func (d *Categorys) ScanFields(p *Category) []interface{} {
return []interface{}{
&p.ID,
&p.CreatedAt,
&p.UpdatedAt,
&p.DeletedAt,
&p.ParentID,
&p.Name,
&p.IsDisabled,
}
}
// nolint hugeParam
func (d *Categorys) CategoryToStore(ctx context.Context, p Category) error {
return goerd.WithTx(ctx, func(ctxTx context.Context) error {
q := goerd.ReplaceQuery(d, "id")
p.CreatedAt = time.Now()
p.UpdatedAt = time.Now()
_, err := goerd.SqlxTxFromContext(ctxTx).
ExecContext(ctx, q, d.Fields(p)...)
return err
})
}
func (d *Categorys) ListCategoriesUpdatedFrom(ctx context.Context, updatedFrom time.Time) ([]Category, error) {
ret := make([]Category, 0, 100)
err := goerd.WithTx(ctx, func(ctxTx context.Context) error {
q := `select %s from %s where $1 OR updated_at >= $2`
rows, err := goerd.SqlxTxFromContext(ctxTx).
QueryxContext(ctx, fmt.Sprintf(q,
strings.Join(d.Columns(), ","),
d.Table()), updatedFrom.IsZero(), updatedFrom)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
p := &Category{}
scanFields := d.ScanFields(p)
if err := rows.Scan(scanFields...); err != nil {
return err
}
ret = append(ret, *p)
}
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}