/
blog_entry_gen.go
174 lines (152 loc) · 4.66 KB
/
blog_entry_gen.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package service
// Automatically generated by genmodel utility. DO NOT EDIT!
import (
"context"
"database/sql"
"sync"
"time"
"github.com/builderscon/octav/octav/cache"
"github.com/builderscon/octav/octav/db"
"github.com/builderscon/octav/octav/internal/errors"
"github.com/builderscon/octav/octav/model"
"github.com/lestrrat/go-pdebug"
)
var _ = time.Time{}
var _ = cache.WithExpires(time.Minute)
var _ = context.Background
var _ = errors.Wrap
var _ = model.BlogEntry{}
var _ = db.BlogEntry{}
var _ = sql.ErrNoRows
var _ = pdebug.Enabled
var blogEntrySvc BlogEntrySvc
var blogEntryOnce sync.Once
func BlogEntry() *BlogEntrySvc {
blogEntryOnce.Do(blogEntrySvc.Init)
return &blogEntrySvc
}
func (v *BlogEntrySvc) LookupFromPayload(ctx context.Context, tx *sql.Tx, m *model.BlogEntry, payload *model.LookupBlogEntryRequest) (err error) {
if pdebug.Enabled {
g := pdebug.Marker("service.BlogEntry.LookupFromPayload %s", payload.ID).BindError(&err)
defer g.End()
}
if err = v.Lookup(ctx, tx, m, payload.ID); err != nil {
return errors.Wrap(err, "failed to load model.BlogEntry from database")
}
return nil
}
func (v *BlogEntrySvc) Lookup(ctx context.Context, tx *sql.Tx, m *model.BlogEntry, id string) (err error) {
if pdebug.Enabled {
g := pdebug.Marker("service.BlogEntry.Lookup %s", id).BindError(&err)
defer g.End()
}
var r model.BlogEntry
c := Cache()
key := c.Key("BlogEntry", id)
var cacheMiss bool
_, err = c.GetOrSet(key, &r, func() (interface{}, error) {
if pdebug.Enabled {
cacheMiss = true
}
if err := r.Load(tx, id); err != nil {
return nil, errors.Wrap(err, "failed to load model.BlogEntry from database")
}
return &r, nil
}, cache.WithExpires(time.Hour))
if pdebug.Enabled {
cacheSt := `HIT`
if cacheMiss {
cacheSt = `MISS`
}
pdebug.Printf(`CACHE %s: %s`, cacheSt, key)
}
*m = r
return nil
}
// Create takes in the transaction, the incoming payload, and a reference to
// a database row. The database row is initialized/populated so that the
// caller can use it afterwards.
func (v *BlogEntrySvc) Create(ctx context.Context, tx *sql.Tx, vdb *db.BlogEntry, payload *model.CreateBlogEntryRequest) (err error) {
if pdebug.Enabled {
g := pdebug.Marker("service.BlogEntry.Create").BindError(&err)
defer g.End()
}
if err := v.populateRowForCreate(ctx, vdb, payload); err != nil {
return errors.Wrap(err, `failed to populate row`)
}
if err := vdb.Create(tx, payload.DatabaseOptions...); err != nil {
return errors.Wrap(err, `failed to insert into database`)
}
if err := v.PostCreateHook(ctx, tx, vdb); err != nil {
return errors.Wrap(err, `post create hook failed`)
}
return nil
}
func (v *BlogEntrySvc) Update(tx *sql.Tx, vdb *db.BlogEntry) (err error) {
if pdebug.Enabled {
g := pdebug.Marker("service.BlogEntry.Update (%s)", vdb.EID).BindError(&err)
defer g.End()
}
if vdb.EID == `` {
return errors.New("vdb.EID is required (did you forget to call vdb.Load(tx) before hand?)")
}
if err := vdb.Update(tx); err != nil {
return errors.Wrap(err, `failed to update database`)
}
c := Cache()
key := c.Key("BlogEntry", vdb.EID)
if pdebug.Enabled {
pdebug.Printf(`CACHE DEL %s`, key)
}
cerr := c.Delete(key)
if pdebug.Enabled {
if cerr != nil {
pdebug.Printf(`CACHE ERR: %s`, cerr)
}
}
if err := v.PostUpdateHook(tx, vdb); err != nil {
return errors.Wrap(err, `post update hook failed`)
}
return nil
}
func (v *BlogEntrySvc) UpdateFromPayload(ctx context.Context, tx *sql.Tx, payload *model.UpdateBlogEntryRequest) (err error) {
if pdebug.Enabled {
g := pdebug.Marker("service.BlogEntry.UpdateFromPayload (%s)", payload.ID).BindError(&err)
defer g.End()
}
var vdb db.BlogEntry
if err := vdb.LoadByEID(tx, payload.ID); err != nil {
return errors.Wrap(err, `failed to load from database`)
}
if err := v.populateRowForUpdate(ctx, &vdb, payload); err != nil {
return errors.Wrap(err, `failed to populate row data`)
}
if err := v.Update(tx, &vdb); err != nil {
return errors.Wrap(err, `failed to update row in database`)
}
return nil
}
func (v *BlogEntrySvc) Delete(tx *sql.Tx, id string) error {
if pdebug.Enabled {
g := pdebug.Marker("BlogEntry.Delete (%s)", id)
defer g.End()
}
original := db.BlogEntry{EID: id}
if err := original.LoadByEID(tx, id); err != nil {
return errors.Wrap(err, `failed load before delete`)
}
vdb := db.BlogEntry{EID: id}
if err := vdb.Delete(tx); err != nil {
return errors.Wrap(err, `failed to delete from database`)
}
c := Cache()
key := c.Key("BlogEntry", id)
c.Delete(key)
if pdebug.Enabled {
pdebug.Printf(`CACHE DEL %s`, key)
}
if err := v.PostDeleteHook(tx, &original); err != nil {
return errors.Wrap(err, `post delete hook failed`)
}
return nil
}