diff --git a/backend/internal/data/ent/client.go b/backend/internal/data/ent/client.go index 44755c8..4aaad4c 100644 --- a/backend/internal/data/ent/client.go +++ b/backend/internal/data/ent/client.go @@ -22,6 +22,7 @@ import ( "github.com/hay-kot/homebox/backend/internal/data/ent/label" "github.com/hay-kot/homebox/backend/internal/data/ent/location" "github.com/hay-kot/homebox/backend/internal/data/ent/maintenanceentry" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/user" "entgo.io/ent/dialect" @@ -56,6 +57,8 @@ type Client struct { Location *LocationClient // MaintenanceEntry is the client for interacting with the MaintenanceEntry builders. MaintenanceEntry *MaintenanceEntryClient + // Notifier is the client for interacting with the Notifier builders. + Notifier *NotifierClient // User is the client for interacting with the User builders. User *UserClient } @@ -82,6 +85,7 @@ func (c *Client) init() { c.Label = NewLabelClient(c.config) c.Location = NewLocationClient(c.config) c.MaintenanceEntry = NewMaintenanceEntryClient(c.config) + c.Notifier = NewNotifierClient(c.config) c.User = NewUserClient(c.config) } @@ -127,6 +131,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { Label: NewLabelClient(cfg), Location: NewLocationClient(cfg), MaintenanceEntry: NewMaintenanceEntryClient(cfg), + Notifier: NewNotifierClient(cfg), User: NewUserClient(cfg), }, nil } @@ -158,6 +163,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) Label: NewLabelClient(cfg), Location: NewLocationClient(cfg), MaintenanceEntry: NewMaintenanceEntryClient(cfg), + Notifier: NewNotifierClient(cfg), User: NewUserClient(cfg), }, nil } @@ -198,6 +204,7 @@ func (c *Client) Use(hooks ...Hook) { c.Label.Use(hooks...) c.Location.Use(hooks...) c.MaintenanceEntry.Use(hooks...) + c.Notifier.Use(hooks...) c.User.Use(hooks...) } @@ -215,6 +222,7 @@ func (c *Client) Intercept(interceptors ...Interceptor) { c.Label.Intercept(interceptors...) c.Location.Intercept(interceptors...) c.MaintenanceEntry.Intercept(interceptors...) + c.Notifier.Intercept(interceptors...) c.User.Intercept(interceptors...) } @@ -243,6 +251,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { return c.Location.mutate(ctx, m) case *MaintenanceEntryMutation: return c.MaintenanceEntry.mutate(ctx, m) + case *NotifierMutation: + return c.Notifier.mutate(ctx, m) case *UserMutation: return c.User.mutate(ctx, m) default: @@ -2028,6 +2038,140 @@ func (c *MaintenanceEntryClient) mutate(ctx context.Context, m *MaintenanceEntry } } +// NotifierClient is a client for the Notifier schema. +type NotifierClient struct { + config +} + +// NewNotifierClient returns a client for the Notifier from the given config. +func NewNotifierClient(c config) *NotifierClient { + return &NotifierClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `notifier.Hooks(f(g(h())))`. +func (c *NotifierClient) Use(hooks ...Hook) { + c.hooks.Notifier = append(c.hooks.Notifier, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `notifier.Intercept(f(g(h())))`. +func (c *NotifierClient) Intercept(interceptors ...Interceptor) { + c.inters.Notifier = append(c.inters.Notifier, interceptors...) +} + +// Create returns a builder for creating a Notifier entity. +func (c *NotifierClient) Create() *NotifierCreate { + mutation := newNotifierMutation(c.config, OpCreate) + return &NotifierCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of Notifier entities. +func (c *NotifierClient) CreateBulk(builders ...*NotifierCreate) *NotifierCreateBulk { + return &NotifierCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for Notifier. +func (c *NotifierClient) Update() *NotifierUpdate { + mutation := newNotifierMutation(c.config, OpUpdate) + return &NotifierUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *NotifierClient) UpdateOne(n *Notifier) *NotifierUpdateOne { + mutation := newNotifierMutation(c.config, OpUpdateOne, withNotifier(n)) + return &NotifierUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *NotifierClient) UpdateOneID(id uuid.UUID) *NotifierUpdateOne { + mutation := newNotifierMutation(c.config, OpUpdateOne, withNotifierID(id)) + return &NotifierUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for Notifier. +func (c *NotifierClient) Delete() *NotifierDelete { + mutation := newNotifierMutation(c.config, OpDelete) + return &NotifierDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *NotifierClient) DeleteOne(n *Notifier) *NotifierDeleteOne { + return c.DeleteOneID(n.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *NotifierClient) DeleteOneID(id uuid.UUID) *NotifierDeleteOne { + builder := c.Delete().Where(notifier.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &NotifierDeleteOne{builder} +} + +// Query returns a query builder for Notifier. +func (c *NotifierClient) Query() *NotifierQuery { + return &NotifierQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeNotifier}, + inters: c.Interceptors(), + } +} + +// Get returns a Notifier entity by its id. +func (c *NotifierClient) Get(ctx context.Context, id uuid.UUID) (*Notifier, error) { + return c.Query().Where(notifier.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *NotifierClient) GetX(ctx context.Context, id uuid.UUID) *Notifier { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryUser queries the user edge of a Notifier. +func (c *NotifierClient) QueryUser(n *Notifier) *UserQuery { + query := (&UserClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := n.ID + step := sqlgraph.NewStep( + sqlgraph.From(notifier.Table, notifier.FieldID, id), + sqlgraph.To(user.Table, user.FieldID), + sqlgraph.Edge(sqlgraph.M2O, true, notifier.UserTable, notifier.UserColumn), + ) + fromV = sqlgraph.Neighbors(n.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *NotifierClient) Hooks() []Hook { + return c.hooks.Notifier +} + +// Interceptors returns the client interceptors. +func (c *NotifierClient) Interceptors() []Interceptor { + return c.inters.Notifier +} + +func (c *NotifierClient) mutate(ctx context.Context, m *NotifierMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&NotifierCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&NotifierUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&NotifierUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&NotifierDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown Notifier mutation op: %q", m.Op()) + } +} + // UserClient is a client for the User schema. type UserClient struct { config @@ -2153,6 +2297,22 @@ func (c *UserClient) QueryAuthTokens(u *User) *AuthTokensQuery { return query } +// QueryNotifiers queries the notifiers edge of a User. +func (c *UserClient) QueryNotifiers(u *User) *NotifierQuery { + query := (&NotifierClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := u.ID + step := sqlgraph.NewStep( + sqlgraph.From(user.Table, user.FieldID, id), + sqlgraph.To(notifier.Table, notifier.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, user.NotifiersTable, user.NotifiersColumn), + ) + fromV = sqlgraph.Neighbors(u.driver.Dialect(), step) + return fromV, nil + } + return query +} + // Hooks returns the client hooks. func (c *UserClient) Hooks() []Hook { return c.hooks.User diff --git a/backend/internal/data/ent/config.go b/backend/internal/data/ent/config.go index c4dc769..afb648b 100644 --- a/backend/internal/data/ent/config.go +++ b/backend/internal/data/ent/config.go @@ -38,6 +38,7 @@ type ( Label []ent.Hook Location []ent.Hook MaintenanceEntry []ent.Hook + Notifier []ent.Hook User []ent.Hook } inters struct { @@ -52,6 +53,7 @@ type ( Label []ent.Interceptor Location []ent.Interceptor MaintenanceEntry []ent.Interceptor + Notifier []ent.Interceptor User []ent.Interceptor } ) diff --git a/backend/internal/data/ent/ent.go b/backend/internal/data/ent/ent.go index 27d53ca..6963add 100644 --- a/backend/internal/data/ent/ent.go +++ b/backend/internal/data/ent/ent.go @@ -22,6 +22,7 @@ import ( "github.com/hay-kot/homebox/backend/internal/data/ent/label" "github.com/hay-kot/homebox/backend/internal/data/ent/location" "github.com/hay-kot/homebox/backend/internal/data/ent/maintenanceentry" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/user" ) @@ -61,6 +62,7 @@ func columnChecker(table string) func(string) error { label.Table: label.ValidColumn, location.Table: location.ValidColumn, maintenanceentry.Table: maintenanceentry.ValidColumn, + notifier.Table: notifier.ValidColumn, user.Table: user.ValidColumn, } check, ok := checks[table] diff --git a/backend/internal/data/ent/has_id.go b/backend/internal/data/ent/has_id.go index 875ba0d..0877caa 100644 --- a/backend/internal/data/ent/has_id.go +++ b/backend/internal/data/ent/has_id.go @@ -48,6 +48,10 @@ func (me *MaintenanceEntry) GetID() uuid.UUID { return me.ID } +func (n *Notifier) GetID() uuid.UUID { + return n.ID +} + func (u *User) GetID() uuid.UUID { return u.ID } diff --git a/backend/internal/data/ent/hook/hook.go b/backend/internal/data/ent/hook/hook.go index f4fb2ca..4648b23 100644 --- a/backend/internal/data/ent/hook/hook.go +++ b/backend/internal/data/ent/hook/hook.go @@ -141,6 +141,18 @@ func (f MaintenanceEntryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.V return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MaintenanceEntryMutation", m) } +// The NotifierFunc type is an adapter to allow the use of ordinary +// function as Notifier mutator. +type NotifierFunc func(context.Context, *ent.NotifierMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f NotifierFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.NotifierMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.NotifierMutation", m) +} + // The UserFunc type is an adapter to allow the use of ordinary // function as User mutator. type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error) diff --git a/backend/internal/data/ent/migrate/schema.go b/backend/internal/data/ent/migrate/schema.go index 9cfefa9..929bc5c 100644 --- a/backend/internal/data/ent/migrate/schema.go +++ b/backend/internal/data/ent/migrate/schema.go @@ -344,6 +344,42 @@ var ( }, }, } + // NotifiersColumns holds the columns for the "notifiers" table. + NotifiersColumns = []*schema.Column{ + {Name: "id", Type: field.TypeUUID}, + {Name: "created_at", Type: field.TypeTime}, + {Name: "updated_at", Type: field.TypeTime}, + {Name: "name", Type: field.TypeString, Size: 255}, + {Name: "url", Type: field.TypeString, Size: 2083}, + {Name: "is_active", Type: field.TypeBool, Default: true}, + {Name: "user_id", Type: field.TypeUUID}, + } + // NotifiersTable holds the schema information for the "notifiers" table. + NotifiersTable = &schema.Table{ + Name: "notifiers", + Columns: NotifiersColumns, + PrimaryKey: []*schema.Column{NotifiersColumns[0]}, + ForeignKeys: []*schema.ForeignKey{ + { + Symbol: "notifiers_users_notifiers", + Columns: []*schema.Column{NotifiersColumns[6]}, + RefColumns: []*schema.Column{UsersColumns[0]}, + OnDelete: schema.Cascade, + }, + }, + Indexes: []*schema.Index{ + { + Name: "notifier_user_id", + Unique: false, + Columns: []*schema.Column{NotifiersColumns[6]}, + }, + { + Name: "notifier_user_id_is_active", + Unique: false, + Columns: []*schema.Column{NotifiersColumns[6], NotifiersColumns[5]}, + }, + }, + } // UsersColumns holds the columns for the "users" table. UsersColumns = []*schema.Column{ {Name: "id", Type: field.TypeUUID}, @@ -353,8 +389,8 @@ var ( {Name: "email", Type: field.TypeString, Unique: true, Size: 255}, {Name: "password", Type: field.TypeString, Size: 255}, {Name: "is_superuser", Type: field.TypeBool, Default: false}, - {Name: "role", Type: field.TypeEnum, Enums: []string{"user", "owner"}, Default: "user"}, {Name: "superuser", Type: field.TypeBool, Default: false}, + {Name: "role", Type: field.TypeEnum, Enums: []string{"user", "owner"}, Default: "user"}, {Name: "activated_on", Type: field.TypeTime, Nullable: true}, {Name: "group_users", Type: field.TypeUUID}, } @@ -410,6 +446,7 @@ var ( LabelsTable, LocationsTable, MaintenanceEntriesTable, + NotifiersTable, UsersTable, LabelItemsTable, } @@ -430,6 +467,7 @@ func init() { LocationsTable.ForeignKeys[0].RefTable = GroupsTable LocationsTable.ForeignKeys[1].RefTable = LocationsTable MaintenanceEntriesTable.ForeignKeys[0].RefTable = ItemsTable + NotifiersTable.ForeignKeys[0].RefTable = UsersTable UsersTable.ForeignKeys[0].RefTable = GroupsTable LabelItemsTable.ForeignKeys[0].RefTable = LabelsTable LabelItemsTable.ForeignKeys[1].RefTable = ItemsTable diff --git a/backend/internal/data/ent/mutation.go b/backend/internal/data/ent/mutation.go index 89a3a71..30862cb 100644 --- a/backend/internal/data/ent/mutation.go +++ b/backend/internal/data/ent/mutation.go @@ -21,6 +21,7 @@ import ( "github.com/hay-kot/homebox/backend/internal/data/ent/label" "github.com/hay-kot/homebox/backend/internal/data/ent/location" "github.com/hay-kot/homebox/backend/internal/data/ent/maintenanceentry" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" "github.com/hay-kot/homebox/backend/internal/data/ent/user" @@ -48,6 +49,7 @@ const ( TypeLabel = "Label" TypeLocation = "Location" TypeMaintenanceEntry = "MaintenanceEntry" + TypeNotifier = "Notifier" TypeUser = "User" ) @@ -9774,6 +9776,661 @@ func (m *MaintenanceEntryMutation) ResetEdge(name string) error { return fmt.Errorf("unknown MaintenanceEntry edge %s", name) } +// NotifierMutation represents an operation that mutates the Notifier nodes in the graph. +type NotifierMutation struct { + config + op Op + typ string + id *uuid.UUID + created_at *time.Time + updated_at *time.Time + name *string + url *string + is_active *bool + clearedFields map[string]struct{} + user *uuid.UUID + cleareduser bool + done bool + oldValue func(context.Context) (*Notifier, error) + predicates []predicate.Notifier +} + +var _ ent.Mutation = (*NotifierMutation)(nil) + +// notifierOption allows management of the mutation configuration using functional options. +type notifierOption func(*NotifierMutation) + +// newNotifierMutation creates new mutation for the Notifier entity. +func newNotifierMutation(c config, op Op, opts ...notifierOption) *NotifierMutation { + m := &NotifierMutation{ + config: c, + op: op, + typ: TypeNotifier, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withNotifierID sets the ID field of the mutation. +func withNotifierID(id uuid.UUID) notifierOption { + return func(m *NotifierMutation) { + var ( + err error + once sync.Once + value *Notifier + ) + m.oldValue = func(ctx context.Context) (*Notifier, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().Notifier.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withNotifier sets the old Notifier of the mutation. +func withNotifier(node *Notifier) notifierOption { + return func(m *NotifierMutation) { + m.oldValue = func(context.Context) (*Notifier, error) { + return node, nil + } + m.id = &node.ID + } +} + +// Client returns a new `ent.Client` from the mutation. If the mutation was +// executed in a transaction (ent.Tx), a transactional client is returned. +func (m NotifierMutation) Client() *Client { + client := &Client{config: m.config} + client.init() + return client +} + +// Tx returns an `ent.Tx` for mutations that were executed in transactions; +// it returns an error otherwise. +func (m NotifierMutation) Tx() (*Tx, error) { + if _, ok := m.driver.(*txDriver); !ok { + return nil, errors.New("ent: mutation is not running in a transaction") + } + tx := &Tx{config: m.config} + tx.init() + return tx, nil +} + +// SetID sets the value of the id field. Note that this +// operation is only accepted on creation of Notifier entities. +func (m *NotifierMutation) SetID(id uuid.UUID) { + m.id = &id +} + +// ID returns the ID value in the mutation. Note that the ID is only available +// if it was provided to the builder or after it was returned from the database. +func (m *NotifierMutation) ID() (id uuid.UUID, exists bool) { + if m.id == nil { + return + } + return *m.id, true +} + +// IDs queries the database and returns the entity ids that match the mutation's predicate. +// That means, if the mutation is applied within a transaction with an isolation level such +// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated +// or updated by the mutation. +func (m *NotifierMutation) IDs(ctx context.Context) ([]uuid.UUID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []uuid.UUID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Notifier.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetCreatedAt sets the "created_at" field. +func (m *NotifierMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *NotifierMutation) CreatedAt() (r time.Time, exists bool) { + v := m.created_at + if v == nil { + return + } + return *v, true +} + +// OldCreatedAt returns the old "created_at" field's value of the Notifier entity. +// If the Notifier object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *NotifierMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldCreatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err) + } + return oldValue.CreatedAt, nil +} + +// ResetCreatedAt resets all changes to the "created_at" field. +func (m *NotifierMutation) ResetCreatedAt() { + m.created_at = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *NotifierMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *NotifierMutation) UpdatedAt() (r time.Time, exists bool) { + v := m.updated_at + if v == nil { + return + } + return *v, true +} + +// OldUpdatedAt returns the old "updated_at" field's value of the Notifier entity. +// If the Notifier object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *NotifierMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUpdatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUpdatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err) + } + return oldValue.UpdatedAt, nil +} + +// ResetUpdatedAt resets all changes to the "updated_at" field. +func (m *NotifierMutation) ResetUpdatedAt() { + m.updated_at = nil +} + +// SetUserID sets the "user_id" field. +func (m *NotifierMutation) SetUserID(u uuid.UUID) { + m.user = &u +} + +// UserID returns the value of the "user_id" field in the mutation. +func (m *NotifierMutation) UserID() (r uuid.UUID, exists bool) { + v := m.user + if v == nil { + return + } + return *v, true +} + +// OldUserID returns the old "user_id" field's value of the Notifier entity. +// If the Notifier object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *NotifierMutation) OldUserID(ctx context.Context) (v uuid.UUID, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUserID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUserID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUserID: %w", err) + } + return oldValue.UserID, nil +} + +// ResetUserID resets all changes to the "user_id" field. +func (m *NotifierMutation) ResetUserID() { + m.user = nil +} + +// SetName sets the "name" field. +func (m *NotifierMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *NotifierMutation) Name() (r string, exists bool) { + v := m.name + if v == nil { + return + } + return *v, true +} + +// OldName returns the old "name" field's value of the Notifier entity. +// If the Notifier object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *NotifierMutation) OldName(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldName is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldName requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldName: %w", err) + } + return oldValue.Name, nil +} + +// ResetName resets all changes to the "name" field. +func (m *NotifierMutation) ResetName() { + m.name = nil +} + +// SetURL sets the "url" field. +func (m *NotifierMutation) SetURL(s string) { + m.url = &s +} + +// URL returns the value of the "url" field in the mutation. +func (m *NotifierMutation) URL() (r string, exists bool) { + v := m.url + if v == nil { + return + } + return *v, true +} + +// OldURL returns the old "url" field's value of the Notifier entity. +// If the Notifier object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *NotifierMutation) OldURL(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldURL is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldURL requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldURL: %w", err) + } + return oldValue.URL, nil +} + +// ResetURL resets all changes to the "url" field. +func (m *NotifierMutation) ResetURL() { + m.url = nil +} + +// SetIsActive sets the "is_active" field. +func (m *NotifierMutation) SetIsActive(b bool) { + m.is_active = &b +} + +// IsActive returns the value of the "is_active" field in the mutation. +func (m *NotifierMutation) IsActive() (r bool, exists bool) { + v := m.is_active + if v == nil { + return + } + return *v, true +} + +// OldIsActive returns the old "is_active" field's value of the Notifier entity. +// If the Notifier object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *NotifierMutation) OldIsActive(ctx context.Context) (v bool, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldIsActive is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldIsActive requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldIsActive: %w", err) + } + return oldValue.IsActive, nil +} + +// ResetIsActive resets all changes to the "is_active" field. +func (m *NotifierMutation) ResetIsActive() { + m.is_active = nil +} + +// ClearUser clears the "user" edge to the User entity. +func (m *NotifierMutation) ClearUser() { + m.cleareduser = true +} + +// UserCleared reports if the "user" edge to the User entity was cleared. +func (m *NotifierMutation) UserCleared() bool { + return m.cleareduser +} + +// UserIDs returns the "user" edge IDs in the mutation. +// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use +// UserID instead. It exists only for internal usage by the builders. +func (m *NotifierMutation) UserIDs() (ids []uuid.UUID) { + if id := m.user; id != nil { + ids = append(ids, *id) + } + return +} + +// ResetUser resets all changes to the "user" edge. +func (m *NotifierMutation) ResetUser() { + m.user = nil + m.cleareduser = false +} + +// Where appends a list predicates to the NotifierMutation builder. +func (m *NotifierMutation) Where(ps ...predicate.Notifier) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the NotifierMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *NotifierMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.Notifier, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *NotifierMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *NotifierMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (Notifier). +func (m *NotifierMutation) Type() string { + return m.typ +} + +// Fields returns all fields that were changed during this mutation. Note that in +// order to get all numeric fields that were incremented/decremented, call +// AddedFields(). +func (m *NotifierMutation) Fields() []string { + fields := make([]string, 0, 6) + if m.created_at != nil { + fields = append(fields, notifier.FieldCreatedAt) + } + if m.updated_at != nil { + fields = append(fields, notifier.FieldUpdatedAt) + } + if m.user != nil { + fields = append(fields, notifier.FieldUserID) + } + if m.name != nil { + fields = append(fields, notifier.FieldName) + } + if m.url != nil { + fields = append(fields, notifier.FieldURL) + } + if m.is_active != nil { + fields = append(fields, notifier.FieldIsActive) + } + return fields +} + +// Field returns the value of a field with the given name. The second boolean +// return value indicates that this field was not set, or was not defined in the +// schema. +func (m *NotifierMutation) Field(name string) (ent.Value, bool) { + switch name { + case notifier.FieldCreatedAt: + return m.CreatedAt() + case notifier.FieldUpdatedAt: + return m.UpdatedAt() + case notifier.FieldUserID: + return m.UserID() + case notifier.FieldName: + return m.Name() + case notifier.FieldURL: + return m.URL() + case notifier.FieldIsActive: + return m.IsActive() + } + return nil, false +} + +// OldField returns the old value of the field from the database. An error is +// returned if the mutation operation is not UpdateOne, or the query to the +// database failed. +func (m *NotifierMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case notifier.FieldCreatedAt: + return m.OldCreatedAt(ctx) + case notifier.FieldUpdatedAt: + return m.OldUpdatedAt(ctx) + case notifier.FieldUserID: + return m.OldUserID(ctx) + case notifier.FieldName: + return m.OldName(ctx) + case notifier.FieldURL: + return m.OldURL(ctx) + case notifier.FieldIsActive: + return m.OldIsActive(ctx) + } + return nil, fmt.Errorf("unknown Notifier field %s", name) +} + +// SetField sets the value of a field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *NotifierMutation) SetField(name string, value ent.Value) error { + switch name { + case notifier.FieldCreatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetCreatedAt(v) + return nil + case notifier.FieldUpdatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUpdatedAt(v) + return nil + case notifier.FieldUserID: + v, ok := value.(uuid.UUID) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUserID(v) + return nil + case notifier.FieldName: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetName(v) + return nil + case notifier.FieldURL: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetURL(v) + return nil + case notifier.FieldIsActive: + v, ok := value.(bool) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetIsActive(v) + return nil + } + return fmt.Errorf("unknown Notifier field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *NotifierMutation) AddedFields() []string { + return nil +} + +// AddedField returns the numeric value that was incremented/decremented on a field +// with the given name. The second boolean return value indicates that this field +// was not set, or was not defined in the schema. +func (m *NotifierMutation) AddedField(name string) (ent.Value, bool) { + return nil, false +} + +// AddField adds the value to the field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *NotifierMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown Notifier numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *NotifierMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *NotifierMutation) FieldCleared(name string) bool { + _, ok := m.clearedFields[name] + return ok +} + +// ClearField clears the value of the field with the given name. It returns an +// error if the field is not defined in the schema. +func (m *NotifierMutation) ClearField(name string) error { + return fmt.Errorf("unknown Notifier nullable field %s", name) +} + +// ResetField resets all changes in the mutation for the field with the given name. +// It returns an error if the field is not defined in the schema. +func (m *NotifierMutation) ResetField(name string) error { + switch name { + case notifier.FieldCreatedAt: + m.ResetCreatedAt() + return nil + case notifier.FieldUpdatedAt: + m.ResetUpdatedAt() + return nil + case notifier.FieldUserID: + m.ResetUserID() + return nil + case notifier.FieldName: + m.ResetName() + return nil + case notifier.FieldURL: + m.ResetURL() + return nil + case notifier.FieldIsActive: + m.ResetIsActive() + return nil + } + return fmt.Errorf("unknown Notifier field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *NotifierMutation) AddedEdges() []string { + edges := make([]string, 0, 1) + if m.user != nil { + edges = append(edges, notifier.EdgeUser) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *NotifierMutation) AddedIDs(name string) []ent.Value { + switch name { + case notifier.EdgeUser: + if id := m.user; id != nil { + return []ent.Value{*id} + } + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *NotifierMutation) RemovedEdges() []string { + edges := make([]string, 0, 1) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *NotifierMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *NotifierMutation) ClearedEdges() []string { + edges := make([]string, 0, 1) + if m.cleareduser { + edges = append(edges, notifier.EdgeUser) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *NotifierMutation) EdgeCleared(name string) bool { + switch name { + case notifier.EdgeUser: + return m.cleareduser + } + return false +} + +// ClearEdge clears the value of the edge with the given name. It returns an error +// if that edge is not defined in the schema. +func (m *NotifierMutation) ClearEdge(name string) error { + switch name { + case notifier.EdgeUser: + m.ClearUser() + return nil + } + return fmt.Errorf("unknown Notifier unique edge %s", name) +} + +// ResetEdge resets all changes to the edge with the given name in this mutation. +// It returns an error if the edge is not defined in the schema. +func (m *NotifierMutation) ResetEdge(name string) error { + switch name { + case notifier.EdgeUser: + m.ResetUser() + return nil + } + return fmt.Errorf("unknown Notifier edge %s", name) +} + // UserMutation represents an operation that mutates the User nodes in the graph. type UserMutation struct { config @@ -9786,8 +10443,8 @@ type UserMutation struct { email *string password *string is_superuser *bool - role *user.Role superuser *bool + role *user.Role activated_on *time.Time clearedFields map[string]struct{} group *uuid.UUID @@ -9795,6 +10452,9 @@ type UserMutation struct { auth_tokens map[uuid.UUID]struct{} removedauth_tokens map[uuid.UUID]struct{} clearedauth_tokens bool + notifiers map[uuid.UUID]struct{} + removednotifiers map[uuid.UUID]struct{} + clearednotifiers bool done bool oldValue func(context.Context) (*User, error) predicates []predicate.User @@ -10120,42 +10780,6 @@ func (m *UserMutation) ResetIsSuperuser() { m.is_superuser = nil } -// SetRole sets the "role" field. -func (m *UserMutation) SetRole(u user.Role) { - m.role = &u -} - -// Role returns the value of the "role" field in the mutation. -func (m *UserMutation) Role() (r user.Role, exists bool) { - v := m.role - if v == nil { - return - } - return *v, true -} - -// OldRole returns the old "role" field's value of the User entity. -// If the User object wasn't provided to the builder, the object is fetched from the database. -// An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *UserMutation) OldRole(ctx context.Context) (v user.Role, err error) { - if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldRole is only allowed on UpdateOne operations") - } - if m.id == nil || m.oldValue == nil { - return v, errors.New("OldRole requires an ID field in the mutation") - } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldRole: %w", err) - } - return oldValue.Role, nil -} - -// ResetRole resets all changes to the "role" field. -func (m *UserMutation) ResetRole() { - m.role = nil -} - // SetSuperuser sets the "superuser" field. func (m *UserMutation) SetSuperuser(b bool) { m.superuser = &b @@ -10192,6 +10816,42 @@ func (m *UserMutation) ResetSuperuser() { m.superuser = nil } +// SetRole sets the "role" field. +func (m *UserMutation) SetRole(u user.Role) { + m.role = &u +} + +// Role returns the value of the "role" field in the mutation. +func (m *UserMutation) Role() (r user.Role, exists bool) { + v := m.role + if v == nil { + return + } + return *v, true +} + +// OldRole returns the old "role" field's value of the User entity. +// If the User object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UserMutation) OldRole(ctx context.Context) (v user.Role, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldRole is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldRole requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldRole: %w", err) + } + return oldValue.Role, nil +} + +// ResetRole resets all changes to the "role" field. +func (m *UserMutation) ResetRole() { + m.role = nil +} + // SetActivatedOn sets the "activated_on" field. func (m *UserMutation) SetActivatedOn(t time.Time) { m.activated_on = &t @@ -10334,6 +10994,60 @@ func (m *UserMutation) ResetAuthTokens() { m.removedauth_tokens = nil } +// AddNotifierIDs adds the "notifiers" edge to the Notifier entity by ids. +func (m *UserMutation) AddNotifierIDs(ids ...uuid.UUID) { + if m.notifiers == nil { + m.notifiers = make(map[uuid.UUID]struct{}) + } + for i := range ids { + m.notifiers[ids[i]] = struct{}{} + } +} + +// ClearNotifiers clears the "notifiers" edge to the Notifier entity. +func (m *UserMutation) ClearNotifiers() { + m.clearednotifiers = true +} + +// NotifiersCleared reports if the "notifiers" edge to the Notifier entity was cleared. +func (m *UserMutation) NotifiersCleared() bool { + return m.clearednotifiers +} + +// RemoveNotifierIDs removes the "notifiers" edge to the Notifier entity by IDs. +func (m *UserMutation) RemoveNotifierIDs(ids ...uuid.UUID) { + if m.removednotifiers == nil { + m.removednotifiers = make(map[uuid.UUID]struct{}) + } + for i := range ids { + delete(m.notifiers, ids[i]) + m.removednotifiers[ids[i]] = struct{}{} + } +} + +// RemovedNotifiers returns the removed IDs of the "notifiers" edge to the Notifier entity. +func (m *UserMutation) RemovedNotifiersIDs() (ids []uuid.UUID) { + for id := range m.removednotifiers { + ids = append(ids, id) + } + return +} + +// NotifiersIDs returns the "notifiers" edge IDs in the mutation. +func (m *UserMutation) NotifiersIDs() (ids []uuid.UUID) { + for id := range m.notifiers { + ids = append(ids, id) + } + return +} + +// ResetNotifiers resets all changes to the "notifiers" edge. +func (m *UserMutation) ResetNotifiers() { + m.notifiers = nil + m.clearednotifiers = false + m.removednotifiers = nil +} + // Where appends a list predicates to the UserMutation builder. func (m *UserMutation) Where(ps ...predicate.User) { m.predicates = append(m.predicates, ps...) @@ -10387,12 +11101,12 @@ func (m *UserMutation) Fields() []string { if m.is_superuser != nil { fields = append(fields, user.FieldIsSuperuser) } - if m.role != nil { - fields = append(fields, user.FieldRole) - } if m.superuser != nil { fields = append(fields, user.FieldSuperuser) } + if m.role != nil { + fields = append(fields, user.FieldRole) + } if m.activated_on != nil { fields = append(fields, user.FieldActivatedOn) } @@ -10416,10 +11130,10 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { return m.Password() case user.FieldIsSuperuser: return m.IsSuperuser() - case user.FieldRole: - return m.Role() case user.FieldSuperuser: return m.Superuser() + case user.FieldRole: + return m.Role() case user.FieldActivatedOn: return m.ActivatedOn() } @@ -10443,10 +11157,10 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldPassword(ctx) case user.FieldIsSuperuser: return m.OldIsSuperuser(ctx) - case user.FieldRole: - return m.OldRole(ctx) case user.FieldSuperuser: return m.OldSuperuser(ctx) + case user.FieldRole: + return m.OldRole(ctx) case user.FieldActivatedOn: return m.OldActivatedOn(ctx) } @@ -10500,13 +11214,6 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetIsSuperuser(v) return nil - case user.FieldRole: - v, ok := value.(user.Role) - if !ok { - return fmt.Errorf("unexpected type %T for field %s", value, name) - } - m.SetRole(v) - return nil case user.FieldSuperuser: v, ok := value.(bool) if !ok { @@ -10514,6 +11221,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetSuperuser(v) return nil + case user.FieldRole: + v, ok := value.(user.Role) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetRole(v) + return nil case user.FieldActivatedOn: v, ok := value.(time.Time) if !ok { @@ -10597,12 +11311,12 @@ func (m *UserMutation) ResetField(name string) error { case user.FieldIsSuperuser: m.ResetIsSuperuser() return nil - case user.FieldRole: - m.ResetRole() - return nil case user.FieldSuperuser: m.ResetSuperuser() return nil + case user.FieldRole: + m.ResetRole() + return nil case user.FieldActivatedOn: m.ResetActivatedOn() return nil @@ -10612,13 +11326,16 @@ func (m *UserMutation) ResetField(name string) error { // AddedEdges returns all edge names that were set/added in this mutation. func (m *UserMutation) AddedEdges() []string { - edges := make([]string, 0, 2) + edges := make([]string, 0, 3) if m.group != nil { edges = append(edges, user.EdgeGroup) } if m.auth_tokens != nil { edges = append(edges, user.EdgeAuthTokens) } + if m.notifiers != nil { + edges = append(edges, user.EdgeNotifiers) + } return edges } @@ -10636,16 +11353,25 @@ func (m *UserMutation) AddedIDs(name string) []ent.Value { ids = append(ids, id) } return ids + case user.EdgeNotifiers: + ids := make([]ent.Value, 0, len(m.notifiers)) + for id := range m.notifiers { + ids = append(ids, id) + } + return ids } return nil } // RemovedEdges returns all edge names that were removed in this mutation. func (m *UserMutation) RemovedEdges() []string { - edges := make([]string, 0, 2) + edges := make([]string, 0, 3) if m.removedauth_tokens != nil { edges = append(edges, user.EdgeAuthTokens) } + if m.removednotifiers != nil { + edges = append(edges, user.EdgeNotifiers) + } return edges } @@ -10659,19 +11385,28 @@ func (m *UserMutation) RemovedIDs(name string) []ent.Value { ids = append(ids, id) } return ids + case user.EdgeNotifiers: + ids := make([]ent.Value, 0, len(m.removednotifiers)) + for id := range m.removednotifiers { + ids = append(ids, id) + } + return ids } return nil } // ClearedEdges returns all edge names that were cleared in this mutation. func (m *UserMutation) ClearedEdges() []string { - edges := make([]string, 0, 2) + edges := make([]string, 0, 3) if m.clearedgroup { edges = append(edges, user.EdgeGroup) } if m.clearedauth_tokens { edges = append(edges, user.EdgeAuthTokens) } + if m.clearednotifiers { + edges = append(edges, user.EdgeNotifiers) + } return edges } @@ -10683,6 +11418,8 @@ func (m *UserMutation) EdgeCleared(name string) bool { return m.clearedgroup case user.EdgeAuthTokens: return m.clearedauth_tokens + case user.EdgeNotifiers: + return m.clearednotifiers } return false } @@ -10708,6 +11445,9 @@ func (m *UserMutation) ResetEdge(name string) error { case user.EdgeAuthTokens: m.ResetAuthTokens() return nil + case user.EdgeNotifiers: + m.ResetNotifiers() + return nil } return fmt.Errorf("unknown User edge %s", name) } diff --git a/backend/internal/data/ent/notifier.go b/backend/internal/data/ent/notifier.go new file mode 100644 index 0000000..369a43c --- /dev/null +++ b/backend/internal/data/ent/notifier.go @@ -0,0 +1,184 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + "time" + + "entgo.io/ent/dialect/sql" + "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" + "github.com/hay-kot/homebox/backend/internal/data/ent/user" +) + +// Notifier is the model entity for the Notifier schema. +type Notifier struct { + config `json:"-"` + // ID of the ent. + ID uuid.UUID `json:"id,omitempty"` + // CreatedAt holds the value of the "created_at" field. + CreatedAt time.Time `json:"created_at,omitempty"` + // UpdatedAt holds the value of the "updated_at" field. + UpdatedAt time.Time `json:"updated_at,omitempty"` + // UserID holds the value of the "user_id" field. + UserID uuid.UUID `json:"user_id,omitempty"` + // Name holds the value of the "name" field. + Name string `json:"name,omitempty"` + // URL holds the value of the "url" field. + URL string `json:"-"` + // IsActive holds the value of the "is_active" field. + IsActive bool `json:"is_active,omitempty"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the NotifierQuery when eager-loading is set. + Edges NotifierEdges `json:"edges"` +} + +// NotifierEdges holds the relations/edges for other nodes in the graph. +type NotifierEdges struct { + // User holds the value of the user edge. + User *User `json:"user,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [1]bool +} + +// UserOrErr returns the User value or an error if the edge +// was not loaded in eager-loading, or loaded but was not found. +func (e NotifierEdges) UserOrErr() (*User, error) { + if e.loadedTypes[0] { + if e.User == nil { + // Edge was loaded but was not found. + return nil, &NotFoundError{label: user.Label} + } + return e.User, nil + } + return nil, &NotLoadedError{edge: "user"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*Notifier) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case notifier.FieldIsActive: + values[i] = new(sql.NullBool) + case notifier.FieldName, notifier.FieldURL: + values[i] = new(sql.NullString) + case notifier.FieldCreatedAt, notifier.FieldUpdatedAt: + values[i] = new(sql.NullTime) + case notifier.FieldID, notifier.FieldUserID: + values[i] = new(uuid.UUID) + default: + return nil, fmt.Errorf("unexpected column %q for type Notifier", columns[i]) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the Notifier fields. +func (n *Notifier) assignValues(columns []string, values []any) error { + if m, n := len(values), len(columns); m < n { + return fmt.Errorf("mismatch number of scan values: %d != %d", m, n) + } + for i := range columns { + switch columns[i] { + case notifier.FieldID: + if value, ok := values[i].(*uuid.UUID); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value != nil { + n.ID = *value + } + case notifier.FieldCreatedAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field created_at", values[i]) + } else if value.Valid { + n.CreatedAt = value.Time + } + case notifier.FieldUpdatedAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field updated_at", values[i]) + } else if value.Valid { + n.UpdatedAt = value.Time + } + case notifier.FieldUserID: + if value, ok := values[i].(*uuid.UUID); !ok { + return fmt.Errorf("unexpected type %T for field user_id", values[i]) + } else if value != nil { + n.UserID = *value + } + case notifier.FieldName: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field name", values[i]) + } else if value.Valid { + n.Name = value.String + } + case notifier.FieldURL: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field url", values[i]) + } else if value.Valid { + n.URL = value.String + } + case notifier.FieldIsActive: + if value, ok := values[i].(*sql.NullBool); !ok { + return fmt.Errorf("unexpected type %T for field is_active", values[i]) + } else if value.Valid { + n.IsActive = value.Bool + } + } + } + return nil +} + +// QueryUser queries the "user" edge of the Notifier entity. +func (n *Notifier) QueryUser() *UserQuery { + return NewNotifierClient(n.config).QueryUser(n) +} + +// Update returns a builder for updating this Notifier. +// Note that you need to call Notifier.Unwrap() before calling this method if this Notifier +// was returned from a transaction, and the transaction was committed or rolled back. +func (n *Notifier) Update() *NotifierUpdateOne { + return NewNotifierClient(n.config).UpdateOne(n) +} + +// Unwrap unwraps the Notifier entity that was returned from a transaction after it was closed, +// so that all future queries will be executed through the driver which created the transaction. +func (n *Notifier) Unwrap() *Notifier { + _tx, ok := n.config.driver.(*txDriver) + if !ok { + panic("ent: Notifier is not a transactional entity") + } + n.config.driver = _tx.drv + return n +} + +// String implements the fmt.Stringer. +func (n *Notifier) String() string { + var builder strings.Builder + builder.WriteString("Notifier(") + builder.WriteString(fmt.Sprintf("id=%v, ", n.ID)) + builder.WriteString("created_at=") + builder.WriteString(n.CreatedAt.Format(time.ANSIC)) + builder.WriteString(", ") + builder.WriteString("updated_at=") + builder.WriteString(n.UpdatedAt.Format(time.ANSIC)) + builder.WriteString(", ") + builder.WriteString("user_id=") + builder.WriteString(fmt.Sprintf("%v", n.UserID)) + builder.WriteString(", ") + builder.WriteString("name=") + builder.WriteString(n.Name) + builder.WriteString(", ") + builder.WriteString("url=") + builder.WriteString(", ") + builder.WriteString("is_active=") + builder.WriteString(fmt.Sprintf("%v", n.IsActive)) + builder.WriteByte(')') + return builder.String() +} + +// Notifiers is a parsable slice of Notifier. +type Notifiers []*Notifier diff --git a/backend/internal/data/ent/notifier/notifier.go b/backend/internal/data/ent/notifier/notifier.go new file mode 100644 index 0000000..b8955b5 --- /dev/null +++ b/backend/internal/data/ent/notifier/notifier.go @@ -0,0 +1,77 @@ +// Code generated by ent, DO NOT EDIT. + +package notifier + +import ( + "time" + + "github.com/google/uuid" +) + +const ( + // Label holds the string label denoting the notifier type in the database. + Label = "notifier" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldCreatedAt holds the string denoting the created_at field in the database. + FieldCreatedAt = "created_at" + // FieldUpdatedAt holds the string denoting the updated_at field in the database. + FieldUpdatedAt = "updated_at" + // FieldUserID holds the string denoting the user_id field in the database. + FieldUserID = "user_id" + // FieldName holds the string denoting the name field in the database. + FieldName = "name" + // FieldURL holds the string denoting the url field in the database. + FieldURL = "url" + // FieldIsActive holds the string denoting the is_active field in the database. + FieldIsActive = "is_active" + // EdgeUser holds the string denoting the user edge name in mutations. + EdgeUser = "user" + // Table holds the table name of the notifier in the database. + Table = "notifiers" + // UserTable is the table that holds the user relation/edge. + UserTable = "notifiers" + // UserInverseTable is the table name for the User entity. + // It exists in this package in order to avoid circular dependency with the "user" package. + UserInverseTable = "users" + // UserColumn is the table column denoting the user relation/edge. + UserColumn = "user_id" +) + +// Columns holds all SQL columns for notifier fields. +var Columns = []string{ + FieldID, + FieldCreatedAt, + FieldUpdatedAt, + FieldUserID, + FieldName, + FieldURL, + FieldIsActive, +} + +// ValidColumn reports if the column name is valid (part of the table columns). +func ValidColumn(column string) bool { + for i := range Columns { + if column == Columns[i] { + return true + } + } + return false +} + +var ( + // DefaultCreatedAt holds the default value on creation for the "created_at" field. + DefaultCreatedAt func() time.Time + // DefaultUpdatedAt holds the default value on creation for the "updated_at" field. + DefaultUpdatedAt func() time.Time + // UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field. + UpdateDefaultUpdatedAt func() time.Time + // NameValidator is a validator for the "name" field. It is called by the builders before save. + NameValidator func(string) error + // URLValidator is a validator for the "url" field. It is called by the builders before save. + URLValidator func(string) error + // DefaultIsActive holds the default value on creation for the "is_active" field. + DefaultIsActive bool + // DefaultID holds the default value on creation for the "id" field. + DefaultID func() uuid.UUID +) diff --git a/backend/internal/data/ent/notifier/where.go b/backend/internal/data/ent/notifier/where.go new file mode 100644 index 0000000..8764341 --- /dev/null +++ b/backend/internal/data/ent/notifier/where.go @@ -0,0 +1,386 @@ +// Code generated by ent, DO NOT EDIT. + +package notifier + +import ( + "time" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldLTE(FieldID, id)) +} + +// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. +func CreatedAt(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldCreatedAt, v)) +} + +// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ. +func UpdatedAt(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldUpdatedAt, v)) +} + +// UserID applies equality check predicate on the "user_id" field. It's identical to UserIDEQ. +func UserID(v uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldUserID, v)) +} + +// Name applies equality check predicate on the "name" field. It's identical to NameEQ. +func Name(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldName, v)) +} + +// URL applies equality check predicate on the "url" field. It's identical to URLEQ. +func URL(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldURL, v)) +} + +// IsActive applies equality check predicate on the "is_active" field. It's identical to IsActiveEQ. +func IsActive(v bool) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldIsActive, v)) +} + +// CreatedAtEQ applies the EQ predicate on the "created_at" field. +func CreatedAtEQ(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldCreatedAt, v)) +} + +// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. +func CreatedAtNEQ(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldCreatedAt, v)) +} + +// CreatedAtIn applies the In predicate on the "created_at" field. +func CreatedAtIn(vs ...time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldIn(FieldCreatedAt, vs...)) +} + +// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. +func CreatedAtNotIn(vs ...time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldNotIn(FieldCreatedAt, vs...)) +} + +// CreatedAtGT applies the GT predicate on the "created_at" field. +func CreatedAtGT(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldGT(FieldCreatedAt, v)) +} + +// CreatedAtGTE applies the GTE predicate on the "created_at" field. +func CreatedAtGTE(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldGTE(FieldCreatedAt, v)) +} + +// CreatedAtLT applies the LT predicate on the "created_at" field. +func CreatedAtLT(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldLT(FieldCreatedAt, v)) +} + +// CreatedAtLTE applies the LTE predicate on the "created_at" field. +func CreatedAtLTE(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldLTE(FieldCreatedAt, v)) +} + +// UpdatedAtEQ applies the EQ predicate on the "updated_at" field. +func UpdatedAtEQ(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field. +func UpdatedAtNEQ(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtIn applies the In predicate on the "updated_at" field. +func UpdatedAtIn(vs ...time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field. +func UpdatedAtNotIn(vs ...time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldNotIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtGT applies the GT predicate on the "updated_at" field. +func UpdatedAtGT(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldGT(FieldUpdatedAt, v)) +} + +// UpdatedAtGTE applies the GTE predicate on the "updated_at" field. +func UpdatedAtGTE(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldGTE(FieldUpdatedAt, v)) +} + +// UpdatedAtLT applies the LT predicate on the "updated_at" field. +func UpdatedAtLT(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldLT(FieldUpdatedAt, v)) +} + +// UpdatedAtLTE applies the LTE predicate on the "updated_at" field. +func UpdatedAtLTE(v time.Time) predicate.Notifier { + return predicate.Notifier(sql.FieldLTE(FieldUpdatedAt, v)) +} + +// UserIDEQ applies the EQ predicate on the "user_id" field. +func UserIDEQ(v uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldUserID, v)) +} + +// UserIDNEQ applies the NEQ predicate on the "user_id" field. +func UserIDNEQ(v uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldUserID, v)) +} + +// UserIDIn applies the In predicate on the "user_id" field. +func UserIDIn(vs ...uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldIn(FieldUserID, vs...)) +} + +// UserIDNotIn applies the NotIn predicate on the "user_id" field. +func UserIDNotIn(vs ...uuid.UUID) predicate.Notifier { + return predicate.Notifier(sql.FieldNotIn(FieldUserID, vs...)) +} + +// NameEQ applies the EQ predicate on the "name" field. +func NameEQ(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldName, v)) +} + +// NameNEQ applies the NEQ predicate on the "name" field. +func NameNEQ(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldName, v)) +} + +// NameIn applies the In predicate on the "name" field. +func NameIn(vs ...string) predicate.Notifier { + return predicate.Notifier(sql.FieldIn(FieldName, vs...)) +} + +// NameNotIn applies the NotIn predicate on the "name" field. +func NameNotIn(vs ...string) predicate.Notifier { + return predicate.Notifier(sql.FieldNotIn(FieldName, vs...)) +} + +// NameGT applies the GT predicate on the "name" field. +func NameGT(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldGT(FieldName, v)) +} + +// NameGTE applies the GTE predicate on the "name" field. +func NameGTE(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldGTE(FieldName, v)) +} + +// NameLT applies the LT predicate on the "name" field. +func NameLT(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldLT(FieldName, v)) +} + +// NameLTE applies the LTE predicate on the "name" field. +func NameLTE(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldLTE(FieldName, v)) +} + +// NameContains applies the Contains predicate on the "name" field. +func NameContains(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldContains(FieldName, v)) +} + +// NameHasPrefix applies the HasPrefix predicate on the "name" field. +func NameHasPrefix(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldHasPrefix(FieldName, v)) +} + +// NameHasSuffix applies the HasSuffix predicate on the "name" field. +func NameHasSuffix(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldHasSuffix(FieldName, v)) +} + +// NameEqualFold applies the EqualFold predicate on the "name" field. +func NameEqualFold(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldEqualFold(FieldName, v)) +} + +// NameContainsFold applies the ContainsFold predicate on the "name" field. +func NameContainsFold(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldContainsFold(FieldName, v)) +} + +// URLEQ applies the EQ predicate on the "url" field. +func URLEQ(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldURL, v)) +} + +// URLNEQ applies the NEQ predicate on the "url" field. +func URLNEQ(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldURL, v)) +} + +// URLIn applies the In predicate on the "url" field. +func URLIn(vs ...string) predicate.Notifier { + return predicate.Notifier(sql.FieldIn(FieldURL, vs...)) +} + +// URLNotIn applies the NotIn predicate on the "url" field. +func URLNotIn(vs ...string) predicate.Notifier { + return predicate.Notifier(sql.FieldNotIn(FieldURL, vs...)) +} + +// URLGT applies the GT predicate on the "url" field. +func URLGT(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldGT(FieldURL, v)) +} + +// URLGTE applies the GTE predicate on the "url" field. +func URLGTE(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldGTE(FieldURL, v)) +} + +// URLLT applies the LT predicate on the "url" field. +func URLLT(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldLT(FieldURL, v)) +} + +// URLLTE applies the LTE predicate on the "url" field. +func URLLTE(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldLTE(FieldURL, v)) +} + +// URLContains applies the Contains predicate on the "url" field. +func URLContains(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldContains(FieldURL, v)) +} + +// URLHasPrefix applies the HasPrefix predicate on the "url" field. +func URLHasPrefix(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldHasPrefix(FieldURL, v)) +} + +// URLHasSuffix applies the HasSuffix predicate on the "url" field. +func URLHasSuffix(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldHasSuffix(FieldURL, v)) +} + +// URLEqualFold applies the EqualFold predicate on the "url" field. +func URLEqualFold(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldEqualFold(FieldURL, v)) +} + +// URLContainsFold applies the ContainsFold predicate on the "url" field. +func URLContainsFold(v string) predicate.Notifier { + return predicate.Notifier(sql.FieldContainsFold(FieldURL, v)) +} + +// IsActiveEQ applies the EQ predicate on the "is_active" field. +func IsActiveEQ(v bool) predicate.Notifier { + return predicate.Notifier(sql.FieldEQ(FieldIsActive, v)) +} + +// IsActiveNEQ applies the NEQ predicate on the "is_active" field. +func IsActiveNEQ(v bool) predicate.Notifier { + return predicate.Notifier(sql.FieldNEQ(FieldIsActive, v)) +} + +// HasUser applies the HasEdge predicate on the "user" edge. +func HasUser() predicate.Notifier { + return predicate.Notifier(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, true, UserTable, UserColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasUserWith applies the HasEdge predicate on the "user" edge with a given conditions (other predicates). +func HasUserWith(preds ...predicate.User) predicate.Notifier { + return predicate.Notifier(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(UserInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, true, UserTable, UserColumn), + ) + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// And groups predicates with the AND operator between them. +func And(predicates ...predicate.Notifier) predicate.Notifier { + return predicate.Notifier(func(s *sql.Selector) { + s1 := s.Clone().SetP(nil) + for _, p := range predicates { + p(s1) + } + s.Where(s1.P()) + }) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.Notifier) predicate.Notifier { + return predicate.Notifier(func(s *sql.Selector) { + s1 := s.Clone().SetP(nil) + for i, p := range predicates { + if i > 0 { + s1.Or() + } + p(s1) + } + s.Where(s1.P()) + }) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.Notifier) predicate.Notifier { + return predicate.Notifier(func(s *sql.Selector) { + p(s.Not()) + }) +} diff --git a/backend/internal/data/ent/notifier_create.go b/backend/internal/data/ent/notifier_create.go new file mode 100644 index 0000000..a9c6f78 --- /dev/null +++ b/backend/internal/data/ent/notifier_create.go @@ -0,0 +1,346 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + "time" + + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" + "github.com/hay-kot/homebox/backend/internal/data/ent/user" +) + +// NotifierCreate is the builder for creating a Notifier entity. +type NotifierCreate struct { + config + mutation *NotifierMutation + hooks []Hook +} + +// SetCreatedAt sets the "created_at" field. +func (nc *NotifierCreate) SetCreatedAt(t time.Time) *NotifierCreate { + nc.mutation.SetCreatedAt(t) + return nc +} + +// SetNillableCreatedAt sets the "created_at" field if the given value is not nil. +func (nc *NotifierCreate) SetNillableCreatedAt(t *time.Time) *NotifierCreate { + if t != nil { + nc.SetCreatedAt(*t) + } + return nc +} + +// SetUpdatedAt sets the "updated_at" field. +func (nc *NotifierCreate) SetUpdatedAt(t time.Time) *NotifierCreate { + nc.mutation.SetUpdatedAt(t) + return nc +} + +// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. +func (nc *NotifierCreate) SetNillableUpdatedAt(t *time.Time) *NotifierCreate { + if t != nil { + nc.SetUpdatedAt(*t) + } + return nc +} + +// SetUserID sets the "user_id" field. +func (nc *NotifierCreate) SetUserID(u uuid.UUID) *NotifierCreate { + nc.mutation.SetUserID(u) + return nc +} + +// SetName sets the "name" field. +func (nc *NotifierCreate) SetName(s string) *NotifierCreate { + nc.mutation.SetName(s) + return nc +} + +// SetURL sets the "url" field. +func (nc *NotifierCreate) SetURL(s string) *NotifierCreate { + nc.mutation.SetURL(s) + return nc +} + +// SetIsActive sets the "is_active" field. +func (nc *NotifierCreate) SetIsActive(b bool) *NotifierCreate { + nc.mutation.SetIsActive(b) + return nc +} + +// SetNillableIsActive sets the "is_active" field if the given value is not nil. +func (nc *NotifierCreate) SetNillableIsActive(b *bool) *NotifierCreate { + if b != nil { + nc.SetIsActive(*b) + } + return nc +} + +// SetID sets the "id" field. +func (nc *NotifierCreate) SetID(u uuid.UUID) *NotifierCreate { + nc.mutation.SetID(u) + return nc +} + +// SetNillableID sets the "id" field if the given value is not nil. +func (nc *NotifierCreate) SetNillableID(u *uuid.UUID) *NotifierCreate { + if u != nil { + nc.SetID(*u) + } + return nc +} + +// SetUser sets the "user" edge to the User entity. +func (nc *NotifierCreate) SetUser(u *User) *NotifierCreate { + return nc.SetUserID(u.ID) +} + +// Mutation returns the NotifierMutation object of the builder. +func (nc *NotifierCreate) Mutation() *NotifierMutation { + return nc.mutation +} + +// Save creates the Notifier in the database. +func (nc *NotifierCreate) Save(ctx context.Context) (*Notifier, error) { + nc.defaults() + return withHooks[*Notifier, NotifierMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (nc *NotifierCreate) SaveX(ctx context.Context) *Notifier { + v, err := nc.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (nc *NotifierCreate) Exec(ctx context.Context) error { + _, err := nc.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (nc *NotifierCreate) ExecX(ctx context.Context) { + if err := nc.Exec(ctx); err != nil { + panic(err) + } +} + +// defaults sets the default values of the builder before save. +func (nc *NotifierCreate) defaults() { + if _, ok := nc.mutation.CreatedAt(); !ok { + v := notifier.DefaultCreatedAt() + nc.mutation.SetCreatedAt(v) + } + if _, ok := nc.mutation.UpdatedAt(); !ok { + v := notifier.DefaultUpdatedAt() + nc.mutation.SetUpdatedAt(v) + } + if _, ok := nc.mutation.IsActive(); !ok { + v := notifier.DefaultIsActive + nc.mutation.SetIsActive(v) + } + if _, ok := nc.mutation.ID(); !ok { + v := notifier.DefaultID() + nc.mutation.SetID(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (nc *NotifierCreate) check() error { + if _, ok := nc.mutation.CreatedAt(); !ok { + return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Notifier.created_at"`)} + } + if _, ok := nc.mutation.UpdatedAt(); !ok { + return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Notifier.updated_at"`)} + } + if _, ok := nc.mutation.UserID(); !ok { + return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "Notifier.user_id"`)} + } + if _, ok := nc.mutation.Name(); !ok { + return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Notifier.name"`)} + } + if v, ok := nc.mutation.Name(); ok { + if err := notifier.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Notifier.name": %w`, err)} + } + } + if _, ok := nc.mutation.URL(); !ok { + return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "Notifier.url"`)} + } + if v, ok := nc.mutation.URL(); ok { + if err := notifier.URLValidator(v); err != nil { + return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Notifier.url": %w`, err)} + } + } + if _, ok := nc.mutation.IsActive(); !ok { + return &ValidationError{Name: "is_active", err: errors.New(`ent: missing required field "Notifier.is_active"`)} + } + if _, ok := nc.mutation.UserID(); !ok { + return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "Notifier.user"`)} + } + return nil +} + +func (nc *NotifierCreate) sqlSave(ctx context.Context) (*Notifier, error) { + if err := nc.check(); err != nil { + return nil, err + } + _node, _spec := nc.createSpec() + if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil { + if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + if _spec.ID.Value != nil { + if id, ok := _spec.ID.Value.(*uuid.UUID); ok { + _node.ID = *id + } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { + return nil, err + } + } + nc.mutation.id = &_node.ID + nc.mutation.done = true + return _node, nil +} + +func (nc *NotifierCreate) createSpec() (*Notifier, *sqlgraph.CreateSpec) { + var ( + _node = &Notifier{config: nc.config} + _spec = sqlgraph.NewCreateSpec(notifier.Table, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID)) + ) + if id, ok := nc.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = &id + } + if value, ok := nc.mutation.CreatedAt(); ok { + _spec.SetField(notifier.FieldCreatedAt, field.TypeTime, value) + _node.CreatedAt = value + } + if value, ok := nc.mutation.UpdatedAt(); ok { + _spec.SetField(notifier.FieldUpdatedAt, field.TypeTime, value) + _node.UpdatedAt = value + } + if value, ok := nc.mutation.Name(); ok { + _spec.SetField(notifier.FieldName, field.TypeString, value) + _node.Name = value + } + if value, ok := nc.mutation.URL(); ok { + _spec.SetField(notifier.FieldURL, field.TypeString, value) + _node.URL = value + } + if value, ok := nc.mutation.IsActive(); ok { + _spec.SetField(notifier.FieldIsActive, field.TypeBool, value) + _node.IsActive = value + } + if nodes := nc.mutation.UserIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: true, + Table: notifier.UserTable, + Columns: []string{notifier.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: user.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.UserID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + return _node, _spec +} + +// NotifierCreateBulk is the builder for creating many Notifier entities in bulk. +type NotifierCreateBulk struct { + config + builders []*NotifierCreate +} + +// Save creates the Notifier entities in the database. +func (ncb *NotifierCreateBulk) Save(ctx context.Context) ([]*Notifier, error) { + specs := make([]*sqlgraph.CreateSpec, len(ncb.builders)) + nodes := make([]*Notifier, len(ncb.builders)) + mutators := make([]Mutator, len(ncb.builders)) + for i := range ncb.builders { + func(i int, root context.Context) { + builder := ncb.builders[i] + builder.defaults() + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*NotifierMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + if err := builder.check(); err != nil { + return nil, err + } + builder.mutation = mutation + nodes[i], specs[i] = builder.createSpec() + var err error + if i < len(mutators)-1 { + _, err = mutators[i+1].Mutate(root, ncb.builders[i+1].mutation) + } else { + spec := &sqlgraph.BatchCreateSpec{Nodes: specs} + // Invoke the actual operation on the latest mutation in the chain. + if err = sqlgraph.BatchCreate(ctx, ncb.driver, spec); err != nil { + if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + } + } + if err != nil { + return nil, err + } + mutation.id = &nodes[i].ID + mutation.done = true + return nodes[i], nil + }) + for i := len(builder.hooks) - 1; i >= 0; i-- { + mut = builder.hooks[i](mut) + } + mutators[i] = mut + }(i, ctx) + } + if len(mutators) > 0 { + if _, err := mutators[0].Mutate(ctx, ncb.builders[0].mutation); err != nil { + return nil, err + } + } + return nodes, nil +} + +// SaveX is like Save, but panics if an error occurs. +func (ncb *NotifierCreateBulk) SaveX(ctx context.Context) []*Notifier { + v, err := ncb.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (ncb *NotifierCreateBulk) Exec(ctx context.Context) error { + _, err := ncb.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (ncb *NotifierCreateBulk) ExecX(ctx context.Context) { + if err := ncb.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/backend/internal/data/ent/notifier_delete.go b/backend/internal/data/ent/notifier_delete.go new file mode 100644 index 0000000..c4f0d45 --- /dev/null +++ b/backend/internal/data/ent/notifier_delete.go @@ -0,0 +1,88 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" + "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" +) + +// NotifierDelete is the builder for deleting a Notifier entity. +type NotifierDelete struct { + config + hooks []Hook + mutation *NotifierMutation +} + +// Where appends a list predicates to the NotifierDelete builder. +func (nd *NotifierDelete) Where(ps ...predicate.Notifier) *NotifierDelete { + nd.mutation.Where(ps...) + return nd +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (nd *NotifierDelete) Exec(ctx context.Context) (int, error) { + return withHooks[int, NotifierMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (nd *NotifierDelete) ExecX(ctx context.Context) int { + n, err := nd.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (nd *NotifierDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(notifier.Table, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID)) + if ps := nd.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, nd.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + nd.mutation.done = true + return affected, err +} + +// NotifierDeleteOne is the builder for deleting a single Notifier entity. +type NotifierDeleteOne struct { + nd *NotifierDelete +} + +// Where appends a list predicates to the NotifierDelete builder. +func (ndo *NotifierDeleteOne) Where(ps ...predicate.Notifier) *NotifierDeleteOne { + ndo.nd.mutation.Where(ps...) + return ndo +} + +// Exec executes the deletion query. +func (ndo *NotifierDeleteOne) Exec(ctx context.Context) error { + n, err := ndo.nd.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{notifier.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (ndo *NotifierDeleteOne) ExecX(ctx context.Context) { + if err := ndo.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/backend/internal/data/ent/notifier_query.go b/backend/internal/data/ent/notifier_query.go new file mode 100644 index 0000000..31826d9 --- /dev/null +++ b/backend/internal/data/ent/notifier_query.go @@ -0,0 +1,603 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + "math" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" + "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" + "github.com/hay-kot/homebox/backend/internal/data/ent/user" +) + +// NotifierQuery is the builder for querying Notifier entities. +type NotifierQuery struct { + config + ctx *QueryContext + order []OrderFunc + inters []Interceptor + predicates []predicate.Notifier + withUser *UserQuery + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the NotifierQuery builder. +func (nq *NotifierQuery) Where(ps ...predicate.Notifier) *NotifierQuery { + nq.predicates = append(nq.predicates, ps...) + return nq +} + +// Limit the number of records to be returned by this query. +func (nq *NotifierQuery) Limit(limit int) *NotifierQuery { + nq.ctx.Limit = &limit + return nq +} + +// Offset to start from. +func (nq *NotifierQuery) Offset(offset int) *NotifierQuery { + nq.ctx.Offset = &offset + return nq +} + +// Unique configures the query builder to filter duplicate records on query. +// By default, unique is set to true, and can be disabled using this method. +func (nq *NotifierQuery) Unique(unique bool) *NotifierQuery { + nq.ctx.Unique = &unique + return nq +} + +// Order specifies how the records should be ordered. +func (nq *NotifierQuery) Order(o ...OrderFunc) *NotifierQuery { + nq.order = append(nq.order, o...) + return nq +} + +// QueryUser chains the current query on the "user" edge. +func (nq *NotifierQuery) QueryUser() *UserQuery { + query := (&UserClient{config: nq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := nq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := nq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(notifier.Table, notifier.FieldID, selector), + sqlgraph.To(user.Table, user.FieldID), + sqlgraph.Edge(sqlgraph.M2O, true, notifier.UserTable, notifier.UserColumn), + ) + fromU = sqlgraph.SetNeighbors(nq.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// First returns the first Notifier entity from the query. +// Returns a *NotFoundError when no Notifier was found. +func (nq *NotifierQuery) First(ctx context.Context) (*Notifier, error) { + nodes, err := nq.Limit(1).All(setContextOp(ctx, nq.ctx, "First")) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{notifier.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (nq *NotifierQuery) FirstX(ctx context.Context) *Notifier { + node, err := nq.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first Notifier ID from the query. +// Returns a *NotFoundError when no Notifier ID was found. +func (nq *NotifierQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) { + var ids []uuid.UUID + if ids, err = nq.Limit(1).IDs(setContextOp(ctx, nq.ctx, "FirstID")); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{notifier.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (nq *NotifierQuery) FirstIDX(ctx context.Context) uuid.UUID { + id, err := nq.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single Notifier entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one Notifier entity is found. +// Returns a *NotFoundError when no Notifier entities are found. +func (nq *NotifierQuery) Only(ctx context.Context) (*Notifier, error) { + nodes, err := nq.Limit(2).All(setContextOp(ctx, nq.ctx, "Only")) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{notifier.Label} + default: + return nil, &NotSingularError{notifier.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (nq *NotifierQuery) OnlyX(ctx context.Context) *Notifier { + node, err := nq.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only Notifier ID in the query. +// Returns a *NotSingularError when more than one Notifier ID is found. +// Returns a *NotFoundError when no entities are found. +func (nq *NotifierQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) { + var ids []uuid.UUID + if ids, err = nq.Limit(2).IDs(setContextOp(ctx, nq.ctx, "OnlyID")); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{notifier.Label} + default: + err = &NotSingularError{notifier.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (nq *NotifierQuery) OnlyIDX(ctx context.Context) uuid.UUID { + id, err := nq.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of Notifiers. +func (nq *NotifierQuery) All(ctx context.Context) ([]*Notifier, error) { + ctx = setContextOp(ctx, nq.ctx, "All") + if err := nq.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*Notifier, *NotifierQuery]() + return withInterceptors[[]*Notifier](ctx, nq, qr, nq.inters) +} + +// AllX is like All, but panics if an error occurs. +func (nq *NotifierQuery) AllX(ctx context.Context) []*Notifier { + nodes, err := nq.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of Notifier IDs. +func (nq *NotifierQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) { + if nq.ctx.Unique == nil && nq.path != nil { + nq.Unique(true) + } + ctx = setContextOp(ctx, nq.ctx, "IDs") + if err = nq.Select(notifier.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (nq *NotifierQuery) IDsX(ctx context.Context) []uuid.UUID { + ids, err := nq.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (nq *NotifierQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, nq.ctx, "Count") + if err := nq.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, nq, querierCount[*NotifierQuery](), nq.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (nq *NotifierQuery) CountX(ctx context.Context) int { + count, err := nq.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (nq *NotifierQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, nq.ctx, "Exist") + switch _, err := nq.FirstID(ctx); { + case IsNotFound(err): + return false, nil + case err != nil: + return false, fmt.Errorf("ent: check existence: %w", err) + default: + return true, nil + } +} + +// ExistX is like Exist, but panics if an error occurs. +func (nq *NotifierQuery) ExistX(ctx context.Context) bool { + exist, err := nq.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the NotifierQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (nq *NotifierQuery) Clone() *NotifierQuery { + if nq == nil { + return nil + } + return &NotifierQuery{ + config: nq.config, + ctx: nq.ctx.Clone(), + order: append([]OrderFunc{}, nq.order...), + inters: append([]Interceptor{}, nq.inters...), + predicates: append([]predicate.Notifier{}, nq.predicates...), + withUser: nq.withUser.Clone(), + // clone intermediate query. + sql: nq.sql.Clone(), + path: nq.path, + } +} + +// WithUser tells the query-builder to eager-load the nodes that are connected to +// the "user" edge. The optional arguments are used to configure the query builder of the edge. +func (nq *NotifierQuery) WithUser(opts ...func(*UserQuery)) *NotifierQuery { + query := (&UserClient{config: nq.config}).Query() + for _, opt := range opts { + opt(query) + } + nq.withUser = query + return nq +} + +// GroupBy is used to group vertices by one or more fields/columns. +// It is often used with aggregate functions, like: count, max, mean, min, sum. +// +// Example: +// +// var v []struct { +// CreatedAt time.Time `json:"created_at,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.Notifier.Query(). +// GroupBy(notifier.FieldCreatedAt). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (nq *NotifierQuery) GroupBy(field string, fields ...string) *NotifierGroupBy { + nq.ctx.Fields = append([]string{field}, fields...) + grbuild := &NotifierGroupBy{build: nq} + grbuild.flds = &nq.ctx.Fields + grbuild.label = notifier.Label + grbuild.scan = grbuild.Scan + return grbuild +} + +// Select allows the selection one or more fields/columns for the given query, +// instead of selecting all fields in the entity. +// +// Example: +// +// var v []struct { +// CreatedAt time.Time `json:"created_at,omitempty"` +// } +// +// client.Notifier.Query(). +// Select(notifier.FieldCreatedAt). +// Scan(ctx, &v) +func (nq *NotifierQuery) Select(fields ...string) *NotifierSelect { + nq.ctx.Fields = append(nq.ctx.Fields, fields...) + sbuild := &NotifierSelect{NotifierQuery: nq} + sbuild.label = notifier.Label + sbuild.flds, sbuild.scan = &nq.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a NotifierSelect configured with the given aggregations. +func (nq *NotifierQuery) Aggregate(fns ...AggregateFunc) *NotifierSelect { + return nq.Select().Aggregate(fns...) +} + +func (nq *NotifierQuery) prepareQuery(ctx context.Context) error { + for _, inter := range nq.inters { + if inter == nil { + return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)") + } + if trv, ok := inter.(Traverser); ok { + if err := trv.Traverse(ctx, nq); err != nil { + return err + } + } + } + for _, f := range nq.ctx.Fields { + if !notifier.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if nq.path != nil { + prev, err := nq.path(ctx) + if err != nil { + return err + } + nq.sql = prev + } + return nil +} + +func (nq *NotifierQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Notifier, error) { + var ( + nodes = []*Notifier{} + _spec = nq.querySpec() + loadedTypes = [1]bool{ + nq.withUser != nil, + } + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*Notifier).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &Notifier{config: nq.config} + nodes = append(nodes, node) + node.Edges.loadedTypes = loadedTypes + return node.assignValues(columns, values) + } + for i := range hooks { + hooks[i](ctx, _spec) + } + if err := sqlgraph.QueryNodes(ctx, nq.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := nq.withUser; query != nil { + if err := nq.loadUser(ctx, query, nodes, nil, + func(n *Notifier, e *User) { n.Edges.User = e }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (nq *NotifierQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*Notifier, init func(*Notifier), assign func(*Notifier, *User)) error { + ids := make([]uuid.UUID, 0, len(nodes)) + nodeids := make(map[uuid.UUID][]*Notifier) + for i := range nodes { + fk := nodes[i].UserID + if _, ok := nodeids[fk]; !ok { + ids = append(ids, fk) + } + nodeids[fk] = append(nodeids[fk], nodes[i]) + } + if len(ids) == 0 { + return nil + } + query.Where(user.IDIn(ids...)) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + nodes, ok := nodeids[n.ID] + if !ok { + return fmt.Errorf(`unexpected foreign-key "user_id" returned %v`, n.ID) + } + for i := range nodes { + assign(nodes[i], n) + } + } + return nil +} + +func (nq *NotifierQuery) sqlCount(ctx context.Context) (int, error) { + _spec := nq.querySpec() + _spec.Node.Columns = nq.ctx.Fields + if len(nq.ctx.Fields) > 0 { + _spec.Unique = nq.ctx.Unique != nil && *nq.ctx.Unique + } + return sqlgraph.CountNodes(ctx, nq.driver, _spec) +} + +func (nq *NotifierQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(notifier.Table, notifier.Columns, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID)) + _spec.From = nq.sql + if unique := nq.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if nq.path != nil { + _spec.Unique = true + } + if fields := nq.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, notifier.FieldID) + for i := range fields { + if fields[i] != notifier.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := nq.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := nq.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := nq.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := nq.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (nq *NotifierQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(nq.driver.Dialect()) + t1 := builder.Table(notifier.Table) + columns := nq.ctx.Fields + if len(columns) == 0 { + columns = notifier.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if nq.sql != nil { + selector = nq.sql + selector.Select(selector.Columns(columns...)...) + } + if nq.ctx.Unique != nil && *nq.ctx.Unique { + selector.Distinct() + } + for _, p := range nq.predicates { + p(selector) + } + for _, p := range nq.order { + p(selector) + } + if offset := nq.ctx.Offset; offset != nil { + // limit is mandatory for offset clause. We start + // with default value, and override it below if needed. + selector.Offset(*offset).Limit(math.MaxInt32) + } + if limit := nq.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// NotifierGroupBy is the group-by builder for Notifier entities. +type NotifierGroupBy struct { + selector + build *NotifierQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (ngb *NotifierGroupBy) Aggregate(fns ...AggregateFunc) *NotifierGroupBy { + ngb.fns = append(ngb.fns, fns...) + return ngb +} + +// Scan applies the selector query and scans the result into the given value. +func (ngb *NotifierGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, ngb.build.ctx, "GroupBy") + if err := ngb.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*NotifierQuery, *NotifierGroupBy](ctx, ngb.build, ngb, ngb.build.inters, v) +} + +func (ngb *NotifierGroupBy) sqlScan(ctx context.Context, root *NotifierQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(ngb.fns)) + for _, fn := range ngb.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*ngb.flds)+len(ngb.fns)) + for _, f := range *ngb.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*ngb.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := ngb.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// NotifierSelect is the builder for selecting fields of Notifier entities. +type NotifierSelect struct { + *NotifierQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (ns *NotifierSelect) Aggregate(fns ...AggregateFunc) *NotifierSelect { + ns.fns = append(ns.fns, fns...) + return ns +} + +// Scan applies the selector query and scans the result into the given value. +func (ns *NotifierSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, ns.ctx, "Select") + if err := ns.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*NotifierQuery, *NotifierSelect](ctx, ns.NotifierQuery, ns, ns.inters, v) +} + +func (ns *NotifierSelect) sqlScan(ctx context.Context, root *NotifierQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(ns.fns)) + for _, fn := range ns.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*ns.selector.flds); { + case n == 0 && len(aggregation) > 0: + selector.Select(aggregation...) + case n != 0 && len(aggregation) > 0: + selector.AppendSelect(aggregation...) + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := ns.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/backend/internal/data/ent/notifier_update.go b/backend/internal/data/ent/notifier_update.go new file mode 100644 index 0000000..04875e0 --- /dev/null +++ b/backend/internal/data/ent/notifier_update.go @@ -0,0 +1,430 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + "time" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" + "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" + "github.com/hay-kot/homebox/backend/internal/data/ent/user" +) + +// NotifierUpdate is the builder for updating Notifier entities. +type NotifierUpdate struct { + config + hooks []Hook + mutation *NotifierMutation +} + +// Where appends a list predicates to the NotifierUpdate builder. +func (nu *NotifierUpdate) Where(ps ...predicate.Notifier) *NotifierUpdate { + nu.mutation.Where(ps...) + return nu +} + +// SetUpdatedAt sets the "updated_at" field. +func (nu *NotifierUpdate) SetUpdatedAt(t time.Time) *NotifierUpdate { + nu.mutation.SetUpdatedAt(t) + return nu +} + +// SetUserID sets the "user_id" field. +func (nu *NotifierUpdate) SetUserID(u uuid.UUID) *NotifierUpdate { + nu.mutation.SetUserID(u) + return nu +} + +// SetName sets the "name" field. +func (nu *NotifierUpdate) SetName(s string) *NotifierUpdate { + nu.mutation.SetName(s) + return nu +} + +// SetURL sets the "url" field. +func (nu *NotifierUpdate) SetURL(s string) *NotifierUpdate { + nu.mutation.SetURL(s) + return nu +} + +// SetIsActive sets the "is_active" field. +func (nu *NotifierUpdate) SetIsActive(b bool) *NotifierUpdate { + nu.mutation.SetIsActive(b) + return nu +} + +// SetNillableIsActive sets the "is_active" field if the given value is not nil. +func (nu *NotifierUpdate) SetNillableIsActive(b *bool) *NotifierUpdate { + if b != nil { + nu.SetIsActive(*b) + } + return nu +} + +// SetUser sets the "user" edge to the User entity. +func (nu *NotifierUpdate) SetUser(u *User) *NotifierUpdate { + return nu.SetUserID(u.ID) +} + +// Mutation returns the NotifierMutation object of the builder. +func (nu *NotifierUpdate) Mutation() *NotifierMutation { + return nu.mutation +} + +// ClearUser clears the "user" edge to the User entity. +func (nu *NotifierUpdate) ClearUser() *NotifierUpdate { + nu.mutation.ClearUser() + return nu +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (nu *NotifierUpdate) Save(ctx context.Context) (int, error) { + nu.defaults() + return withHooks[int, NotifierMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (nu *NotifierUpdate) SaveX(ctx context.Context) int { + affected, err := nu.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (nu *NotifierUpdate) Exec(ctx context.Context) error { + _, err := nu.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (nu *NotifierUpdate) ExecX(ctx context.Context) { + if err := nu.Exec(ctx); err != nil { + panic(err) + } +} + +// defaults sets the default values of the builder before save. +func (nu *NotifierUpdate) defaults() { + if _, ok := nu.mutation.UpdatedAt(); !ok { + v := notifier.UpdateDefaultUpdatedAt() + nu.mutation.SetUpdatedAt(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (nu *NotifierUpdate) check() error { + if v, ok := nu.mutation.Name(); ok { + if err := notifier.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Notifier.name": %w`, err)} + } + } + if v, ok := nu.mutation.URL(); ok { + if err := notifier.URLValidator(v); err != nil { + return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Notifier.url": %w`, err)} + } + } + if _, ok := nu.mutation.UserID(); nu.mutation.UserCleared() && !ok { + return errors.New(`ent: clearing a required unique edge "Notifier.user"`) + } + return nil +} + +func (nu *NotifierUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := nu.check(); err != nil { + return n, err + } + _spec := sqlgraph.NewUpdateSpec(notifier.Table, notifier.Columns, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID)) + if ps := nu.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := nu.mutation.UpdatedAt(); ok { + _spec.SetField(notifier.FieldUpdatedAt, field.TypeTime, value) + } + if value, ok := nu.mutation.Name(); ok { + _spec.SetField(notifier.FieldName, field.TypeString, value) + } + if value, ok := nu.mutation.URL(); ok { + _spec.SetField(notifier.FieldURL, field.TypeString, value) + } + if value, ok := nu.mutation.IsActive(); ok { + _spec.SetField(notifier.FieldIsActive, field.TypeBool, value) + } + if nu.mutation.UserCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: true, + Table: notifier.UserTable, + Columns: []string{notifier.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: user.FieldID, + }, + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := nu.mutation.UserIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: true, + Table: notifier.UserTable, + Columns: []string{notifier.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: user.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if n, err = sqlgraph.UpdateNodes(ctx, nu.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{notifier.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + nu.mutation.done = true + return n, nil +} + +// NotifierUpdateOne is the builder for updating a single Notifier entity. +type NotifierUpdateOne struct { + config + fields []string + hooks []Hook + mutation *NotifierMutation +} + +// SetUpdatedAt sets the "updated_at" field. +func (nuo *NotifierUpdateOne) SetUpdatedAt(t time.Time) *NotifierUpdateOne { + nuo.mutation.SetUpdatedAt(t) + return nuo +} + +// SetUserID sets the "user_id" field. +func (nuo *NotifierUpdateOne) SetUserID(u uuid.UUID) *NotifierUpdateOne { + nuo.mutation.SetUserID(u) + return nuo +} + +// SetName sets the "name" field. +func (nuo *NotifierUpdateOne) SetName(s string) *NotifierUpdateOne { + nuo.mutation.SetName(s) + return nuo +} + +// SetURL sets the "url" field. +func (nuo *NotifierUpdateOne) SetURL(s string) *NotifierUpdateOne { + nuo.mutation.SetURL(s) + return nuo +} + +// SetIsActive sets the "is_active" field. +func (nuo *NotifierUpdateOne) SetIsActive(b bool) *NotifierUpdateOne { + nuo.mutation.SetIsActive(b) + return nuo +} + +// SetNillableIsActive sets the "is_active" field if the given value is not nil. +func (nuo *NotifierUpdateOne) SetNillableIsActive(b *bool) *NotifierUpdateOne { + if b != nil { + nuo.SetIsActive(*b) + } + return nuo +} + +// SetUser sets the "user" edge to the User entity. +func (nuo *NotifierUpdateOne) SetUser(u *User) *NotifierUpdateOne { + return nuo.SetUserID(u.ID) +} + +// Mutation returns the NotifierMutation object of the builder. +func (nuo *NotifierUpdateOne) Mutation() *NotifierMutation { + return nuo.mutation +} + +// ClearUser clears the "user" edge to the User entity. +func (nuo *NotifierUpdateOne) ClearUser() *NotifierUpdateOne { + nuo.mutation.ClearUser() + return nuo +} + +// Where appends a list predicates to the NotifierUpdate builder. +func (nuo *NotifierUpdateOne) Where(ps ...predicate.Notifier) *NotifierUpdateOne { + nuo.mutation.Where(ps...) + return nuo +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (nuo *NotifierUpdateOne) Select(field string, fields ...string) *NotifierUpdateOne { + nuo.fields = append([]string{field}, fields...) + return nuo +} + +// Save executes the query and returns the updated Notifier entity. +func (nuo *NotifierUpdateOne) Save(ctx context.Context) (*Notifier, error) { + nuo.defaults() + return withHooks[*Notifier, NotifierMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (nuo *NotifierUpdateOne) SaveX(ctx context.Context) *Notifier { + node, err := nuo.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (nuo *NotifierUpdateOne) Exec(ctx context.Context) error { + _, err := nuo.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (nuo *NotifierUpdateOne) ExecX(ctx context.Context) { + if err := nuo.Exec(ctx); err != nil { + panic(err) + } +} + +// defaults sets the default values of the builder before save. +func (nuo *NotifierUpdateOne) defaults() { + if _, ok := nuo.mutation.UpdatedAt(); !ok { + v := notifier.UpdateDefaultUpdatedAt() + nuo.mutation.SetUpdatedAt(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (nuo *NotifierUpdateOne) check() error { + if v, ok := nuo.mutation.Name(); ok { + if err := notifier.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Notifier.name": %w`, err)} + } + } + if v, ok := nuo.mutation.URL(); ok { + if err := notifier.URLValidator(v); err != nil { + return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Notifier.url": %w`, err)} + } + } + if _, ok := nuo.mutation.UserID(); nuo.mutation.UserCleared() && !ok { + return errors.New(`ent: clearing a required unique edge "Notifier.user"`) + } + return nil +} + +func (nuo *NotifierUpdateOne) sqlSave(ctx context.Context) (_node *Notifier, err error) { + if err := nuo.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(notifier.Table, notifier.Columns, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID)) + id, ok := nuo.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Notifier.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := nuo.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, notifier.FieldID) + for _, f := range fields { + if !notifier.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != notifier.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := nuo.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := nuo.mutation.UpdatedAt(); ok { + _spec.SetField(notifier.FieldUpdatedAt, field.TypeTime, value) + } + if value, ok := nuo.mutation.Name(); ok { + _spec.SetField(notifier.FieldName, field.TypeString, value) + } + if value, ok := nuo.mutation.URL(); ok { + _spec.SetField(notifier.FieldURL, field.TypeString, value) + } + if value, ok := nuo.mutation.IsActive(); ok { + _spec.SetField(notifier.FieldIsActive, field.TypeBool, value) + } + if nuo.mutation.UserCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: true, + Table: notifier.UserTable, + Columns: []string{notifier.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: user.FieldID, + }, + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := nuo.mutation.UserIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: true, + Table: notifier.UserTable, + Columns: []string{notifier.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: user.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &Notifier{config: nuo.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, nuo.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{notifier.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + nuo.mutation.done = true + return _node, nil +} diff --git a/backend/internal/data/ent/predicate/predicate.go b/backend/internal/data/ent/predicate/predicate.go index b1fbe67..bd36616 100644 --- a/backend/internal/data/ent/predicate/predicate.go +++ b/backend/internal/data/ent/predicate/predicate.go @@ -39,5 +39,8 @@ type Location func(*sql.Selector) // MaintenanceEntry is the predicate function for maintenanceentry builders. type MaintenanceEntry func(*sql.Selector) +// Notifier is the predicate function for notifier builders. +type Notifier func(*sql.Selector) + // User is the predicate function for user builders. type User func(*sql.Selector) diff --git a/backend/internal/data/ent/runtime.go b/backend/internal/data/ent/runtime.go index 5cbd076..cb10fe8 100644 --- a/backend/internal/data/ent/runtime.go +++ b/backend/internal/data/ent/runtime.go @@ -16,6 +16,7 @@ import ( "github.com/hay-kot/homebox/backend/internal/data/ent/label" "github.com/hay-kot/homebox/backend/internal/data/ent/location" "github.com/hay-kot/homebox/backend/internal/data/ent/maintenanceentry" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/schema" "github.com/hay-kot/homebox/backend/internal/data/ent/user" ) @@ -476,6 +477,65 @@ func init() { maintenanceentryDescID := maintenanceentryMixinFields0[0].Descriptor() // maintenanceentry.DefaultID holds the default value on creation for the id field. maintenanceentry.DefaultID = maintenanceentryDescID.Default.(func() uuid.UUID) + notifierMixin := schema.Notifier{}.Mixin() + notifierMixinFields0 := notifierMixin[0].Fields() + _ = notifierMixinFields0 + notifierFields := schema.Notifier{}.Fields() + _ = notifierFields + // notifierDescCreatedAt is the schema descriptor for created_at field. + notifierDescCreatedAt := notifierMixinFields0[1].Descriptor() + // notifier.DefaultCreatedAt holds the default value on creation for the created_at field. + notifier.DefaultCreatedAt = notifierDescCreatedAt.Default.(func() time.Time) + // notifierDescUpdatedAt is the schema descriptor for updated_at field. + notifierDescUpdatedAt := notifierMixinFields0[2].Descriptor() + // notifier.DefaultUpdatedAt holds the default value on creation for the updated_at field. + notifier.DefaultUpdatedAt = notifierDescUpdatedAt.Default.(func() time.Time) + // notifier.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. + notifier.UpdateDefaultUpdatedAt = notifierDescUpdatedAt.UpdateDefault.(func() time.Time) + // notifierDescName is the schema descriptor for name field. + notifierDescName := notifierFields[1].Descriptor() + // notifier.NameValidator is a validator for the "name" field. It is called by the builders before save. + notifier.NameValidator = func() func(string) error { + validators := notifierDescName.Validators + fns := [...]func(string) error{ + validators[0].(func(string) error), + validators[1].(func(string) error), + } + return func(name string) error { + for _, fn := range fns { + if err := fn(name); err != nil { + return err + } + } + return nil + } + }() + // notifierDescURL is the schema descriptor for url field. + notifierDescURL := notifierFields[2].Descriptor() + // notifier.URLValidator is a validator for the "url" field. It is called by the builders before save. + notifier.URLValidator = func() func(string) error { + validators := notifierDescURL.Validators + fns := [...]func(string) error{ + validators[0].(func(string) error), + validators[1].(func(string) error), + } + return func(url string) error { + for _, fn := range fns { + if err := fn(url); err != nil { + return err + } + } + return nil + } + }() + // notifierDescIsActive is the schema descriptor for is_active field. + notifierDescIsActive := notifierFields[3].Descriptor() + // notifier.DefaultIsActive holds the default value on creation for the is_active field. + notifier.DefaultIsActive = notifierDescIsActive.Default.(bool) + // notifierDescID is the schema descriptor for id field. + notifierDescID := notifierMixinFields0[0].Descriptor() + // notifier.DefaultID holds the default value on creation for the id field. + notifier.DefaultID = notifierDescID.Default.(func() uuid.UUID) userMixin := schema.User{}.Mixin() userMixinFields0 := userMixin[0].Fields() _ = userMixinFields0 @@ -550,7 +610,7 @@ func init() { // user.DefaultIsSuperuser holds the default value on creation for the is_superuser field. user.DefaultIsSuperuser = userDescIsSuperuser.Default.(bool) // userDescSuperuser is the schema descriptor for superuser field. - userDescSuperuser := userFields[5].Descriptor() + userDescSuperuser := userFields[4].Descriptor() // user.DefaultSuperuser holds the default value on creation for the superuser field. user.DefaultSuperuser = userDescSuperuser.Default.(bool) // userDescID is the schema descriptor for id field. diff --git a/backend/internal/data/ent/tx.go b/backend/internal/data/ent/tx.go index 0703ce5..f51f2ac 100644 --- a/backend/internal/data/ent/tx.go +++ b/backend/internal/data/ent/tx.go @@ -34,6 +34,8 @@ type Tx struct { Location *LocationClient // MaintenanceEntry is the client for interacting with the MaintenanceEntry builders. MaintenanceEntry *MaintenanceEntryClient + // Notifier is the client for interacting with the Notifier builders. + Notifier *NotifierClient // User is the client for interacting with the User builders. User *UserClient @@ -178,6 +180,7 @@ func (tx *Tx) init() { tx.Label = NewLabelClient(tx.config) tx.Location = NewLocationClient(tx.config) tx.MaintenanceEntry = NewMaintenanceEntryClient(tx.config) + tx.Notifier = NewNotifierClient(tx.config) tx.User = NewUserClient(tx.config) } diff --git a/backend/internal/data/ent/user.go b/backend/internal/data/ent/user.go index 97a9279..5acfa9b 100644 --- a/backend/internal/data/ent/user.go +++ b/backend/internal/data/ent/user.go @@ -30,10 +30,10 @@ type User struct { Password string `json:"-"` // IsSuperuser holds the value of the "is_superuser" field. IsSuperuser bool `json:"is_superuser,omitempty"` - // Role holds the value of the "role" field. - Role user.Role `json:"role,omitempty"` // Superuser holds the value of the "superuser" field. Superuser bool `json:"superuser,omitempty"` + // Role holds the value of the "role" field. + Role user.Role `json:"role,omitempty"` // ActivatedOn holds the value of the "activated_on" field. ActivatedOn time.Time `json:"activated_on,omitempty"` // Edges holds the relations/edges for other nodes in the graph. @@ -48,9 +48,11 @@ type UserEdges struct { Group *Group `json:"group,omitempty"` // AuthTokens holds the value of the auth_tokens edge. AuthTokens []*AuthTokens `json:"auth_tokens,omitempty"` + // Notifiers holds the value of the notifiers edge. + Notifiers []*Notifier `json:"notifiers,omitempty"` // loadedTypes holds the information for reporting if a // type was loaded (or requested) in eager-loading or not. - loadedTypes [2]bool + loadedTypes [3]bool } // GroupOrErr returns the Group value or an error if the edge @@ -75,6 +77,15 @@ func (e UserEdges) AuthTokensOrErr() ([]*AuthTokens, error) { return nil, &NotLoadedError{edge: "auth_tokens"} } +// NotifiersOrErr returns the Notifiers value or an error if the edge +// was not loaded in eager-loading. +func (e UserEdges) NotifiersOrErr() ([]*Notifier, error) { + if e.loadedTypes[2] { + return e.Notifiers, nil + } + return nil, &NotLoadedError{edge: "notifiers"} +} + // scanValues returns the types for scanning values from sql.Rows. func (*User) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) @@ -147,18 +158,18 @@ func (u *User) assignValues(columns []string, values []any) error { } else if value.Valid { u.IsSuperuser = value.Bool } - case user.FieldRole: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field role", values[i]) - } else if value.Valid { - u.Role = user.Role(value.String) - } case user.FieldSuperuser: if value, ok := values[i].(*sql.NullBool); !ok { return fmt.Errorf("unexpected type %T for field superuser", values[i]) } else if value.Valid { u.Superuser = value.Bool } + case user.FieldRole: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field role", values[i]) + } else if value.Valid { + u.Role = user.Role(value.String) + } case user.FieldActivatedOn: if value, ok := values[i].(*sql.NullTime); !ok { return fmt.Errorf("unexpected type %T for field activated_on", values[i]) @@ -187,6 +198,11 @@ func (u *User) QueryAuthTokens() *AuthTokensQuery { return NewUserClient(u.config).QueryAuthTokens(u) } +// QueryNotifiers queries the "notifiers" edge of the User entity. +func (u *User) QueryNotifiers() *NotifierQuery { + return NewUserClient(u.config).QueryNotifiers(u) +} + // Update returns a builder for updating this User. // Note that you need to call User.Unwrap() before calling this method if this User // was returned from a transaction, and the transaction was committed or rolled back. @@ -227,12 +243,12 @@ func (u *User) String() string { builder.WriteString("is_superuser=") builder.WriteString(fmt.Sprintf("%v", u.IsSuperuser)) builder.WriteString(", ") - builder.WriteString("role=") - builder.WriteString(fmt.Sprintf("%v", u.Role)) - builder.WriteString(", ") builder.WriteString("superuser=") builder.WriteString(fmt.Sprintf("%v", u.Superuser)) builder.WriteString(", ") + builder.WriteString("role=") + builder.WriteString(fmt.Sprintf("%v", u.Role)) + builder.WriteString(", ") builder.WriteString("activated_on=") builder.WriteString(u.ActivatedOn.Format(time.ANSIC)) builder.WriteByte(')') diff --git a/backend/internal/data/ent/user/user.go b/backend/internal/data/ent/user/user.go index c8b61c2..93c0c03 100644 --- a/backend/internal/data/ent/user/user.go +++ b/backend/internal/data/ent/user/user.go @@ -26,16 +26,18 @@ const ( FieldPassword = "password" // FieldIsSuperuser holds the string denoting the is_superuser field in the database. FieldIsSuperuser = "is_superuser" - // FieldRole holds the string denoting the role field in the database. - FieldRole = "role" // FieldSuperuser holds the string denoting the superuser field in the database. FieldSuperuser = "superuser" + // FieldRole holds the string denoting the role field in the database. + FieldRole = "role" // FieldActivatedOn holds the string denoting the activated_on field in the database. FieldActivatedOn = "activated_on" // EdgeGroup holds the string denoting the group edge name in mutations. EdgeGroup = "group" // EdgeAuthTokens holds the string denoting the auth_tokens edge name in mutations. EdgeAuthTokens = "auth_tokens" + // EdgeNotifiers holds the string denoting the notifiers edge name in mutations. + EdgeNotifiers = "notifiers" // Table holds the table name of the user in the database. Table = "users" // GroupTable is the table that holds the group relation/edge. @@ -52,6 +54,13 @@ const ( AuthTokensInverseTable = "auth_tokens" // AuthTokensColumn is the table column denoting the auth_tokens relation/edge. AuthTokensColumn = "user_auth_tokens" + // NotifiersTable is the table that holds the notifiers relation/edge. + NotifiersTable = "notifiers" + // NotifiersInverseTable is the table name for the Notifier entity. + // It exists in this package in order to avoid circular dependency with the "notifier" package. + NotifiersInverseTable = "notifiers" + // NotifiersColumn is the table column denoting the notifiers relation/edge. + NotifiersColumn = "user_id" ) // Columns holds all SQL columns for user fields. @@ -63,8 +72,8 @@ var Columns = []string{ FieldEmail, FieldPassword, FieldIsSuperuser, - FieldRole, FieldSuperuser, + FieldRole, FieldActivatedOn, } diff --git a/backend/internal/data/ent/user/where.go b/backend/internal/data/ent/user/where.go index 78335a7..2ad23bb 100644 --- a/backend/internal/data/ent/user/where.go +++ b/backend/internal/data/ent/user/where.go @@ -381,6 +381,16 @@ func IsSuperuserNEQ(v bool) predicate.User { return predicate.User(sql.FieldNEQ(FieldIsSuperuser, v)) } +// SuperuserEQ applies the EQ predicate on the "superuser" field. +func SuperuserEQ(v bool) predicate.User { + return predicate.User(sql.FieldEQ(FieldSuperuser, v)) +} + +// SuperuserNEQ applies the NEQ predicate on the "superuser" field. +func SuperuserNEQ(v bool) predicate.User { + return predicate.User(sql.FieldNEQ(FieldSuperuser, v)) +} + // RoleEQ applies the EQ predicate on the "role" field. func RoleEQ(v Role) predicate.User { return predicate.User(sql.FieldEQ(FieldRole, v)) @@ -401,16 +411,6 @@ func RoleNotIn(vs ...Role) predicate.User { return predicate.User(sql.FieldNotIn(FieldRole, vs...)) } -// SuperuserEQ applies the EQ predicate on the "superuser" field. -func SuperuserEQ(v bool) predicate.User { - return predicate.User(sql.FieldEQ(FieldSuperuser, v)) -} - -// SuperuserNEQ applies the NEQ predicate on the "superuser" field. -func SuperuserNEQ(v bool) predicate.User { - return predicate.User(sql.FieldNEQ(FieldSuperuser, v)) -} - // ActivatedOnEQ applies the EQ predicate on the "activated_on" field. func ActivatedOnEQ(v time.Time) predicate.User { return predicate.User(sql.FieldEQ(FieldActivatedOn, v)) @@ -515,6 +515,33 @@ func HasAuthTokensWith(preds ...predicate.AuthTokens) predicate.User { }) } +// HasNotifiers applies the HasEdge predicate on the "notifiers" edge. +func HasNotifiers() predicate.User { + return predicate.User(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, NotifiersTable, NotifiersColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasNotifiersWith applies the HasEdge predicate on the "notifiers" edge with a given conditions (other predicates). +func HasNotifiersWith(preds ...predicate.Notifier) predicate.User { + return predicate.User(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(NotifiersInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, NotifiersTable, NotifiersColumn), + ) + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + // And groups predicates with the AND operator between them. func And(predicates ...predicate.User) predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/backend/internal/data/ent/user_create.go b/backend/internal/data/ent/user_create.go index 3dc703d..14b4282 100644 --- a/backend/internal/data/ent/user_create.go +++ b/backend/internal/data/ent/user_create.go @@ -13,6 +13,7 @@ import ( "github.com/google/uuid" "github.com/hay-kot/homebox/backend/internal/data/ent/authtokens" "github.com/hay-kot/homebox/backend/internal/data/ent/group" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/user" ) @@ -83,20 +84,6 @@ func (uc *UserCreate) SetNillableIsSuperuser(b *bool) *UserCreate { return uc } -// SetRole sets the "role" field. -func (uc *UserCreate) SetRole(u user.Role) *UserCreate { - uc.mutation.SetRole(u) - return uc -} - -// SetNillableRole sets the "role" field if the given value is not nil. -func (uc *UserCreate) SetNillableRole(u *user.Role) *UserCreate { - if u != nil { - uc.SetRole(*u) - } - return uc -} - // SetSuperuser sets the "superuser" field. func (uc *UserCreate) SetSuperuser(b bool) *UserCreate { uc.mutation.SetSuperuser(b) @@ -111,6 +98,20 @@ func (uc *UserCreate) SetNillableSuperuser(b *bool) *UserCreate { return uc } +// SetRole sets the "role" field. +func (uc *UserCreate) SetRole(u user.Role) *UserCreate { + uc.mutation.SetRole(u) + return uc +} + +// SetNillableRole sets the "role" field if the given value is not nil. +func (uc *UserCreate) SetNillableRole(u *user.Role) *UserCreate { + if u != nil { + uc.SetRole(*u) + } + return uc +} + // SetActivatedOn sets the "activated_on" field. func (uc *UserCreate) SetActivatedOn(t time.Time) *UserCreate { uc.mutation.SetActivatedOn(t) @@ -165,6 +166,21 @@ func (uc *UserCreate) AddAuthTokens(a ...*AuthTokens) *UserCreate { return uc.AddAuthTokenIDs(ids...) } +// AddNotifierIDs adds the "notifiers" edge to the Notifier entity by IDs. +func (uc *UserCreate) AddNotifierIDs(ids ...uuid.UUID) *UserCreate { + uc.mutation.AddNotifierIDs(ids...) + return uc +} + +// AddNotifiers adds the "notifiers" edges to the Notifier entity. +func (uc *UserCreate) AddNotifiers(n ...*Notifier) *UserCreate { + ids := make([]uuid.UUID, len(n)) + for i := range n { + ids[i] = n[i].ID + } + return uc.AddNotifierIDs(ids...) +} + // Mutation returns the UserMutation object of the builder. func (uc *UserCreate) Mutation() *UserMutation { return uc.mutation @@ -212,14 +228,14 @@ func (uc *UserCreate) defaults() { v := user.DefaultIsSuperuser uc.mutation.SetIsSuperuser(v) } - if _, ok := uc.mutation.Role(); !ok { - v := user.DefaultRole - uc.mutation.SetRole(v) - } if _, ok := uc.mutation.Superuser(); !ok { v := user.DefaultSuperuser uc.mutation.SetSuperuser(v) } + if _, ok := uc.mutation.Role(); !ok { + v := user.DefaultRole + uc.mutation.SetRole(v) + } if _, ok := uc.mutation.ID(); !ok { v := user.DefaultID() uc.mutation.SetID(v) @@ -261,6 +277,9 @@ func (uc *UserCreate) check() error { if _, ok := uc.mutation.IsSuperuser(); !ok { return &ValidationError{Name: "is_superuser", err: errors.New(`ent: missing required field "User.is_superuser"`)} } + if _, ok := uc.mutation.Superuser(); !ok { + return &ValidationError{Name: "superuser", err: errors.New(`ent: missing required field "User.superuser"`)} + } if _, ok := uc.mutation.Role(); !ok { return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "User.role"`)} } @@ -269,9 +288,6 @@ func (uc *UserCreate) check() error { return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "User.role": %w`, err)} } } - if _, ok := uc.mutation.Superuser(); !ok { - return &ValidationError{Name: "superuser", err: errors.New(`ent: missing required field "User.superuser"`)} - } if _, ok := uc.mutation.GroupID(); !ok { return &ValidationError{Name: "group", err: errors.New(`ent: missing required edge "User.group"`)} } @@ -334,14 +350,14 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { _spec.SetField(user.FieldIsSuperuser, field.TypeBool, value) _node.IsSuperuser = value } - if value, ok := uc.mutation.Role(); ok { - _spec.SetField(user.FieldRole, field.TypeEnum, value) - _node.Role = value - } if value, ok := uc.mutation.Superuser(); ok { _spec.SetField(user.FieldSuperuser, field.TypeBool, value) _node.Superuser = value } + if value, ok := uc.mutation.Role(); ok { + _spec.SetField(user.FieldRole, field.TypeEnum, value) + _node.Role = value + } if value, ok := uc.mutation.ActivatedOn(); ok { _spec.SetField(user.FieldActivatedOn, field.TypeTime, value) _node.ActivatedOn = value @@ -385,6 +401,25 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { } _spec.Edges = append(_spec.Edges, edge) } + if nodes := uc.mutation.NotifiersIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } return _node, _spec } diff --git a/backend/internal/data/ent/user_query.go b/backend/internal/data/ent/user_query.go index a722f2e..aa90822 100644 --- a/backend/internal/data/ent/user_query.go +++ b/backend/internal/data/ent/user_query.go @@ -14,6 +14,7 @@ import ( "github.com/google/uuid" "github.com/hay-kot/homebox/backend/internal/data/ent/authtokens" "github.com/hay-kot/homebox/backend/internal/data/ent/group" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" "github.com/hay-kot/homebox/backend/internal/data/ent/user" ) @@ -27,6 +28,7 @@ type UserQuery struct { predicates []predicate.User withGroup *GroupQuery withAuthTokens *AuthTokensQuery + withNotifiers *NotifierQuery withFKs bool // intermediate query (i.e. traversal path). sql *sql.Selector @@ -108,6 +110,28 @@ func (uq *UserQuery) QueryAuthTokens() *AuthTokensQuery { return query } +// QueryNotifiers chains the current query on the "notifiers" edge. +func (uq *UserQuery) QueryNotifiers() *NotifierQuery { + query := (&NotifierClient{config: uq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := uq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := uq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(user.Table, user.FieldID, selector), + sqlgraph.To(notifier.Table, notifier.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, user.NotifiersTable, user.NotifiersColumn), + ) + fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step) + return fromU, nil + } + return query +} + // First returns the first User entity from the query. // Returns a *NotFoundError when no User was found. func (uq *UserQuery) First(ctx context.Context) (*User, error) { @@ -302,6 +326,7 @@ func (uq *UserQuery) Clone() *UserQuery { predicates: append([]predicate.User{}, uq.predicates...), withGroup: uq.withGroup.Clone(), withAuthTokens: uq.withAuthTokens.Clone(), + withNotifiers: uq.withNotifiers.Clone(), // clone intermediate query. sql: uq.sql.Clone(), path: uq.path, @@ -330,6 +355,17 @@ func (uq *UserQuery) WithAuthTokens(opts ...func(*AuthTokensQuery)) *UserQuery { return uq } +// WithNotifiers tells the query-builder to eager-load the nodes that are connected to +// the "notifiers" edge. The optional arguments are used to configure the query builder of the edge. +func (uq *UserQuery) WithNotifiers(opts ...func(*NotifierQuery)) *UserQuery { + query := (&NotifierClient{config: uq.config}).Query() + for _, opt := range opts { + opt(query) + } + uq.withNotifiers = query + return uq +} + // GroupBy is used to group vertices by one or more fields/columns. // It is often used with aggregate functions, like: count, max, mean, min, sum. // @@ -409,9 +445,10 @@ func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, e nodes = []*User{} withFKs = uq.withFKs _spec = uq.querySpec() - loadedTypes = [2]bool{ + loadedTypes = [3]bool{ uq.withGroup != nil, uq.withAuthTokens != nil, + uq.withNotifiers != nil, } ) if uq.withGroup != nil { @@ -451,6 +488,13 @@ func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, e return nil, err } } + if query := uq.withNotifiers; query != nil { + if err := uq.loadNotifiers(ctx, query, nodes, + func(n *User) { n.Edges.Notifiers = []*Notifier{} }, + func(n *User, e *Notifier) { n.Edges.Notifiers = append(n.Edges.Notifiers, e) }); err != nil { + return nil, err + } + } return nodes, nil } @@ -517,6 +561,33 @@ func (uq *UserQuery) loadAuthTokens(ctx context.Context, query *AuthTokensQuery, } return nil } +func (uq *UserQuery) loadNotifiers(ctx context.Context, query *NotifierQuery, nodes []*User, init func(*User), assign func(*User, *Notifier)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[uuid.UUID]*User) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + query.Where(predicate.Notifier(func(s *sql.Selector) { + s.Where(sql.InValues(user.NotifiersColumn, fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.UserID + node, ok := nodeids[fk] + if !ok { + return fmt.Errorf(`unexpected foreign-key "user_id" returned %v for node %v`, fk, n.ID) + } + assign(node, n) + } + return nil +} func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) { _spec := uq.querySpec() diff --git a/backend/internal/data/ent/user_update.go b/backend/internal/data/ent/user_update.go index 4bb0296..26c2330 100644 --- a/backend/internal/data/ent/user_update.go +++ b/backend/internal/data/ent/user_update.go @@ -14,6 +14,7 @@ import ( "github.com/google/uuid" "github.com/hay-kot/homebox/backend/internal/data/ent/authtokens" "github.com/hay-kot/homebox/backend/internal/data/ent/group" + "github.com/hay-kot/homebox/backend/internal/data/ent/notifier" "github.com/hay-kot/homebox/backend/internal/data/ent/predicate" "github.com/hay-kot/homebox/backend/internal/data/ent/user" ) @@ -69,20 +70,6 @@ func (uu *UserUpdate) SetNillableIsSuperuser(b *bool) *UserUpdate { return uu } -// SetRole sets the "role" field. -func (uu *UserUpdate) SetRole(u user.Role) *UserUpdate { - uu.mutation.SetRole(u) - return uu -} - -// SetNillableRole sets the "role" field if the given value is not nil. -func (uu *UserUpdate) SetNillableRole(u *user.Role) *UserUpdate { - if u != nil { - uu.SetRole(*u) - } - return uu -} - // SetSuperuser sets the "superuser" field. func (uu *UserUpdate) SetSuperuser(b bool) *UserUpdate { uu.mutation.SetSuperuser(b) @@ -97,6 +84,20 @@ func (uu *UserUpdate) SetNillableSuperuser(b *bool) *UserUpdate { return uu } +// SetRole sets the "role" field. +func (uu *UserUpdate) SetRole(u user.Role) *UserUpdate { + uu.mutation.SetRole(u) + return uu +} + +// SetNillableRole sets the "role" field if the given value is not nil. +func (uu *UserUpdate) SetNillableRole(u *user.Role) *UserUpdate { + if u != nil { + uu.SetRole(*u) + } + return uu +} + // SetActivatedOn sets the "activated_on" field. func (uu *UserUpdate) SetActivatedOn(t time.Time) *UserUpdate { uu.mutation.SetActivatedOn(t) @@ -143,6 +144,21 @@ func (uu *UserUpdate) AddAuthTokens(a ...*AuthTokens) *UserUpdate { return uu.AddAuthTokenIDs(ids...) } +// AddNotifierIDs adds the "notifiers" edge to the Notifier entity by IDs. +func (uu *UserUpdate) AddNotifierIDs(ids ...uuid.UUID) *UserUpdate { + uu.mutation.AddNotifierIDs(ids...) + return uu +} + +// AddNotifiers adds the "notifiers" edges to the Notifier entity. +func (uu *UserUpdate) AddNotifiers(n ...*Notifier) *UserUpdate { + ids := make([]uuid.UUID, len(n)) + for i := range n { + ids[i] = n[i].ID + } + return uu.AddNotifierIDs(ids...) +} + // Mutation returns the UserMutation object of the builder. func (uu *UserUpdate) Mutation() *UserMutation { return uu.mutation @@ -175,6 +191,27 @@ func (uu *UserUpdate) RemoveAuthTokens(a ...*AuthTokens) *UserUpdate { return uu.RemoveAuthTokenIDs(ids...) } +// ClearNotifiers clears all "notifiers" edges to the Notifier entity. +func (uu *UserUpdate) ClearNotifiers() *UserUpdate { + uu.mutation.ClearNotifiers() + return uu +} + +// RemoveNotifierIDs removes the "notifiers" edge to Notifier entities by IDs. +func (uu *UserUpdate) RemoveNotifierIDs(ids ...uuid.UUID) *UserUpdate { + uu.mutation.RemoveNotifierIDs(ids...) + return uu +} + +// RemoveNotifiers removes "notifiers" edges to Notifier entities. +func (uu *UserUpdate) RemoveNotifiers(n ...*Notifier) *UserUpdate { + ids := make([]uuid.UUID, len(n)) + for i := range n { + ids[i] = n[i].ID + } + return uu.RemoveNotifierIDs(ids...) +} + // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { uu.defaults() @@ -266,12 +303,12 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := uu.mutation.IsSuperuser(); ok { _spec.SetField(user.FieldIsSuperuser, field.TypeBool, value) } - if value, ok := uu.mutation.Role(); ok { - _spec.SetField(user.FieldRole, field.TypeEnum, value) - } if value, ok := uu.mutation.Superuser(); ok { _spec.SetField(user.FieldSuperuser, field.TypeBool, value) } + if value, ok := uu.mutation.Role(); ok { + _spec.SetField(user.FieldRole, field.TypeEnum, value) + } if value, ok := uu.mutation.ActivatedOn(); ok { _spec.SetField(user.FieldActivatedOn, field.TypeTime, value) } @@ -367,6 +404,60 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } _spec.Edges.Add = append(_spec.Edges.Add, edge) } + if uu.mutation.NotifiersCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := uu.mutation.RemovedNotifiersIDs(); len(nodes) > 0 && !uu.mutation.NotifiersCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := uu.mutation.NotifiersIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } if n, err = sqlgraph.UpdateNodes(ctx, uu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{user.Label} @@ -425,20 +516,6 @@ func (uuo *UserUpdateOne) SetNillableIsSuperuser(b *bool) *UserUpdateOne { return uuo } -// SetRole sets the "role" field. -func (uuo *UserUpdateOne) SetRole(u user.Role) *UserUpdateOne { - uuo.mutation.SetRole(u) - return uuo -} - -// SetNillableRole sets the "role" field if the given value is not nil. -func (uuo *UserUpdateOne) SetNillableRole(u *user.Role) *UserUpdateOne { - if u != nil { - uuo.SetRole(*u) - } - return uuo -} - // SetSuperuser sets the "superuser" field. func (uuo *UserUpdateOne) SetSuperuser(b bool) *UserUpdateOne { uuo.mutation.SetSuperuser(b) @@ -453,6 +530,20 @@ func (uuo *UserUpdateOne) SetNillableSuperuser(b *bool) *UserUpdateOne { return uuo } +// SetRole sets the "role" field. +func (uuo *UserUpdateOne) SetRole(u user.Role) *UserUpdateOne { + uuo.mutation.SetRole(u) + return uuo +} + +// SetNillableRole sets the "role" field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableRole(u *user.Role) *UserUpdateOne { + if u != nil { + uuo.SetRole(*u) + } + return uuo +} + // SetActivatedOn sets the "activated_on" field. func (uuo *UserUpdateOne) SetActivatedOn(t time.Time) *UserUpdateOne { uuo.mutation.SetActivatedOn(t) @@ -499,6 +590,21 @@ func (uuo *UserUpdateOne) AddAuthTokens(a ...*AuthTokens) *UserUpdateOne { return uuo.AddAuthTokenIDs(ids...) } +// AddNotifierIDs adds the "notifiers" edge to the Notifier entity by IDs. +func (uuo *UserUpdateOne) AddNotifierIDs(ids ...uuid.UUID) *UserUpdateOne { + uuo.mutation.AddNotifierIDs(ids...) + return uuo +} + +// AddNotifiers adds the "notifiers" edges to the Notifier entity. +func (uuo *UserUpdateOne) AddNotifiers(n ...*Notifier) *UserUpdateOne { + ids := make([]uuid.UUID, len(n)) + for i := range n { + ids[i] = n[i].ID + } + return uuo.AddNotifierIDs(ids...) +} + // Mutation returns the UserMutation object of the builder. func (uuo *UserUpdateOne) Mutation() *UserMutation { return uuo.mutation @@ -531,6 +637,27 @@ func (uuo *UserUpdateOne) RemoveAuthTokens(a ...*AuthTokens) *UserUpdateOne { return uuo.RemoveAuthTokenIDs(ids...) } +// ClearNotifiers clears all "notifiers" edges to the Notifier entity. +func (uuo *UserUpdateOne) ClearNotifiers() *UserUpdateOne { + uuo.mutation.ClearNotifiers() + return uuo +} + +// RemoveNotifierIDs removes the "notifiers" edge to Notifier entities by IDs. +func (uuo *UserUpdateOne) RemoveNotifierIDs(ids ...uuid.UUID) *UserUpdateOne { + uuo.mutation.RemoveNotifierIDs(ids...) + return uuo +} + +// RemoveNotifiers removes "notifiers" edges to Notifier entities. +func (uuo *UserUpdateOne) RemoveNotifiers(n ...*Notifier) *UserUpdateOne { + ids := make([]uuid.UUID, len(n)) + for i := range n { + ids[i] = n[i].ID + } + return uuo.RemoveNotifierIDs(ids...) +} + // Where appends a list predicates to the UserUpdate builder. func (uuo *UserUpdateOne) Where(ps ...predicate.User) *UserUpdateOne { uuo.mutation.Where(ps...) @@ -652,12 +779,12 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) if value, ok := uuo.mutation.IsSuperuser(); ok { _spec.SetField(user.FieldIsSuperuser, field.TypeBool, value) } - if value, ok := uuo.mutation.Role(); ok { - _spec.SetField(user.FieldRole, field.TypeEnum, value) - } if value, ok := uuo.mutation.Superuser(); ok { _spec.SetField(user.FieldSuperuser, field.TypeBool, value) } + if value, ok := uuo.mutation.Role(); ok { + _spec.SetField(user.FieldRole, field.TypeEnum, value) + } if value, ok := uuo.mutation.ActivatedOn(); ok { _spec.SetField(user.FieldActivatedOn, field.TypeTime, value) } @@ -753,6 +880,60 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } + if uuo.mutation.NotifiersCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := uuo.mutation.RemovedNotifiersIDs(); len(nodes) > 0 && !uuo.mutation.NotifiersCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := uuo.mutation.NotifiersIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.NotifiersTable, + Columns: []string{user.NotifiersColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: &sqlgraph.FieldSpec{ + Type: field.TypeUUID, + Column: notifier.FieldID, + }, + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } _node = &User{config: uuo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues