diff options
Diffstat (limited to 'src/models/users.go')
| -rw-r--r-- | src/models/users.go | 383 |
1 files changed, 382 insertions, 1 deletions
diff --git a/src/models/users.go b/src/models/users.go index ca9bcf8..bdb6a31 100644 --- a/src/models/users.go +++ b/src/models/users.go @@ -87,10 +87,17 @@ var UserWhere = struct { // UserRels is where relationship names are stored. var UserRels = struct { -}{} + Posts string + Tags string +}{ + Posts: "Posts", + Tags: "Tags", +} // userR is where relationships are stored. type userR struct { + Posts PostSlice `boil:"Posts" json:"Posts" toml:"Posts" yaml:"Posts"` + Tags TagSlice `boil:"Tags" json:"Tags" toml:"Tags" yaml:"Tags"` } // NewStruct creates a new relationship struct @@ -98,6 +105,20 @@ func (*userR) NewStruct() *userR { return &userR{} } +func (r *userR) GetPosts() PostSlice { + if r == nil { + return nil + } + return r.Posts +} + +func (r *userR) GetTags() TagSlice { + if r == nil { + return nil + } + return r.Tags +} + // userL is where Load methods for each relationship are stored. type userL struct{} @@ -414,6 +435,366 @@ func (q userQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, return count > 0, nil } +// Posts retrieves all the post's Posts with an executor. +func (o *User) Posts(mods ...qm.QueryMod) postQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"posts\".\"user_id\"=?", o.ID), + ) + + return Posts(queryMods...) +} + +// Tags retrieves all the tag's Tags with an executor. +func (o *User) Tags(mods ...qm.QueryMod) tagQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"tags\".\"user_id\"=?", o.ID), + ) + + return Tags(queryMods...) +} + +// LoadPosts allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for a 1-M or N-M relationship. +func (userL) LoadPosts(ctx context.Context, e boil.ContextExecutor, singular bool, maybeUser interface{}, mods queries.Applicator) error { + var slice []*User + var object *User + + if singular { + var ok bool + object, ok = maybeUser.(*User) + if !ok { + object = new(User) + ok = queries.SetFromEmbeddedStruct(&object, &maybeUser) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeUser)) + } + } + } else { + s, ok := maybeUser.(*[]*User) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeUser) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeUser)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &userR{} + } + args[object.ID] = struct{}{} + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &userR{} + } + args[obj.ID] = struct{}{} + } + } + + if len(args) == 0 { + return nil + } + + argsSlice := make([]interface{}, len(args)) + i := 0 + for arg := range args { + argsSlice[i] = arg + i++ + } + + query := NewQuery( + qm.From(`posts`), + qm.WhereIn(`posts.user_id in ?`, argsSlice...), + ) + if mods != nil { + mods.Apply(query) + } + + results, err := query.QueryContext(ctx, e) + if err != nil { + return errors.Wrap(err, "failed to eager load posts") + } + + var resultSlice []*Post + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice posts") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on posts") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for posts") + } + + if len(postAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.Posts = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &postR{} + } + foreign.R.User = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if local.ID == foreign.UserID { + local.R.Posts = append(local.R.Posts, foreign) + if foreign.R == nil { + foreign.R = &postR{} + } + foreign.R.User = local + break + } + } + } + + return nil +} + +// LoadTags allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for a 1-M or N-M relationship. +func (userL) LoadTags(ctx context.Context, e boil.ContextExecutor, singular bool, maybeUser interface{}, mods queries.Applicator) error { + var slice []*User + var object *User + + if singular { + var ok bool + object, ok = maybeUser.(*User) + if !ok { + object = new(User) + ok = queries.SetFromEmbeddedStruct(&object, &maybeUser) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeUser)) + } + } + } else { + s, ok := maybeUser.(*[]*User) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeUser) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeUser)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &userR{} + } + args[object.ID] = struct{}{} + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &userR{} + } + args[obj.ID] = struct{}{} + } + } + + if len(args) == 0 { + return nil + } + + argsSlice := make([]interface{}, len(args)) + i := 0 + for arg := range args { + argsSlice[i] = arg + i++ + } + + query := NewQuery( + qm.From(`tags`), + qm.WhereIn(`tags.user_id in ?`, argsSlice...), + ) + if mods != nil { + mods.Apply(query) + } + + results, err := query.QueryContext(ctx, e) + if err != nil { + return errors.Wrap(err, "failed to eager load tags") + } + + var resultSlice []*Tag + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice tags") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on tags") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for tags") + } + + if len(tagAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.Tags = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &tagR{} + } + foreign.R.User = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if local.ID == foreign.UserID { + local.R.Tags = append(local.R.Tags, foreign) + if foreign.R == nil { + foreign.R = &tagR{} + } + foreign.R.User = local + break + } + } + } + + return nil +} + +// AddPosts adds the given related objects to the existing relationships +// of the user, optionally inserting them as new records. +// Appends related to o.R.Posts. +// Sets related.R.User appropriately. +func (o *User) AddPosts(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Post) error { + var err error + for _, rel := range related { + if insert { + rel.UserID = o.ID + if err = rel.Insert(ctx, exec, boil.Infer()); err != nil { + return errors.Wrap(err, "failed to insert into foreign table") + } + } else { + updateQuery := fmt.Sprintf( + "UPDATE \"posts\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"user_id"}), + strmangle.WhereClause("\"", "\"", 2, postPrimaryKeyColumns), + ) + values := []interface{}{o.ID, rel.ID} + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, updateQuery) + fmt.Fprintln(writer, values) + } + if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil { + return errors.Wrap(err, "failed to update foreign table") + } + + rel.UserID = o.ID + } + } + + if o.R == nil { + o.R = &userR{ + Posts: related, + } + } else { + o.R.Posts = append(o.R.Posts, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &postR{ + User: o, + } + } else { + rel.R.User = o + } + } + return nil +} + +// AddTags adds the given related objects to the existing relationships +// of the user, optionally inserting them as new records. +// Appends related to o.R.Tags. +// Sets related.R.User appropriately. +func (o *User) AddTags(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Tag) error { + var err error + for _, rel := range related { + if insert { + rel.UserID = o.ID + if err = rel.Insert(ctx, exec, boil.Infer()); err != nil { + return errors.Wrap(err, "failed to insert into foreign table") + } + } else { + updateQuery := fmt.Sprintf( + "UPDATE \"tags\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"user_id"}), + strmangle.WhereClause("\"", "\"", 2, tagPrimaryKeyColumns), + ) + values := []interface{}{o.ID, rel.ID} + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, updateQuery) + fmt.Fprintln(writer, values) + } + if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil { + return errors.Wrap(err, "failed to update foreign table") + } + + rel.UserID = o.ID + } + } + + if o.R == nil { + o.R = &userR{ + Tags: related, + } + } else { + o.R.Tags = append(o.R.Tags, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &tagR{ + User: o, + } + } else { + rel.R.User = o + } + } + return nil +} + // Users retrieves all the records using an executor. func Users(mods ...qm.QueryMod) userQuery { mods = append(mods, qm.From("\"users\"")) |
