WIP: stuff
This commit is contained in:
162
server/internal/models/bob_main.bob.go
Normal file
162
server/internal/models/bob_main.bob.go
Normal file
@ -0,0 +1,162 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"hash/maphash"
|
||||
"strings"
|
||||
|
||||
"github.com/stephenafamo/bob"
|
||||
"github.com/stephenafamo/bob/clause"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
|
||||
sqliteDriver "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var TableNames = struct {
|
||||
Files string
|
||||
Items string
|
||||
Users string
|
||||
}{
|
||||
Files: "files",
|
||||
Items: "items",
|
||||
Users: "users",
|
||||
}
|
||||
|
||||
var ColumnNames = struct {
|
||||
Files fileColumnNames
|
||||
Items itemColumnNames
|
||||
Users userColumnNames
|
||||
}{
|
||||
Files: fileColumnNames{
|
||||
ID: "id",
|
||||
Name: "name",
|
||||
Data: "data",
|
||||
UserID: "user_id",
|
||||
},
|
||||
Items: itemColumnNames{
|
||||
ID: "id",
|
||||
Name: "name",
|
||||
Description: "description",
|
||||
Price: "price",
|
||||
Quantity: "quantity",
|
||||
Added: "added",
|
||||
UserID: "user_id",
|
||||
},
|
||||
Users: userColumnNames{
|
||||
ID: "id",
|
||||
Username: "username",
|
||||
Password: "password",
|
||||
ProfilePictureID: "profile_picture_id",
|
||||
Challenge: "challenge",
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
SelectWhere = Where[*dialect.SelectQuery]()
|
||||
InsertWhere = Where[*dialect.InsertQuery]()
|
||||
UpdateWhere = Where[*dialect.UpdateQuery]()
|
||||
DeleteWhere = Where[*dialect.DeleteQuery]()
|
||||
)
|
||||
|
||||
func Where[Q sqlite.Filterable]() struct {
|
||||
Files fileWhere[Q]
|
||||
Items itemWhere[Q]
|
||||
Users userWhere[Q]
|
||||
} {
|
||||
return struct {
|
||||
Files fileWhere[Q]
|
||||
Items itemWhere[Q]
|
||||
Users userWhere[Q]
|
||||
}{
|
||||
Files: buildFileWhere[Q](FileColumns),
|
||||
Items: buildItemWhere[Q](ItemColumns),
|
||||
Users: buildUserWhere[Q](UserColumns),
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
SelectJoins = getJoins[*dialect.SelectQuery]
|
||||
UpdateJoins = getJoins[*dialect.UpdateQuery]
|
||||
)
|
||||
|
||||
type joinSet[Q interface{ aliasedAs(string) Q }] struct {
|
||||
InnerJoin Q
|
||||
LeftJoin Q
|
||||
RightJoin Q
|
||||
}
|
||||
|
||||
func (j joinSet[Q]) AliasedAs(alias string) joinSet[Q] {
|
||||
return joinSet[Q]{
|
||||
InnerJoin: j.InnerJoin.aliasedAs(alias),
|
||||
LeftJoin: j.LeftJoin.aliasedAs(alias),
|
||||
RightJoin: j.RightJoin.aliasedAs(alias),
|
||||
}
|
||||
}
|
||||
|
||||
type joins[Q dialect.Joinable] struct {
|
||||
Files joinSet[fileJoins[Q]]
|
||||
Items joinSet[itemJoins[Q]]
|
||||
Users joinSet[userJoins[Q]]
|
||||
}
|
||||
|
||||
func buildJoinSet[Q interface{ aliasedAs(string) Q }, C any, F func(C, string) Q](c C, f F) joinSet[Q] {
|
||||
return joinSet[Q]{
|
||||
InnerJoin: f(c, clause.InnerJoin),
|
||||
LeftJoin: f(c, clause.LeftJoin),
|
||||
RightJoin: f(c, clause.RightJoin),
|
||||
}
|
||||
}
|
||||
|
||||
func getJoins[Q dialect.Joinable]() joins[Q] {
|
||||
return joins[Q]{
|
||||
Files: buildJoinSet[fileJoins[Q]](FileColumns, buildFileJoins),
|
||||
Items: buildJoinSet[itemJoins[Q]](ItemColumns, buildItemJoins),
|
||||
Users: buildJoinSet[userJoins[Q]](UserColumns, buildUserJoins),
|
||||
}
|
||||
}
|
||||
|
||||
type modAs[Q any, C interface{ AliasedAs(string) C }] struct {
|
||||
c C
|
||||
f func(C) bob.Mod[Q]
|
||||
}
|
||||
|
||||
func (m modAs[Q, C]) Apply(q Q) {
|
||||
m.f(m.c).Apply(q)
|
||||
}
|
||||
|
||||
func (m modAs[Q, C]) AliasedAs(alias string) bob.Mod[Q] {
|
||||
m.c = m.c.AliasedAs(alias)
|
||||
return m
|
||||
}
|
||||
|
||||
func randInt() int64 {
|
||||
out := int64(new(maphash.Hash).Sum64())
|
||||
|
||||
if out < 0 {
|
||||
return -out % 10000
|
||||
}
|
||||
|
||||
return out % 10000
|
||||
}
|
||||
|
||||
// ErrUniqueConstraint captures all unique constraint errors by explicitly leaving `s` empty.
|
||||
var ErrUniqueConstraint = &UniqueConstraintError{s: ""}
|
||||
|
||||
type UniqueConstraintError struct {
|
||||
// s is a string uniquely identifying the constraint in the raw error message returned from the database.
|
||||
s string
|
||||
}
|
||||
|
||||
func (e *UniqueConstraintError) Error() string {
|
||||
return e.s
|
||||
}
|
||||
|
||||
func (e *UniqueConstraintError) Is(target error) bool {
|
||||
err, ok := target.(*sqliteDriver.Error)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return err.Code() == 2067 && strings.Contains(err.Error(), e.s)
|
||||
}
|
15
server/internal/models/bob_main_test.bob.go
Normal file
15
server/internal/models/bob_main_test.bob.go
Normal file
@ -0,0 +1,15 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package models
|
||||
|
||||
import "github.com/stephenafamo/bob"
|
||||
|
||||
// Make sure the type File runs hooks after queries
|
||||
var _ bob.HookableType = &File{}
|
||||
|
||||
// Make sure the type Item runs hooks after queries
|
||||
var _ bob.HookableType = &Item{}
|
||||
|
||||
// Make sure the type User runs hooks after queries
|
||||
var _ bob.HookableType = &User{}
|
36
server/internal/models/factory/bobfactory_context.bob.go
Normal file
36
server/internal/models/factory/bobfactory_context.bob.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
var (
|
||||
fileCtx = newContextual[*models.File]("file")
|
||||
itemCtx = newContextual[*models.Item]("item")
|
||||
userCtx = newContextual[*models.User]("user")
|
||||
)
|
||||
|
||||
// Contextual is a convienience wrapper around context.WithValue and context.Value
|
||||
type contextual[V any] struct {
|
||||
key contextKey
|
||||
}
|
||||
|
||||
func newContextual[V any](key string) contextual[V] {
|
||||
return contextual[V]{key: contextKey(key)}
|
||||
}
|
||||
|
||||
func (k contextual[V]) WithValue(ctx context.Context, val V) context.Context {
|
||||
return context.WithValue(ctx, k.key, val)
|
||||
}
|
||||
|
||||
func (k contextual[V]) Value(ctx context.Context) (V, bool) {
|
||||
v, ok := ctx.Value(k.key).(V)
|
||||
return v, ok
|
||||
}
|
74
server/internal/models/factory/bobfactory_main.bob.go
Normal file
74
server/internal/models/factory/bobfactory_main.bob.go
Normal file
@ -0,0 +1,74 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
type Factory struct {
|
||||
baseFileMods FileModSlice
|
||||
baseItemMods ItemModSlice
|
||||
baseUserMods UserModSlice
|
||||
}
|
||||
|
||||
func New() *Factory {
|
||||
return &Factory{}
|
||||
}
|
||||
|
||||
func (f *Factory) NewFile(mods ...FileMod) *FileTemplate {
|
||||
o := &FileTemplate{f: f}
|
||||
|
||||
if f != nil {
|
||||
f.baseFileMods.Apply(o)
|
||||
}
|
||||
|
||||
FileModSlice(mods).Apply(o)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (f *Factory) NewItem(mods ...ItemMod) *ItemTemplate {
|
||||
o := &ItemTemplate{f: f}
|
||||
|
||||
if f != nil {
|
||||
f.baseItemMods.Apply(o)
|
||||
}
|
||||
|
||||
ItemModSlice(mods).Apply(o)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (f *Factory) NewUser(mods ...UserMod) *UserTemplate {
|
||||
o := &UserTemplate{f: f}
|
||||
|
||||
if f != nil {
|
||||
f.baseUserMods.Apply(o)
|
||||
}
|
||||
|
||||
UserModSlice(mods).Apply(o)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (f *Factory) ClearBaseFileMods() {
|
||||
f.baseFileMods = nil
|
||||
}
|
||||
|
||||
func (f *Factory) AddBaseFileMod(mods ...FileMod) {
|
||||
f.baseFileMods = append(f.baseFileMods, mods...)
|
||||
}
|
||||
|
||||
func (f *Factory) ClearBaseItemMods() {
|
||||
f.baseItemMods = nil
|
||||
}
|
||||
|
||||
func (f *Factory) AddBaseItemMod(mods ...ItemMod) {
|
||||
f.baseItemMods = append(f.baseItemMods, mods...)
|
||||
}
|
||||
|
||||
func (f *Factory) ClearBaseUserMods() {
|
||||
f.baseUserMods = nil
|
||||
}
|
||||
|
||||
func (f *Factory) AddBaseUserMod(mods ...UserMod) {
|
||||
f.baseUserMods = append(f.baseUserMods, mods...)
|
||||
}
|
56
server/internal/models/factory/bobfactory_random.bob.go
Normal file
56
server/internal/models/factory/bobfactory_random.bob.go
Normal file
@ -0,0 +1,56 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jaswdr/faker/v2"
|
||||
)
|
||||
|
||||
var defaultFaker = faker.New()
|
||||
|
||||
func random___byte(f *faker.Faker) []byte {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return []byte(random_string(f))
|
||||
}
|
||||
|
||||
func random_float32(f *faker.Faker) float32 {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return f.Float32(10, -1_000_000, 1_000_000)
|
||||
}
|
||||
|
||||
func random_int32(f *faker.Faker) int32 {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return f.Int32()
|
||||
}
|
||||
|
||||
func random_string(f *faker.Faker) string {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return strings.Join(f.Lorem().Words(f.IntBetween(1, 5)), " ")
|
||||
}
|
||||
|
||||
func random_time_Time(f *faker.Faker) time.Time {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
year := time.Hour * 24 * 365
|
||||
min := time.Now().Add(-year)
|
||||
max := time.Now().Add(year)
|
||||
return f.Time().TimeBetween(min, max)
|
||||
}
|
64
server/internal/models/factory/bobfactory_random_test.bob.go
Normal file
64
server/internal/models/factory/bobfactory_random_test.bob.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRandom_int32(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_int32(nil)
|
||||
val2 := random_int32(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_int32() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_string(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_string(nil)
|
||||
val2 := random_string(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_string() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom___byte(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random___byte(nil)
|
||||
val2 := random___byte(nil)
|
||||
|
||||
if bytes.Equal(val1, val2) {
|
||||
t.Fatalf("random___byte() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_float32(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_float32(nil)
|
||||
val2 := random_float32(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_float32() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_time_Time(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_time_Time(nil)
|
||||
val2 := random_time_Time(nil)
|
||||
|
||||
if val1.Equal(val2) {
|
||||
t.Fatalf("random_time_Time() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
466
server/internal/models/factory/files.bob.go
Normal file
466
server/internal/models/factory/files.bob.go
Normal file
@ -0,0 +1,466 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/jaswdr/faker/v2"
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
"github.com/stephenafamo/bob"
|
||||
)
|
||||
|
||||
type FileMod interface {
|
||||
Apply(*FileTemplate)
|
||||
}
|
||||
|
||||
type FileModFunc func(*FileTemplate)
|
||||
|
||||
func (f FileModFunc) Apply(n *FileTemplate) {
|
||||
f(n)
|
||||
}
|
||||
|
||||
type FileModSlice []FileMod
|
||||
|
||||
func (mods FileModSlice) Apply(n *FileTemplate) {
|
||||
for _, f := range mods {
|
||||
f.Apply(n)
|
||||
}
|
||||
}
|
||||
|
||||
// FileTemplate is an object representing the database table.
|
||||
// all columns are optional and should be set by mods
|
||||
type FileTemplate struct {
|
||||
ID func() int32
|
||||
Name func() null.Val[string]
|
||||
Data func() null.Val[[]byte]
|
||||
UserID func() null.Val[int32]
|
||||
|
||||
r fileR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type fileR struct {
|
||||
User *fileRUserR
|
||||
}
|
||||
|
||||
type fileRUserR struct {
|
||||
o *UserTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the FileTemplate
|
||||
func (o *FileTemplate) Apply(mods ...FileMod) {
|
||||
for _, mod := range mods {
|
||||
mod.Apply(o)
|
||||
}
|
||||
}
|
||||
|
||||
// toModel returns an *models.File
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) toModel() *models.File {
|
||||
m := &models.File{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = o.ID()
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = o.Name()
|
||||
}
|
||||
if o.Data != nil {
|
||||
m.Data = o.Data()
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = o.UserID()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// toModels returns an models.FileSlice
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) toModels(number int) models.FileSlice {
|
||||
m := make(models.FileSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.toModel()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// setModelRels creates and sets the relationships on *models.File
|
||||
// according to the relationships in the template. Nothing is inserted into the db
|
||||
func (t FileTemplate) setModelRels(o *models.File) {
|
||||
if t.r.User != nil {
|
||||
rel := t.r.User.o.toModel()
|
||||
rel.R.Files = append(rel.R.Files, o)
|
||||
o.UserID = null.From(rel.ID)
|
||||
o.R.User = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.FileSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) BuildSetter() *models.FileSetter {
|
||||
m := &models.FileSetter{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = omitnull.FromNull(o.Name())
|
||||
}
|
||||
if o.Data != nil {
|
||||
m.Data = omitnull.FromNull(o.Data())
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = omitnull.FromNull(o.UserID())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildManySetter returns an []*models.FileSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) BuildManySetter(number int) []*models.FileSetter {
|
||||
m := make([]*models.FileSetter, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.BuildSetter()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Build returns an *models.File
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use FileTemplate.Create
|
||||
func (o FileTemplate) Build() *models.File {
|
||||
m := o.toModel()
|
||||
o.setModelRels(m)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildMany returns an models.FileSlice
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use FileTemplate.CreateMany
|
||||
func (o FileTemplate) BuildMany(number int) models.FileSlice {
|
||||
m := make(models.FileSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.Build()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func ensureCreatableFile(m *models.FileSetter) {
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.File
|
||||
// according to the relationships in the template.
|
||||
// any required relationship should have already exist on the model
|
||||
func (o *FileTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.File) (context.Context, error) {
|
||||
var err error
|
||||
|
||||
if o.r.User != nil {
|
||||
var rel0 *models.User
|
||||
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
err = m.AttachUser(ctx, exec, rel0)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
// Create builds a file and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o *FileTemplate) Create(ctx context.Context, exec bob.Executor) (*models.File, error) {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreate builds a file and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o *FileTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.File {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateOrFail builds a file and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
||||
func (o *FileTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.File {
|
||||
tb.Helper()
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// create builds a file and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// this returns a context that includes the newly inserted model
|
||||
func (o *FileTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.File, error) {
|
||||
var err error
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableFile(opt)
|
||||
|
||||
m, err := models.Files.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = fileCtx.WithValue(ctx, m)
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
|
||||
// CreateMany builds multiple files and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o FileTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.FileSlice, error) {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreateMany builds multiple files and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o FileTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.FileSlice {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateManyOrFail builds multiple files and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
||||
func (o FileTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.FileSlice {
|
||||
tb.Helper()
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// createMany builds multiple files and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// this returns a context that includes the newly inserted models
|
||||
func (o FileTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.FileSlice, error) {
|
||||
var err error
|
||||
m := make(models.FileSlice, number)
|
||||
|
||||
for i := range m {
|
||||
ctx, m[i], err = o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, m, nil
|
||||
}
|
||||
|
||||
// File has methods that act as mods for the FileTemplate
|
||||
var FileMods fileMods
|
||||
|
||||
type fileMods struct{}
|
||||
|
||||
func (m fileMods) RandomizeAllColumns(f *faker.Faker) FileMod {
|
||||
return FileModSlice{
|
||||
FileMods.RandomID(f),
|
||||
FileMods.RandomName(f),
|
||||
FileMods.RandomData(f),
|
||||
FileMods.RandomUserID(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) ID(val int32) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) IDFunc(f func() int32) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetID() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m fileMods) RandomID(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = func() int32 {
|
||||
return random_int32(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) Name(val null.Val[string]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) NameFunc(f func() null.Val[string]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetName() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m fileMods) RandomName(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) Data(val null.Val[[]byte]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = func() null.Val[[]byte] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) DataFunc(f func() null.Val[[]byte]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetData() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m fileMods) RandomData(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = func() null.Val[[]byte] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[[]byte](nil)
|
||||
}
|
||||
|
||||
return null.From(random___byte(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) UserID(val null.Val[int32]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) UserIDFunc(f func() null.Val[int32]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetUserID() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m fileMods) RandomUserID(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = func() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithUser(rel *UserTemplate) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.User = &fileRUserR{
|
||||
o: rel,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithNewUser(mods ...UserMod) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
related := o.f.NewUser(mods...)
|
||||
|
||||
m.WithUser(related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithoutUser() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.User = nil
|
||||
})
|
||||
}
|
608
server/internal/models/factory/items.bob.go
Normal file
608
server/internal/models/factory/items.bob.go
Normal file
@ -0,0 +1,608 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/jaswdr/faker/v2"
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
"github.com/stephenafamo/bob"
|
||||
)
|
||||
|
||||
type ItemMod interface {
|
||||
Apply(*ItemTemplate)
|
||||
}
|
||||
|
||||
type ItemModFunc func(*ItemTemplate)
|
||||
|
||||
func (f ItemModFunc) Apply(n *ItemTemplate) {
|
||||
f(n)
|
||||
}
|
||||
|
||||
type ItemModSlice []ItemMod
|
||||
|
||||
func (mods ItemModSlice) Apply(n *ItemTemplate) {
|
||||
for _, f := range mods {
|
||||
f.Apply(n)
|
||||
}
|
||||
}
|
||||
|
||||
// ItemTemplate is an object representing the database table.
|
||||
// all columns are optional and should be set by mods
|
||||
type ItemTemplate struct {
|
||||
ID func() int32
|
||||
Name func() null.Val[string]
|
||||
Description func() null.Val[string]
|
||||
Price func() null.Val[float32]
|
||||
Quantity func() null.Val[int32]
|
||||
Added func() null.Val[time.Time]
|
||||
UserID func() null.Val[int32]
|
||||
|
||||
r itemR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type itemR struct {
|
||||
User *itemRUserR
|
||||
}
|
||||
|
||||
type itemRUserR struct {
|
||||
o *UserTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the ItemTemplate
|
||||
func (o *ItemTemplate) Apply(mods ...ItemMod) {
|
||||
for _, mod := range mods {
|
||||
mod.Apply(o)
|
||||
}
|
||||
}
|
||||
|
||||
// toModel returns an *models.Item
|
||||
// this does nothing with the relationship templates
|
||||
func (o ItemTemplate) toModel() *models.Item {
|
||||
m := &models.Item{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = o.ID()
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = o.Name()
|
||||
}
|
||||
if o.Description != nil {
|
||||
m.Description = o.Description()
|
||||
}
|
||||
if o.Price != nil {
|
||||
m.Price = o.Price()
|
||||
}
|
||||
if o.Quantity != nil {
|
||||
m.Quantity = o.Quantity()
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = o.Added()
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = o.UserID()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// toModels returns an models.ItemSlice
|
||||
// this does nothing with the relationship templates
|
||||
func (o ItemTemplate) toModels(number int) models.ItemSlice {
|
||||
m := make(models.ItemSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.toModel()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// setModelRels creates and sets the relationships on *models.Item
|
||||
// according to the relationships in the template. Nothing is inserted into the db
|
||||
func (t ItemTemplate) setModelRels(o *models.Item) {
|
||||
if t.r.User != nil {
|
||||
rel := t.r.User.o.toModel()
|
||||
rel.R.Items = append(rel.R.Items, o)
|
||||
o.UserID = null.From(rel.ID)
|
||||
o.R.User = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.ItemSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o ItemTemplate) BuildSetter() *models.ItemSetter {
|
||||
m := &models.ItemSetter{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = omitnull.FromNull(o.Name())
|
||||
}
|
||||
if o.Description != nil {
|
||||
m.Description = omitnull.FromNull(o.Description())
|
||||
}
|
||||
if o.Price != nil {
|
||||
m.Price = omitnull.FromNull(o.Price())
|
||||
}
|
||||
if o.Quantity != nil {
|
||||
m.Quantity = omitnull.FromNull(o.Quantity())
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = omitnull.FromNull(o.Added())
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = omitnull.FromNull(o.UserID())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildManySetter returns an []*models.ItemSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o ItemTemplate) BuildManySetter(number int) []*models.ItemSetter {
|
||||
m := make([]*models.ItemSetter, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.BuildSetter()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Build returns an *models.Item
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use ItemTemplate.Create
|
||||
func (o ItemTemplate) Build() *models.Item {
|
||||
m := o.toModel()
|
||||
o.setModelRels(m)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildMany returns an models.ItemSlice
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use ItemTemplate.CreateMany
|
||||
func (o ItemTemplate) BuildMany(number int) models.ItemSlice {
|
||||
m := make(models.ItemSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.Build()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func ensureCreatableItem(m *models.ItemSetter) {
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.Item
|
||||
// according to the relationships in the template.
|
||||
// any required relationship should have already exist on the model
|
||||
func (o *ItemTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Item) (context.Context, error) {
|
||||
var err error
|
||||
|
||||
if o.r.User != nil {
|
||||
var rel0 *models.User
|
||||
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
err = m.AttachUser(ctx, exec, rel0)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
// Create builds a item and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o *ItemTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Item, error) {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreate builds a item and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o *ItemTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Item {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateOrFail builds a item and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
||||
func (o *ItemTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Item {
|
||||
tb.Helper()
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// create builds a item and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// this returns a context that includes the newly inserted model
|
||||
func (o *ItemTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.Item, error) {
|
||||
var err error
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableItem(opt)
|
||||
|
||||
m, err := models.Items.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = itemCtx.WithValue(ctx, m)
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
|
||||
// CreateMany builds multiple items and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o ItemTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.ItemSlice, error) {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreateMany builds multiple items and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o ItemTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.ItemSlice {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateManyOrFail builds multiple items and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
||||
func (o ItemTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.ItemSlice {
|
||||
tb.Helper()
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// createMany builds multiple items and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// this returns a context that includes the newly inserted models
|
||||
func (o ItemTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.ItemSlice, error) {
|
||||
var err error
|
||||
m := make(models.ItemSlice, number)
|
||||
|
||||
for i := range m {
|
||||
ctx, m[i], err = o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, m, nil
|
||||
}
|
||||
|
||||
// Item has methods that act as mods for the ItemTemplate
|
||||
var ItemMods itemMods
|
||||
|
||||
type itemMods struct{}
|
||||
|
||||
func (m itemMods) RandomizeAllColumns(f *faker.Faker) ItemMod {
|
||||
return ItemModSlice{
|
||||
ItemMods.RandomID(f),
|
||||
ItemMods.RandomName(f),
|
||||
ItemMods.RandomDescription(f),
|
||||
ItemMods.RandomPrice(f),
|
||||
ItemMods.RandomQuantity(f),
|
||||
ItemMods.RandomAdded(f),
|
||||
ItemMods.RandomUserID(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) ID(val int32) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) IDFunc(f func() int32) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetID() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomID(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = func() int32 {
|
||||
return random_int32(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Name(val null.Val[string]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) NameFunc(f func() null.Val[string]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetName() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomName(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Description(val null.Val[string]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Description = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) DescriptionFunc(f func() null.Val[string]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Description = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetDescription() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Description = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomDescription(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Description = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Price(val null.Val[float32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Price = func() null.Val[float32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) PriceFunc(f func() null.Val[float32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Price = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetPrice() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Price = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomPrice(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Price = func() null.Val[float32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[float32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_float32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Quantity(val null.Val[int32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) QuantityFunc(f func() null.Val[int32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetQuantity() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomQuantity(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = func() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Added(val null.Val[time.Time]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = func() null.Val[time.Time] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) AddedFunc(f func() null.Val[time.Time]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetAdded() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomAdded(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = func() null.Val[time.Time] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[time.Time](nil)
|
||||
}
|
||||
|
||||
return null.From(random_time_Time(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) UserID(val null.Val[int32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.UserID = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) UserIDFunc(f func() null.Val[int32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.UserID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m itemMods) UnsetUserID() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.UserID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m itemMods) RandomUserID(f *faker.Faker) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.UserID = func() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m itemMods) WithUser(rel *UserTemplate) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.r.User = &itemRUserR{
|
||||
o: rel,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m itemMods) WithNewUser(mods ...UserMod) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
related := o.f.NewUser(mods...)
|
||||
|
||||
m.WithUser(related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m itemMods) WithoutUser() ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.r.User = nil
|
||||
})
|
||||
}
|
610
server/internal/models/factory/users.bob.go
Normal file
610
server/internal/models/factory/users.bob.go
Normal file
@ -0,0 +1,610 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/jaswdr/faker/v2"
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
"github.com/stephenafamo/bob"
|
||||
)
|
||||
|
||||
type UserMod interface {
|
||||
Apply(*UserTemplate)
|
||||
}
|
||||
|
||||
type UserModFunc func(*UserTemplate)
|
||||
|
||||
func (f UserModFunc) Apply(n *UserTemplate) {
|
||||
f(n)
|
||||
}
|
||||
|
||||
type UserModSlice []UserMod
|
||||
|
||||
func (mods UserModSlice) Apply(n *UserTemplate) {
|
||||
for _, f := range mods {
|
||||
f.Apply(n)
|
||||
}
|
||||
}
|
||||
|
||||
// UserTemplate is an object representing the database table.
|
||||
// all columns are optional and should be set by mods
|
||||
type UserTemplate struct {
|
||||
ID func() int32
|
||||
Username func() null.Val[string]
|
||||
Password func() null.Val[string]
|
||||
ProfilePictureID func() null.Val[int32]
|
||||
Challenge func() null.Val[string]
|
||||
|
||||
r userR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type userR struct {
|
||||
Files []*userRFilesR
|
||||
Items []*userRItemsR
|
||||
}
|
||||
|
||||
type userRFilesR struct {
|
||||
number int
|
||||
o *FileTemplate
|
||||
}
|
||||
type userRItemsR struct {
|
||||
number int
|
||||
o *ItemTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the UserTemplate
|
||||
func (o *UserTemplate) Apply(mods ...UserMod) {
|
||||
for _, mod := range mods {
|
||||
mod.Apply(o)
|
||||
}
|
||||
}
|
||||
|
||||
// toModel returns an *models.User
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) toModel() *models.User {
|
||||
m := &models.User{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = o.ID()
|
||||
}
|
||||
if o.Username != nil {
|
||||
m.Username = o.Username()
|
||||
}
|
||||
if o.Password != nil {
|
||||
m.Password = o.Password()
|
||||
}
|
||||
if o.ProfilePictureID != nil {
|
||||
m.ProfilePictureID = o.ProfilePictureID()
|
||||
}
|
||||
if o.Challenge != nil {
|
||||
m.Challenge = o.Challenge()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// toModels returns an models.UserSlice
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) toModels(number int) models.UserSlice {
|
||||
m := make(models.UserSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.toModel()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// setModelRels creates and sets the relationships on *models.User
|
||||
// according to the relationships in the template. Nothing is inserted into the db
|
||||
func (t UserTemplate) setModelRels(o *models.User) {
|
||||
if t.r.Files != nil {
|
||||
rel := models.FileSlice{}
|
||||
for _, r := range t.r.Files {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.UserID = null.From(o.ID)
|
||||
rel.R.User = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
}
|
||||
o.R.Files = rel
|
||||
}
|
||||
|
||||
if t.r.Items != nil {
|
||||
rel := models.ItemSlice{}
|
||||
for _, r := range t.r.Items {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.UserID = null.From(o.ID)
|
||||
rel.R.User = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
}
|
||||
o.R.Items = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.UserSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) BuildSetter() *models.UserSetter {
|
||||
m := &models.UserSetter{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Username != nil {
|
||||
m.Username = omitnull.FromNull(o.Username())
|
||||
}
|
||||
if o.Password != nil {
|
||||
m.Password = omitnull.FromNull(o.Password())
|
||||
}
|
||||
if o.ProfilePictureID != nil {
|
||||
m.ProfilePictureID = omitnull.FromNull(o.ProfilePictureID())
|
||||
}
|
||||
if o.Challenge != nil {
|
||||
m.Challenge = omitnull.FromNull(o.Challenge())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildManySetter returns an []*models.UserSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) BuildManySetter(number int) []*models.UserSetter {
|
||||
m := make([]*models.UserSetter, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.BuildSetter()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Build returns an *models.User
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use UserTemplate.Create
|
||||
func (o UserTemplate) Build() *models.User {
|
||||
m := o.toModel()
|
||||
o.setModelRels(m)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildMany returns an models.UserSlice
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use UserTemplate.CreateMany
|
||||
func (o UserTemplate) BuildMany(number int) models.UserSlice {
|
||||
m := make(models.UserSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.Build()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func ensureCreatableUser(m *models.UserSetter) {
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.User
|
||||
// according to the relationships in the template.
|
||||
// any required relationship should have already exist on the model
|
||||
func (o *UserTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.User) (context.Context, error) {
|
||||
var err error
|
||||
|
||||
if o.r.Files != nil {
|
||||
for _, r := range o.r.Files {
|
||||
var rel0 models.FileSlice
|
||||
ctx, rel0, err = r.o.createMany(ctx, exec, r.number)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
err = m.AttachFiles(ctx, exec, rel0...)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if o.r.Items != nil {
|
||||
for _, r := range o.r.Items {
|
||||
var rel1 models.ItemSlice
|
||||
ctx, rel1, err = r.o.createMany(ctx, exec, r.number)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
err = m.AttachItems(ctx, exec, rel1...)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
// Create builds a user and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o *UserTemplate) Create(ctx context.Context, exec bob.Executor) (*models.User, error) {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreate builds a user and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o *UserTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.User {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateOrFail builds a user and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
||||
func (o *UserTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.User {
|
||||
tb.Helper()
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// create builds a user and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// this returns a context that includes the newly inserted model
|
||||
func (o *UserTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.User, error) {
|
||||
var err error
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableUser(opt)
|
||||
|
||||
m, err := models.Users.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = userCtx.WithValue(ctx, m)
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
|
||||
// CreateMany builds multiple users and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o UserTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.UserSlice, error) {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreateMany builds multiple users and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o UserTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.UserSlice {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateManyOrFail builds multiple users and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
||||
func (o UserTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.UserSlice {
|
||||
tb.Helper()
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// createMany builds multiple users and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// this returns a context that includes the newly inserted models
|
||||
func (o UserTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.UserSlice, error) {
|
||||
var err error
|
||||
m := make(models.UserSlice, number)
|
||||
|
||||
for i := range m {
|
||||
ctx, m[i], err = o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, m, nil
|
||||
}
|
||||
|
||||
// User has methods that act as mods for the UserTemplate
|
||||
var UserMods userMods
|
||||
|
||||
type userMods struct{}
|
||||
|
||||
func (m userMods) RandomizeAllColumns(f *faker.Faker) UserMod {
|
||||
return UserModSlice{
|
||||
UserMods.RandomID(f),
|
||||
UserMods.RandomUsername(f),
|
||||
UserMods.RandomPassword(f),
|
||||
UserMods.RandomProfilePictureID(f),
|
||||
UserMods.RandomChallenge(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ID(val int32) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) IDFunc(f func() int32) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetID() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomID(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = func() int32 {
|
||||
return random_int32(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Username(val null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) UsernameFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetUsername() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomUsername(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Password(val null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) PasswordFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetPassword() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomPassword(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ProfilePictureID(val null.Val[int32]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ProfilePictureIDFunc(f func() null.Val[int32]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetProfilePictureID() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomProfilePictureID(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = func() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Challenge(val null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ChallengeFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetChallenge() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomChallenge(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithFiles(number int, related *FileTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = []*userRFilesR{{
|
||||
number: number,
|
||||
o: related,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithNewFiles(number int, mods ...FileMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewFile(mods...)
|
||||
m.WithFiles(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddFiles(number int, related *FileTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = append(o.r.Files, &userRFilesR{
|
||||
number: number,
|
||||
o: related,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddNewFiles(number int, mods ...FileMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewFile(mods...)
|
||||
m.AddFiles(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithoutFiles() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = nil
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithItems(number int, related *ItemTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Items = []*userRItemsR{{
|
||||
number: number,
|
||||
o: related,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithNewItems(number int, mods ...ItemMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewItem(mods...)
|
||||
m.WithItems(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddItems(number int, related *ItemTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Items = append(o.r.Items, &userRItemsR{
|
||||
number: number,
|
||||
o: related,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddNewItems(number int, mods ...ItemMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewItem(mods...)
|
||||
m.AddItems(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithoutItems() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Items = nil
|
||||
})
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package models
|
||||
|
||||
type File struct {
|
||||
ID uint32 `gorm:"primaryKey"`
|
||||
|
||||
Name string
|
||||
Data []byte
|
||||
|
||||
// User
|
||||
UserID uint
|
||||
User User
|
||||
}
|
658
server/internal/models/files.bob.go
Normal file
658
server/internal/models/files.bob.go
Normal file
@ -0,0 +1,658 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/stephenafamo/bob"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dm"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/sm"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/um"
|
||||
"github.com/stephenafamo/bob/expr"
|
||||
"github.com/stephenafamo/bob/mods"
|
||||
"github.com/stephenafamo/bob/orm"
|
||||
)
|
||||
|
||||
// File is an object representing the database table.
|
||||
type File struct {
|
||||
ID int32 `db:"id,pk" `
|
||||
Name null.Val[string] `db:"name" `
|
||||
Data null.Val[[]byte] `db:"data" `
|
||||
UserID null.Val[int32] `db:"user_id" `
|
||||
|
||||
R fileR `db:"-" `
|
||||
}
|
||||
|
||||
// FileSlice is an alias for a slice of pointers to File.
|
||||
// This should almost always be used instead of []*File.
|
||||
type FileSlice []*File
|
||||
|
||||
// Files contains methods to work with the files table
|
||||
var Files = sqlite.NewTablex[*File, FileSlice, *FileSetter]("", "files")
|
||||
|
||||
// FilesQuery is a query on the files table
|
||||
type FilesQuery = *sqlite.ViewQuery[*File, FileSlice]
|
||||
|
||||
// fileR is where relationships are stored.
|
||||
type fileR struct {
|
||||
User *User // fk_files_0
|
||||
}
|
||||
|
||||
type fileColumnNames struct {
|
||||
ID string
|
||||
Name string
|
||||
Data string
|
||||
UserID string
|
||||
}
|
||||
|
||||
var FileColumns = buildFileColumns("files")
|
||||
|
||||
type fileColumns struct {
|
||||
tableAlias string
|
||||
ID sqlite.Expression
|
||||
Name sqlite.Expression
|
||||
Data sqlite.Expression
|
||||
UserID sqlite.Expression
|
||||
}
|
||||
|
||||
func (c fileColumns) Alias() string {
|
||||
return c.tableAlias
|
||||
}
|
||||
|
||||
func (fileColumns) AliasedAs(alias string) fileColumns {
|
||||
return buildFileColumns(alias)
|
||||
}
|
||||
|
||||
func buildFileColumns(alias string) fileColumns {
|
||||
return fileColumns{
|
||||
tableAlias: alias,
|
||||
ID: sqlite.Quote(alias, "id"),
|
||||
Name: sqlite.Quote(alias, "name"),
|
||||
Data: sqlite.Quote(alias, "data"),
|
||||
UserID: sqlite.Quote(alias, "user_id"),
|
||||
}
|
||||
}
|
||||
|
||||
type fileWhere[Q sqlite.Filterable] struct {
|
||||
ID sqlite.WhereMod[Q, int32]
|
||||
Name sqlite.WhereNullMod[Q, string]
|
||||
Data sqlite.WhereNullMod[Q, []byte]
|
||||
UserID sqlite.WhereNullMod[Q, int32]
|
||||
}
|
||||
|
||||
func (fileWhere[Q]) AliasedAs(alias string) fileWhere[Q] {
|
||||
return buildFileWhere[Q](buildFileColumns(alias))
|
||||
}
|
||||
|
||||
func buildFileWhere[Q sqlite.Filterable](cols fileColumns) fileWhere[Q] {
|
||||
return fileWhere[Q]{
|
||||
ID: sqlite.Where[Q, int32](cols.ID),
|
||||
Name: sqlite.WhereNull[Q, string](cols.Name),
|
||||
Data: sqlite.WhereNull[Q, []byte](cols.Data),
|
||||
UserID: sqlite.WhereNull[Q, int32](cols.UserID),
|
||||
}
|
||||
}
|
||||
|
||||
var FileErrors = &fileErrors{
|
||||
ErrUniquePkMainFiles: &UniqueConstraintError{s: "pk_main_files"},
|
||||
}
|
||||
|
||||
type fileErrors struct {
|
||||
ErrUniquePkMainFiles *UniqueConstraintError
|
||||
}
|
||||
|
||||
// FileSetter is used for insert/upsert/update operations
|
||||
// All values are optional, and do not have to be set
|
||||
// Generated columns are not included
|
||||
type FileSetter struct {
|
||||
ID omit.Val[int32] `db:"id,pk" `
|
||||
Name omitnull.Val[string] `db:"name" `
|
||||
Data omitnull.Val[[]byte] `db:"data" `
|
||||
UserID omitnull.Val[int32] `db:"user_id" `
|
||||
}
|
||||
|
||||
func (s FileSetter) SetColumns() []string {
|
||||
vals := make([]string, 0, 4)
|
||||
if !s.ID.IsUnset() {
|
||||
vals = append(vals, "id")
|
||||
}
|
||||
|
||||
if !s.Name.IsUnset() {
|
||||
vals = append(vals, "name")
|
||||
}
|
||||
|
||||
if !s.Data.IsUnset() {
|
||||
vals = append(vals, "data")
|
||||
}
|
||||
|
||||
if !s.UserID.IsUnset() {
|
||||
vals = append(vals, "user_id")
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (s FileSetter) Overwrite(t *File) {
|
||||
if !s.ID.IsUnset() {
|
||||
t.ID, _ = s.ID.Get()
|
||||
}
|
||||
if !s.Name.IsUnset() {
|
||||
t.Name, _ = s.Name.GetNull()
|
||||
}
|
||||
if !s.Data.IsUnset() {
|
||||
t.Data, _ = s.Data.GetNull()
|
||||
}
|
||||
if !s.UserID.IsUnset() {
|
||||
t.UserID, _ = s.UserID.GetNull()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *FileSetter) Apply(q *dialect.InsertQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Files.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
||||
})
|
||||
|
||||
if len(q.Table.Columns) == 0 {
|
||||
q.Table.Columns = s.SetColumns()
|
||||
}
|
||||
|
||||
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
vals := make([]bob.Expression, 0, 4)
|
||||
if !s.ID.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.ID))
|
||||
}
|
||||
|
||||
if !s.Name.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Name))
|
||||
}
|
||||
|
||||
if !s.Data.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Data))
|
||||
}
|
||||
|
||||
if !s.UserID.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.UserID))
|
||||
}
|
||||
|
||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
func (s FileSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return um.Set(s.Expressions()...)
|
||||
}
|
||||
|
||||
func (s FileSetter) Expressions(prefix ...string) []bob.Expression {
|
||||
exprs := make([]bob.Expression, 0, 4)
|
||||
|
||||
if !s.ID.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "id")...),
|
||||
sqlite.Arg(s.ID),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Name.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "name")...),
|
||||
sqlite.Arg(s.Name),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Data.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "data")...),
|
||||
sqlite.Arg(s.Data),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.UserID.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "user_id")...),
|
||||
sqlite.Arg(s.UserID),
|
||||
}})
|
||||
}
|
||||
|
||||
return exprs
|
||||
}
|
||||
|
||||
// FindFile retrieves a single record by primary key
|
||||
// If cols is empty Find will return all columns.
|
||||
func FindFile(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*File, error) {
|
||||
if len(cols) == 0 {
|
||||
return Files.Query(
|
||||
SelectWhere.Files.ID.EQ(IDPK),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
return Files.Query(
|
||||
SelectWhere.Files.ID.EQ(IDPK),
|
||||
sm.Columns(Files.Columns().Only(cols...)),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
// FileExists checks the presence of a single record by primary key
|
||||
func FileExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
||||
return Files.Query(
|
||||
SelectWhere.Files.ID.EQ(IDPK),
|
||||
).Exists(ctx, exec)
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after File is retrieved from the database
|
||||
func (o *File) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = Files.AfterSelectHooks.RunHooks(ctx, exec, FileSlice{o})
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = Files.AfterInsertHooks.RunHooks(ctx, exec, FileSlice{o})
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = Files.AfterUpdateHooks.RunHooks(ctx, exec, FileSlice{o})
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = Files.AfterDeleteHooks.RunHooks(ctx, exec, FileSlice{o})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// PrimaryKeyVals returns the primary key values of the File
|
||||
func (o *File) PrimaryKeyVals() bob.Expression {
|
||||
return sqlite.Arg(o.ID)
|
||||
}
|
||||
|
||||
func (o *File) pkEQ() dialect.Expression {
|
||||
return sqlite.Quote("files", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
return o.PrimaryKeyVals().WriteSQL(ctx, w, d, start)
|
||||
}))
|
||||
}
|
||||
|
||||
// Update uses an executor to update the File
|
||||
func (o *File) Update(ctx context.Context, exec bob.Executor, s *FileSetter) error {
|
||||
v, err := Files.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.R = v.R
|
||||
*o = *v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete deletes a single File record with an executor
|
||||
func (o *File) Delete(ctx context.Context, exec bob.Executor) error {
|
||||
_, err := Files.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload refreshes the File using the executor
|
||||
func (o *File) Reload(ctx context.Context, exec bob.Executor) error {
|
||||
o2, err := Files.Query(
|
||||
SelectWhere.Files.ID.EQ(o.ID),
|
||||
).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o2.R = o.R
|
||||
*o = *o2
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after FileSlice is retrieved from the database
|
||||
func (o FileSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = Files.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = Files.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = Files.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = Files.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (o FileSlice) pkIN() dialect.Expression {
|
||||
if len(o) == 0 {
|
||||
return sqlite.Raw("NULL")
|
||||
}
|
||||
|
||||
return sqlite.Quote("files", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
pkPairs := make([]bob.Expression, len(o))
|
||||
for i, row := range o {
|
||||
pkPairs[i] = row.PrimaryKeyVals()
|
||||
}
|
||||
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
// copyMatchingRows finds models in the given slice that have the same primary key
|
||||
// then it first copies the existing relationships from the old model to the new model
|
||||
// and then replaces the old model in the slice with the new model
|
||||
func (o FileSlice) copyMatchingRows(from ...*File) {
|
||||
for i, old := range o {
|
||||
for _, new := range from {
|
||||
if new.ID != old.ID {
|
||||
continue
|
||||
}
|
||||
new.R = old.R
|
||||
o[i] = new
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
|
||||
func (o FileSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Files.BeforeUpdateHooks.RunHooks(ctx, exec, o)
|
||||
})
|
||||
|
||||
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
var err error
|
||||
switch retrieved := retrieved.(type) {
|
||||
case *File:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*File:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case FileSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a File or a slice of File
|
||||
// then run the AfterUpdateHooks on the slice
|
||||
_, err = Files.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||||
func (o FileSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
|
||||
return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Files.BeforeDeleteHooks.RunHooks(ctx, exec, o)
|
||||
})
|
||||
|
||||
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
var err error
|
||||
switch retrieved := retrieved.(type) {
|
||||
case *File:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*File:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case FileSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a File or a slice of File
|
||||
// then run the AfterDeleteHooks on the slice
|
||||
_, err = Files.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
func (o FileSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals FileSetter) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := Files.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o FileSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := Files.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o FileSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
o2, err := Files.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.copyMatchingRows(o2...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type fileJoins[Q dialect.Joinable] struct {
|
||||
typ string
|
||||
User func(context.Context) modAs[Q, userColumns]
|
||||
}
|
||||
|
||||
func (j fileJoins[Q]) aliasedAs(alias string) fileJoins[Q] {
|
||||
return buildFileJoins[Q](buildFileColumns(alias), j.typ)
|
||||
}
|
||||
|
||||
func buildFileJoins[Q dialect.Joinable](cols fileColumns, typ string) fileJoins[Q] {
|
||||
return fileJoins[Q]{
|
||||
typ: typ,
|
||||
User: filesJoinUser[Q](cols, typ),
|
||||
}
|
||||
}
|
||||
|
||||
func filesJoinUser[Q dialect.Joinable](from fileColumns, typ string) func(context.Context) modAs[Q, userColumns] {
|
||||
return func(ctx context.Context) modAs[Q, userColumns] {
|
||||
return modAs[Q, userColumns]{
|
||||
c: UserColumns,
|
||||
f: func(to userColumns) bob.Mod[Q] {
|
||||
mods := make(mods.QueryMods[Q], 0, 1)
|
||||
|
||||
{
|
||||
mods = append(mods, dialect.Join[Q](typ, Users.Name().As(to.Alias())).On(
|
||||
to.ID.EQ(from.UserID),
|
||||
))
|
||||
}
|
||||
|
||||
return mods
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// User starts a query for related objects on users
|
||||
func (o *File) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(UserColumns.ID.EQ(sqlite.Arg(o.UserID))),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (os FileSlice) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
PKArgs := make([]bob.Expression, len(os))
|
||||
for i, o := range os {
|
||||
PKArgs[i] = sqlite.ArgGroup(o.UserID)
|
||||
}
|
||||
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(sqlite.Group(UserColumns.ID).In(PKArgs...)),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (o *File) Preload(name string, retrieved any) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch name {
|
||||
case "User":
|
||||
rel, ok := retrieved.(*User)
|
||||
if !ok {
|
||||
return fmt.Errorf("file cannot load %T as %q", retrieved, name)
|
||||
}
|
||||
|
||||
o.R.User = rel
|
||||
|
||||
if rel != nil {
|
||||
rel.R.Files = FileSlice{o}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("file has no relationship %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
func PreloadFileUser(opts ...sqlite.PreloadOption) sqlite.Preloader {
|
||||
return sqlite.Preload[*User, UserSlice](orm.Relationship{
|
||||
Name: "User",
|
||||
Sides: []orm.RelSide{
|
||||
{
|
||||
From: TableNames.Files,
|
||||
To: TableNames.Users,
|
||||
FromColumns: []string{
|
||||
ColumnNames.Files.UserID,
|
||||
},
|
||||
ToColumns: []string{
|
||||
ColumnNames.Users.ID,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, Users.Columns().Names(), opts...)
|
||||
}
|
||||
|
||||
func ThenLoadFileUser(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
||||
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
loader, isLoader := retrieved.(interface {
|
||||
LoadFileUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||||
})
|
||||
if !isLoader {
|
||||
return fmt.Errorf("object %T cannot load FileUser", retrieved)
|
||||
}
|
||||
|
||||
err := loader.LoadFileUser(ctx, exec, queryMods...)
|
||||
|
||||
// Don't cause an issue due to missing relationships
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// LoadFileUser loads the file's User into the .R struct
|
||||
func (o *File) LoadFileUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset the relationship
|
||||
o.R.User = nil
|
||||
|
||||
related, err := o.User(mods...).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
related.R.Files = FileSlice{o}
|
||||
|
||||
o.R.User = related
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadFileUser loads the file's User into the .R struct
|
||||
func (os FileSlice) LoadFileUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if len(os) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
users, err := os.User(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
for _, rel := range users {
|
||||
if o.UserID.GetOrZero() != rel.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
rel.R.Files = append(rel.R.Files, o)
|
||||
|
||||
o.R.User = rel
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func attachFileUser0(ctx context.Context, exec bob.Executor, count int, file0 *File, user1 *User) (*File, error) {
|
||||
setter := &FileSetter{
|
||||
UserID: omitnull.From(user1.ID),
|
||||
}
|
||||
|
||||
err := file0.Update(ctx, exec, setter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("attachFileUser0: %w", err)
|
||||
}
|
||||
|
||||
return file0, nil
|
||||
}
|
||||
|
||||
func (file0 *File) InsertUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
|
||||
user1, err := Users.Insert(related).One(ctx, exec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inserting related objects: %w", err)
|
||||
}
|
||||
|
||||
_, err = attachFileUser0(ctx, exec, 1, file0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file0.R.User = user1
|
||||
|
||||
user1.R.Files = append(user1.R.Files, file0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (file0 *File) AttachUser(ctx context.Context, exec bob.Executor, user1 *User) error {
|
||||
var err error
|
||||
|
||||
_, err = attachFileUser0(ctx, exec, 1, file0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file0.R.User = user1
|
||||
|
||||
user1.R.Files = append(user1.R.Files, file0)
|
||||
|
||||
return nil
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
itemv1 "github.com/spotdemo4/trevstack/server/internal/services/item/v1"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
ID uint32 `gorm:"primaryKey"`
|
||||
|
||||
Name string
|
||||
Description string
|
||||
Price float32
|
||||
Quantity int
|
||||
Added time.Time
|
||||
|
||||
// User
|
||||
UserID uint
|
||||
User User
|
||||
}
|
||||
|
||||
func (i Item) ToConnectV1() *itemv1.Item {
|
||||
return &itemv1.Item{
|
||||
Id: &i.ID,
|
||||
Name: i.Name,
|
||||
Description: i.Description,
|
||||
Price: i.Price,
|
||||
Quantity: uint32(i.Quantity),
|
||||
Added: timestamppb.New(i.Added),
|
||||
}
|
||||
}
|
734
server/internal/models/items.bob.go
Normal file
734
server/internal/models/items.bob.go
Normal file
@ -0,0 +1,734 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/stephenafamo/bob"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dm"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/sm"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/um"
|
||||
"github.com/stephenafamo/bob/expr"
|
||||
"github.com/stephenafamo/bob/mods"
|
||||
"github.com/stephenafamo/bob/orm"
|
||||
)
|
||||
|
||||
// Item is an object representing the database table.
|
||||
type Item struct {
|
||||
ID int32 `db:"id,pk" `
|
||||
Name null.Val[string] `db:"name" `
|
||||
Description null.Val[string] `db:"description" `
|
||||
Price null.Val[float32] `db:"price" `
|
||||
Quantity null.Val[int32] `db:"quantity" `
|
||||
Added null.Val[time.Time] `db:"added" `
|
||||
UserID null.Val[int32] `db:"user_id" `
|
||||
|
||||
R itemR `db:"-" `
|
||||
}
|
||||
|
||||
// ItemSlice is an alias for a slice of pointers to Item.
|
||||
// This should almost always be used instead of []*Item.
|
||||
type ItemSlice []*Item
|
||||
|
||||
// Items contains methods to work with the items table
|
||||
var Items = sqlite.NewTablex[*Item, ItemSlice, *ItemSetter]("", "items")
|
||||
|
||||
// ItemsQuery is a query on the items table
|
||||
type ItemsQuery = *sqlite.ViewQuery[*Item, ItemSlice]
|
||||
|
||||
// itemR is where relationships are stored.
|
||||
type itemR struct {
|
||||
User *User // fk_items_0
|
||||
}
|
||||
|
||||
type itemColumnNames struct {
|
||||
ID string
|
||||
Name string
|
||||
Description string
|
||||
Price string
|
||||
Quantity string
|
||||
Added string
|
||||
UserID string
|
||||
}
|
||||
|
||||
var ItemColumns = buildItemColumns("items")
|
||||
|
||||
type itemColumns struct {
|
||||
tableAlias string
|
||||
ID sqlite.Expression
|
||||
Name sqlite.Expression
|
||||
Description sqlite.Expression
|
||||
Price sqlite.Expression
|
||||
Quantity sqlite.Expression
|
||||
Added sqlite.Expression
|
||||
UserID sqlite.Expression
|
||||
}
|
||||
|
||||
func (c itemColumns) Alias() string {
|
||||
return c.tableAlias
|
||||
}
|
||||
|
||||
func (itemColumns) AliasedAs(alias string) itemColumns {
|
||||
return buildItemColumns(alias)
|
||||
}
|
||||
|
||||
func buildItemColumns(alias string) itemColumns {
|
||||
return itemColumns{
|
||||
tableAlias: alias,
|
||||
ID: sqlite.Quote(alias, "id"),
|
||||
Name: sqlite.Quote(alias, "name"),
|
||||
Description: sqlite.Quote(alias, "description"),
|
||||
Price: sqlite.Quote(alias, "price"),
|
||||
Quantity: sqlite.Quote(alias, "quantity"),
|
||||
Added: sqlite.Quote(alias, "added"),
|
||||
UserID: sqlite.Quote(alias, "user_id"),
|
||||
}
|
||||
}
|
||||
|
||||
type itemWhere[Q sqlite.Filterable] struct {
|
||||
ID sqlite.WhereMod[Q, int32]
|
||||
Name sqlite.WhereNullMod[Q, string]
|
||||
Description sqlite.WhereNullMod[Q, string]
|
||||
Price sqlite.WhereNullMod[Q, float32]
|
||||
Quantity sqlite.WhereNullMod[Q, int32]
|
||||
Added sqlite.WhereNullMod[Q, time.Time]
|
||||
UserID sqlite.WhereNullMod[Q, int32]
|
||||
}
|
||||
|
||||
func (itemWhere[Q]) AliasedAs(alias string) itemWhere[Q] {
|
||||
return buildItemWhere[Q](buildItemColumns(alias))
|
||||
}
|
||||
|
||||
func buildItemWhere[Q sqlite.Filterable](cols itemColumns) itemWhere[Q] {
|
||||
return itemWhere[Q]{
|
||||
ID: sqlite.Where[Q, int32](cols.ID),
|
||||
Name: sqlite.WhereNull[Q, string](cols.Name),
|
||||
Description: sqlite.WhereNull[Q, string](cols.Description),
|
||||
Price: sqlite.WhereNull[Q, float32](cols.Price),
|
||||
Quantity: sqlite.WhereNull[Q, int32](cols.Quantity),
|
||||
Added: sqlite.WhereNull[Q, time.Time](cols.Added),
|
||||
UserID: sqlite.WhereNull[Q, int32](cols.UserID),
|
||||
}
|
||||
}
|
||||
|
||||
var ItemErrors = &itemErrors{
|
||||
ErrUniquePkMainItems: &UniqueConstraintError{s: "pk_main_items"},
|
||||
}
|
||||
|
||||
type itemErrors struct {
|
||||
ErrUniquePkMainItems *UniqueConstraintError
|
||||
}
|
||||
|
||||
// ItemSetter is used for insert/upsert/update operations
|
||||
// All values are optional, and do not have to be set
|
||||
// Generated columns are not included
|
||||
type ItemSetter struct {
|
||||
ID omit.Val[int32] `db:"id,pk" `
|
||||
Name omitnull.Val[string] `db:"name" `
|
||||
Description omitnull.Val[string] `db:"description" `
|
||||
Price omitnull.Val[float32] `db:"price" `
|
||||
Quantity omitnull.Val[int32] `db:"quantity" `
|
||||
Added omitnull.Val[time.Time] `db:"added" `
|
||||
UserID omitnull.Val[int32] `db:"user_id" `
|
||||
}
|
||||
|
||||
func (s ItemSetter) SetColumns() []string {
|
||||
vals := make([]string, 0, 7)
|
||||
if !s.ID.IsUnset() {
|
||||
vals = append(vals, "id")
|
||||
}
|
||||
|
||||
if !s.Name.IsUnset() {
|
||||
vals = append(vals, "name")
|
||||
}
|
||||
|
||||
if !s.Description.IsUnset() {
|
||||
vals = append(vals, "description")
|
||||
}
|
||||
|
||||
if !s.Price.IsUnset() {
|
||||
vals = append(vals, "price")
|
||||
}
|
||||
|
||||
if !s.Quantity.IsUnset() {
|
||||
vals = append(vals, "quantity")
|
||||
}
|
||||
|
||||
if !s.Added.IsUnset() {
|
||||
vals = append(vals, "added")
|
||||
}
|
||||
|
||||
if !s.UserID.IsUnset() {
|
||||
vals = append(vals, "user_id")
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (s ItemSetter) Overwrite(t *Item) {
|
||||
if !s.ID.IsUnset() {
|
||||
t.ID, _ = s.ID.Get()
|
||||
}
|
||||
if !s.Name.IsUnset() {
|
||||
t.Name, _ = s.Name.GetNull()
|
||||
}
|
||||
if !s.Description.IsUnset() {
|
||||
t.Description, _ = s.Description.GetNull()
|
||||
}
|
||||
if !s.Price.IsUnset() {
|
||||
t.Price, _ = s.Price.GetNull()
|
||||
}
|
||||
if !s.Quantity.IsUnset() {
|
||||
t.Quantity, _ = s.Quantity.GetNull()
|
||||
}
|
||||
if !s.Added.IsUnset() {
|
||||
t.Added, _ = s.Added.GetNull()
|
||||
}
|
||||
if !s.UserID.IsUnset() {
|
||||
t.UserID, _ = s.UserID.GetNull()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ItemSetter) Apply(q *dialect.InsertQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Items.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
||||
})
|
||||
|
||||
if len(q.Table.Columns) == 0 {
|
||||
q.Table.Columns = s.SetColumns()
|
||||
}
|
||||
|
||||
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
vals := make([]bob.Expression, 0, 7)
|
||||
if !s.ID.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.ID))
|
||||
}
|
||||
|
||||
if !s.Name.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Name))
|
||||
}
|
||||
|
||||
if !s.Description.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Description))
|
||||
}
|
||||
|
||||
if !s.Price.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Price))
|
||||
}
|
||||
|
||||
if !s.Quantity.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Quantity))
|
||||
}
|
||||
|
||||
if !s.Added.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Added))
|
||||
}
|
||||
|
||||
if !s.UserID.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.UserID))
|
||||
}
|
||||
|
||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
func (s ItemSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return um.Set(s.Expressions()...)
|
||||
}
|
||||
|
||||
func (s ItemSetter) Expressions(prefix ...string) []bob.Expression {
|
||||
exprs := make([]bob.Expression, 0, 7)
|
||||
|
||||
if !s.ID.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "id")...),
|
||||
sqlite.Arg(s.ID),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Name.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "name")...),
|
||||
sqlite.Arg(s.Name),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Description.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "description")...),
|
||||
sqlite.Arg(s.Description),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Price.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "price")...),
|
||||
sqlite.Arg(s.Price),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Quantity.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "quantity")...),
|
||||
sqlite.Arg(s.Quantity),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Added.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "added")...),
|
||||
sqlite.Arg(s.Added),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.UserID.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "user_id")...),
|
||||
sqlite.Arg(s.UserID),
|
||||
}})
|
||||
}
|
||||
|
||||
return exprs
|
||||
}
|
||||
|
||||
// FindItem retrieves a single record by primary key
|
||||
// If cols is empty Find will return all columns.
|
||||
func FindItem(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*Item, error) {
|
||||
if len(cols) == 0 {
|
||||
return Items.Query(
|
||||
SelectWhere.Items.ID.EQ(IDPK),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
return Items.Query(
|
||||
SelectWhere.Items.ID.EQ(IDPK),
|
||||
sm.Columns(Items.Columns().Only(cols...)),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
// ItemExists checks the presence of a single record by primary key
|
||||
func ItemExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
||||
return Items.Query(
|
||||
SelectWhere.Items.ID.EQ(IDPK),
|
||||
).Exists(ctx, exec)
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after Item is retrieved from the database
|
||||
func (o *Item) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = Items.AfterSelectHooks.RunHooks(ctx, exec, ItemSlice{o})
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = Items.AfterInsertHooks.RunHooks(ctx, exec, ItemSlice{o})
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = Items.AfterUpdateHooks.RunHooks(ctx, exec, ItemSlice{o})
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = Items.AfterDeleteHooks.RunHooks(ctx, exec, ItemSlice{o})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// PrimaryKeyVals returns the primary key values of the Item
|
||||
func (o *Item) PrimaryKeyVals() bob.Expression {
|
||||
return sqlite.Arg(o.ID)
|
||||
}
|
||||
|
||||
func (o *Item) pkEQ() dialect.Expression {
|
||||
return sqlite.Quote("items", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
return o.PrimaryKeyVals().WriteSQL(ctx, w, d, start)
|
||||
}))
|
||||
}
|
||||
|
||||
// Update uses an executor to update the Item
|
||||
func (o *Item) Update(ctx context.Context, exec bob.Executor, s *ItemSetter) error {
|
||||
v, err := Items.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.R = v.R
|
||||
*o = *v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete deletes a single Item record with an executor
|
||||
func (o *Item) Delete(ctx context.Context, exec bob.Executor) error {
|
||||
_, err := Items.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload refreshes the Item using the executor
|
||||
func (o *Item) Reload(ctx context.Context, exec bob.Executor) error {
|
||||
o2, err := Items.Query(
|
||||
SelectWhere.Items.ID.EQ(o.ID),
|
||||
).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o2.R = o.R
|
||||
*o = *o2
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after ItemSlice is retrieved from the database
|
||||
func (o ItemSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = Items.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = Items.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = Items.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = Items.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (o ItemSlice) pkIN() dialect.Expression {
|
||||
if len(o) == 0 {
|
||||
return sqlite.Raw("NULL")
|
||||
}
|
||||
|
||||
return sqlite.Quote("items", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
pkPairs := make([]bob.Expression, len(o))
|
||||
for i, row := range o {
|
||||
pkPairs[i] = row.PrimaryKeyVals()
|
||||
}
|
||||
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
// copyMatchingRows finds models in the given slice that have the same primary key
|
||||
// then it first copies the existing relationships from the old model to the new model
|
||||
// and then replaces the old model in the slice with the new model
|
||||
func (o ItemSlice) copyMatchingRows(from ...*Item) {
|
||||
for i, old := range o {
|
||||
for _, new := range from {
|
||||
if new.ID != old.ID {
|
||||
continue
|
||||
}
|
||||
new.R = old.R
|
||||
o[i] = new
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
|
||||
func (o ItemSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Items.BeforeUpdateHooks.RunHooks(ctx, exec, o)
|
||||
})
|
||||
|
||||
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
var err error
|
||||
switch retrieved := retrieved.(type) {
|
||||
case *Item:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*Item:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case ItemSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a Item or a slice of Item
|
||||
// then run the AfterUpdateHooks on the slice
|
||||
_, err = Items.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||||
func (o ItemSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
|
||||
return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Items.BeforeDeleteHooks.RunHooks(ctx, exec, o)
|
||||
})
|
||||
|
||||
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
var err error
|
||||
switch retrieved := retrieved.(type) {
|
||||
case *Item:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*Item:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case ItemSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a Item or a slice of Item
|
||||
// then run the AfterDeleteHooks on the slice
|
||||
_, err = Items.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
func (o ItemSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals ItemSetter) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := Items.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o ItemSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := Items.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o ItemSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
o2, err := Items.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.copyMatchingRows(o2...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type itemJoins[Q dialect.Joinable] struct {
|
||||
typ string
|
||||
User func(context.Context) modAs[Q, userColumns]
|
||||
}
|
||||
|
||||
func (j itemJoins[Q]) aliasedAs(alias string) itemJoins[Q] {
|
||||
return buildItemJoins[Q](buildItemColumns(alias), j.typ)
|
||||
}
|
||||
|
||||
func buildItemJoins[Q dialect.Joinable](cols itemColumns, typ string) itemJoins[Q] {
|
||||
return itemJoins[Q]{
|
||||
typ: typ,
|
||||
User: itemsJoinUser[Q](cols, typ),
|
||||
}
|
||||
}
|
||||
|
||||
func itemsJoinUser[Q dialect.Joinable](from itemColumns, typ string) func(context.Context) modAs[Q, userColumns] {
|
||||
return func(ctx context.Context) modAs[Q, userColumns] {
|
||||
return modAs[Q, userColumns]{
|
||||
c: UserColumns,
|
||||
f: func(to userColumns) bob.Mod[Q] {
|
||||
mods := make(mods.QueryMods[Q], 0, 1)
|
||||
|
||||
{
|
||||
mods = append(mods, dialect.Join[Q](typ, Users.Name().As(to.Alias())).On(
|
||||
to.ID.EQ(from.UserID),
|
||||
))
|
||||
}
|
||||
|
||||
return mods
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// User starts a query for related objects on users
|
||||
func (o *Item) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(UserColumns.ID.EQ(sqlite.Arg(o.UserID))),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (os ItemSlice) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
PKArgs := make([]bob.Expression, len(os))
|
||||
for i, o := range os {
|
||||
PKArgs[i] = sqlite.ArgGroup(o.UserID)
|
||||
}
|
||||
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(sqlite.Group(UserColumns.ID).In(PKArgs...)),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (o *Item) Preload(name string, retrieved any) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch name {
|
||||
case "User":
|
||||
rel, ok := retrieved.(*User)
|
||||
if !ok {
|
||||
return fmt.Errorf("item cannot load %T as %q", retrieved, name)
|
||||
}
|
||||
|
||||
o.R.User = rel
|
||||
|
||||
if rel != nil {
|
||||
rel.R.Items = ItemSlice{o}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("item has no relationship %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
func PreloadItemUser(opts ...sqlite.PreloadOption) sqlite.Preloader {
|
||||
return sqlite.Preload[*User, UserSlice](orm.Relationship{
|
||||
Name: "User",
|
||||
Sides: []orm.RelSide{
|
||||
{
|
||||
From: TableNames.Items,
|
||||
To: TableNames.Users,
|
||||
FromColumns: []string{
|
||||
ColumnNames.Items.UserID,
|
||||
},
|
||||
ToColumns: []string{
|
||||
ColumnNames.Users.ID,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, Users.Columns().Names(), opts...)
|
||||
}
|
||||
|
||||
func ThenLoadItemUser(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
||||
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
loader, isLoader := retrieved.(interface {
|
||||
LoadItemUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||||
})
|
||||
if !isLoader {
|
||||
return fmt.Errorf("object %T cannot load ItemUser", retrieved)
|
||||
}
|
||||
|
||||
err := loader.LoadItemUser(ctx, exec, queryMods...)
|
||||
|
||||
// Don't cause an issue due to missing relationships
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// LoadItemUser loads the item's User into the .R struct
|
||||
func (o *Item) LoadItemUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset the relationship
|
||||
o.R.User = nil
|
||||
|
||||
related, err := o.User(mods...).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
related.R.Items = ItemSlice{o}
|
||||
|
||||
o.R.User = related
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadItemUser loads the item's User into the .R struct
|
||||
func (os ItemSlice) LoadItemUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if len(os) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
users, err := os.User(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
for _, rel := range users {
|
||||
if o.UserID.GetOrZero() != rel.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
rel.R.Items = append(rel.R.Items, o)
|
||||
|
||||
o.R.User = rel
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func attachItemUser0(ctx context.Context, exec bob.Executor, count int, item0 *Item, user1 *User) (*Item, error) {
|
||||
setter := &ItemSetter{
|
||||
UserID: omitnull.From(user1.ID),
|
||||
}
|
||||
|
||||
err := item0.Update(ctx, exec, setter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("attachItemUser0: %w", err)
|
||||
}
|
||||
|
||||
return item0, nil
|
||||
}
|
||||
|
||||
func (item0 *Item) InsertUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
|
||||
user1, err := Users.Insert(related).One(ctx, exec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inserting related objects: %w", err)
|
||||
}
|
||||
|
||||
_, err = attachItemUser0(ctx, exec, 1, item0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
item0.R.User = user1
|
||||
|
||||
user1.R.Items = append(user1.R.Items, item0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (item0 *Item) AttachUser(ctx context.Context, exec bob.Executor, user1 *User) error {
|
||||
var err error
|
||||
|
||||
_, err = attachItemUser0(ctx, exec, 1, item0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
item0.R.User = user1
|
||||
|
||||
user1.R.Items = append(user1.R.Items, item0)
|
||||
|
||||
return nil
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Passkey struct {
|
||||
ID string `gorm:"primaryKey"`
|
||||
|
||||
PublicKey string
|
||||
Algorithm int
|
||||
CreatedAt time.Time
|
||||
LastUsed time.Time
|
||||
|
||||
// User
|
||||
UserID uint
|
||||
User User
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
userv1 "github.com/spotdemo4/trevstack/server/internal/services/user/v1"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uint32 `gorm:"primaryKey"`
|
||||
|
||||
Username string
|
||||
Password string
|
||||
Challenge *string
|
||||
|
||||
// Passkeys
|
||||
Passkeys []Passkey
|
||||
|
||||
// Profile picture
|
||||
ProfilePictureID *uint
|
||||
ProfilePicture *File
|
||||
}
|
||||
|
||||
func (u User) ToConnectV1() *userv1.User {
|
||||
var ppid *string
|
||||
if u.ProfilePicture != nil {
|
||||
id := fmt.Sprintf("/file/%d", u.ProfilePicture.ID)
|
||||
ppid = &id
|
||||
}
|
||||
|
||||
return &userv1.User{
|
||||
Id: u.ID,
|
||||
Username: u.Username,
|
||||
ProfilePicture: ppid,
|
||||
}
|
||||
}
|
887
server/internal/models/users.bob.go
Normal file
887
server/internal/models/users.bob.go
Normal file
@ -0,0 +1,887 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/stephenafamo/bob"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/dm"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/sm"
|
||||
"github.com/stephenafamo/bob/dialect/sqlite/um"
|
||||
"github.com/stephenafamo/bob/expr"
|
||||
"github.com/stephenafamo/bob/mods"
|
||||
)
|
||||
|
||||
// User is an object representing the database table.
|
||||
type User struct {
|
||||
ID int32 `db:"id,pk" `
|
||||
Username null.Val[string] `db:"username" `
|
||||
Password null.Val[string] `db:"password" `
|
||||
ProfilePictureID null.Val[int32] `db:"profile_picture_id" `
|
||||
Challenge null.Val[string] `db:"challenge" `
|
||||
|
||||
R userR `db:"-" `
|
||||
}
|
||||
|
||||
// UserSlice is an alias for a slice of pointers to User.
|
||||
// This should almost always be used instead of []*User.
|
||||
type UserSlice []*User
|
||||
|
||||
// Users contains methods to work with the users table
|
||||
var Users = sqlite.NewTablex[*User, UserSlice, *UserSetter]("", "users")
|
||||
|
||||
// UsersQuery is a query on the users table
|
||||
type UsersQuery = *sqlite.ViewQuery[*User, UserSlice]
|
||||
|
||||
// userR is where relationships are stored.
|
||||
type userR struct {
|
||||
Files FileSlice // fk_files_0
|
||||
Items ItemSlice // fk_items_0
|
||||
}
|
||||
|
||||
type userColumnNames struct {
|
||||
ID string
|
||||
Username string
|
||||
Password string
|
||||
ProfilePictureID string
|
||||
Challenge string
|
||||
}
|
||||
|
||||
var UserColumns = buildUserColumns("users")
|
||||
|
||||
type userColumns struct {
|
||||
tableAlias string
|
||||
ID sqlite.Expression
|
||||
Username sqlite.Expression
|
||||
Password sqlite.Expression
|
||||
ProfilePictureID sqlite.Expression
|
||||
Challenge sqlite.Expression
|
||||
}
|
||||
|
||||
func (c userColumns) Alias() string {
|
||||
return c.tableAlias
|
||||
}
|
||||
|
||||
func (userColumns) AliasedAs(alias string) userColumns {
|
||||
return buildUserColumns(alias)
|
||||
}
|
||||
|
||||
func buildUserColumns(alias string) userColumns {
|
||||
return userColumns{
|
||||
tableAlias: alias,
|
||||
ID: sqlite.Quote(alias, "id"),
|
||||
Username: sqlite.Quote(alias, "username"),
|
||||
Password: sqlite.Quote(alias, "password"),
|
||||
ProfilePictureID: sqlite.Quote(alias, "profile_picture_id"),
|
||||
Challenge: sqlite.Quote(alias, "challenge"),
|
||||
}
|
||||
}
|
||||
|
||||
type userWhere[Q sqlite.Filterable] struct {
|
||||
ID sqlite.WhereMod[Q, int32]
|
||||
Username sqlite.WhereNullMod[Q, string]
|
||||
Password sqlite.WhereNullMod[Q, string]
|
||||
ProfilePictureID sqlite.WhereNullMod[Q, int32]
|
||||
Challenge sqlite.WhereNullMod[Q, string]
|
||||
}
|
||||
|
||||
func (userWhere[Q]) AliasedAs(alias string) userWhere[Q] {
|
||||
return buildUserWhere[Q](buildUserColumns(alias))
|
||||
}
|
||||
|
||||
func buildUserWhere[Q sqlite.Filterable](cols userColumns) userWhere[Q] {
|
||||
return userWhere[Q]{
|
||||
ID: sqlite.Where[Q, int32](cols.ID),
|
||||
Username: sqlite.WhereNull[Q, string](cols.Username),
|
||||
Password: sqlite.WhereNull[Q, string](cols.Password),
|
||||
ProfilePictureID: sqlite.WhereNull[Q, int32](cols.ProfilePictureID),
|
||||
Challenge: sqlite.WhereNull[Q, string](cols.Challenge),
|
||||
}
|
||||
}
|
||||
|
||||
var UserErrors = &userErrors{
|
||||
ErrUniquePkMainUsers: &UniqueConstraintError{s: "pk_main_users"},
|
||||
}
|
||||
|
||||
type userErrors struct {
|
||||
ErrUniquePkMainUsers *UniqueConstraintError
|
||||
}
|
||||
|
||||
// UserSetter is used for insert/upsert/update operations
|
||||
// All values are optional, and do not have to be set
|
||||
// Generated columns are not included
|
||||
type UserSetter struct {
|
||||
ID omit.Val[int32] `db:"id,pk" `
|
||||
Username omitnull.Val[string] `db:"username" `
|
||||
Password omitnull.Val[string] `db:"password" `
|
||||
ProfilePictureID omitnull.Val[int32] `db:"profile_picture_id" `
|
||||
Challenge omitnull.Val[string] `db:"challenge" `
|
||||
}
|
||||
|
||||
func (s UserSetter) SetColumns() []string {
|
||||
vals := make([]string, 0, 5)
|
||||
if !s.ID.IsUnset() {
|
||||
vals = append(vals, "id")
|
||||
}
|
||||
|
||||
if !s.Username.IsUnset() {
|
||||
vals = append(vals, "username")
|
||||
}
|
||||
|
||||
if !s.Password.IsUnset() {
|
||||
vals = append(vals, "password")
|
||||
}
|
||||
|
||||
if !s.ProfilePictureID.IsUnset() {
|
||||
vals = append(vals, "profile_picture_id")
|
||||
}
|
||||
|
||||
if !s.Challenge.IsUnset() {
|
||||
vals = append(vals, "challenge")
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (s UserSetter) Overwrite(t *User) {
|
||||
if !s.ID.IsUnset() {
|
||||
t.ID, _ = s.ID.Get()
|
||||
}
|
||||
if !s.Username.IsUnset() {
|
||||
t.Username, _ = s.Username.GetNull()
|
||||
}
|
||||
if !s.Password.IsUnset() {
|
||||
t.Password, _ = s.Password.GetNull()
|
||||
}
|
||||
if !s.ProfilePictureID.IsUnset() {
|
||||
t.ProfilePictureID, _ = s.ProfilePictureID.GetNull()
|
||||
}
|
||||
if !s.Challenge.IsUnset() {
|
||||
t.Challenge, _ = s.Challenge.GetNull()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UserSetter) Apply(q *dialect.InsertQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Users.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
||||
})
|
||||
|
||||
if len(q.Table.Columns) == 0 {
|
||||
q.Table.Columns = s.SetColumns()
|
||||
}
|
||||
|
||||
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
vals := make([]bob.Expression, 0, 5)
|
||||
if !s.ID.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.ID))
|
||||
}
|
||||
|
||||
if !s.Username.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Username))
|
||||
}
|
||||
|
||||
if !s.Password.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Password))
|
||||
}
|
||||
|
||||
if !s.ProfilePictureID.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.ProfilePictureID))
|
||||
}
|
||||
|
||||
if !s.Challenge.IsUnset() {
|
||||
vals = append(vals, sqlite.Arg(s.Challenge))
|
||||
}
|
||||
|
||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
func (s UserSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return um.Set(s.Expressions()...)
|
||||
}
|
||||
|
||||
func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
|
||||
exprs := make([]bob.Expression, 0, 5)
|
||||
|
||||
if !s.ID.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "id")...),
|
||||
sqlite.Arg(s.ID),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Username.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "username")...),
|
||||
sqlite.Arg(s.Username),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Password.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "password")...),
|
||||
sqlite.Arg(s.Password),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.ProfilePictureID.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "profile_picture_id")...),
|
||||
sqlite.Arg(s.ProfilePictureID),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.Challenge.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
sqlite.Quote(append(prefix, "challenge")...),
|
||||
sqlite.Arg(s.Challenge),
|
||||
}})
|
||||
}
|
||||
|
||||
return exprs
|
||||
}
|
||||
|
||||
// FindUser retrieves a single record by primary key
|
||||
// If cols is empty Find will return all columns.
|
||||
func FindUser(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*User, error) {
|
||||
if len(cols) == 0 {
|
||||
return Users.Query(
|
||||
SelectWhere.Users.ID.EQ(IDPK),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
return Users.Query(
|
||||
SelectWhere.Users.ID.EQ(IDPK),
|
||||
sm.Columns(Users.Columns().Only(cols...)),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
// UserExists checks the presence of a single record by primary key
|
||||
func UserExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
||||
return Users.Query(
|
||||
SelectWhere.Users.ID.EQ(IDPK),
|
||||
).Exists(ctx, exec)
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after User is retrieved from the database
|
||||
func (o *User) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = Users.AfterSelectHooks.RunHooks(ctx, exec, UserSlice{o})
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = Users.AfterInsertHooks.RunHooks(ctx, exec, UserSlice{o})
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, UserSlice{o})
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, UserSlice{o})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// PrimaryKeyVals returns the primary key values of the User
|
||||
func (o *User) PrimaryKeyVals() bob.Expression {
|
||||
return sqlite.Arg(o.ID)
|
||||
}
|
||||
|
||||
func (o *User) pkEQ() dialect.Expression {
|
||||
return sqlite.Quote("users", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
return o.PrimaryKeyVals().WriteSQL(ctx, w, d, start)
|
||||
}))
|
||||
}
|
||||
|
||||
// Update uses an executor to update the User
|
||||
func (o *User) Update(ctx context.Context, exec bob.Executor, s *UserSetter) error {
|
||||
v, err := Users.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.R = v.R
|
||||
*o = *v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete deletes a single User record with an executor
|
||||
func (o *User) Delete(ctx context.Context, exec bob.Executor) error {
|
||||
_, err := Users.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload refreshes the User using the executor
|
||||
func (o *User) Reload(ctx context.Context, exec bob.Executor) error {
|
||||
o2, err := Users.Query(
|
||||
SelectWhere.Users.ID.EQ(o.ID),
|
||||
).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o2.R = o.R
|
||||
*o = *o2
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after UserSlice is retrieved from the database
|
||||
func (o UserSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = Users.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = Users.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (o UserSlice) pkIN() dialect.Expression {
|
||||
if len(o) == 0 {
|
||||
return sqlite.Raw("NULL")
|
||||
}
|
||||
|
||||
return sqlite.Quote("users", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||||
pkPairs := make([]bob.Expression, len(o))
|
||||
for i, row := range o {
|
||||
pkPairs[i] = row.PrimaryKeyVals()
|
||||
}
|
||||
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
// copyMatchingRows finds models in the given slice that have the same primary key
|
||||
// then it first copies the existing relationships from the old model to the new model
|
||||
// and then replaces the old model in the slice with the new model
|
||||
func (o UserSlice) copyMatchingRows(from ...*User) {
|
||||
for i, old := range o {
|
||||
for _, new := range from {
|
||||
if new.ID != old.ID {
|
||||
continue
|
||||
}
|
||||
new.R = old.R
|
||||
o[i] = new
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
|
||||
func (o UserSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Users.BeforeUpdateHooks.RunHooks(ctx, exec, o)
|
||||
})
|
||||
|
||||
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
var err error
|
||||
switch retrieved := retrieved.(type) {
|
||||
case *User:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*User:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case UserSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a User or a slice of User
|
||||
// then run the AfterUpdateHooks on the slice
|
||||
_, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||||
func (o UserSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
|
||||
return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return Users.BeforeDeleteHooks.RunHooks(ctx, exec, o)
|
||||
})
|
||||
|
||||
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
var err error
|
||||
switch retrieved := retrieved.(type) {
|
||||
case *User:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*User:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case UserSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a User or a slice of User
|
||||
// then run the AfterDeleteHooks on the slice
|
||||
_, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
func (o UserSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals UserSetter) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := Users.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o UserSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := Users.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o UserSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
o2, err := Users.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.copyMatchingRows(o2...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type userJoins[Q dialect.Joinable] struct {
|
||||
typ string
|
||||
Files func(context.Context) modAs[Q, fileColumns]
|
||||
Items func(context.Context) modAs[Q, itemColumns]
|
||||
}
|
||||
|
||||
func (j userJoins[Q]) aliasedAs(alias string) userJoins[Q] {
|
||||
return buildUserJoins[Q](buildUserColumns(alias), j.typ)
|
||||
}
|
||||
|
||||
func buildUserJoins[Q dialect.Joinable](cols userColumns, typ string) userJoins[Q] {
|
||||
return userJoins[Q]{
|
||||
typ: typ,
|
||||
Files: usersJoinFiles[Q](cols, typ),
|
||||
Items: usersJoinItems[Q](cols, typ),
|
||||
}
|
||||
}
|
||||
|
||||
func usersJoinFiles[Q dialect.Joinable](from userColumns, typ string) func(context.Context) modAs[Q, fileColumns] {
|
||||
return func(ctx context.Context) modAs[Q, fileColumns] {
|
||||
return modAs[Q, fileColumns]{
|
||||
c: FileColumns,
|
||||
f: func(to fileColumns) bob.Mod[Q] {
|
||||
mods := make(mods.QueryMods[Q], 0, 1)
|
||||
|
||||
{
|
||||
mods = append(mods, dialect.Join[Q](typ, Files.Name().As(to.Alias())).On(
|
||||
to.UserID.EQ(from.ID),
|
||||
))
|
||||
}
|
||||
|
||||
return mods
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func usersJoinItems[Q dialect.Joinable](from userColumns, typ string) func(context.Context) modAs[Q, itemColumns] {
|
||||
return func(ctx context.Context) modAs[Q, itemColumns] {
|
||||
return modAs[Q, itemColumns]{
|
||||
c: ItemColumns,
|
||||
f: func(to itemColumns) bob.Mod[Q] {
|
||||
mods := make(mods.QueryMods[Q], 0, 1)
|
||||
|
||||
{
|
||||
mods = append(mods, dialect.Join[Q](typ, Items.Name().As(to.Alias())).On(
|
||||
to.UserID.EQ(from.ID),
|
||||
))
|
||||
}
|
||||
|
||||
return mods
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Files starts a query for related objects on files
|
||||
func (o *User) Files(mods ...bob.Mod[*dialect.SelectQuery]) FilesQuery {
|
||||
return Files.Query(append(mods,
|
||||
sm.Where(FileColumns.UserID.EQ(sqlite.Arg(o.ID))),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (os UserSlice) Files(mods ...bob.Mod[*dialect.SelectQuery]) FilesQuery {
|
||||
PKArgs := make([]bob.Expression, len(os))
|
||||
for i, o := range os {
|
||||
PKArgs[i] = sqlite.ArgGroup(o.ID)
|
||||
}
|
||||
|
||||
return Files.Query(append(mods,
|
||||
sm.Where(sqlite.Group(FileColumns.UserID).In(PKArgs...)),
|
||||
)...)
|
||||
}
|
||||
|
||||
// Items starts a query for related objects on items
|
||||
func (o *User) Items(mods ...bob.Mod[*dialect.SelectQuery]) ItemsQuery {
|
||||
return Items.Query(append(mods,
|
||||
sm.Where(ItemColumns.UserID.EQ(sqlite.Arg(o.ID))),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (os UserSlice) Items(mods ...bob.Mod[*dialect.SelectQuery]) ItemsQuery {
|
||||
PKArgs := make([]bob.Expression, len(os))
|
||||
for i, o := range os {
|
||||
PKArgs[i] = sqlite.ArgGroup(o.ID)
|
||||
}
|
||||
|
||||
return Items.Query(append(mods,
|
||||
sm.Where(sqlite.Group(ItemColumns.UserID).In(PKArgs...)),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (o *User) Preload(name string, retrieved any) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch name {
|
||||
case "Files":
|
||||
rels, ok := retrieved.(FileSlice)
|
||||
if !ok {
|
||||
return fmt.Errorf("user cannot load %T as %q", retrieved, name)
|
||||
}
|
||||
|
||||
o.R.Files = rels
|
||||
|
||||
for _, rel := range rels {
|
||||
if rel != nil {
|
||||
rel.R.User = o
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case "Items":
|
||||
rels, ok := retrieved.(ItemSlice)
|
||||
if !ok {
|
||||
return fmt.Errorf("user cannot load %T as %q", retrieved, name)
|
||||
}
|
||||
|
||||
o.R.Items = rels
|
||||
|
||||
for _, rel := range rels {
|
||||
if rel != nil {
|
||||
rel.R.User = o
|
||||
}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("user has no relationship %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
func ThenLoadUserFiles(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
||||
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
loader, isLoader := retrieved.(interface {
|
||||
LoadUserFiles(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||||
})
|
||||
if !isLoader {
|
||||
return fmt.Errorf("object %T cannot load UserFiles", retrieved)
|
||||
}
|
||||
|
||||
err := loader.LoadUserFiles(ctx, exec, queryMods...)
|
||||
|
||||
// Don't cause an issue due to missing relationships
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// LoadUserFiles loads the user's Files into the .R struct
|
||||
func (o *User) LoadUserFiles(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset the relationship
|
||||
o.R.Files = nil
|
||||
|
||||
related, err := o.Files(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, rel := range related {
|
||||
rel.R.User = o
|
||||
}
|
||||
|
||||
o.R.Files = related
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadUserFiles loads the user's Files into the .R struct
|
||||
func (os UserSlice) LoadUserFiles(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if len(os) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
files, err := os.Files(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
o.R.Files = nil
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
for _, rel := range files {
|
||||
if o.ID != rel.UserID.GetOrZero() {
|
||||
continue
|
||||
}
|
||||
|
||||
rel.R.User = o
|
||||
|
||||
o.R.Files = append(o.R.Files, rel)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ThenLoadUserItems(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
||||
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||||
loader, isLoader := retrieved.(interface {
|
||||
LoadUserItems(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||||
})
|
||||
if !isLoader {
|
||||
return fmt.Errorf("object %T cannot load UserItems", retrieved)
|
||||
}
|
||||
|
||||
err := loader.LoadUserItems(ctx, exec, queryMods...)
|
||||
|
||||
// Don't cause an issue due to missing relationships
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// LoadUserItems loads the user's Items into the .R struct
|
||||
func (o *User) LoadUserItems(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset the relationship
|
||||
o.R.Items = nil
|
||||
|
||||
related, err := o.Items(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, rel := range related {
|
||||
rel.R.User = o
|
||||
}
|
||||
|
||||
o.R.Items = related
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadUserItems loads the user's Items into the .R struct
|
||||
func (os UserSlice) LoadUserItems(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if len(os) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
items, err := os.Items(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
o.R.Items = nil
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
for _, rel := range items {
|
||||
if o.ID != rel.UserID.GetOrZero() {
|
||||
continue
|
||||
}
|
||||
|
||||
rel.R.User = o
|
||||
|
||||
o.R.Items = append(o.R.Items, rel)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertUserFiles0(ctx context.Context, exec bob.Executor, files1 []*FileSetter, user0 *User) (FileSlice, error) {
|
||||
for i := range files1 {
|
||||
files1[i].UserID = omitnull.From(user0.ID)
|
||||
}
|
||||
|
||||
ret, err := Files.Insert(bob.ToMods(files1...)).All(ctx, exec)
|
||||
if err != nil {
|
||||
return ret, fmt.Errorf("insertUserFiles0: %w", err)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func attachUserFiles0(ctx context.Context, exec bob.Executor, count int, files1 FileSlice, user0 *User) (FileSlice, error) {
|
||||
setter := &FileSetter{
|
||||
UserID: omitnull.From(user0.ID),
|
||||
}
|
||||
|
||||
err := files1.UpdateAll(ctx, exec, *setter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("attachUserFiles0: %w", err)
|
||||
}
|
||||
|
||||
return files1, nil
|
||||
}
|
||||
|
||||
func (user0 *User) InsertFiles(ctx context.Context, exec bob.Executor, related ...*FileSetter) error {
|
||||
if len(related) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
files1, err := insertUserFiles0(ctx, exec, related, user0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user0.R.Files = append(user0.R.Files, files1...)
|
||||
|
||||
for _, rel := range files1 {
|
||||
rel.R.User = user0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (user0 *User) AttachFiles(ctx context.Context, exec bob.Executor, related ...*File) error {
|
||||
if len(related) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
files1 := FileSlice(related)
|
||||
|
||||
_, err = attachUserFiles0(ctx, exec, len(related), files1, user0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user0.R.Files = append(user0.R.Files, files1...)
|
||||
|
||||
for _, rel := range related {
|
||||
rel.R.User = user0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertUserItems0(ctx context.Context, exec bob.Executor, items1 []*ItemSetter, user0 *User) (ItemSlice, error) {
|
||||
for i := range items1 {
|
||||
items1[i].UserID = omitnull.From(user0.ID)
|
||||
}
|
||||
|
||||
ret, err := Items.Insert(bob.ToMods(items1...)).All(ctx, exec)
|
||||
if err != nil {
|
||||
return ret, fmt.Errorf("insertUserItems0: %w", err)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func attachUserItems0(ctx context.Context, exec bob.Executor, count int, items1 ItemSlice, user0 *User) (ItemSlice, error) {
|
||||
setter := &ItemSetter{
|
||||
UserID: omitnull.From(user0.ID),
|
||||
}
|
||||
|
||||
err := items1.UpdateAll(ctx, exec, *setter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("attachUserItems0: %w", err)
|
||||
}
|
||||
|
||||
return items1, nil
|
||||
}
|
||||
|
||||
func (user0 *User) InsertItems(ctx context.Context, exec bob.Executor, related ...*ItemSetter) error {
|
||||
if len(related) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
items1, err := insertUserItems0(ctx, exec, related, user0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user0.R.Items = append(user0.R.Items, items1...)
|
||||
|
||||
for _, rel := range items1 {
|
||||
rel.R.User = user0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (user0 *User) AttachItems(ctx context.Context, exec bob.Executor, related ...*Item) error {
|
||||
if len(related) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
items1 := ItemSlice(related)
|
||||
|
||||
_, err = attachUserItems0(ctx, exec, len(related), items1, user0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user0.R.Items = append(user0.R.Items, items1...)
|
||||
|
||||
for _, rel := range related {
|
||||
rel.R.User = user0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user