pcmt/ent/user_create.go
surtur 74546f996b
All checks were successful
continuous-integration/drone/push Build is passing
ent: add/extend entities, tests, validation
2023-08-19 04:52:15 +02:00

477 lines
13 KiB
Go

// 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"
"git.dotya.ml/mirre-mt/pcmt/ent/agekey"
"git.dotya.ml/mirre-mt/pcmt/ent/searchquery"
"git.dotya.ml/mirre-mt/pcmt/ent/trackedbreaches"
"git.dotya.ml/mirre-mt/pcmt/ent/user"
"github.com/google/uuid"
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
// SetUsername sets the "username" field.
func (uc *UserCreate) SetUsername(s string) *UserCreate {
uc.mutation.SetUsername(s)
return uc
}
// SetEmail sets the "email" field.
func (uc *UserCreate) SetEmail(s string) *UserCreate {
uc.mutation.SetEmail(s)
return uc
}
// SetPassword sets the "password" field.
func (uc *UserCreate) SetPassword(b []byte) *UserCreate {
uc.mutation.SetPassword(b)
return uc
}
// SetIsAdmin sets the "is_admin" field.
func (uc *UserCreate) SetIsAdmin(b bool) *UserCreate {
uc.mutation.SetIsAdmin(b)
return uc
}
// SetNillableIsAdmin sets the "is_admin" field if the given value is not nil.
func (uc *UserCreate) SetNillableIsAdmin(b *bool) *UserCreate {
if b != nil {
uc.SetIsAdmin(*b)
}
return uc
}
// SetIsActive sets the "is_active" field.
func (uc *UserCreate) SetIsActive(b bool) *UserCreate {
uc.mutation.SetIsActive(b)
return uc
}
// SetNillableIsActive sets the "is_active" field if the given value is not nil.
func (uc *UserCreate) SetNillableIsActive(b *bool) *UserCreate {
if b != nil {
uc.SetIsActive(*b)
}
return uc
}
// SetCreatedAt sets the "created_at" field.
func (uc *UserCreate) SetCreatedAt(t time.Time) *UserCreate {
uc.mutation.SetCreatedAt(t)
return uc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (uc *UserCreate) SetNillableCreatedAt(t *time.Time) *UserCreate {
if t != nil {
uc.SetCreatedAt(*t)
}
return uc
}
// SetUpdatedAt sets the "updated_at" field.
func (uc *UserCreate) SetUpdatedAt(t time.Time) *UserCreate {
uc.mutation.SetUpdatedAt(t)
return uc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (uc *UserCreate) SetNillableUpdatedAt(t *time.Time) *UserCreate {
if t != nil {
uc.SetUpdatedAt(*t)
}
return uc
}
// SetLastLogin sets the "last_login" field.
func (uc *UserCreate) SetLastLogin(t time.Time) *UserCreate {
uc.mutation.SetLastLogin(t)
return uc
}
// SetNillableLastLogin sets the "last_login" field if the given value is not nil.
func (uc *UserCreate) SetNillableLastLogin(t *time.Time) *UserCreate {
if t != nil {
uc.SetLastLogin(*t)
}
return uc
}
// SetID sets the "id" field.
func (uc *UserCreate) SetID(u uuid.UUID) *UserCreate {
uc.mutation.SetID(u)
return uc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (uc *UserCreate) SetNillableID(u *uuid.UUID) *UserCreate {
if u != nil {
uc.SetID(*u)
}
return uc
}
// SetAgekeyID sets the "agekey" edge to the AgeKey entity by ID.
func (uc *UserCreate) SetAgekeyID(id uuid.UUID) *UserCreate {
uc.mutation.SetAgekeyID(id)
return uc
}
// SetNillableAgekeyID sets the "agekey" edge to the AgeKey entity by ID if the given value is not nil.
func (uc *UserCreate) SetNillableAgekeyID(id *uuid.UUID) *UserCreate {
if id != nil {
uc = uc.SetAgekeyID(*id)
}
return uc
}
// SetAgekey sets the "agekey" edge to the AgeKey entity.
func (uc *UserCreate) SetAgekey(a *AgeKey) *UserCreate {
return uc.SetAgekeyID(a.ID)
}
// AddTrackedBreachIDs adds the "tracked_breaches" edge to the TrackedBreaches entity by IDs.
func (uc *UserCreate) AddTrackedBreachIDs(ids ...uuid.UUID) *UserCreate {
uc.mutation.AddTrackedBreachIDs(ids...)
return uc
}
// AddTrackedBreaches adds the "tracked_breaches" edges to the TrackedBreaches entity.
func (uc *UserCreate) AddTrackedBreaches(t ...*TrackedBreaches) *UserCreate {
ids := make([]uuid.UUID, len(t))
for i := range t {
ids[i] = t[i].ID
}
return uc.AddTrackedBreachIDs(ids...)
}
// AddSearchQueryIDs adds the "search_queries" edge to the SearchQuery entity by IDs.
func (uc *UserCreate) AddSearchQueryIDs(ids ...uuid.UUID) *UserCreate {
uc.mutation.AddSearchQueryIDs(ids...)
return uc
}
// AddSearchQueries adds the "search_queries" edges to the SearchQuery entity.
func (uc *UserCreate) AddSearchQueries(s ...*SearchQuery) *UserCreate {
ids := make([]uuid.UUID, len(s))
for i := range s {
ids[i] = s[i].ID
}
return uc.AddSearchQueryIDs(ids...)
}
// Mutation returns the UserMutation object of the builder.
func (uc *UserCreate) Mutation() *UserMutation {
return uc.mutation
}
// Save creates the User in the database.
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
uc.defaults()
return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (uc *UserCreate) SaveX(ctx context.Context) *User {
v, err := uc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (uc *UserCreate) Exec(ctx context.Context) error {
_, err := uc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (uc *UserCreate) ExecX(ctx context.Context) {
if err := uc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (uc *UserCreate) defaults() {
if _, ok := uc.mutation.IsAdmin(); !ok {
v := user.DefaultIsAdmin
uc.mutation.SetIsAdmin(v)
}
if _, ok := uc.mutation.IsActive(); !ok {
v := user.DefaultIsActive
uc.mutation.SetIsActive(v)
}
if _, ok := uc.mutation.CreatedAt(); !ok {
v := user.DefaultCreatedAt()
uc.mutation.SetCreatedAt(v)
}
if _, ok := uc.mutation.UpdatedAt(); !ok {
v := user.DefaultUpdatedAt()
uc.mutation.SetUpdatedAt(v)
}
if _, ok := uc.mutation.LastLogin(); !ok {
v := user.DefaultLastLogin
uc.mutation.SetLastLogin(v)
}
if _, ok := uc.mutation.ID(); !ok {
v := user.DefaultID()
uc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (uc *UserCreate) check() error {
if _, ok := uc.mutation.Username(); !ok {
return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "User.username"`)}
}
if v, ok := uc.mutation.Username(); ok {
if err := user.UsernameValidator(v); err != nil {
return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)}
}
}
if _, ok := uc.mutation.Email(); !ok {
return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)}
}
if v, ok := uc.mutation.Email(); ok {
if err := user.EmailValidator(v); err != nil {
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
}
}
if _, ok := uc.mutation.Password(); !ok {
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)}
}
if v, ok := uc.mutation.Password(); ok {
if err := user.PasswordValidator(v); err != nil {
return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "User.password": %w`, err)}
}
}
if _, ok := uc.mutation.IsAdmin(); !ok {
return &ValidationError{Name: "is_admin", err: errors.New(`ent: missing required field "User.is_admin"`)}
}
if _, ok := uc.mutation.IsActive(); !ok {
return &ValidationError{Name: "is_active", err: errors.New(`ent: missing required field "User.is_active"`)}
}
if _, ok := uc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)}
}
if _, ok := uc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)}
}
if _, ok := uc.mutation.LastLogin(); !ok {
return &ValidationError{Name: "last_login", err: errors.New(`ent: missing required field "User.last_login"`)}
}
return nil
}
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
if err := uc.check(); err != nil {
return nil, err
}
_node, _spec := uc.createSpec()
if err := sqlgraph.CreateNode(ctx, uc.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
}
}
uc.mutation.id = &_node.ID
uc.mutation.done = true
return _node, nil
}
func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
var (
_node = &User{config: uc.config}
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID))
)
if id, ok := uc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := uc.mutation.Username(); ok {
_spec.SetField(user.FieldUsername, field.TypeString, value)
_node.Username = value
}
if value, ok := uc.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
_node.Email = value
}
if value, ok := uc.mutation.Password(); ok {
_spec.SetField(user.FieldPassword, field.TypeBytes, value)
_node.Password = value
}
if value, ok := uc.mutation.IsAdmin(); ok {
_spec.SetField(user.FieldIsAdmin, field.TypeBool, value)
_node.IsAdmin = value
}
if value, ok := uc.mutation.IsActive(); ok {
_spec.SetField(user.FieldIsActive, field.TypeBool, value)
_node.IsActive = value
}
if value, ok := uc.mutation.CreatedAt(); ok {
_spec.SetField(user.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := uc.mutation.UpdatedAt(); ok {
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := uc.mutation.LastLogin(); ok {
_spec.SetField(user.FieldLastLogin, field.TypeTime, value)
_node.LastLogin = value
}
if nodes := uc.mutation.AgekeyIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: false,
Table: user.AgekeyTable,
Columns: []string{user.AgekeyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(agekey.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.TrackedBreachesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.TrackedBreachesTable,
Columns: []string{user.TrackedBreachesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(trackedbreaches.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.SearchQueriesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.SearchQueriesTable,
Columns: []string{user.SearchQueriesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(searchquery.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// UserCreateBulk is the builder for creating many User entities in bulk.
type UserCreateBulk struct {
config
builders []*UserCreate
}
// Save creates the User entities in the database.
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
nodes := make([]*User, len(ucb.builders))
mutators := make([]Mutator, len(ucb.builders))
for i := range ucb.builders {
func(i int, root context.Context) {
builder := ucb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*UserMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
var err error
nodes[i], specs[i] = builder.createSpec()
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, ucb.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, ucb.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, ucb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
v, err := ucb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ucb *UserCreateBulk) Exec(ctx context.Context) error {
_, err := ucb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ucb *UserCreateBulk) ExecX(ctx context.Context) {
if err := ucb.Exec(ctx); err != nil {
panic(err)
}
}