diff options
Diffstat (limited to 'src/models/users.go')
| -rw-r--r-- | src/models/users.go | 202 |
1 files changed, 196 insertions, 6 deletions
diff --git a/src/models/users.go b/src/models/users.go index bdb6a31..8155a5d 100644 --- a/src/models/users.go +++ b/src/models/users.go @@ -87,17 +87,20 @@ var UserWhere = struct { // UserRels is where relationship names are stored. var UserRels = struct { - Posts string - Tags string + Comments string + Posts string + Tags string }{ - Posts: "Posts", - Tags: "Tags", + Comments: "Comments", + 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"` + Comments CommentSlice `boil:"Comments" json:"Comments" toml:"Comments" yaml:"Comments"` + 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 @@ -105,6 +108,13 @@ func (*userR) NewStruct() *userR { return &userR{} } +func (r *userR) GetComments() CommentSlice { + if r == nil { + return nil + } + return r.Comments +} + func (r *userR) GetPosts() PostSlice { if r == nil { return nil @@ -435,6 +445,20 @@ func (q userQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, return count > 0, nil } +// Comments retrieves all the comment's Comments with an executor. +func (o *User) Comments(mods ...qm.QueryMod) commentQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"comments\".\"user_id\"=?", o.ID), + ) + + return Comments(queryMods...) +} + // Posts retrieves all the post's Posts with an executor. func (o *User) Posts(mods ...qm.QueryMod) postQuery { var queryMods []qm.QueryMod @@ -463,6 +487,119 @@ func (o *User) Tags(mods ...qm.QueryMod) tagQuery { return Tags(queryMods...) } +// LoadComments 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) LoadComments(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(`comments`), + qm.WhereIn(`comments.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 comments") + } + + var resultSlice []*Comment + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice comments") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on comments") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for comments") + } + + if len(commentAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.Comments = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.User = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if local.ID == foreign.UserID { + local.R.Comments = append(local.R.Comments, foreign) + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.User = local + break + } + } + } + + return nil +} + // 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 { @@ -689,6 +826,59 @@ func (userL) LoadTags(ctx context.Context, e boil.ContextExecutor, singular bool return nil } +// AddComments adds the given related objects to the existing relationships +// of the user, optionally inserting them as new records. +// Appends related to o.R.Comments. +// Sets related.R.User appropriately. +func (o *User) AddComments(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Comment) 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 \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"user_id"}), + strmangle.WhereClause("\"", "\"", 2, commentPrimaryKeyColumns), + ) + 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{ + Comments: related, + } + } else { + o.R.Comments = append(o.R.Comments, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &commentR{ + User: o, + } + } else { + rel.R.User = o + } + } + 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. |
