// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/community/rpc/internal/models/posts"
	"juwan-backend/app/community/rpc/internal/models/predicate"
	"juwan-backend/pkg/types"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// PostsUpdate is the builder for updating Posts entities.
type PostsUpdate struct {
	config
	hooks    []Hook
	mutation *PostsMutation
}

// Where appends a list predicates to the PostsUpdate builder.
func (_u *PostsUpdate) Where(ps ...predicate.Posts) *PostsUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetAuthorID sets the "author_id" field.
func (_u *PostsUpdate) SetAuthorID(v int64) *PostsUpdate {
	_u.mutation.ResetAuthorID()
	_u.mutation.SetAuthorID(v)
	return _u
}

// SetNillableAuthorID sets the "author_id" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableAuthorID(v *int64) *PostsUpdate {
	if v != nil {
		_u.SetAuthorID(*v)
	}
	return _u
}

// AddAuthorID adds value to the "author_id" field.
func (_u *PostsUpdate) AddAuthorID(v int64) *PostsUpdate {
	_u.mutation.AddAuthorID(v)
	return _u
}

// SetAuthorRole sets the "author_role" field.
func (_u *PostsUpdate) SetAuthorRole(v string) *PostsUpdate {
	_u.mutation.SetAuthorRole(v)
	return _u
}

// SetNillableAuthorRole sets the "author_role" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableAuthorRole(v *string) *PostsUpdate {
	if v != nil {
		_u.SetAuthorRole(*v)
	}
	return _u
}

// SetTitle sets the "title" field.
func (_u *PostsUpdate) SetTitle(v string) *PostsUpdate {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableTitle(v *string) *PostsUpdate {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetContent sets the "content" field.
func (_u *PostsUpdate) SetContent(v string) *PostsUpdate {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableContent(v *string) *PostsUpdate {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetImages sets the "images" field.
func (_u *PostsUpdate) SetImages(v types.TextArray) *PostsUpdate {
	_u.mutation.SetImages(v)
	return _u
}

// SetNillableImages sets the "images" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableImages(v *types.TextArray) *PostsUpdate {
	if v != nil {
		_u.SetImages(*v)
	}
	return _u
}

// ClearImages clears the value of the "images" field.
func (_u *PostsUpdate) ClearImages() *PostsUpdate {
	_u.mutation.ClearImages()
	return _u
}

// SetTags sets the "tags" field.
func (_u *PostsUpdate) SetTags(v types.TextArray) *PostsUpdate {
	_u.mutation.SetTags(v)
	return _u
}

// SetNillableTags sets the "tags" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableTags(v *types.TextArray) *PostsUpdate {
	if v != nil {
		_u.SetTags(*v)
	}
	return _u
}

// ClearTags clears the value of the "tags" field.
func (_u *PostsUpdate) ClearTags() *PostsUpdate {
	_u.mutation.ClearTags()
	return _u
}

// SetLinkedOrderID sets the "linked_order_id" field.
func (_u *PostsUpdate) SetLinkedOrderID(v int64) *PostsUpdate {
	_u.mutation.ResetLinkedOrderID()
	_u.mutation.SetLinkedOrderID(v)
	return _u
}

// SetNillableLinkedOrderID sets the "linked_order_id" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableLinkedOrderID(v *int64) *PostsUpdate {
	if v != nil {
		_u.SetLinkedOrderID(*v)
	}
	return _u
}

// AddLinkedOrderID adds value to the "linked_order_id" field.
func (_u *PostsUpdate) AddLinkedOrderID(v int64) *PostsUpdate {
	_u.mutation.AddLinkedOrderID(v)
	return _u
}

// ClearLinkedOrderID clears the value of the "linked_order_id" field.
func (_u *PostsUpdate) ClearLinkedOrderID() *PostsUpdate {
	_u.mutation.ClearLinkedOrderID()
	return _u
}

// SetQuotedPostID sets the "quoted_post_id" field.
func (_u *PostsUpdate) SetQuotedPostID(v int64) *PostsUpdate {
	_u.mutation.ResetQuotedPostID()
	_u.mutation.SetQuotedPostID(v)
	return _u
}

// SetNillableQuotedPostID sets the "quoted_post_id" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableQuotedPostID(v *int64) *PostsUpdate {
	if v != nil {
		_u.SetQuotedPostID(*v)
	}
	return _u
}

