pcmt/ent/hibp_create.go
surtur 6b45213649
All checks were successful
continuous-integration/drone/push Build is passing
go: add user onboarding, HIBP search functionality
* add user onboarding workflow
* fix user editing (no edits of passwords of regular users after
  onboarding)
* refresh HIBP breach cache in DB on app start-up
* display HIBP breach details
* fix request scheduling to prevent panics (this still needs some love..)
* fix middleware auth
* add TODOs
* update head.tmpl
* reword some error messages
2023-08-24 18:43:24 +02:00

530 lines
15 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/hibp"
"git.dotya.ml/mirre-mt/pcmt/ent/trackedbreaches"
"github.com/google/uuid"
)
// HIBPCreate is the builder for creating a HIBP entity.
type HIBPCreate struct {
config
mutation *HIBPMutation
hooks []Hook
}
// SetName sets the "name" field.
func (hc *HIBPCreate) SetName(s string) *HIBPCreate {
hc.mutation.SetName(s)
return hc
}
// SetTitle sets the "title" field.
func (hc *HIBPCreate) SetTitle(s string) *HIBPCreate {
hc.mutation.SetTitle(s)
return hc
}
// SetDomain sets the "domain" field.
func (hc *HIBPCreate) SetDomain(s string) *HIBPCreate {
hc.mutation.SetDomain(s)
return hc
}
// SetBreachDate sets the "breach_date" field.
func (hc *HIBPCreate) SetBreachDate(s string) *HIBPCreate {
hc.mutation.SetBreachDate(s)
return hc
}
// SetAddedDate sets the "added_date" field.
func (hc *HIBPCreate) SetAddedDate(t time.Time) *HIBPCreate {
hc.mutation.SetAddedDate(t)
return hc
}
// SetModifiedDate sets the "modified_date" field.
func (hc *HIBPCreate) SetModifiedDate(t time.Time) *HIBPCreate {
hc.mutation.SetModifiedDate(t)
return hc
}
// SetPwnCount sets the "pwn_count" field.
func (hc *HIBPCreate) SetPwnCount(i int) *HIBPCreate {
hc.mutation.SetPwnCount(i)
return hc
}
// SetDescription sets the "description" field.
func (hc *HIBPCreate) SetDescription(s string) *HIBPCreate {
hc.mutation.SetDescription(s)
return hc
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableDescription(s *string) *HIBPCreate {
if s != nil {
hc.SetDescription(*s)
}
return hc
}
// SetDataclasses sets the "dataclasses" field.
func (hc *HIBPCreate) SetDataclasses(s []string) *HIBPCreate {
hc.mutation.SetDataclasses(s)
return hc
}
// SetIsVerified sets the "is_verified" field.
func (hc *HIBPCreate) SetIsVerified(b bool) *HIBPCreate {
hc.mutation.SetIsVerified(b)
return hc
}
// SetNillableIsVerified sets the "is_verified" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsVerified(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsVerified(*b)
}
return hc
}
// SetIsFabricated sets the "is_fabricated" field.
func (hc *HIBPCreate) SetIsFabricated(b bool) *HIBPCreate {
hc.mutation.SetIsFabricated(b)
return hc
}
// SetNillableIsFabricated sets the "is_fabricated" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsFabricated(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsFabricated(*b)
}
return hc
}
// SetIsSensitive sets the "is_sensitive" field.
func (hc *HIBPCreate) SetIsSensitive(b bool) *HIBPCreate {
hc.mutation.SetIsSensitive(b)
return hc
}
// SetNillableIsSensitive sets the "is_sensitive" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsSensitive(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsSensitive(*b)
}
return hc
}
// SetIsRetired sets the "is_retired" field.
func (hc *HIBPCreate) SetIsRetired(b bool) *HIBPCreate {
hc.mutation.SetIsRetired(b)
return hc
}
// SetNillableIsRetired sets the "is_retired" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsRetired(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsRetired(*b)
}
return hc
}
// SetIsSpamList sets the "is_spamList" field.
func (hc *HIBPCreate) SetIsSpamList(b bool) *HIBPCreate {
hc.mutation.SetIsSpamList(b)
return hc
}
// SetNillableIsSpamList sets the "is_spamList" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsSpamList(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsSpamList(*b)
}
return hc
}
// SetIsMalware sets the "is_malware" field.
func (hc *HIBPCreate) SetIsMalware(b bool) *HIBPCreate {
hc.mutation.SetIsMalware(b)
return hc
}
// SetNillableIsMalware sets the "is_malware" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsMalware(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsMalware(*b)
}
return hc
}
// SetLogoPath sets the "logo_path" field.
func (hc *HIBPCreate) SetLogoPath(s string) *HIBPCreate {
hc.mutation.SetLogoPath(s)
return hc
}
// SetNillableLogoPath sets the "logo_path" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableLogoPath(s *string) *HIBPCreate {
if s != nil {
hc.SetLogoPath(*s)
}
return hc
}
// SetID sets the "id" field.
func (hc *HIBPCreate) SetID(u uuid.UUID) *HIBPCreate {
hc.mutation.SetID(u)
return hc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableID(u *uuid.UUID) *HIBPCreate {
if u != nil {
hc.SetID(*u)
}
return hc
}
// SetTrackedBreachesID sets the "tracked_breaches" edge to the TrackedBreaches entity by ID.
func (hc *HIBPCreate) SetTrackedBreachesID(id uuid.UUID) *HIBPCreate {
hc.mutation.SetTrackedBreachesID(id)
return hc
}
// SetNillableTrackedBreachesID sets the "tracked_breaches" edge to the TrackedBreaches entity by ID if the given value is not nil.
func (hc *HIBPCreate) SetNillableTrackedBreachesID(id *uuid.UUID) *HIBPCreate {
if id != nil {
hc = hc.SetTrackedBreachesID(*id)
}
return hc
}
// SetTrackedBreaches sets the "tracked_breaches" edge to the TrackedBreaches entity.
func (hc *HIBPCreate) SetTrackedBreaches(t *TrackedBreaches) *HIBPCreate {
return hc.SetTrackedBreachesID(t.ID)
}
// Mutation returns the HIBPMutation object of the builder.
func (hc *HIBPCreate) Mutation() *HIBPMutation {
return hc.mutation
}
// Save creates the HIBP in the database.
func (hc *HIBPCreate) Save(ctx context.Context) (*HIBP, error) {
hc.defaults()
return withHooks(ctx, hc.sqlSave, hc.mutation, hc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (hc *HIBPCreate) SaveX(ctx context.Context) *HIBP {
v, err := hc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (hc *HIBPCreate) Exec(ctx context.Context) error {
_, err := hc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (hc *HIBPCreate) ExecX(ctx context.Context) {
if err := hc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (hc *HIBPCreate) defaults() {
if _, ok := hc.mutation.IsVerified(); !ok {
v := hibp.DefaultIsVerified
hc.mutation.SetIsVerified(v)
}
if _, ok := hc.mutation.IsFabricated(); !ok {
v := hibp.DefaultIsFabricated
hc.mutation.SetIsFabricated(v)
}
if _, ok := hc.mutation.IsSensitive(); !ok {
v := hibp.DefaultIsSensitive
hc.mutation.SetIsSensitive(v)
}
if _, ok := hc.mutation.IsRetired(); !ok {
v := hibp.DefaultIsRetired
hc.mutation.SetIsRetired(v)
}
if _, ok := hc.mutation.IsSpamList(); !ok {
v := hibp.DefaultIsSpamList
hc.mutation.SetIsSpamList(v)
}
if _, ok := hc.mutation.IsMalware(); !ok {
v := hibp.DefaultIsMalware
hc.mutation.SetIsMalware(v)
}
if _, ok := hc.mutation.ID(); !ok {
v := hibp.DefaultID()
hc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (hc *HIBPCreate) check() error {
if _, ok := hc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "HIBP.name"`)}
}
if v, ok := hc.mutation.Name(); ok {
if err := hibp.NameValidator(v); err != nil {
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "HIBP.name": %w`, err)}
}
}
if _, ok := hc.mutation.Title(); !ok {
return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "HIBP.title"`)}
}
if _, ok := hc.mutation.Domain(); !ok {
return &ValidationError{Name: "domain", err: errors.New(`ent: missing required field "HIBP.domain"`)}
}
if _, ok := hc.mutation.BreachDate(); !ok {
return &ValidationError{Name: "breach_date", err: errors.New(`ent: missing required field "HIBP.breach_date"`)}
}
if _, ok := hc.mutation.AddedDate(); !ok {
return &ValidationError{Name: "added_date", err: errors.New(`ent: missing required field "HIBP.added_date"`)}
}
if _, ok := hc.mutation.ModifiedDate(); !ok {
return &ValidationError{Name: "modified_date", err: errors.New(`ent: missing required field "HIBP.modified_date"`)}
}
if _, ok := hc.mutation.PwnCount(); !ok {
return &ValidationError{Name: "pwn_count", err: errors.New(`ent: missing required field "HIBP.pwn_count"`)}
}
if _, ok := hc.mutation.Dataclasses(); !ok {
return &ValidationError{Name: "dataclasses", err: errors.New(`ent: missing required field "HIBP.dataclasses"`)}
}
if _, ok := hc.mutation.IsVerified(); !ok {
return &ValidationError{Name: "is_verified", err: errors.New(`ent: missing required field "HIBP.is_verified"`)}
}
if _, ok := hc.mutation.IsFabricated(); !ok {
return &ValidationError{Name: "is_fabricated", err: errors.New(`ent: missing required field "HIBP.is_fabricated"`)}
}
if _, ok := hc.mutation.IsSensitive(); !ok {
return &ValidationError{Name: "is_sensitive", err: errors.New(`ent: missing required field "HIBP.is_sensitive"`)}
}
if _, ok := hc.mutation.IsRetired(); !ok {
return &ValidationError{Name: "is_retired", err: errors.New(`ent: missing required field "HIBP.is_retired"`)}
}
if _, ok := hc.mutation.IsSpamList(); !ok {
return &ValidationError{Name: "is_spamList", err: errors.New(`ent: missing required field "HIBP.is_spamList"`)}
}
if _, ok := hc.mutation.IsMalware(); !ok {
return &ValidationError{Name: "is_malware", err: errors.New(`ent: missing required field "HIBP.is_malware"`)}
}
return nil
}
func (hc *HIBPCreate) sqlSave(ctx context.Context) (*HIBP, error) {
if err := hc.check(); err != nil {
return nil, err
}
_node, _spec := hc.createSpec()
if err := sqlgraph.CreateNode(ctx, hc.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
}
}
hc.mutation.id = &_node.ID
hc.mutation.done = true
return _node, nil
}
func (hc *HIBPCreate) createSpec() (*HIBP, *sqlgraph.CreateSpec) {
var (
_node = &HIBP{config: hc.config}
_spec = sqlgraph.NewCreateSpec(hibp.Table, sqlgraph.NewFieldSpec(hibp.FieldID, field.TypeUUID))
)
if id, ok := hc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := hc.mutation.Name(); ok {
_spec.SetField(hibp.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := hc.mutation.Title(); ok {
_spec.SetField(hibp.FieldTitle, field.TypeString, value)
_node.Title = value
}
if value, ok := hc.mutation.Domain(); ok {
_spec.SetField(hibp.FieldDomain, field.TypeString, value)
_node.Domain = value
}
if value, ok := hc.mutation.BreachDate(); ok {
_spec.SetField(hibp.FieldBreachDate, field.TypeString, value)
_node.BreachDate = value
}
if value, ok := hc.mutation.AddedDate(); ok {
_spec.SetField(hibp.FieldAddedDate, field.TypeTime, value)
_node.AddedDate = value
}
if value, ok := hc.mutation.ModifiedDate(); ok {
_spec.SetField(hibp.FieldModifiedDate, field.TypeTime, value)
_node.ModifiedDate = value
}
if value, ok := hc.mutation.PwnCount(); ok {
_spec.SetField(hibp.FieldPwnCount, field.TypeInt, value)
_node.PwnCount = value
}
if value, ok := hc.mutation.Description(); ok {
_spec.SetField(hibp.FieldDescription, field.TypeString, value)
_node.Description = &value
}
if value, ok := hc.mutation.Dataclasses(); ok {
_spec.SetField(hibp.FieldDataclasses, field.TypeJSON, value)
_node.Dataclasses = value
}
if value, ok := hc.mutation.IsVerified(); ok {
_spec.SetField(hibp.FieldIsVerified, field.TypeBool, value)
_node.IsVerified = value
}
if value, ok := hc.mutation.IsFabricated(); ok {
_spec.SetField(hibp.FieldIsFabricated, field.TypeBool, value)
_node.IsFabricated = value
}
if value, ok := hc.mutation.IsSensitive(); ok {
_spec.SetField(hibp.FieldIsSensitive, field.TypeBool, value)
_node.IsSensitive = value
}
if value, ok := hc.mutation.IsRetired(); ok {
_spec.SetField(hibp.FieldIsRetired, field.TypeBool, value)
_node.IsRetired = value
}
if value, ok := hc.mutation.IsSpamList(); ok {
_spec.SetField(hibp.FieldIsSpamList, field.TypeBool, value)
_node.IsSpamList = value
}
if value, ok := hc.mutation.IsMalware(); ok {
_spec.SetField(hibp.FieldIsMalware, field.TypeBool, value)
_node.IsMalware = value
}
if value, ok := hc.mutation.LogoPath(); ok {
_spec.SetField(hibp.FieldLogoPath, field.TypeString, value)
_node.LogoPath = &value
}
if nodes := hc.mutation.TrackedBreachesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: hibp.TrackedBreachesTable,
Columns: []string{hibp.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)
}
_node.tracked_breaches_hibp = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// HIBPCreateBulk is the builder for creating many HIBP entities in bulk.
type HIBPCreateBulk struct {
config
builders []*HIBPCreate
}
// Save creates the HIBP entities in the database.
func (hcb *HIBPCreateBulk) Save(ctx context.Context) ([]*HIBP, error) {
specs := make([]*sqlgraph.CreateSpec, len(hcb.builders))
nodes := make([]*HIBP, len(hcb.builders))
mutators := make([]Mutator, len(hcb.builders))
for i := range hcb.builders {
func(i int, root context.Context) {
builder := hcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*HIBPMutation)
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, hcb.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, hcb.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, hcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (hcb *HIBPCreateBulk) SaveX(ctx context.Context) []*HIBP {
v, err := hcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (hcb *HIBPCreateBulk) Exec(ctx context.Context) error {
_, err := hcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (hcb *HIBPCreateBulk) ExecX(ctx context.Context) {
if err := hcb.Exec(ctx); err != nil {
panic(err)
}
}