diff options
Diffstat (limited to 'src/models/posts.go')
| -rw-r--r-- | src/models/posts.go | 598 |
1 files changed, 469 insertions, 129 deletions
diff --git a/src/models/posts.go b/src/models/posts.go index 7fb455c..ed337d9 100644 --- a/src/models/posts.go +++ b/src/models/posts.go @@ -29,6 +29,7 @@ type Post struct { Description null.String `boil:"description" json:"description,omitempty" toml:"description" yaml:"description,omitempty"` URL null.String `boil:"url" json:"url,omitempty" toml:"url" yaml:"url,omitempty"` UserID int `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"` + State null.String `boil:"state" json:"state,omitempty" toml:"state" yaml:"state,omitempty"` CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"` UpdatedAt time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"` @@ -42,6 +43,7 @@ var PostColumns = struct { Description string URL string UserID string + State string CreatedAt string UpdatedAt string }{ @@ -50,6 +52,7 @@ var PostColumns = struct { Description: "description", URL: "url", UserID: "user_id", + State: "state", CreatedAt: "created_at", UpdatedAt: "updated_at", } @@ -60,6 +63,7 @@ var PostTableColumns = struct { Description string URL string UserID string + State string CreatedAt string UpdatedAt string }{ @@ -68,139 +72,20 @@ var PostTableColumns = struct { Description: "posts.description", URL: "posts.url", UserID: "posts.user_id", + State: "posts.state", CreatedAt: "posts.created_at", UpdatedAt: "posts.updated_at", } // Generated where -type whereHelperint struct{ field string } - -func (w whereHelperint) EQ(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } -func (w whereHelperint) NEQ(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } -func (w whereHelperint) LT(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } -func (w whereHelperint) LTE(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } -func (w whereHelperint) GT(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } -func (w whereHelperint) GTE(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } -func (w whereHelperint) IN(slice []int) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) -} -func (w whereHelperint) NIN(slice []int) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) -} - -type whereHelperstring struct{ field string } - -func (w whereHelperstring) EQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } -func (w whereHelperstring) NEQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } -func (w whereHelperstring) LT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } -func (w whereHelperstring) LTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } -func (w whereHelperstring) GT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } -func (w whereHelperstring) GTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } -func (w whereHelperstring) LIKE(x string) qm.QueryMod { return qm.Where(w.field+" LIKE ?", x) } -func (w whereHelperstring) NLIKE(x string) qm.QueryMod { return qm.Where(w.field+" NOT LIKE ?", x) } -func (w whereHelperstring) ILIKE(x string) qm.QueryMod { return qm.Where(w.field+" ILIKE ?", x) } -func (w whereHelperstring) NILIKE(x string) qm.QueryMod { return qm.Where(w.field+" NOT ILIKE ?", x) } -func (w whereHelperstring) IN(slice []string) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) -} -func (w whereHelperstring) NIN(slice []string) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) -} - -type whereHelpernull_String struct{ field string } - -func (w whereHelpernull_String) EQ(x null.String) qm.QueryMod { - return qmhelper.WhereNullEQ(w.field, false, x) -} -func (w whereHelpernull_String) NEQ(x null.String) qm.QueryMod { - return qmhelper.WhereNullEQ(w.field, true, x) -} -func (w whereHelpernull_String) LT(x null.String) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.LT, x) -} -func (w whereHelpernull_String) LTE(x null.String) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.LTE, x) -} -func (w whereHelpernull_String) GT(x null.String) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.GT, x) -} -func (w whereHelpernull_String) GTE(x null.String) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.GTE, x) -} -func (w whereHelpernull_String) LIKE(x null.String) qm.QueryMod { - return qm.Where(w.field+" LIKE ?", x) -} -func (w whereHelpernull_String) NLIKE(x null.String) qm.QueryMod { - return qm.Where(w.field+" NOT LIKE ?", x) -} -func (w whereHelpernull_String) ILIKE(x null.String) qm.QueryMod { - return qm.Where(w.field+" ILIKE ?", x) -} -func (w whereHelpernull_String) NILIKE(x null.String) qm.QueryMod { - return qm.Where(w.field+" NOT ILIKE ?", x) -} -func (w whereHelpernull_String) IN(slice []string) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) -} -func (w whereHelpernull_String) NIN(slice []string) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) -} - -func (w whereHelpernull_String) IsNull() qm.QueryMod { return qmhelper.WhereIsNull(w.field) } -func (w whereHelpernull_String) IsNotNull() qm.QueryMod { return qmhelper.WhereIsNotNull(w.field) } - -type whereHelpertime_Time struct{ field string } - -func (w whereHelpertime_Time) EQ(x time.Time) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.EQ, x) -} -func (w whereHelpertime_Time) NEQ(x time.Time) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.NEQ, x) -} -func (w whereHelpertime_Time) LT(x time.Time) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.LT, x) -} -func (w whereHelpertime_Time) LTE(x time.Time) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.LTE, x) -} -func (w whereHelpertime_Time) GT(x time.Time) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.GT, x) -} -func (w whereHelpertime_Time) GTE(x time.Time) qm.QueryMod { - return qmhelper.Where(w.field, qmhelper.GTE, x) -} - var PostWhere = struct { ID whereHelperint Title whereHelperstring Description whereHelpernull_String URL whereHelpernull_String UserID whereHelperint + State whereHelpernull_String CreatedAt whereHelpertime_Time UpdatedAt whereHelpertime_Time }{ @@ -209,23 +94,30 @@ var PostWhere = struct { Description: whereHelpernull_String{field: "\"posts\".\"description\""}, URL: whereHelpernull_String{field: "\"posts\".\"url\""}, UserID: whereHelperint{field: "\"posts\".\"user_id\""}, + State: whereHelpernull_String{field: "\"posts\".\"state\""}, CreatedAt: whereHelpertime_Time{field: "\"posts\".\"created_at\""}, UpdatedAt: whereHelpertime_Time{field: "\"posts\".\"updated_at\""}, } // PostRels is where relationship names are stored. var PostRels = struct { - User string - Tags string + User string + Comments string + Tags string + Votes string }{ - User: "User", - Tags: "Tags", + User: "User", + Comments: "Comments", + Tags: "Tags", + Votes: "Votes", } // postR is where relationships are stored. type postR struct { - User *User `boil:"User" json:"User" toml:"User" yaml:"User"` - Tags TagSlice `boil:"Tags" json:"Tags" toml:"Tags" yaml:"Tags"` + User *User `boil:"User" json:"User" toml:"User" yaml:"User"` + Comments CommentSlice `boil:"Comments" json:"Comments" toml:"Comments" yaml:"Comments"` + Tags TagSlice `boil:"Tags" json:"Tags" toml:"Tags" yaml:"Tags"` + Votes VoteSlice `boil:"Votes" json:"Votes" toml:"Votes" yaml:"Votes"` } // NewStruct creates a new relationship struct @@ -240,6 +132,13 @@ func (r *postR) GetUser() *User { return r.User } +func (r *postR) GetComments() CommentSlice { + if r == nil { + return nil + } + return r.Comments +} + func (r *postR) GetTags() TagSlice { if r == nil { return nil @@ -247,13 +146,20 @@ func (r *postR) GetTags() TagSlice { return r.Tags } +func (r *postR) GetVotes() VoteSlice { + if r == nil { + return nil + } + return r.Votes +} + // postL is where Load methods for each relationship are stored. type postL struct{} var ( - postAllColumns = []string{"id", "title", "description", "url", "user_id", "created_at", "updated_at"} + postAllColumns = []string{"id", "title", "description", "url", "user_id", "state", "created_at", "updated_at"} postColumnsWithoutDefault = []string{"title", "user_id"} - postColumnsWithDefault = []string{"id", "description", "url", "created_at", "updated_at"} + postColumnsWithDefault = []string{"id", "description", "url", "state", "created_at", "updated_at"} postPrimaryKeyColumns = []string{"id"} postGeneratedColumns = []string{} ) @@ -574,6 +480,20 @@ func (o *Post) User(mods ...qm.QueryMod) userQuery { return Users(queryMods...) } +// Comments retrieves all the comment's Comments with an executor. +func (o *Post) Comments(mods ...qm.QueryMod) commentQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"comments\".\"post_id\"=?", o.ID), + ) + + return Comments(queryMods...) +} + // Tags retrieves all the tag's Tags with an executor. func (o *Post) Tags(mods ...qm.QueryMod) tagQuery { var queryMods []qm.QueryMod @@ -589,6 +509,20 @@ func (o *Post) Tags(mods ...qm.QueryMod) tagQuery { return Tags(queryMods...) } +// Votes retrieves all the vote's Votes with an executor. +func (o *Post) Votes(mods ...qm.QueryMod) voteQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"votes\".\"post_id\"=?", o.ID), + ) + + return Votes(queryMods...) +} + // LoadUser allows an eager lookup of values, cached into the // loaded structs of the objects. This is for an N-1 relationship. func (postL) LoadUser(ctx context.Context, e boil.ContextExecutor, singular bool, maybePost interface{}, mods queries.Applicator) error { @@ -709,6 +643,119 @@ func (postL) LoadUser(ctx context.Context, e boil.ContextExecutor, singular bool return nil } +// 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 (postL) LoadComments(ctx context.Context, e boil.ContextExecutor, singular bool, maybePost interface{}, mods queries.Applicator) error { + var slice []*Post + var object *Post + + if singular { + var ok bool + object, ok = maybePost.(*Post) + if !ok { + object = new(Post) + ok = queries.SetFromEmbeddedStruct(&object, &maybePost) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybePost)) + } + } + } else { + s, ok := maybePost.(*[]*Post) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybePost) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybePost)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &postR{} + } + args[object.ID] = struct{}{} + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &postR{} + } + 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.post_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.Post = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if local.ID == foreign.PostID { + local.R.Comments = append(local.R.Comments, foreign) + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.Post = 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 (postL) LoadTags(ctx context.Context, e boil.ContextExecutor, singular bool, maybePost interface{}, mods queries.Applicator) error { @@ -839,6 +886,119 @@ func (postL) LoadTags(ctx context.Context, e boil.ContextExecutor, singular bool return nil } +// LoadVotes 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 (postL) LoadVotes(ctx context.Context, e boil.ContextExecutor, singular bool, maybePost interface{}, mods queries.Applicator) error { + var slice []*Post + var object *Post + + if singular { + var ok bool + object, ok = maybePost.(*Post) + if !ok { + object = new(Post) + ok = queries.SetFromEmbeddedStruct(&object, &maybePost) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybePost)) + } + } + } else { + s, ok := maybePost.(*[]*Post) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybePost) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybePost)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &postR{} + } + args[object.ID] = struct{}{} + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &postR{} + } + 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(`votes`), + qm.WhereIn(`votes.post_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 votes") + } + + var resultSlice []*Vote + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice votes") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on votes") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for votes") + } + + if len(voteAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.Votes = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &voteR{} + } + foreign.R.Post = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if queries.Equal(local.ID, foreign.PostID) { + local.R.Votes = append(local.R.Votes, foreign) + if foreign.R == nil { + foreign.R = &voteR{} + } + foreign.R.Post = local + break + } + } + } + + return nil +} + // SetUser of the post to the related item. // Sets o.R.User to related. // Adds o to related.R.Posts. @@ -886,6 +1046,59 @@ func (o *Post) SetUser(ctx context.Context, exec boil.ContextExecutor, insert bo return nil } +// AddComments adds the given related objects to the existing relationships +// of the post, optionally inserting them as new records. +// Appends related to o.R.Comments. +// Sets related.R.Post appropriately. +func (o *Post) AddComments(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Comment) error { + var err error + for _, rel := range related { + if insert { + rel.PostID = 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{"post_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.PostID = o.ID + } + } + + if o.R == nil { + o.R = &postR{ + Comments: related, + } + } else { + o.R.Comments = append(o.R.Comments, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &commentR{ + Post: o, + } + } else { + rel.R.Post = o + } + } + return nil +} + // AddTags adds the given related objects to the existing relationships // of the post, optionally inserting them as new records. // Appends related to o.R.Tags. @@ -1031,6 +1244,133 @@ func removeTagsFromPostsSlice(o *Post, related []*Tag) { } } +// AddVotes adds the given related objects to the existing relationships +// of the post, optionally inserting them as new records. +// Appends related to o.R.Votes. +// Sets related.R.Post appropriately. +func (o *Post) AddVotes(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Vote) error { + var err error + for _, rel := range related { + if insert { + queries.Assign(&rel.PostID, 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 \"votes\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"post_id"}), + strmangle.WhereClause("\"", "\"", 2, votePrimaryKeyColumns), + ) + 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") + } + + queries.Assign(&rel.PostID, o.ID) + } + } + + if o.R == nil { + o.R = &postR{ + Votes: related, + } + } else { + o.R.Votes = append(o.R.Votes, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &voteR{ + Post: o, + } + } else { + rel.R.Post = o + } + } + return nil +} + +// SetVotes removes all previously related items of the +// post replacing them completely with the passed +// in related items, optionally inserting them as new records. +// Sets o.R.Post's Votes accordingly. +// Replaces o.R.Votes with related. +// Sets related.R.Post's Votes accordingly. +func (o *Post) SetVotes(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Vote) error { + query := "update \"votes\" set \"post_id\" = null where \"post_id\" = $1" + values := []interface{}{o.ID} + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, query) + fmt.Fprintln(writer, values) + } + _, err := exec.ExecContext(ctx, query, values...) + if err != nil { + return errors.Wrap(err, "failed to remove relationships before set") + } + + if o.R != nil { + for _, rel := range o.R.Votes { + queries.SetScanner(&rel.PostID, nil) + if rel.R == nil { + continue + } + + rel.R.Post = nil + } + o.R.Votes = nil + } + + return o.AddVotes(ctx, exec, insert, related...) +} + +// RemoveVotes relationships from objects passed in. +// Removes related items from R.Votes (uses pointer comparison, removal does not keep order) +// Sets related.R.Post. +func (o *Post) RemoveVotes(ctx context.Context, exec boil.ContextExecutor, related ...*Vote) error { + if len(related) == 0 { + return nil + } + + var err error + for _, rel := range related { + queries.SetScanner(&rel.PostID, nil) + if rel.R != nil { + rel.R.Post = nil + } + if _, err = rel.Update(ctx, exec, boil.Whitelist("post_id")); err != nil { + return err + } + } + if o.R == nil { + return nil + } + + for _, rel := range related { + for i, ri := range o.R.Votes { + if rel != ri { + continue + } + + ln := len(o.R.Votes) + if ln > 1 && i < ln-1 { + o.R.Votes[i] = o.R.Votes[ln-1] + } + o.R.Votes = o.R.Votes[:ln-1] + break + } + } + + return nil +} + // Posts retrieves all the records using an executor. func Posts(mods ...qm.QueryMod) postQuery { mods = append(mods, qm.From("\"posts\"")) |