// AddQuotedPostID adds value to the "quoted_post_id" field.
func (_u *PostsUpdate) AddQuotedPostID(v int64) *PostsUpdate {
	_u.mutation.AddQuotedPostID(v)
	return _u
}

// ClearQuotedPostID clears the value of the "quoted_post_id" field.
func (_u *PostsUpdate) ClearQuotedPostID() *PostsUpdate {
	_u.mutation.ClearQuotedPostID()
	return _u
}

// SetLikeCount sets the "like_count" field.
func (_u *PostsUpdate) SetLikeCount(v int) *PostsUpdate {
	_u.mutation.ResetLikeCount()
	_u.mutation.SetLikeCount(v)
	return _u
}

// SetNillableLikeCount sets the "like_count" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableLikeCount(v *int) *PostsUpdate {
	if v != nil {
		_u.SetLikeCount(*v)
	}
	return _u
}

// AddLikeCount adds value to the "like_count" field.
func (_u *PostsUpdate) AddLikeCount(v int) *PostsUpdate {
	_u.mutation.AddLikeCount(v)
	return _u
}

// ClearLikeCount clears the value of the "like_count" field.
func (_u *PostsUpdate) ClearLikeCount() *PostsUpdate {
	_u.mutation.ClearLikeCount()
	return _u
}

// SetCommentCount sets the "comment_count" field.
func (_u *PostsUpdate) SetCommentCount(v int) *PostsUpdate {
	_u.mutation.ResetCommentCount()
	_u.mutation.SetCommentCount(v)
	return _u
}

// SetNillableCommentCount sets the "comment_count" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableCommentCount(v *int) *PostsUpdate {
	if v != nil {
		_u.SetCommentCount(*v)
	}
	return _u
}

// AddCommentCount adds value to the "comment_count" field.
func (_u *PostsUpdate) AddCommentCount(v int) *PostsUpdate {
	_u.mutation.AddCommentCount(v)
	return _u
}

// ClearCommentCount clears the value of the "comment_count" field.
func (_u *PostsUpdate) ClearCommentCount() *PostsUpdate {
	_u.mutation.ClearCommentCount()
	return _u
}

// SetPinned sets the "pinned" field.
func (_u *PostsUpdate) SetPinned(v bool) *PostsUpdate {
	_u.mutation.SetPinned(v)
	return _u
}

// SetNillablePinned sets the "pinned" field if the given value is not nil.
func (_u *PostsUpdate) SetNillablePinned(v *bool) *PostsUpdate {
	if v != nil {
		_u.SetPinned(*v)
	}
	return _u
}

// ClearPinned clears the value of the "pinned" field.
func (_u *PostsUpdate) ClearPinned() *PostsUpdate {
	_u.mutation.ClearPinned()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *PostsUpdate) SetUpdatedAt(v time.Time) *PostsUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *PostsUpdate) SetDeletedAt(v time.Time) *PostsUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *PostsUpdate) SetNillableDeletedAt(v *time.Time) *PostsUpdate {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *PostsUpdate) ClearDeletedAt() *PostsUpdate {
	_u.mutation.ClearDeletedAt()
	return _u
}

// Mutation returns the PostsMutation object of the builder.
func (_u *PostsUpdate) Mutation() *PostsMutation {
	return _u.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *PostsUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *PostsUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *PostsUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *PostsUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *PostsUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := posts.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *PostsUpdate) check() error {
	if v, ok := _u.mutation.AuthorRole(); ok {
		if err := posts.AuthorRoleValidator(v); err != nil {
			return &ValidationError{Name: "author_role", err: fmt.Errorf(`models: validator failed for field "Posts.author_role": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Title(); ok {
		if err := posts.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "Posts.title": %w`, err)}
		}
	}
	return nil
}

