Implemented Epic 1 core kernel and infrastructure stories: Story 1.1: Enhanced DI Container - Added providers for database, health, metrics, and error bus - Extended CoreModule to include all core services Story 1.2: Database Layer with Ent ORM - Created Ent schema for User, Role, Permission, AuditLog entities - Implemented many-to-many relationships (User-Role, Role-Permission) - Created database client wrapper with connection pooling - Added database provider to DI container with migration support Story 1.3: Health Monitoring and Metrics System - Implemented health check registry and interface - Added database health checker - Created Prometheus metrics system with HTTP instrumentation - Added health and metrics providers to DI container Story 1.4: Error Handling and Error Bus - Implemented channel-based error bus - Created ErrorPublisher interface - Added error bus provider with lifecycle management Story 1.5: HTTP Server Foundation - Created HTTP server with Gin framework - Implemented comprehensive middleware stack: - Request ID generation - Structured logging - Panic recovery with error bus integration - Prometheus metrics collection - CORS support - Registered core routes: /healthz, /ready, /metrics - Integrated with FX lifecycle for graceful shutdown All components are integrated via DI container and ready for use.
278 lines
7.6 KiB
Go
278 lines
7.6 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.dcentral.systems/toolz/goplt/internal/ent/auditlog"
|
|
)
|
|
|
|
// AuditLogCreate is the builder for creating a AuditLog entity.
|
|
type AuditLogCreate struct {
|
|
config
|
|
mutation *AuditLogMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetActorID sets the "actor_id" field.
|
|
func (_c *AuditLogCreate) SetActorID(v string) *AuditLogCreate {
|
|
_c.mutation.SetActorID(v)
|
|
return _c
|
|
}
|
|
|
|
// SetAction sets the "action" field.
|
|
func (_c *AuditLogCreate) SetAction(v string) *AuditLogCreate {
|
|
_c.mutation.SetAction(v)
|
|
return _c
|
|
}
|
|
|
|
// SetTargetID sets the "target_id" field.
|
|
func (_c *AuditLogCreate) SetTargetID(v string) *AuditLogCreate {
|
|
_c.mutation.SetTargetID(v)
|
|
return _c
|
|
}
|
|
|
|
// SetNillableTargetID sets the "target_id" field if the given value is not nil.
|
|
func (_c *AuditLogCreate) SetNillableTargetID(v *string) *AuditLogCreate {
|
|
if v != nil {
|
|
_c.SetTargetID(*v)
|
|
}
|
|
return _c
|
|
}
|
|
|
|
// SetMetadata sets the "metadata" field.
|
|
func (_c *AuditLogCreate) SetMetadata(v map[string]interface{}) *AuditLogCreate {
|
|
_c.mutation.SetMetadata(v)
|
|
return _c
|
|
}
|
|
|
|
// SetTimestamp sets the "timestamp" field.
|
|
func (_c *AuditLogCreate) SetTimestamp(v time.Time) *AuditLogCreate {
|
|
_c.mutation.SetTimestamp(v)
|
|
return _c
|
|
}
|
|
|
|
// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
|
|
func (_c *AuditLogCreate) SetNillableTimestamp(v *time.Time) *AuditLogCreate {
|
|
if v != nil {
|
|
_c.SetTimestamp(*v)
|
|
}
|
|
return _c
|
|
}
|
|
|
|
// SetID sets the "id" field.
|
|
func (_c *AuditLogCreate) SetID(v string) *AuditLogCreate {
|
|
_c.mutation.SetID(v)
|
|
return _c
|
|
}
|
|
|
|
// Mutation returns the AuditLogMutation object of the builder.
|
|
func (_c *AuditLogCreate) Mutation() *AuditLogMutation {
|
|
return _c.mutation
|
|
}
|
|
|
|
// Save creates the AuditLog in the database.
|
|
func (_c *AuditLogCreate) Save(ctx context.Context) (*AuditLog, error) {
|
|
_c.defaults()
|
|
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (_c *AuditLogCreate) SaveX(ctx context.Context) *AuditLog {
|
|
v, err := _c.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_c *AuditLogCreate) Exec(ctx context.Context) error {
|
|
_, err := _c.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_c *AuditLogCreate) ExecX(ctx context.Context) {
|
|
if err := _c.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (_c *AuditLogCreate) defaults() {
|
|
if _, ok := _c.mutation.Timestamp(); !ok {
|
|
v := auditlog.DefaultTimestamp()
|
|
_c.mutation.SetTimestamp(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (_c *AuditLogCreate) check() error {
|
|
if _, ok := _c.mutation.ActorID(); !ok {
|
|
return &ValidationError{Name: "actor_id", err: errors.New(`ent: missing required field "AuditLog.actor_id"`)}
|
|
}
|
|
if v, ok := _c.mutation.ActorID(); ok {
|
|
if err := auditlog.ActorIDValidator(v); err != nil {
|
|
return &ValidationError{Name: "actor_id", err: fmt.Errorf(`ent: validator failed for field "AuditLog.actor_id": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := _c.mutation.Action(); !ok {
|
|
return &ValidationError{Name: "action", err: errors.New(`ent: missing required field "AuditLog.action"`)}
|
|
}
|
|
if v, ok := _c.mutation.Action(); ok {
|
|
if err := auditlog.ActionValidator(v); err != nil {
|
|
return &ValidationError{Name: "action", err: fmt.Errorf(`ent: validator failed for field "AuditLog.action": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := _c.mutation.Timestamp(); !ok {
|
|
return &ValidationError{Name: "timestamp", err: errors.New(`ent: missing required field "AuditLog.timestamp"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_c *AuditLogCreate) sqlSave(ctx context.Context) (*AuditLog, error) {
|
|
if err := _c.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := _c.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, _c.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.(string); ok {
|
|
_node.ID = id
|
|
} else {
|
|
return nil, fmt.Errorf("unexpected AuditLog.ID type: %T", _spec.ID.Value)
|
|
}
|
|
}
|
|
_c.mutation.id = &_node.ID
|
|
_c.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (_c *AuditLogCreate) createSpec() (*AuditLog, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &AuditLog{config: _c.config}
|
|
_spec = sqlgraph.NewCreateSpec(auditlog.Table, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString))
|
|
)
|
|
if id, ok := _c.mutation.ID(); ok {
|
|
_node.ID = id
|
|
_spec.ID.Value = id
|
|
}
|
|
if value, ok := _c.mutation.ActorID(); ok {
|
|
_spec.SetField(auditlog.FieldActorID, field.TypeString, value)
|
|
_node.ActorID = value
|
|
}
|
|
if value, ok := _c.mutation.Action(); ok {
|
|
_spec.SetField(auditlog.FieldAction, field.TypeString, value)
|
|
_node.Action = value
|
|
}
|
|
if value, ok := _c.mutation.TargetID(); ok {
|
|
_spec.SetField(auditlog.FieldTargetID, field.TypeString, value)
|
|
_node.TargetID = value
|
|
}
|
|
if value, ok := _c.mutation.Metadata(); ok {
|
|
_spec.SetField(auditlog.FieldMetadata, field.TypeJSON, value)
|
|
_node.Metadata = value
|
|
}
|
|
if value, ok := _c.mutation.Timestamp(); ok {
|
|
_spec.SetField(auditlog.FieldTimestamp, field.TypeTime, value)
|
|
_node.Timestamp = value
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// AuditLogCreateBulk is the builder for creating many AuditLog entities in bulk.
|
|
type AuditLogCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*AuditLogCreate
|
|
}
|
|
|
|
// Save creates the AuditLog entities in the database.
|
|
func (_c *AuditLogCreateBulk) Save(ctx context.Context) ([]*AuditLog, error) {
|
|
if _c.err != nil {
|
|
return nil, _c.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
|
|
nodes := make([]*AuditLog, len(_c.builders))
|
|
mutators := make([]Mutator, len(_c.builders))
|
|
for i := range _c.builders {
|
|
func(i int, root context.Context) {
|
|
builder := _c.builders[i]
|
|
builder.defaults()
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*AuditLogMutation)
|
|
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, _c.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, _c.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, _c.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_c *AuditLogCreateBulk) SaveX(ctx context.Context) []*AuditLog {
|
|
v, err := _c.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_c *AuditLogCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := _c.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_c *AuditLogCreateBulk) ExecX(ctx context.Context) {
|
|
if err := _c.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|