mirror of
https://github.com/holos-run/holos.git
synced 2026-03-20 01:04:59 +00:00
This patch simplifies the user and organization registration and query
for the UI. The pattern clients are expected to follow is to create if
the get fails. For example, the following pseudo-go-code is the
expected calling convention:
var entity *ent.User
entity, err := Get()
if err != nil {
if ent.MaskNotFound(err) == nil {
entity = Create()
} else {
return err
}
}
return entity
This patch adds the following service methods. For initial
registration, all input data comes from the id token claims of the
authenticated user.
```
❯ grpcurl -H "x-oidc-id-token: $(holos token)" jeff.app.dev.k2.holos.run:443 list | xargs -n1 grpcurl -H "x-oidc-id-token: $(holos token)" jeff.app.dev.k2.holos.run:443 list
holos.v1alpha1.OrganizationService.CreateCallerOrganization
holos.v1alpha1.OrganizationService.GetCallerOrganizations
holos.v1alpha1.UserService.CreateCallerUser
holos.v1alpha1.UserService.GetCallerClaims
holos.v1alpha1.UserService.GetCallerUser
```
539 lines
15 KiB
Go
539 lines
15 KiB
Go
// 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/gofrs/uuid"
|
|
"github.com/holos-run/holos/internal/ent/organization"
|
|
"github.com/holos-run/holos/internal/ent/predicate"
|
|
"github.com/holos-run/holos/internal/ent/user"
|
|
)
|
|
|
|
// UserUpdate is the builder for updating User entities.
|
|
type UserUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *UserMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the UserUpdate builder.
|
|
func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
|
|
uu.mutation.Where(ps...)
|
|
return uu
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (uu *UserUpdate) SetUpdatedAt(t time.Time) *UserUpdate {
|
|
uu.mutation.SetUpdatedAt(t)
|
|
return uu
|
|
}
|
|
|
|
// SetEmail sets the "email" field.
|
|
func (uu *UserUpdate) SetEmail(s string) *UserUpdate {
|
|
uu.mutation.SetEmail(s)
|
|
return uu
|
|
}
|
|
|
|
// SetNillableEmail sets the "email" field if the given value is not nil.
|
|
func (uu *UserUpdate) SetNillableEmail(s *string) *UserUpdate {
|
|
if s != nil {
|
|
uu.SetEmail(*s)
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// SetIss sets the "iss" field.
|
|
func (uu *UserUpdate) SetIss(s string) *UserUpdate {
|
|
uu.mutation.SetIss(s)
|
|
return uu
|
|
}
|
|
|
|
// SetNillableIss sets the "iss" field if the given value is not nil.
|
|
func (uu *UserUpdate) SetNillableIss(s *string) *UserUpdate {
|
|
if s != nil {
|
|
uu.SetIss(*s)
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// SetSub sets the "sub" field.
|
|
func (uu *UserUpdate) SetSub(s string) *UserUpdate {
|
|
uu.mutation.SetSub(s)
|
|
return uu
|
|
}
|
|
|
|
// SetNillableSub sets the "sub" field if the given value is not nil.
|
|
func (uu *UserUpdate) SetNillableSub(s *string) *UserUpdate {
|
|
if s != nil {
|
|
uu.SetSub(*s)
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (uu *UserUpdate) SetName(s string) *UserUpdate {
|
|
uu.mutation.SetName(s)
|
|
return uu
|
|
}
|
|
|
|
// SetNillableName sets the "name" field if the given value is not nil.
|
|
func (uu *UserUpdate) SetNillableName(s *string) *UserUpdate {
|
|
if s != nil {
|
|
uu.SetName(*s)
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// AddOrganizationIDs adds the "organizations" edge to the Organization entity by IDs.
|
|
func (uu *UserUpdate) AddOrganizationIDs(ids ...uuid.UUID) *UserUpdate {
|
|
uu.mutation.AddOrganizationIDs(ids...)
|
|
return uu
|
|
}
|
|
|
|
// AddOrganizations adds the "organizations" edges to the Organization entity.
|
|
func (uu *UserUpdate) AddOrganizations(o ...*Organization) *UserUpdate {
|
|
ids := make([]uuid.UUID, len(o))
|
|
for i := range o {
|
|
ids[i] = o[i].ID
|
|
}
|
|
return uu.AddOrganizationIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the UserMutation object of the builder.
|
|
func (uu *UserUpdate) Mutation() *UserMutation {
|
|
return uu.mutation
|
|
}
|
|
|
|
// ClearOrganizations clears all "organizations" edges to the Organization entity.
|
|
func (uu *UserUpdate) ClearOrganizations() *UserUpdate {
|
|
uu.mutation.ClearOrganizations()
|
|
return uu
|
|
}
|
|
|
|
// RemoveOrganizationIDs removes the "organizations" edge to Organization entities by IDs.
|
|
func (uu *UserUpdate) RemoveOrganizationIDs(ids ...uuid.UUID) *UserUpdate {
|
|
uu.mutation.RemoveOrganizationIDs(ids...)
|
|
return uu
|
|
}
|
|
|
|
// RemoveOrganizations removes "organizations" edges to Organization entities.
|
|
func (uu *UserUpdate) RemoveOrganizations(o ...*Organization) *UserUpdate {
|
|
ids := make([]uuid.UUID, len(o))
|
|
for i := range o {
|
|
ids[i] = o[i].ID
|
|
}
|
|
return uu.RemoveOrganizationIDs(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()
|
|
return withHooks(ctx, uu.sqlSave, uu.mutation, uu.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (uu *UserUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := uu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (uu *UserUpdate) Exec(ctx context.Context) error {
|
|
_, err := uu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (uu *UserUpdate) ExecX(ctx context.Context) {
|
|
if err := uu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (uu *UserUpdate) defaults() {
|
|
if _, ok := uu.mutation.UpdatedAt(); !ok {
|
|
v := user.UpdateDefaultUpdatedAt()
|
|
uu.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (uu *UserUpdate) check() error {
|
|
if v, ok := uu.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)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|
if err := uu.check(); err != nil {
|
|
return n, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID))
|
|
if ps := uu.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := uu.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
|
|
}
|
|
if value, ok := uu.mutation.Email(); ok {
|
|
_spec.SetField(user.FieldEmail, field.TypeString, value)
|
|
}
|
|
if value, ok := uu.mutation.Iss(); ok {
|
|
_spec.SetField(user.FieldIss, field.TypeString, value)
|
|
}
|
|
if value, ok := uu.mutation.Sub(); ok {
|
|
_spec.SetField(user.FieldSub, field.TypeString, value)
|
|
}
|
|
if value, ok := uu.mutation.Name(); ok {
|
|
_spec.SetField(user.FieldName, field.TypeString, value)
|
|
}
|
|
if uu.mutation.OrganizationsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: user.OrganizationsTable,
|
|
Columns: user.OrganizationsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uu.mutation.RemovedOrganizationsIDs(); len(nodes) > 0 && !uu.mutation.OrganizationsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: user.OrganizationsTable,
|
|
Columns: user.OrganizationsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uu.mutation.OrganizationsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: user.OrganizationsTable,
|
|
Columns: user.OrganizationsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
|
|
},
|
|
}
|
|
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}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
uu.mutation.done = true
|
|
return n, nil
|
|
}
|
|
|
|
// UserUpdateOne is the builder for updating a single User entity.
|
|
type UserUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *UserMutation
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (uuo *UserUpdateOne) SetUpdatedAt(t time.Time) *UserUpdateOne {
|
|
uuo.mutation.SetUpdatedAt(t)
|
|
return uuo
|
|
}
|
|
|
|
// SetEmail sets the "email" field.
|
|
func (uuo *UserUpdateOne) SetEmail(s string) *UserUpdateOne {
|
|
uuo.mutation.SetEmail(s)
|
|
return uuo
|
|
}
|
|
|
|
// SetNillableEmail sets the "email" field if the given value is not nil.
|
|
func (uuo *UserUpdateOne) SetNillableEmail(s *string) *UserUpdateOne {
|
|
if s != nil {
|
|
uuo.SetEmail(*s)
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// SetIss sets the "iss" field.
|
|
func (uuo *UserUpdateOne) SetIss(s string) *UserUpdateOne {
|
|
uuo.mutation.SetIss(s)
|
|
return uuo
|
|
}
|
|
|
|
// SetNillableIss sets the "iss" field if the given value is not nil.
|
|
func (uuo *UserUpdateOne) SetNillableIss(s *string) *UserUpdateOne {
|
|
if s != nil {
|
|
uuo.SetIss(*s)
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// SetSub sets the "sub" field.
|
|
func (uuo *UserUpdateOne) SetSub(s string) *UserUpdateOne {
|
|
uuo.mutation.SetSub(s)
|
|
return uuo
|
|
}
|
|
|
|
// SetNillableSub sets the "sub" field if the given value is not nil.
|
|
func (uuo *UserUpdateOne) SetNillableSub(s *string) *UserUpdateOne {
|
|
if s != nil {
|
|
uuo.SetSub(*s)
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (uuo *UserUpdateOne) SetName(s string) *UserUpdateOne {
|
|
uuo.mutation.SetName(s)
|
|
return uuo
|
|
}
|
|
|
|
// SetNillableName sets the "name" field if the given value is not nil.
|
|
func (uuo *UserUpdateOne) SetNillableName(s *string) *UserUpdateOne {
|
|
if s != nil {
|
|
uuo.SetName(*s)
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// AddOrganizationIDs adds the "organizations" edge to the Organization entity by IDs.
|
|
func (uuo *UserUpdateOne) AddOrganizationIDs(ids ...uuid.UUID) *UserUpdateOne {
|
|
uuo.mutation.AddOrganizationIDs(ids...)
|
|
return uuo
|
|
}
|
|
|
|
// AddOrganizations adds the "organizations" edges to the Organization entity.
|
|
func (uuo *UserUpdateOne) AddOrganizations(o ...*Organization) *UserUpdateOne {
|
|
ids := make([]uuid.UUID, len(o))
|
|
for i := range o {
|
|
ids[i] = o[i].ID
|
|
}
|
|
return uuo.AddOrganizationIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the UserMutation object of the builder.
|
|
func (uuo *UserUpdateOne) Mutation() *UserMutation {
|
|
return uuo.mutation
|
|
}
|
|
|
|
// ClearOrganizations clears all "organizations" edges to the Organization entity.
|
|
func (uuo *UserUpdateOne) ClearOrganizations() *UserUpdateOne {
|
|
uuo.mutation.ClearOrganizations()
|
|
return uuo
|
|
}
|
|
|
|
// RemoveOrganizationIDs removes the "organizations" edge to Organization entities by IDs.
|
|
func (uuo *UserUpdateOne) RemoveOrganizationIDs(ids ...uuid.UUID) *UserUpdateOne {
|
|
uuo.mutation.RemoveOrganizationIDs(ids...)
|
|
return uuo
|
|
}
|
|
|
|
// RemoveOrganizations removes "organizations" edges to Organization entities.
|
|
func (uuo *UserUpdateOne) RemoveOrganizations(o ...*Organization) *UserUpdateOne {
|
|
ids := make([]uuid.UUID, len(o))
|
|
for i := range o {
|
|
ids[i] = o[i].ID
|
|
}
|
|
return uuo.RemoveOrganizationIDs(ids...)
|
|
}
|
|
|
|
// Where appends a list predicates to the UserUpdate builder.
|
|
func (uuo *UserUpdateOne) Where(ps ...predicate.User) *UserUpdateOne {
|
|
uuo.mutation.Where(ps...)
|
|
return uuo
|
|
}
|
|
|
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
|
// The default is selecting all fields defined in the entity schema.
|
|
func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne {
|
|
uuo.fields = append([]string{field}, fields...)
|
|
return uuo
|
|
}
|
|
|
|
// Save executes the query and returns the updated User entity.
|
|
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
|
uuo.defaults()
|
|
return withHooks(ctx, uuo.sqlSave, uuo.mutation, uuo.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (uuo *UserUpdateOne) SaveX(ctx context.Context) *User {
|
|
node, err := uuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (uuo *UserUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := uuo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (uuo *UserUpdateOne) ExecX(ctx context.Context) {
|
|
if err := uuo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (uuo *UserUpdateOne) defaults() {
|
|
if _, ok := uuo.mutation.UpdatedAt(); !ok {
|
|
v := user.UpdateDefaultUpdatedAt()
|
|
uuo.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (uuo *UserUpdateOne) check() error {
|
|
if v, ok := uuo.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)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
|
|
if err := uuo.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID))
|
|
id, ok := uuo.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "User.id" for update`)}
|
|
}
|
|
_spec.Node.ID.Value = id
|
|
if fields := uuo.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
|
|
for _, f := range fields {
|
|
if !user.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != user.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
|
}
|
|
}
|
|
}
|
|
if ps := uuo.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := uuo.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
|
|
}
|
|
if value, ok := uuo.mutation.Email(); ok {
|
|
_spec.SetField(user.FieldEmail, field.TypeString, value)
|
|
}
|
|
if value, ok := uuo.mutation.Iss(); ok {
|
|
_spec.SetField(user.FieldIss, field.TypeString, value)
|
|
}
|
|
if value, ok := uuo.mutation.Sub(); ok {
|
|
_spec.SetField(user.FieldSub, field.TypeString, value)
|
|
}
|
|
if value, ok := uuo.mutation.Name(); ok {
|
|
_spec.SetField(user.FieldName, field.TypeString, value)
|
|
}
|
|
if uuo.mutation.OrganizationsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: user.OrganizationsTable,
|
|
Columns: user.OrganizationsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uuo.mutation.RemovedOrganizationsIDs(); len(nodes) > 0 && !uuo.mutation.OrganizationsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: user.OrganizationsTable,
|
|
Columns: user.OrganizationsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uuo.mutation.OrganizationsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: user.OrganizationsTable,
|
|
Columns: user.OrganizationsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
|
|
},
|
|
}
|
|
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
|
|
if err = sqlgraph.UpdateNode(ctx, uuo.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{user.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
uuo.mutation.done = true
|
|
return _node, nil
|
|
}
|