func (_u *PostsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(posts.Table, posts.Columns, sqlgraph.NewFieldSpec(posts.FieldID, field.TypeInt64))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.AuthorID(); ok {
		_spec.SetField(posts.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedAuthorID(); ok {
		_spec.AddField(posts.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AuthorRole(); ok {
		_spec.SetField(posts.FieldAuthorRole, field.TypeString, value)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(posts.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(posts.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Images(); ok {
		_spec.SetField(posts.FieldImages, field.TypeOther, value)
	}
	if _u.mutation.ImagesCleared() {
		_spec.ClearField(posts.FieldImages, field.TypeOther)
	}
	if value, ok := _u.mutation.Tags(); ok {
		_spec.SetField(posts.FieldTags, field.TypeOther, value)
	}
	if _u.mutation.TagsCleared() {
		_spec.ClearField(posts.FieldTags, field.TypeOther)
	}
	if value, ok := _u.mutation.LinkedOrderID(); ok {
		_spec.SetField(posts.FieldLinkedOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedLinkedOrderID(); ok {
		_spec.AddField(posts.FieldLinkedOrderID, field.TypeInt64, value)
	}
	if _u.mutation.LinkedOrderIDCleared() {
		_spec.ClearField(posts.FieldLinkedOrderID, field.TypeInt64)
	}
	if value, ok := _u.mutation.QuotedPostID(); ok {
		_spec.SetField(posts.FieldQuotedPostID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedQuotedPostID(); ok {
		_spec.AddField(posts.FieldQuotedPostID, field.TypeInt64, value)
	}
	if _u.mutation.QuotedPostIDCleared() {
		_spec.ClearField(posts.FieldQuotedPostID, field.TypeInt64)
	}
	if value, ok := _u.mutation.LikeCount(); ok {
		_spec.SetField(posts.FieldLikeCount, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedLikeCount(); ok {
		_spec.AddField(posts.FieldLikeCount, field.TypeInt, value)
	}
	if _u.mutation.LikeCountCleared() {
		_spec.ClearField(posts.FieldLikeCount, field.TypeInt)
	}
	if value, ok := _u.mutation.CommentCount(); ok {
		_spec.SetField(posts.FieldCommentCount, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedCommentCount(); ok {
		_spec.AddField(posts.FieldCommentCount, field.TypeInt, value)
	}
	if _u.mutation.CommentCountCleared() {
		_spec.ClearField(posts.FieldCommentCount, field.TypeInt)
	}
	if value, ok := _u.mutation.Pinned(); ok {
		_spec.SetField(posts.FieldPinned, field.TypeBool, value)
	}
	if _u.mutation.PinnedCleared() {
		_spec.ClearField(posts.FieldPinned, field.TypeBool)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(posts.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(posts.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(posts.FieldDeletedAt, field.TypeTime)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{posts.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// PostsUpdateOne is the builder for updating a single Posts entity.
type PostsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PostsMutation
}

// SetAuthorID sets the "author_id" field.
func (_u *PostsUpdateOne) SetAuthorID(v int64) *PostsUpdateOne {
	_u.mutation.ResetAuthorID()
	_u.mutation.SetAuthorID(v)
	return _u
}

// SetNillableAuthorID sets the "author_id" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableAuthorID(v *int64) *PostsUpdateOne {
	if v != nil {
		_u.SetAuthorID(*v)
	}
	return _u
}

// AddAuthorID adds value to the "author_id" field.
func (_u *PostsUpdateOne) AddAuthorID(v int64) *PostsUpdateOne {
	_u.mutation.AddAuthorID(v)
	return _u
}

// SetAuthorRole sets the "author_role" field.
func (_u *PostsUpdateOne) SetAuthorRole(v string) *PostsUpdateOne {
	_u.mutation.SetAuthorRole(v)
	return _u
}

// SetNillableAuthorRole sets the "author_role" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableAuthorRole(v *string) *PostsUpdateOne {
	if v != nil {
		_u.SetAuthorRole(*v)
	}
	return _u
}

// SetTitle sets the "title" field.
func (_u *PostsUpdateOne) SetTitle(v string) *PostsUpdateOne {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableTitle(v *string) *PostsUpdateOne {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetContent sets the "content" field.
func (_u *PostsUpdateOne) SetContent(v string) *PostsUpdateOne {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableContent(v *string) *PostsUpdateOne {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetImages sets the "images" field.
func (_u *PostsUpdateOne) SetImages(v types.TextArray) *PostsUpdateOne {
	_u.mutation.SetImages(v)
	return _u
}

// SetNillableImages sets the "images" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableImages(v *types.TextArray) *PostsUpdateOne {
	if v != nil {
		_u.SetImages(*v)
	}
	return _u
}

// ClearImages clears the value of the "images" field.
func (_u *PostsUpdateOne) ClearImages() *PostsUpdateOne {
	_u.mutation.ClearImages()
	return _u
}

// SetTags sets the "tags" field.
func (_u *PostsUpdateOne) SetTags(v types.TextArray) *PostsUpdateOne {
	_u.mutation.SetTags(v)
	return _u
}

// SetNillableTags sets the "tags" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableTags(v *types.TextArray) *PostsUpdateOne {
	if v != nil {
		_u.SetTags(*v)
	}
	return _u
}

// ClearTags clears the value of the "tags" field.
func (_u *PostsUpdateOne) ClearTags() *PostsUpdateOne {
	_u.mutation.ClearTags()
	return _u
}

// SetLinkedOrderID sets the "linked_order_id" field.
func (_u *PostsUpdateOne) SetLinkedOrderID(v int64) *PostsUpdateOne {
	_u.mutation.ResetLinkedOrderID()
	_u.mutation.SetLinkedOrderID(v)
	return _u
}

// SetNillableLinkedOrderID sets the "linked_order_id" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableLinkedOrderID(v *int64) *PostsUpdateOne {
	if v != nil {
		_u.SetLinkedOrderID(*v)
	}
	return _u
}

// AddLinkedOrderID adds value to the "linked_order_id" field.
func (_u *PostsUpdateOne) AddLinkedOrderID(v int64) *PostsUpdateOne {
	_u.mutation.AddLinkedOrderID(v)
	return _u
}

// ClearLinkedOrderID clears the value of the "linked_order_id" field.
func (_u *PostsUpdateOne) ClearLinkedOrderID() *PostsUpdateOne {
	_u.mutation.ClearLinkedOrderID()
	return _u
}

// SetQuotedPostID sets the "quoted_post_id" field.
func (_u *PostsUpdateOne) SetQuotedPostID(v int64) *PostsUpdateOne {
	_u.mutation.ResetQuotedPostID()
	_u.mutation.SetQuotedPostID(v)
	return _u
}

// SetNillableQuotedPostID sets the "quoted_post_id" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableQuotedPostID(v *int64) *PostsUpdateOne {
	if v != nil {
		_u.SetQuotedPostID(*v)
	}
	return _u
}

// AddQuotedPostID adds value to the "quoted_post_id" field.
func (_u *PostsUpdateOne) AddQuotedPostID(v int64) *PostsUpdateOne {
	_u.mutation.AddQuotedPostID(v)
	return _u
}

// ClearQuotedPostID clears the value of the "quoted_post_id" field.
func (_u *PostsUpdateOne) ClearQuotedPostID() *PostsUpdateOne {
	_u.mutation.ClearQuotedPostID()
	return _u
}

// SetLikeCount sets the "like_count" field.
func (_u *PostsUpdateOne) SetLikeCount(v int) *PostsUpdateOne {
	_u.mutation.ResetLikeCount()
	_u.mutation.SetLikeCount(v)
	return _u
}

// SetNillableLikeCount sets the "like_count" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableLikeCount(v *int) *PostsUpdateOne {
	if v != nil {
		_u.SetLikeCount(*v)
	}
	return _u
}

// AddLikeCount adds value to the "like_count" field.
func (_u *PostsUpdateOne) AddLikeCount(v int) *PostsUpdateOne {
	_u.mutation.AddLikeCount(v)
	return _u
}

// ClearLikeCount clears the value of the "like_count" field.
func (_u *PostsUpdateOne) ClearLikeCount() *PostsUpdateOne {
	_u.mutation.ClearLikeCount()
	return _u
}

// SetCommentCount sets the "comment_count" field.
func (_u *PostsUpdateOne) SetCommentCount(v int) *PostsUpdateOne {
	_u.mutation.ResetCommentCount()
	_u.mutation.SetCommentCount(v)
	return _u
}

// SetNillableCommentCount sets the "comment_count" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableCommentCount(v *int) *PostsUpdateOne {
	if v != nil {
		_u.SetCommentCount(*v)
	}
	return _u
}

// AddCommentCount adds value to the "comment_count" field.
func (_u *PostsUpdateOne) AddCommentCount(v int) *PostsUpdateOne {
	_u.mutation.AddCommentCount(v)
	return _u
}

// ClearCommentCount clears the value of the "comment_count" field.
func (_u *PostsUpdateOne) ClearCommentCount() *PostsUpdateOne {
	_u.mutation.ClearCommentCount()
	return _u
}

// SetPinned sets the "pinned" field.
func (_u *PostsUpdateOne) SetPinned(v bool) *PostsUpdateOne {
	_u.mutation.SetPinned(v)
	return _u
}

// SetNillablePinned sets the "pinned" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillablePinned(v *bool) *PostsUpdateOne {
	if v != nil {
		_u.SetPinned(*v)
	}
	return _u
}

// ClearPinned clears the value of the "pinned" field.
func (_u *PostsUpdateOne) ClearPinned() *PostsUpdateOne {
	_u.mutation.ClearPinned()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *PostsUpdateOne) SetUpdatedAt(v time.Time) *PostsUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *PostsUpdateOne) SetDeletedAt(v time.Time) *PostsUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *PostsUpdateOne) SetNillableDeletedAt(v *time.Time) *PostsUpdateOne {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *PostsUpdateOne) ClearDeletedAt() *PostsUpdateOne {
	_u.mutation.ClearDeletedAt()
	return _u
}

// Mutation returns the PostsMutation object of the builder.
func (_u *PostsUpdateOne) Mutation() *PostsMutation {
	return _u.mutation
}

// Where appends a list predicates to the PostsUpdate builder.
func (_u *PostsUpdateOne) Where(ps ...predicate.Posts) *PostsUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *PostsUpdateOne) Select(field string, fields ...string) *PostsUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated Posts entity.
func (_u *PostsUpdateOne) Save(ctx context.Context) (*Posts, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *PostsUpdateOne) SaveX(ctx context.Context) *Posts {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *PostsUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *PostsUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *PostsUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := posts.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *PostsUpdateOne) check() error {
	if v, ok := _u.mutation.AuthorRole(); ok {
		if err := posts.AuthorRoleValidator(v); err != nil {
			return &ValidationError{Name: "author_role", err: fmt.Errorf(`models: validator failed for field "Posts.author_role": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Title(); ok {
		if err := posts.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "Posts.title": %w`, err)}
		}
	}
	return nil
}

func (_u *PostsUpdateOne) sqlSave(ctx context.Context) (_node *Posts, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(posts.Table, posts.Columns, sqlgraph.NewFieldSpec(posts.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Posts.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, posts.FieldID)
		for _, f := range fields {
			if !posts.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != posts.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.AuthorID(); ok {
		_spec.SetField(posts.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedAuthorID(); ok {
		_spec.AddField(posts.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AuthorRole(); ok {
		_spec.SetField(posts.FieldAuthorRole, field.TypeString, value)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(posts.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(posts.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Images(); ok {
		_spec.SetField(posts.FieldImages, field.TypeOther, value)
	}
	if _u.mutation.ImagesCleared() {
		_spec.ClearField(posts.FieldImages, field.TypeOther)
	}
	if value, ok := _u.mutation.Tags(); ok {
		_spec.SetField(posts.FieldTags, field.TypeOther, value)
	}
	if _u.mutation.TagsCleared() {
		_spec.ClearField(posts.FieldTags, field.TypeOther)
	}
	if value, ok := _u.mutation.LinkedOrderID(); ok {
		_spec.SetField(posts.FieldLinkedOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedLinkedOrderID(); ok {
		_spec.AddField(posts.FieldLinkedOrderID, field.TypeInt64, value)
	}
	if _u.mutation.LinkedOrderIDCleared() {
		_spec.ClearField(posts.FieldLinkedOrderID, field.TypeInt64)
	}
	if value, ok := _u.mutation.QuotedPostID(); ok {
		_spec.SetField(posts.FieldQuotedPostID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedQuotedPostID(); ok {
		_spec.AddField(posts.FieldQuotedPostID, field.TypeInt64, value)
	}
	if _u.mutation.QuotedPostIDCleared() {
		_spec.ClearField(posts.FieldQuotedPostID, field.TypeInt64)
	}
	if value, ok := _u.mutation.LikeCount(); ok {
		_spec.SetField(posts.FieldLikeCount, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedLikeCount(); ok {
		_spec.AddField(posts.FieldLikeCount, field.TypeInt, value)
	}
	if _u.mutation.LikeCountCleared() {
		_spec.ClearField(posts.FieldLikeCount, field.TypeInt)
	}
	if value, ok := _u.mutation.CommentCount(); ok {
		_spec.SetField(posts.FieldCommentCount, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedCommentCount(); ok {
		_spec.AddField(posts.FieldCommentCount, field.TypeInt, value)
	}
	if _u.mutation.CommentCountCleared() {
		_spec.ClearField(posts.FieldCommentCount, field.TypeInt)
	}
	if value, ok := _u.mutation.Pinned(); ok {
		_spec.SetField(posts.FieldPinned, field.TypeBool, value)
	}
	if _u.mutation.PinnedCleared() {
		_spec.ClearField(posts.FieldPinned, field.TypeBool)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(posts.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(posts.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(posts.FieldDeletedAt, field.TypeTime)
	}
	_node = &Posts{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{posts.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
