From 30320304f6a42f1872fc3cdc9dba2bd9ffe05211 Mon Sep 17 00:00:00 2001 From: 0x1d Date: Wed, 5 Nov 2025 18:11:11 +0100 Subject: [PATCH] feat(epic1): implement core infrastructure (stories 1.1-1.5) 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. --- config/default.yaml | 2 + ent/generate.go | 3 + ent/schema/auditlog.go | 18 + ent/schema/permission.go | 18 + ent/schema/role.go | 18 + ent/schema/user.go | 18 + go.mod | 45 +- go.sum | 102 +- internal/di/providers.go | 145 +- internal/ent/auditlog.go | 153 + internal/ent/auditlog/auditlog.go | 85 + internal/ent/auditlog/where.go | 355 ++ internal/ent/auditlog_create.go | 277 ++ internal/ent/auditlog_delete.go | 88 + internal/ent/auditlog_query.go | 527 +++ internal/ent/auditlog_update.go | 367 ++ internal/ent/client.go | 1182 ++++++ internal/ent/ent.go | 618 ++++ internal/ent/enttest/enttest.go | 84 + internal/ent/hook/hook.go | 259 ++ internal/ent/migrate/migrate.go | 64 + internal/ent/migrate/schema.go | 187 + internal/ent/mutation.go | 3291 +++++++++++++++++ internal/ent/permission.go | 127 + internal/ent/permission/permission.go | 83 + internal/ent/permission/where.go | 172 + internal/ent/permission_create.go | 231 ++ internal/ent/permission_delete.go | 88 + internal/ent/permission_query.go | 607 +++ internal/ent/permission_update.go | 398 ++ internal/ent/predicate/predicate.go | 25 + internal/ent/role.go | 168 + internal/ent/role/role.go | 133 + internal/ent/role/where.go | 322 ++ internal/ent/role_create.go | 313 ++ internal/ent/role_delete.go | 88 + internal/ent/role_query.go | 682 ++++ internal/ent/role_update.go | 613 +++ internal/ent/rolepermission.go | 182 + internal/ent/rolepermission/rolepermission.go | 114 + internal/ent/rolepermission/where.go | 255 ++ internal/ent/rolepermission_create.go | 240 ++ internal/ent/rolepermission_delete.go | 88 + internal/ent/rolepermission_query.go | 686 ++++ internal/ent/rolepermission_update.go | 421 +++ internal/ent/runtime.go | 73 + internal/ent/runtime/runtime.go | 10 + internal/ent/schema/audit_log.go | 49 + internal/ent/schema/permission.go | 33 + internal/ent/schema/role.go | 40 + internal/ent/schema/role_permission.go | 35 + internal/ent/schema/user.go | 44 + internal/ent/schema/user_role.go | 35 + internal/ent/tx.go | 225 ++ internal/ent/user.go | 176 + internal/ent/user/user.go | 127 + internal/ent/user/where.go | 349 ++ internal/ent/user_create.go | 331 ++ internal/ent/user_delete.go | 88 + internal/ent/user_query.go | 607 +++ internal/ent/user_update.go | 513 +++ internal/ent/userrole.go | 182 + internal/ent/userrole/userrole.go | 114 + internal/ent/userrole/where.go | 255 ++ internal/ent/userrole_create.go | 240 ++ internal/ent/userrole_delete.go | 88 + internal/ent/userrole_query.go | 686 ++++ internal/ent/userrole_update.go | 421 +++ internal/errorbus/channel_bus.go | 165 + internal/health/database.go | 26 + internal/health/registry.go | 74 + internal/infra/database/client.go | 87 + internal/metrics/metrics.go | 97 + internal/server/middleware.go | 141 + internal/server/server.go | 131 + pkg/errorbus/errorbus.go | 21 + pkg/health/health.go | 34 + 77 files changed, 19409 insertions(+), 30 deletions(-) create mode 100644 ent/generate.go create mode 100644 ent/schema/auditlog.go create mode 100644 ent/schema/permission.go create mode 100644 ent/schema/role.go create mode 100644 ent/schema/user.go create mode 100644 internal/ent/auditlog.go create mode 100644 internal/ent/auditlog/auditlog.go create mode 100644 internal/ent/auditlog/where.go create mode 100644 internal/ent/auditlog_create.go create mode 100644 internal/ent/auditlog_delete.go create mode 100644 internal/ent/auditlog_query.go create mode 100644 internal/ent/auditlog_update.go create mode 100644 internal/ent/client.go create mode 100644 internal/ent/ent.go create mode 100644 internal/ent/enttest/enttest.go create mode 100644 internal/ent/hook/hook.go create mode 100644 internal/ent/migrate/migrate.go create mode 100644 internal/ent/migrate/schema.go create mode 100644 internal/ent/mutation.go create mode 100644 internal/ent/permission.go create mode 100644 internal/ent/permission/permission.go create mode 100644 internal/ent/permission/where.go create mode 100644 internal/ent/permission_create.go create mode 100644 internal/ent/permission_delete.go create mode 100644 internal/ent/permission_query.go create mode 100644 internal/ent/permission_update.go create mode 100644 internal/ent/predicate/predicate.go create mode 100644 internal/ent/role.go create mode 100644 internal/ent/role/role.go create mode 100644 internal/ent/role/where.go create mode 100644 internal/ent/role_create.go create mode 100644 internal/ent/role_delete.go create mode 100644 internal/ent/role_query.go create mode 100644 internal/ent/role_update.go create mode 100644 internal/ent/rolepermission.go create mode 100644 internal/ent/rolepermission/rolepermission.go create mode 100644 internal/ent/rolepermission/where.go create mode 100644 internal/ent/rolepermission_create.go create mode 100644 internal/ent/rolepermission_delete.go create mode 100644 internal/ent/rolepermission_query.go create mode 100644 internal/ent/rolepermission_update.go create mode 100644 internal/ent/runtime.go create mode 100644 internal/ent/runtime/runtime.go create mode 100644 internal/ent/schema/audit_log.go create mode 100644 internal/ent/schema/permission.go create mode 100644 internal/ent/schema/role.go create mode 100644 internal/ent/schema/role_permission.go create mode 100644 internal/ent/schema/user.go create mode 100644 internal/ent/schema/user_role.go create mode 100644 internal/ent/tx.go create mode 100644 internal/ent/user.go create mode 100644 internal/ent/user/user.go create mode 100644 internal/ent/user/where.go create mode 100644 internal/ent/user_create.go create mode 100644 internal/ent/user_delete.go create mode 100644 internal/ent/user_query.go create mode 100644 internal/ent/user_update.go create mode 100644 internal/ent/userrole.go create mode 100644 internal/ent/userrole/userrole.go create mode 100644 internal/ent/userrole/where.go create mode 100644 internal/ent/userrole_create.go create mode 100644 internal/ent/userrole_delete.go create mode 100644 internal/ent/userrole_query.go create mode 100644 internal/ent/userrole_update.go create mode 100644 internal/errorbus/channel_bus.go create mode 100644 internal/health/database.go create mode 100644 internal/health/registry.go create mode 100644 internal/infra/database/client.go create mode 100644 internal/metrics/metrics.go create mode 100644 internal/server/middleware.go create mode 100644 internal/server/server.go create mode 100644 pkg/errorbus/errorbus.go create mode 100644 pkg/health/health.go diff --git a/config/default.yaml b/config/default.yaml index bc263ab..f208951 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -11,6 +11,8 @@ database: dsn: "" max_connections: 25 max_idle_connections: 5 + conn_max_lifetime: 5m + conn_max_idle_time: 10m logging: level: "info" diff --git a/ent/generate.go b/ent/generate.go new file mode 100644 index 0000000..8d3fdfd --- /dev/null +++ b/ent/generate.go @@ -0,0 +1,3 @@ +package ent + +//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate ./schema diff --git a/ent/schema/auditlog.go b/ent/schema/auditlog.go new file mode 100644 index 0000000..f6e37bc --- /dev/null +++ b/ent/schema/auditlog.go @@ -0,0 +1,18 @@ +package schema + +import "entgo.io/ent" + +// AuditLog holds the schema definition for the AuditLog entity. +type AuditLog struct { + ent.Schema +} + +// Fields of the AuditLog. +func (AuditLog) Fields() []ent.Field { + return nil +} + +// Edges of the AuditLog. +func (AuditLog) Edges() []ent.Edge { + return nil +} diff --git a/ent/schema/permission.go b/ent/schema/permission.go new file mode 100644 index 0000000..cd589ec --- /dev/null +++ b/ent/schema/permission.go @@ -0,0 +1,18 @@ +package schema + +import "entgo.io/ent" + +// Permission holds the schema definition for the Permission entity. +type Permission struct { + ent.Schema +} + +// Fields of the Permission. +func (Permission) Fields() []ent.Field { + return nil +} + +// Edges of the Permission. +func (Permission) Edges() []ent.Edge { + return nil +} diff --git a/ent/schema/role.go b/ent/schema/role.go new file mode 100644 index 0000000..b80c4da --- /dev/null +++ b/ent/schema/role.go @@ -0,0 +1,18 @@ +package schema + +import "entgo.io/ent" + +// Role holds the schema definition for the Role entity. +type Role struct { + ent.Schema +} + +// Fields of the Role. +func (Role) Fields() []ent.Field { + return nil +} + +// Edges of the Role. +func (Role) Edges() []ent.Edge { + return nil +} diff --git a/ent/schema/user.go b/ent/schema/user.go new file mode 100644 index 0000000..7c14fb8 --- /dev/null +++ b/ent/schema/user.go @@ -0,0 +1,18 @@ +package schema + +import "entgo.io/ent" + +// User holds the schema definition for the User entity. +type User struct { + ent.Schema +} + +// Fields of the User. +func (User) Fields() []ent.Field { + return nil +} + +// Edges of the User. +func (User) Edges() []ent.Edge { + return nil +} diff --git a/go.mod b/go.mod index cb4a029..f07031b 100644 --- a/go.mod +++ b/go.mod @@ -3,49 +3,72 @@ module git.dcentral.systems/toolz/goplt go 1.24 require ( + entgo.io/ent v0.14.5 + github.com/gin-gonic/gin v1.9.1 + github.com/google/uuid v1.6.0 + github.com/lib/pq v1.10.9 + github.com/prometheus/client_golang v1.23.2 + github.com/spf13/viper v1.18.0 + go.uber.org/fx v1.24.0 + go.uber.org/zap v1.26.0 +) + +require ( + ariga.io/atlas v0.32.1-0.20250325101103-175b25e1c1b9 // indirect + github.com/agext/levenshtein v1.2.3 // indirect + github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/bmatcuk/doublestar v1.3.4 // indirect github.com/bytedance/sonic v1.9.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.9.1 // indirect + github.com/go-openapi/inflect v0.19.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.14.0 // indirect github.com/goccy/go-json v0.10.2 // indirect - github.com/google/uuid v1.6.0 // indirect + github.com/google/go-cmp v0.7.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/hashicorp/hcl/v2 v2.18.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.16.1 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/spf13/viper v1.18.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect - go.uber.org/atomic v1.9.0 // indirect + github.com/zclconf/go-cty v1.14.4 // indirect + github.com/zclconf/go-cty-yaml v1.1.0 // indirect go.uber.org/dig v1.19.0 // indirect - go.uber.org/fx v1.24.0 // indirect go.uber.org/multierr v1.10.0 // indirect - go.uber.org/zap v1.26.0 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect golang.org/x/arch v0.3.0 // indirect - golang.org/x/crypto v0.16.0 // indirect + golang.org/x/crypto v0.41.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/net v0.19.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/text v0.14.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + golang.org/x/mod v0.26.0 // indirect + golang.org/x/net v0.43.0 // indirect + golang.org/x/sys v0.35.0 // indirect + golang.org/x/text v0.28.0 // indirect + google.golang.org/protobuf v1.36.8 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1beca3b..e54c719 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,31 @@ +ariga.io/atlas v0.32.1-0.20250325101103-175b25e1c1b9 h1:E0wvcUXTkgyN4wy4LGtNzMNGMytJN8afmIWXJVMi4cc= +ariga.io/atlas v0.32.1-0.20250325101103-175b25e1c1b9/go.mod h1:Oe1xWPuu5q9LzyrWfbZmEZxFYeu4BHTyzfjeW2aZp/w= +entgo.io/ent v0.14.5 h1:Rj2WOYJtCkWyFo6a+5wB3EfBRP0rnx1fMk6gGA0UUe4= +entgo.io/ent v0.14.5/go.mod h1:zTzLmWtPvGpmSwtkaayM2cm5m819NdM7z7tYPq3vN0U= +github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= +github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= +github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= +github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= +github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= +github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0= +github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= @@ -14,32 +34,54 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4= +github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hashicorp/hcl/v2 v2.18.1 h1:6nxnOJFku1EuSawSD81fuviYUV8DxFr3fp2dUi3ZYSo= +github.com/hashicorp/hcl/v2 v2.18.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= +github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -47,13 +89,29 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= +github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= @@ -74,44 +132,52 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8= +github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= +github.com/zclconf/go-cty-yaml v1.1.0 h1:nP+jp0qPHv2IhUVqmQSzjvqAWcObN0KBkUl2rWBdig0= +github.com/zclconf/go-cty-yaml v1.1.0/go.mod h1:9YLUH4g7lOhVWqUbctnVlZ5KLpg7JAprQNgxSZ1Gyxs= go.uber.org/dig v1.19.0 h1:BACLhebsYdpQ7IROQ1AGPjrXcP5dF80U3gKoFzbaq/4= go.uber.org/dig v1.19.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= go.uber.org/fx v1.24.0 h1:wE8mruvpg2kiiL1Vqd0CC+tr0/24XIB10Iwp2lLWzkg= go.uber.org/fx v1.24.0/go.mod h1:AmDeGyS+ZARGKM4tlH4FY2Jr63VjbEDJHtqXTGP5hbo= -go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= -go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4= +golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc= golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg= +golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ= +golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE= +golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= +golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= +golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= +google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= +google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/di/providers.go b/internal/di/providers.go index 59ced30..ff3a301 100644 --- a/internal/di/providers.go +++ b/internal/di/providers.go @@ -3,11 +3,19 @@ package di import ( "context" "fmt" + "net/http" "os" + "time" configimpl "git.dcentral.systems/toolz/goplt/internal/config" + errorbusimpl "git.dcentral.systems/toolz/goplt/internal/errorbus" + "git.dcentral.systems/toolz/goplt/internal/health" + "git.dcentral.systems/toolz/goplt/internal/infra/database" loggerimpl "git.dcentral.systems/toolz/goplt/internal/logger" + "git.dcentral.systems/toolz/goplt/internal/metrics" + "git.dcentral.systems/toolz/goplt/internal/server" "git.dcentral.systems/toolz/goplt/pkg/config" + "git.dcentral.systems/toolz/goplt/pkg/errorbus" "git.dcentral.systems/toolz/goplt/pkg/logger" "go.uber.org/fx" ) @@ -55,12 +63,147 @@ func ProvideLogger() fx.Option { }) } +// ProvideDatabase creates an FX option that provides the database client. +func ProvideDatabase() fx.Option { + return fx.Provide(func(cfg config.ConfigProvider, lc fx.Lifecycle) (*database.Client, error) { + dsn := cfg.GetString("database.dsn") + if dsn == "" { + return nil, fmt.Errorf("database DSN is not configured") + } + + maxConns := cfg.GetInt("database.max_connections") + if maxConns == 0 { + maxConns = 25 + } + + maxIdleConns := cfg.GetInt("database.max_idle_connections") + if maxIdleConns == 0 { + maxIdleConns = 5 + } + + connMaxLifetime := cfg.GetDuration("database.conn_max_lifetime") + if connMaxLifetime == 0 { + connMaxLifetime = 5 * time.Minute + } + + connMaxIdleTime := cfg.GetDuration("database.conn_max_idle_time") + if connMaxIdleTime == 0 { + connMaxIdleTime = 10 * time.Minute + } + + dbClient, err := database.NewClient(database.Config{ + DSN: dsn, + MaxConnections: maxConns, + MaxIdleConns: maxIdleConns, + ConnMaxLifetime: connMaxLifetime, + ConnMaxIdleTime: connMaxIdleTime, + }) + if err != nil { + return nil, fmt.Errorf("failed to create database client: %w", err) + } + + // Register lifecycle hooks + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + // Run migrations on startup + if err := dbClient.Migrate(ctx); err != nil { + return fmt.Errorf("failed to run database migrations: %w", err) + } + return nil + }, + OnStop: func(ctx context.Context) error { + return dbClient.Close() + }, + }) + + return dbClient, nil + }) +} + +// ProvideErrorBus creates an FX option that provides the error bus. +func ProvideErrorBus() fx.Option { + return fx.Provide(func(log logger.Logger, lc fx.Lifecycle) (errorbus.ErrorPublisher, error) { + bufferSize := 100 // Can be made configurable + bus := errorbusimpl.NewChannelBus(log, bufferSize) + + // Register lifecycle hook to close the bus on shutdown + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return bus.Close() + }, + }) + + return bus, nil + }) +} + +// ProvideHealthRegistry creates an FX option that provides the health check registry. +func ProvideHealthRegistry() fx.Option { + return fx.Provide(func(dbClient *database.Client) (*health.Registry, error) { + registry := health.NewRegistry() + + // Register database health checker + registry.Register("database", health.NewDatabaseChecker(dbClient)) + + return registry, nil + }) +} + +// ProvideMetrics creates an FX option that provides the Prometheus metrics registry. +func ProvideMetrics() fx.Option { + return fx.Provide(func() *metrics.Metrics { + return metrics.NewMetrics() + }) +} + +// ProvideHTTPServer creates an FX option that provides the HTTP server. +func ProvideHTTPServer() fx.Option { + return fx.Provide(func( + cfg config.ConfigProvider, + log logger.Logger, + healthRegistry *health.Registry, + metricsRegistry *metrics.Metrics, + errorBus errorbus.ErrorPublisher, + lc fx.Lifecycle, + ) (*server.Server, error) { + srv, err := server.NewServer(cfg, log, healthRegistry, metricsRegistry, errorBus) + if err != nil { + return nil, fmt.Errorf("failed to create HTTP server: %w", err) + } + + // Register lifecycle hooks + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + // Start server in a goroutine + go func() { + if err := srv.Start(); err != nil && err != http.ErrServerClosed { + log.Error("HTTP server error", + logger.String("error", err.Error()), + ) + } + }() + return nil + }, + OnStop: func(ctx context.Context) error { + return srv.Shutdown(ctx) + }, + }) + + return srv, nil + }) +} + // CoreModule returns an FX option that provides all core services. -// This includes configuration and logging. +// This includes configuration, logging, database, error bus, health checks, metrics, and HTTP server. func CoreModule() fx.Option { return fx.Options( ProvideConfig(), ProvideLogger(), + ProvideDatabase(), + ProvideErrorBus(), + ProvideHealthRegistry(), + ProvideMetrics(), + ProvideHTTPServer(), ) } diff --git a/internal/ent/auditlog.go b/internal/ent/auditlog.go new file mode 100644 index 0000000..9bb446c --- /dev/null +++ b/internal/ent/auditlog.go @@ -0,0 +1,153 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "encoding/json" + "fmt" + "strings" + "time" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" +) + +// AuditLog is the model entity for the AuditLog schema. +type AuditLog struct { + config `json:"-"` + // ID of the ent. + ID string `json:"id,omitempty"` + // ID of the user/actor performing the action + ActorID string `json:"actor_id,omitempty"` + // Action performed (e.g., create, update, delete) + Action string `json:"action,omitempty"` + // ID of the target resource + TargetID string `json:"target_id,omitempty"` + // Additional metadata as JSON + Metadata map[string]interface{} `json:"metadata,omitempty"` + // Timestamp holds the value of the "timestamp" field. + Timestamp time.Time `json:"timestamp,omitempty"` + selectValues sql.SelectValues +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*AuditLog) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case auditlog.FieldMetadata: + values[i] = new([]byte) + case auditlog.FieldID, auditlog.FieldActorID, auditlog.FieldAction, auditlog.FieldTargetID: + values[i] = new(sql.NullString) + case auditlog.FieldTimestamp: + values[i] = new(sql.NullTime) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the AuditLog fields. +func (_m *AuditLog) 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 auditlog.FieldID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value.Valid { + _m.ID = value.String + } + case auditlog.FieldActorID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field actor_id", values[i]) + } else if value.Valid { + _m.ActorID = value.String + } + case auditlog.FieldAction: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field action", values[i]) + } else if value.Valid { + _m.Action = value.String + } + case auditlog.FieldTargetID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field target_id", values[i]) + } else if value.Valid { + _m.TargetID = value.String + } + case auditlog.FieldMetadata: + if value, ok := values[i].(*[]byte); !ok { + return fmt.Errorf("unexpected type %T for field metadata", values[i]) + } else if value != nil && len(*value) > 0 { + if err := json.Unmarshal(*value, &_m.Metadata); err != nil { + return fmt.Errorf("unmarshal field metadata: %w", err) + } + } + case auditlog.FieldTimestamp: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field timestamp", values[i]) + } else if value.Valid { + _m.Timestamp = value.Time + } + default: + _m.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the AuditLog. +// This includes values selected through modifiers, order, etc. +func (_m *AuditLog) Value(name string) (ent.Value, error) { + return _m.selectValues.Get(name) +} + +// Update returns a builder for updating this AuditLog. +// Note that you need to call AuditLog.Unwrap() before calling this method if this AuditLog +// was returned from a transaction, and the transaction was committed or rolled back. +func (_m *AuditLog) Update() *AuditLogUpdateOne { + return NewAuditLogClient(_m.config).UpdateOne(_m) +} + +// Unwrap unwraps the AuditLog 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 (_m *AuditLog) Unwrap() *AuditLog { + _tx, ok := _m.config.driver.(*txDriver) + if !ok { + panic("ent: AuditLog is not a transactional entity") + } + _m.config.driver = _tx.drv + return _m +} + +// String implements the fmt.Stringer. +func (_m *AuditLog) String() string { + var builder strings.Builder + builder.WriteString("AuditLog(") + builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("actor_id=") + builder.WriteString(_m.ActorID) + builder.WriteString(", ") + builder.WriteString("action=") + builder.WriteString(_m.Action) + builder.WriteString(", ") + builder.WriteString("target_id=") + builder.WriteString(_m.TargetID) + builder.WriteString(", ") + builder.WriteString("metadata=") + builder.WriteString(fmt.Sprintf("%v", _m.Metadata)) + builder.WriteString(", ") + builder.WriteString("timestamp=") + builder.WriteString(_m.Timestamp.Format(time.ANSIC)) + builder.WriteByte(')') + return builder.String() +} + +// AuditLogs is a parsable slice of AuditLog. +type AuditLogs []*AuditLog diff --git a/internal/ent/auditlog/auditlog.go b/internal/ent/auditlog/auditlog.go new file mode 100644 index 0000000..6b72a15 --- /dev/null +++ b/internal/ent/auditlog/auditlog.go @@ -0,0 +1,85 @@ +// Code generated by ent, DO NOT EDIT. + +package auditlog + +import ( + "time" + + "entgo.io/ent/dialect/sql" +) + +const ( + // Label holds the string label denoting the auditlog type in the database. + Label = "audit_log" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldActorID holds the string denoting the actor_id field in the database. + FieldActorID = "actor_id" + // FieldAction holds the string denoting the action field in the database. + FieldAction = "action" + // FieldTargetID holds the string denoting the target_id field in the database. + FieldTargetID = "target_id" + // FieldMetadata holds the string denoting the metadata field in the database. + FieldMetadata = "metadata" + // FieldTimestamp holds the string denoting the timestamp field in the database. + FieldTimestamp = "timestamp" + // Table holds the table name of the auditlog in the database. + Table = "audit_logs" +) + +// Columns holds all SQL columns for auditlog fields. +var Columns = []string{ + FieldID, + FieldActorID, + FieldAction, + FieldTargetID, + FieldMetadata, + FieldTimestamp, +} + +// 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 ( + // ActorIDValidator is a validator for the "actor_id" field. It is called by the builders before save. + ActorIDValidator func(string) error + // ActionValidator is a validator for the "action" field. It is called by the builders before save. + ActionValidator func(string) error + // DefaultTimestamp holds the default value on creation for the "timestamp" field. + DefaultTimestamp func() time.Time +) + +// OrderOption defines the ordering options for the AuditLog queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByActorID orders the results by the actor_id field. +func ByActorID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldActorID, opts...).ToFunc() +} + +// ByAction orders the results by the action field. +func ByAction(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldAction, opts...).ToFunc() +} + +// ByTargetID orders the results by the target_id field. +func ByTargetID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldTargetID, opts...).ToFunc() +} + +// ByTimestamp orders the results by the timestamp field. +func ByTimestamp(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldTimestamp, opts...).ToFunc() +} diff --git a/internal/ent/auditlog/where.go b/internal/ent/auditlog/where.go new file mode 100644 index 0000000..c18fc47 --- /dev/null +++ b/internal/ent/auditlog/where.go @@ -0,0 +1,355 @@ +// Code generated by ent, DO NOT EDIT. + +package auditlog + +import ( + "time" + + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLTE(FieldID, id)) +} + +// IDEqualFold applies the EqualFold predicate on the ID field. +func IDEqualFold(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEqualFold(FieldID, id)) +} + +// IDContainsFold applies the ContainsFold predicate on the ID field. +func IDContainsFold(id string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContainsFold(FieldID, id)) +} + +// ActorID applies equality check predicate on the "actor_id" field. It's identical to ActorIDEQ. +func ActorID(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldActorID, v)) +} + +// Action applies equality check predicate on the "action" field. It's identical to ActionEQ. +func Action(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldAction, v)) +} + +// TargetID applies equality check predicate on the "target_id" field. It's identical to TargetIDEQ. +func TargetID(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldTargetID, v)) +} + +// Timestamp applies equality check predicate on the "timestamp" field. It's identical to TimestampEQ. +func Timestamp(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldTimestamp, v)) +} + +// ActorIDEQ applies the EQ predicate on the "actor_id" field. +func ActorIDEQ(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldActorID, v)) +} + +// ActorIDNEQ applies the NEQ predicate on the "actor_id" field. +func ActorIDNEQ(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNEQ(FieldActorID, v)) +} + +// ActorIDIn applies the In predicate on the "actor_id" field. +func ActorIDIn(vs ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldIn(FieldActorID, vs...)) +} + +// ActorIDNotIn applies the NotIn predicate on the "actor_id" field. +func ActorIDNotIn(vs ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotIn(FieldActorID, vs...)) +} + +// ActorIDGT applies the GT predicate on the "actor_id" field. +func ActorIDGT(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGT(FieldActorID, v)) +} + +// ActorIDGTE applies the GTE predicate on the "actor_id" field. +func ActorIDGTE(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGTE(FieldActorID, v)) +} + +// ActorIDLT applies the LT predicate on the "actor_id" field. +func ActorIDLT(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLT(FieldActorID, v)) +} + +// ActorIDLTE applies the LTE predicate on the "actor_id" field. +func ActorIDLTE(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLTE(FieldActorID, v)) +} + +// ActorIDContains applies the Contains predicate on the "actor_id" field. +func ActorIDContains(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContains(FieldActorID, v)) +} + +// ActorIDHasPrefix applies the HasPrefix predicate on the "actor_id" field. +func ActorIDHasPrefix(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldHasPrefix(FieldActorID, v)) +} + +// ActorIDHasSuffix applies the HasSuffix predicate on the "actor_id" field. +func ActorIDHasSuffix(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldHasSuffix(FieldActorID, v)) +} + +// ActorIDEqualFold applies the EqualFold predicate on the "actor_id" field. +func ActorIDEqualFold(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEqualFold(FieldActorID, v)) +} + +// ActorIDContainsFold applies the ContainsFold predicate on the "actor_id" field. +func ActorIDContainsFold(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContainsFold(FieldActorID, v)) +} + +// ActionEQ applies the EQ predicate on the "action" field. +func ActionEQ(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldAction, v)) +} + +// ActionNEQ applies the NEQ predicate on the "action" field. +func ActionNEQ(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNEQ(FieldAction, v)) +} + +// ActionIn applies the In predicate on the "action" field. +func ActionIn(vs ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldIn(FieldAction, vs...)) +} + +// ActionNotIn applies the NotIn predicate on the "action" field. +func ActionNotIn(vs ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotIn(FieldAction, vs...)) +} + +// ActionGT applies the GT predicate on the "action" field. +func ActionGT(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGT(FieldAction, v)) +} + +// ActionGTE applies the GTE predicate on the "action" field. +func ActionGTE(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGTE(FieldAction, v)) +} + +// ActionLT applies the LT predicate on the "action" field. +func ActionLT(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLT(FieldAction, v)) +} + +// ActionLTE applies the LTE predicate on the "action" field. +func ActionLTE(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLTE(FieldAction, v)) +} + +// ActionContains applies the Contains predicate on the "action" field. +func ActionContains(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContains(FieldAction, v)) +} + +// ActionHasPrefix applies the HasPrefix predicate on the "action" field. +func ActionHasPrefix(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldHasPrefix(FieldAction, v)) +} + +// ActionHasSuffix applies the HasSuffix predicate on the "action" field. +func ActionHasSuffix(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldHasSuffix(FieldAction, v)) +} + +// ActionEqualFold applies the EqualFold predicate on the "action" field. +func ActionEqualFold(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEqualFold(FieldAction, v)) +} + +// ActionContainsFold applies the ContainsFold predicate on the "action" field. +func ActionContainsFold(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContainsFold(FieldAction, v)) +} + +// TargetIDEQ applies the EQ predicate on the "target_id" field. +func TargetIDEQ(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldTargetID, v)) +} + +// TargetIDNEQ applies the NEQ predicate on the "target_id" field. +func TargetIDNEQ(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNEQ(FieldTargetID, v)) +} + +// TargetIDIn applies the In predicate on the "target_id" field. +func TargetIDIn(vs ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldIn(FieldTargetID, vs...)) +} + +// TargetIDNotIn applies the NotIn predicate on the "target_id" field. +func TargetIDNotIn(vs ...string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotIn(FieldTargetID, vs...)) +} + +// TargetIDGT applies the GT predicate on the "target_id" field. +func TargetIDGT(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGT(FieldTargetID, v)) +} + +// TargetIDGTE applies the GTE predicate on the "target_id" field. +func TargetIDGTE(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGTE(FieldTargetID, v)) +} + +// TargetIDLT applies the LT predicate on the "target_id" field. +func TargetIDLT(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLT(FieldTargetID, v)) +} + +// TargetIDLTE applies the LTE predicate on the "target_id" field. +func TargetIDLTE(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLTE(FieldTargetID, v)) +} + +// TargetIDContains applies the Contains predicate on the "target_id" field. +func TargetIDContains(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContains(FieldTargetID, v)) +} + +// TargetIDHasPrefix applies the HasPrefix predicate on the "target_id" field. +func TargetIDHasPrefix(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldHasPrefix(FieldTargetID, v)) +} + +// TargetIDHasSuffix applies the HasSuffix predicate on the "target_id" field. +func TargetIDHasSuffix(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldHasSuffix(FieldTargetID, v)) +} + +// TargetIDIsNil applies the IsNil predicate on the "target_id" field. +func TargetIDIsNil() predicate.AuditLog { + return predicate.AuditLog(sql.FieldIsNull(FieldTargetID)) +} + +// TargetIDNotNil applies the NotNil predicate on the "target_id" field. +func TargetIDNotNil() predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotNull(FieldTargetID)) +} + +// TargetIDEqualFold applies the EqualFold predicate on the "target_id" field. +func TargetIDEqualFold(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEqualFold(FieldTargetID, v)) +} + +// TargetIDContainsFold applies the ContainsFold predicate on the "target_id" field. +func TargetIDContainsFold(v string) predicate.AuditLog { + return predicate.AuditLog(sql.FieldContainsFold(FieldTargetID, v)) +} + +// MetadataIsNil applies the IsNil predicate on the "metadata" field. +func MetadataIsNil() predicate.AuditLog { + return predicate.AuditLog(sql.FieldIsNull(FieldMetadata)) +} + +// MetadataNotNil applies the NotNil predicate on the "metadata" field. +func MetadataNotNil() predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotNull(FieldMetadata)) +} + +// TimestampEQ applies the EQ predicate on the "timestamp" field. +func TimestampEQ(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldEQ(FieldTimestamp, v)) +} + +// TimestampNEQ applies the NEQ predicate on the "timestamp" field. +func TimestampNEQ(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNEQ(FieldTimestamp, v)) +} + +// TimestampIn applies the In predicate on the "timestamp" field. +func TimestampIn(vs ...time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldIn(FieldTimestamp, vs...)) +} + +// TimestampNotIn applies the NotIn predicate on the "timestamp" field. +func TimestampNotIn(vs ...time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldNotIn(FieldTimestamp, vs...)) +} + +// TimestampGT applies the GT predicate on the "timestamp" field. +func TimestampGT(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGT(FieldTimestamp, v)) +} + +// TimestampGTE applies the GTE predicate on the "timestamp" field. +func TimestampGTE(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldGTE(FieldTimestamp, v)) +} + +// TimestampLT applies the LT predicate on the "timestamp" field. +func TimestampLT(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLT(FieldTimestamp, v)) +} + +// TimestampLTE applies the LTE predicate on the "timestamp" field. +func TimestampLTE(v time.Time) predicate.AuditLog { + return predicate.AuditLog(sql.FieldLTE(FieldTimestamp, v)) +} + +// And groups predicates with the AND operator between them. +func And(predicates ...predicate.AuditLog) predicate.AuditLog { + return predicate.AuditLog(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.AuditLog) predicate.AuditLog { + return predicate.AuditLog(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.AuditLog) predicate.AuditLog { + return predicate.AuditLog(sql.NotPredicates(p)) +} diff --git a/internal/ent/auditlog_create.go b/internal/ent/auditlog_create.go new file mode 100644 index 0000000..3e66cbb --- /dev/null +++ b/internal/ent/auditlog_create.go @@ -0,0 +1,277 @@ +// 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) + } +} diff --git a/internal/ent/auditlog_delete.go b/internal/ent/auditlog_delete.go new file mode 100644 index 0000000..47a913b --- /dev/null +++ b/internal/ent/auditlog_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" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// AuditLogDelete is the builder for deleting a AuditLog entity. +type AuditLogDelete struct { + config + hooks []Hook + mutation *AuditLogMutation +} + +// Where appends a list predicates to the AuditLogDelete builder. +func (_d *AuditLogDelete) Where(ps ...predicate.AuditLog) *AuditLogDelete { + _d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (_d *AuditLogDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *AuditLogDelete) ExecX(ctx context.Context) int { + n, err := _d.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (_d *AuditLogDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(auditlog.Table, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString)) + if ps := _d.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + _d.mutation.done = true + return affected, err +} + +// AuditLogDeleteOne is the builder for deleting a single AuditLog entity. +type AuditLogDeleteOne struct { + _d *AuditLogDelete +} + +// Where appends a list predicates to the AuditLogDelete builder. +func (_d *AuditLogDeleteOne) Where(ps ...predicate.AuditLog) *AuditLogDeleteOne { + _d._d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query. +func (_d *AuditLogDeleteOne) Exec(ctx context.Context) error { + n, err := _d._d.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{auditlog.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *AuditLogDeleteOne) ExecX(ctx context.Context) { + if err := _d.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/auditlog_query.go b/internal/ent/auditlog_query.go new file mode 100644 index 0000000..f575343 --- /dev/null +++ b/internal/ent/auditlog_query.go @@ -0,0 +1,527 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + "math" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// AuditLogQuery is the builder for querying AuditLog entities. +type AuditLogQuery struct { + config + ctx *QueryContext + order []auditlog.OrderOption + inters []Interceptor + predicates []predicate.AuditLog + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the AuditLogQuery builder. +func (_q *AuditLogQuery) Where(ps ...predicate.AuditLog) *AuditLogQuery { + _q.predicates = append(_q.predicates, ps...) + return _q +} + +// Limit the number of records to be returned by this query. +func (_q *AuditLogQuery) Limit(limit int) *AuditLogQuery { + _q.ctx.Limit = &limit + return _q +} + +// Offset to start from. +func (_q *AuditLogQuery) Offset(offset int) *AuditLogQuery { + _q.ctx.Offset = &offset + return _q +} + +// 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 (_q *AuditLogQuery) Unique(unique bool) *AuditLogQuery { + _q.ctx.Unique = &unique + return _q +} + +// Order specifies how the records should be ordered. +func (_q *AuditLogQuery) Order(o ...auditlog.OrderOption) *AuditLogQuery { + _q.order = append(_q.order, o...) + return _q +} + +// First returns the first AuditLog entity from the query. +// Returns a *NotFoundError when no AuditLog was found. +func (_q *AuditLogQuery) First(ctx context.Context) (*AuditLog, error) { + nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{auditlog.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (_q *AuditLogQuery) FirstX(ctx context.Context) *AuditLog { + node, err := _q.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first AuditLog ID from the query. +// Returns a *NotFoundError when no AuditLog ID was found. +func (_q *AuditLogQuery) FirstID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{auditlog.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (_q *AuditLogQuery) FirstIDX(ctx context.Context) string { + id, err := _q.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single AuditLog entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one AuditLog entity is found. +// Returns a *NotFoundError when no AuditLog entities are found. +func (_q *AuditLogQuery) Only(ctx context.Context) (*AuditLog, error) { + nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{auditlog.Label} + default: + return nil, &NotSingularError{auditlog.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (_q *AuditLogQuery) OnlyX(ctx context.Context) *AuditLog { + node, err := _q.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only AuditLog ID in the query. +// Returns a *NotSingularError when more than one AuditLog ID is found. +// Returns a *NotFoundError when no entities are found. +func (_q *AuditLogQuery) OnlyID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{auditlog.Label} + default: + err = &NotSingularError{auditlog.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (_q *AuditLogQuery) OnlyIDX(ctx context.Context) string { + id, err := _q.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of AuditLogs. +func (_q *AuditLogQuery) All(ctx context.Context) ([]*AuditLog, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll) + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*AuditLog, *AuditLogQuery]() + return withInterceptors[[]*AuditLog](ctx, _q, qr, _q.inters) +} + +// AllX is like All, but panics if an error occurs. +func (_q *AuditLogQuery) AllX(ctx context.Context) []*AuditLog { + nodes, err := _q.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of AuditLog IDs. +func (_q *AuditLogQuery) IDs(ctx context.Context) (ids []string, err error) { + if _q.ctx.Unique == nil && _q.path != nil { + _q.Unique(true) + } + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs) + if err = _q.Select(auditlog.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (_q *AuditLogQuery) IDsX(ctx context.Context) []string { + ids, err := _q.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (_q *AuditLogQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount) + if err := _q.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, _q, querierCount[*AuditLogQuery](), _q.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (_q *AuditLogQuery) CountX(ctx context.Context) int { + count, err := _q.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (_q *AuditLogQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist) + switch _, err := _q.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 (_q *AuditLogQuery) ExistX(ctx context.Context) bool { + exist, err := _q.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the AuditLogQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (_q *AuditLogQuery) Clone() *AuditLogQuery { + if _q == nil { + return nil + } + return &AuditLogQuery{ + config: _q.config, + ctx: _q.ctx.Clone(), + order: append([]auditlog.OrderOption{}, _q.order...), + inters: append([]Interceptor{}, _q.inters...), + predicates: append([]predicate.AuditLog{}, _q.predicates...), + // clone intermediate query. + sql: _q.sql.Clone(), + path: _q.path, + } +} + +// 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 { +// ActorID string `json:"actor_id,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.AuditLog.Query(). +// GroupBy(auditlog.FieldActorID). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (_q *AuditLogQuery) GroupBy(field string, fields ...string) *AuditLogGroupBy { + _q.ctx.Fields = append([]string{field}, fields...) + grbuild := &AuditLogGroupBy{build: _q} + grbuild.flds = &_q.ctx.Fields + grbuild.label = auditlog.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 { +// ActorID string `json:"actor_id,omitempty"` +// } +// +// client.AuditLog.Query(). +// Select(auditlog.FieldActorID). +// Scan(ctx, &v) +func (_q *AuditLogQuery) Select(fields ...string) *AuditLogSelect { + _q.ctx.Fields = append(_q.ctx.Fields, fields...) + sbuild := &AuditLogSelect{AuditLogQuery: _q} + sbuild.label = auditlog.Label + sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a AuditLogSelect configured with the given aggregations. +func (_q *AuditLogQuery) Aggregate(fns ...AggregateFunc) *AuditLogSelect { + return _q.Select().Aggregate(fns...) +} + +func (_q *AuditLogQuery) prepareQuery(ctx context.Context) error { + for _, inter := range _q.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, _q); err != nil { + return err + } + } + } + for _, f := range _q.ctx.Fields { + if !auditlog.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if _q.path != nil { + prev, err := _q.path(ctx) + if err != nil { + return err + } + _q.sql = prev + } + return nil +} + +func (_q *AuditLogQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*AuditLog, error) { + var ( + nodes = []*AuditLog{} + _spec = _q.querySpec() + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*AuditLog).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &AuditLog{config: _q.config} + nodes = append(nodes, node) + return node.assignValues(columns, values) + } + for i := range hooks { + hooks[i](ctx, _spec) + } + if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + return nodes, nil +} + +func (_q *AuditLogQuery) sqlCount(ctx context.Context) (int, error) { + _spec := _q.querySpec() + _spec.Node.Columns = _q.ctx.Fields + if len(_q.ctx.Fields) > 0 { + _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique + } + return sqlgraph.CountNodes(ctx, _q.driver, _spec) +} + +func (_q *AuditLogQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(auditlog.Table, auditlog.Columns, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString)) + _spec.From = _q.sql + if unique := _q.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if _q.path != nil { + _spec.Unique = true + } + if fields := _q.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, auditlog.FieldID) + for i := range fields { + if fields[i] != auditlog.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := _q.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := _q.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := _q.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := _q.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (_q *AuditLogQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(_q.driver.Dialect()) + t1 := builder.Table(auditlog.Table) + columns := _q.ctx.Fields + if len(columns) == 0 { + columns = auditlog.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if _q.sql != nil { + selector = _q.sql + selector.Select(selector.Columns(columns...)...) + } + if _q.ctx.Unique != nil && *_q.ctx.Unique { + selector.Distinct() + } + for _, p := range _q.predicates { + p(selector) + } + for _, p := range _q.order { + p(selector) + } + if offset := _q.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 := _q.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// AuditLogGroupBy is the group-by builder for AuditLog entities. +type AuditLogGroupBy struct { + selector + build *AuditLogQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (_g *AuditLogGroupBy) Aggregate(fns ...AggregateFunc) *AuditLogGroupBy { + _g.fns = append(_g.fns, fns...) + return _g +} + +// Scan applies the selector query and scans the result into the given value. +func (_g *AuditLogGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*AuditLogQuery, *AuditLogGroupBy](ctx, _g.build, _g, _g.build.inters, v) +} + +func (_g *AuditLogGroupBy) sqlScan(ctx context.Context, root *AuditLogQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*_g.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// AuditLogSelect is the builder for selecting fields of AuditLog entities. +type AuditLogSelect struct { + *AuditLogQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (_s *AuditLogSelect) Aggregate(fns ...AggregateFunc) *AuditLogSelect { + _s.fns = append(_s.fns, fns...) + return _s +} + +// Scan applies the selector query and scans the result into the given value. +func (_s *AuditLogSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*AuditLogQuery, *AuditLogSelect](ctx, _s.AuditLogQuery, _s, _s.inters, v) +} + +func (_s *AuditLogSelect) sqlScan(ctx context.Context, root *AuditLogQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/ent/auditlog_update.go b/internal/ent/auditlog_update.go new file mode 100644 index 0000000..3b3eae5 --- /dev/null +++ b/internal/ent/auditlog_update.go @@ -0,0 +1,367 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// AuditLogUpdate is the builder for updating AuditLog entities. +type AuditLogUpdate struct { + config + hooks []Hook + mutation *AuditLogMutation +} + +// Where appends a list predicates to the AuditLogUpdate builder. +func (_u *AuditLogUpdate) Where(ps ...predicate.AuditLog) *AuditLogUpdate { + _u.mutation.Where(ps...) + return _u +} + +// SetActorID sets the "actor_id" field. +func (_u *AuditLogUpdate) SetActorID(v string) *AuditLogUpdate { + _u.mutation.SetActorID(v) + return _u +} + +// SetNillableActorID sets the "actor_id" field if the given value is not nil. +func (_u *AuditLogUpdate) SetNillableActorID(v *string) *AuditLogUpdate { + if v != nil { + _u.SetActorID(*v) + } + return _u +} + +// SetAction sets the "action" field. +func (_u *AuditLogUpdate) SetAction(v string) *AuditLogUpdate { + _u.mutation.SetAction(v) + return _u +} + +// SetNillableAction sets the "action" field if the given value is not nil. +func (_u *AuditLogUpdate) SetNillableAction(v *string) *AuditLogUpdate { + if v != nil { + _u.SetAction(*v) + } + return _u +} + +// SetTargetID sets the "target_id" field. +func (_u *AuditLogUpdate) SetTargetID(v string) *AuditLogUpdate { + _u.mutation.SetTargetID(v) + return _u +} + +// SetNillableTargetID sets the "target_id" field if the given value is not nil. +func (_u *AuditLogUpdate) SetNillableTargetID(v *string) *AuditLogUpdate { + if v != nil { + _u.SetTargetID(*v) + } + return _u +} + +// ClearTargetID clears the value of the "target_id" field. +func (_u *AuditLogUpdate) ClearTargetID() *AuditLogUpdate { + _u.mutation.ClearTargetID() + return _u +} + +// SetMetadata sets the "metadata" field. +func (_u *AuditLogUpdate) SetMetadata(v map[string]interface{}) *AuditLogUpdate { + _u.mutation.SetMetadata(v) + return _u +} + +// ClearMetadata clears the value of the "metadata" field. +func (_u *AuditLogUpdate) ClearMetadata() *AuditLogUpdate { + _u.mutation.ClearMetadata() + return _u +} + +// Mutation returns the AuditLogMutation object of the builder. +func (_u *AuditLogUpdate) Mutation() *AuditLogMutation { + return _u.mutation +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (_u *AuditLogUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *AuditLogUpdate) SaveX(ctx context.Context) int { + affected, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (_u *AuditLogUpdate) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *AuditLogUpdate) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *AuditLogUpdate) check() error { + if v, ok := _u.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 v, ok := _u.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)} + } + } + return nil +} + +func (_u *AuditLogUpdate) sqlSave(ctx context.Context) (_node int, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(auditlog.Table, auditlog.Columns, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString)) + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.ActorID(); ok { + _spec.SetField(auditlog.FieldActorID, field.TypeString, value) + } + if value, ok := _u.mutation.Action(); ok { + _spec.SetField(auditlog.FieldAction, field.TypeString, value) + } + if value, ok := _u.mutation.TargetID(); ok { + _spec.SetField(auditlog.FieldTargetID, field.TypeString, value) + } + if _u.mutation.TargetIDCleared() { + _spec.ClearField(auditlog.FieldTargetID, field.TypeString) + } + if value, ok := _u.mutation.Metadata(); ok { + _spec.SetField(auditlog.FieldMetadata, field.TypeJSON, value) + } + if _u.mutation.MetadataCleared() { + _spec.ClearField(auditlog.FieldMetadata, field.TypeJSON) + } + if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{auditlog.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + _u.mutation.done = true + return _node, nil +} + +// AuditLogUpdateOne is the builder for updating a single AuditLog entity. +type AuditLogUpdateOne struct { + config + fields []string + hooks []Hook + mutation *AuditLogMutation +} + +// SetActorID sets the "actor_id" field. +func (_u *AuditLogUpdateOne) SetActorID(v string) *AuditLogUpdateOne { + _u.mutation.SetActorID(v) + return _u +} + +// SetNillableActorID sets the "actor_id" field if the given value is not nil. +func (_u *AuditLogUpdateOne) SetNillableActorID(v *string) *AuditLogUpdateOne { + if v != nil { + _u.SetActorID(*v) + } + return _u +} + +// SetAction sets the "action" field. +func (_u *AuditLogUpdateOne) SetAction(v string) *AuditLogUpdateOne { + _u.mutation.SetAction(v) + return _u +} + +// SetNillableAction sets the "action" field if the given value is not nil. +func (_u *AuditLogUpdateOne) SetNillableAction(v *string) *AuditLogUpdateOne { + if v != nil { + _u.SetAction(*v) + } + return _u +} + +// SetTargetID sets the "target_id" field. +func (_u *AuditLogUpdateOne) SetTargetID(v string) *AuditLogUpdateOne { + _u.mutation.SetTargetID(v) + return _u +} + +// SetNillableTargetID sets the "target_id" field if the given value is not nil. +func (_u *AuditLogUpdateOne) SetNillableTargetID(v *string) *AuditLogUpdateOne { + if v != nil { + _u.SetTargetID(*v) + } + return _u +} + +// ClearTargetID clears the value of the "target_id" field. +func (_u *AuditLogUpdateOne) ClearTargetID() *AuditLogUpdateOne { + _u.mutation.ClearTargetID() + return _u +} + +// SetMetadata sets the "metadata" field. +func (_u *AuditLogUpdateOne) SetMetadata(v map[string]interface{}) *AuditLogUpdateOne { + _u.mutation.SetMetadata(v) + return _u +} + +// ClearMetadata clears the value of the "metadata" field. +func (_u *AuditLogUpdateOne) ClearMetadata() *AuditLogUpdateOne { + _u.mutation.ClearMetadata() + return _u +} + +// Mutation returns the AuditLogMutation object of the builder. +func (_u *AuditLogUpdateOne) Mutation() *AuditLogMutation { + return _u.mutation +} + +// Where appends a list predicates to the AuditLogUpdate builder. +func (_u *AuditLogUpdateOne) Where(ps ...predicate.AuditLog) *AuditLogUpdateOne { + _u.mutation.Where(ps...) + return _u +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (_u *AuditLogUpdateOne) Select(field string, fields ...string) *AuditLogUpdateOne { + _u.fields = append([]string{field}, fields...) + return _u +} + +// Save executes the query and returns the updated AuditLog entity. +func (_u *AuditLogUpdateOne) Save(ctx context.Context) (*AuditLog, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *AuditLogUpdateOne) SaveX(ctx context.Context) *AuditLog { + node, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (_u *AuditLogUpdateOne) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *AuditLogUpdateOne) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *AuditLogUpdateOne) check() error { + if v, ok := _u.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 v, ok := _u.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)} + } + } + return nil +} + +func (_u *AuditLogUpdateOne) sqlSave(ctx context.Context) (_node *AuditLog, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(auditlog.Table, auditlog.Columns, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString)) + id, ok := _u.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AuditLog.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := _u.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, auditlog.FieldID) + for _, f := range fields { + if !auditlog.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != auditlog.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.ActorID(); ok { + _spec.SetField(auditlog.FieldActorID, field.TypeString, value) + } + if value, ok := _u.mutation.Action(); ok { + _spec.SetField(auditlog.FieldAction, field.TypeString, value) + } + if value, ok := _u.mutation.TargetID(); ok { + _spec.SetField(auditlog.FieldTargetID, field.TypeString, value) + } + if _u.mutation.TargetIDCleared() { + _spec.ClearField(auditlog.FieldTargetID, field.TypeString) + } + if value, ok := _u.mutation.Metadata(); ok { + _spec.SetField(auditlog.FieldMetadata, field.TypeJSON, value) + } + if _u.mutation.MetadataCleared() { + _spec.ClearField(auditlog.FieldMetadata, field.TypeJSON) + } + _node = &AuditLog{config: _u.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{auditlog.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + _u.mutation.done = true + return _node, nil +} diff --git a/internal/ent/client.go b/internal/ent/client.go new file mode 100644 index 0000000..b9385e0 --- /dev/null +++ b/internal/ent/client.go @@ -0,0 +1,1182 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + "log" + "reflect" + + "git.dcentral.systems/toolz/goplt/internal/ent/migrate" + + "entgo.io/ent" + "entgo.io/ent/dialect" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// Client is the client that holds all ent builders. +type Client struct { + config + // Schema is the client for creating, migrating and dropping schema. + Schema *migrate.Schema + // AuditLog is the client for interacting with the AuditLog builders. + AuditLog *AuditLogClient + // Permission is the client for interacting with the Permission builders. + Permission *PermissionClient + // Role is the client for interacting with the Role builders. + Role *RoleClient + // RolePermission is the client for interacting with the RolePermission builders. + RolePermission *RolePermissionClient + // User is the client for interacting with the User builders. + User *UserClient + // UserRole is the client for interacting with the UserRole builders. + UserRole *UserRoleClient +} + +// NewClient creates a new client configured with the given options. +func NewClient(opts ...Option) *Client { + client := &Client{config: newConfig(opts...)} + client.init() + return client +} + +func (c *Client) init() { + c.Schema = migrate.NewSchema(c.driver) + c.AuditLog = NewAuditLogClient(c.config) + c.Permission = NewPermissionClient(c.config) + c.Role = NewRoleClient(c.config) + c.RolePermission = NewRolePermissionClient(c.config) + c.User = NewUserClient(c.config) + c.UserRole = NewUserRoleClient(c.config) +} + +type ( + // config is the configuration for the client and its builder. + config struct { + // driver used for executing database requests. + driver dialect.Driver + // debug enable a debug logging. + debug bool + // log used for logging on debug mode. + log func(...any) + // hooks to execute on mutations. + hooks *hooks + // interceptors to execute on queries. + inters *inters + } + // Option function to configure the client. + Option func(*config) +) + +// newConfig creates a new config for the client. +func newConfig(opts ...Option) config { + cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}} + cfg.options(opts...) + return cfg +} + +// options applies the options on the config object. +func (c *config) options(opts ...Option) { + for _, opt := range opts { + opt(c) + } + if c.debug { + c.driver = dialect.Debug(c.driver, c.log) + } +} + +// Debug enables debug logging on the ent.Driver. +func Debug() Option { + return func(c *config) { + c.debug = true + } +} + +// Log sets the logging function for debug mode. +func Log(fn func(...any)) Option { + return func(c *config) { + c.log = fn + } +} + +// Driver configures the client driver. +func Driver(driver dialect.Driver) Option { + return func(c *config) { + c.driver = driver + } +} + +// Open opens a database/sql.DB specified by the driver name and +// the data source name, and returns a new client attached to it. +// Optional parameters can be added for configuring the client. +func Open(driverName, dataSourceName string, options ...Option) (*Client, error) { + switch driverName { + case dialect.MySQL, dialect.Postgres, dialect.SQLite: + drv, err := sql.Open(driverName, dataSourceName) + if err != nil { + return nil, err + } + return NewClient(append(options, Driver(drv))...), nil + default: + return nil, fmt.Errorf("unsupported driver: %q", driverName) + } +} + +// ErrTxStarted is returned when trying to start a new transaction from a transactional client. +var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction") + +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. +func (c *Client) Tx(ctx context.Context) (*Tx, error) { + if _, ok := c.driver.(*txDriver); ok { + return nil, ErrTxStarted + } + tx, err := newTx(ctx, c.driver) + if err != nil { + return nil, fmt.Errorf("ent: starting a transaction: %w", err) + } + cfg := c.config + cfg.driver = tx + return &Tx{ + ctx: ctx, + config: cfg, + AuditLog: NewAuditLogClient(cfg), + Permission: NewPermissionClient(cfg), + Role: NewRoleClient(cfg), + RolePermission: NewRolePermissionClient(cfg), + User: NewUserClient(cfg), + UserRole: NewUserRoleClient(cfg), + }, nil +} + +// BeginTx returns a transactional client with specified options. +func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) { + if _, ok := c.driver.(*txDriver); ok { + return nil, errors.New("ent: cannot start a transaction within a transaction") + } + tx, err := c.driver.(interface { + BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error) + }).BeginTx(ctx, opts) + if err != nil { + return nil, fmt.Errorf("ent: starting a transaction: %w", err) + } + cfg := c.config + cfg.driver = &txDriver{tx: tx, drv: c.driver} + return &Tx{ + ctx: ctx, + config: cfg, + AuditLog: NewAuditLogClient(cfg), + Permission: NewPermissionClient(cfg), + Role: NewRoleClient(cfg), + RolePermission: NewRolePermissionClient(cfg), + User: NewUserClient(cfg), + UserRole: NewUserRoleClient(cfg), + }, nil +} + +// Debug returns a new debug-client. It's used to get verbose logging on specific operations. +// +// client.Debug(). +// AuditLog. +// Query(). +// Count(ctx) +func (c *Client) Debug() *Client { + if c.debug { + return c + } + cfg := c.config + cfg.driver = dialect.Debug(c.driver, c.log) + client := &Client{config: cfg} + client.init() + return client +} + +// Close closes the database connection and prevents new queries from starting. +func (c *Client) Close() error { + return c.driver.Close() +} + +// Use adds the mutation hooks to all the entity clients. +// In order to add hooks to a specific client, call: `client.Node.Use(...)`. +func (c *Client) Use(hooks ...Hook) { + for _, n := range []interface{ Use(...Hook) }{ + c.AuditLog, c.Permission, c.Role, c.RolePermission, c.User, c.UserRole, + } { + n.Use(hooks...) + } +} + +// Intercept adds the query interceptors to all the entity clients. +// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`. +func (c *Client) Intercept(interceptors ...Interceptor) { + for _, n := range []interface{ Intercept(...Interceptor) }{ + c.AuditLog, c.Permission, c.Role, c.RolePermission, c.User, c.UserRole, + } { + n.Intercept(interceptors...) + } +} + +// Mutate implements the ent.Mutator interface. +func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { + switch m := m.(type) { + case *AuditLogMutation: + return c.AuditLog.mutate(ctx, m) + case *PermissionMutation: + return c.Permission.mutate(ctx, m) + case *RoleMutation: + return c.Role.mutate(ctx, m) + case *RolePermissionMutation: + return c.RolePermission.mutate(ctx, m) + case *UserMutation: + return c.User.mutate(ctx, m) + case *UserRoleMutation: + return c.UserRole.mutate(ctx, m) + default: + return nil, fmt.Errorf("ent: unknown mutation type %T", m) + } +} + +// AuditLogClient is a client for the AuditLog schema. +type AuditLogClient struct { + config +} + +// NewAuditLogClient returns a client for the AuditLog from the given config. +func NewAuditLogClient(c config) *AuditLogClient { + return &AuditLogClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `auditlog.Hooks(f(g(h())))`. +func (c *AuditLogClient) Use(hooks ...Hook) { + c.hooks.AuditLog = append(c.hooks.AuditLog, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `auditlog.Intercept(f(g(h())))`. +func (c *AuditLogClient) Intercept(interceptors ...Interceptor) { + c.inters.AuditLog = append(c.inters.AuditLog, interceptors...) +} + +// Create returns a builder for creating a AuditLog entity. +func (c *AuditLogClient) Create() *AuditLogCreate { + mutation := newAuditLogMutation(c.config, OpCreate) + return &AuditLogCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of AuditLog entities. +func (c *AuditLogClient) CreateBulk(builders ...*AuditLogCreate) *AuditLogCreateBulk { + return &AuditLogCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *AuditLogClient) MapCreateBulk(slice any, setFunc func(*AuditLogCreate, int)) *AuditLogCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &AuditLogCreateBulk{err: fmt.Errorf("calling to AuditLogClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*AuditLogCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &AuditLogCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for AuditLog. +func (c *AuditLogClient) Update() *AuditLogUpdate { + mutation := newAuditLogMutation(c.config, OpUpdate) + return &AuditLogUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *AuditLogClient) UpdateOne(_m *AuditLog) *AuditLogUpdateOne { + mutation := newAuditLogMutation(c.config, OpUpdateOne, withAuditLog(_m)) + return &AuditLogUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *AuditLogClient) UpdateOneID(id string) *AuditLogUpdateOne { + mutation := newAuditLogMutation(c.config, OpUpdateOne, withAuditLogID(id)) + return &AuditLogUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for AuditLog. +func (c *AuditLogClient) Delete() *AuditLogDelete { + mutation := newAuditLogMutation(c.config, OpDelete) + return &AuditLogDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *AuditLogClient) DeleteOne(_m *AuditLog) *AuditLogDeleteOne { + return c.DeleteOneID(_m.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *AuditLogClient) DeleteOneID(id string) *AuditLogDeleteOne { + builder := c.Delete().Where(auditlog.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &AuditLogDeleteOne{builder} +} + +// Query returns a query builder for AuditLog. +func (c *AuditLogClient) Query() *AuditLogQuery { + return &AuditLogQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeAuditLog}, + inters: c.Interceptors(), + } +} + +// Get returns a AuditLog entity by its id. +func (c *AuditLogClient) Get(ctx context.Context, id string) (*AuditLog, error) { + return c.Query().Where(auditlog.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *AuditLogClient) GetX(ctx context.Context, id string) *AuditLog { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// Hooks returns the client hooks. +func (c *AuditLogClient) Hooks() []Hook { + return c.hooks.AuditLog +} + +// Interceptors returns the client interceptors. +func (c *AuditLogClient) Interceptors() []Interceptor { + return c.inters.AuditLog +} + +func (c *AuditLogClient) mutate(ctx context.Context, m *AuditLogMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&AuditLogCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&AuditLogUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&AuditLogUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&AuditLogDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown AuditLog mutation op: %q", m.Op()) + } +} + +// PermissionClient is a client for the Permission schema. +type PermissionClient struct { + config +} + +// NewPermissionClient returns a client for the Permission from the given config. +func NewPermissionClient(c config) *PermissionClient { + return &PermissionClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `permission.Hooks(f(g(h())))`. +func (c *PermissionClient) Use(hooks ...Hook) { + c.hooks.Permission = append(c.hooks.Permission, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `permission.Intercept(f(g(h())))`. +func (c *PermissionClient) Intercept(interceptors ...Interceptor) { + c.inters.Permission = append(c.inters.Permission, interceptors...) +} + +// Create returns a builder for creating a Permission entity. +func (c *PermissionClient) Create() *PermissionCreate { + mutation := newPermissionMutation(c.config, OpCreate) + return &PermissionCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of Permission entities. +func (c *PermissionClient) CreateBulk(builders ...*PermissionCreate) *PermissionCreateBulk { + return &PermissionCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *PermissionClient) MapCreateBulk(slice any, setFunc func(*PermissionCreate, int)) *PermissionCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &PermissionCreateBulk{err: fmt.Errorf("calling to PermissionClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*PermissionCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &PermissionCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for Permission. +func (c *PermissionClient) Update() *PermissionUpdate { + mutation := newPermissionMutation(c.config, OpUpdate) + return &PermissionUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *PermissionClient) UpdateOne(_m *Permission) *PermissionUpdateOne { + mutation := newPermissionMutation(c.config, OpUpdateOne, withPermission(_m)) + return &PermissionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *PermissionClient) UpdateOneID(id string) *PermissionUpdateOne { + mutation := newPermissionMutation(c.config, OpUpdateOne, withPermissionID(id)) + return &PermissionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for Permission. +func (c *PermissionClient) Delete() *PermissionDelete { + mutation := newPermissionMutation(c.config, OpDelete) + return &PermissionDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *PermissionClient) DeleteOne(_m *Permission) *PermissionDeleteOne { + return c.DeleteOneID(_m.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *PermissionClient) DeleteOneID(id string) *PermissionDeleteOne { + builder := c.Delete().Where(permission.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &PermissionDeleteOne{builder} +} + +// Query returns a query builder for Permission. +func (c *PermissionClient) Query() *PermissionQuery { + return &PermissionQuery{ + config: c.config, + ctx: &QueryContext{Type: TypePermission}, + inters: c.Interceptors(), + } +} + +// Get returns a Permission entity by its id. +func (c *PermissionClient) Get(ctx context.Context, id string) (*Permission, error) { + return c.Query().Where(permission.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *PermissionClient) GetX(ctx context.Context, id string) *Permission { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryRolePermissions queries the role_permissions edge of a Permission. +func (c *PermissionClient) QueryRolePermissions(_m *Permission) *RolePermissionQuery { + query := (&RolePermissionClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(permission.Table, permission.FieldID, id), + sqlgraph.To(rolepermission.Table, rolepermission.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, permission.RolePermissionsTable, permission.RolePermissionsColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *PermissionClient) Hooks() []Hook { + return c.hooks.Permission +} + +// Interceptors returns the client interceptors. +func (c *PermissionClient) Interceptors() []Interceptor { + return c.inters.Permission +} + +func (c *PermissionClient) mutate(ctx context.Context, m *PermissionMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&PermissionCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&PermissionUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&PermissionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&PermissionDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown Permission mutation op: %q", m.Op()) + } +} + +// RoleClient is a client for the Role schema. +type RoleClient struct { + config +} + +// NewRoleClient returns a client for the Role from the given config. +func NewRoleClient(c config) *RoleClient { + return &RoleClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `role.Hooks(f(g(h())))`. +func (c *RoleClient) Use(hooks ...Hook) { + c.hooks.Role = append(c.hooks.Role, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `role.Intercept(f(g(h())))`. +func (c *RoleClient) Intercept(interceptors ...Interceptor) { + c.inters.Role = append(c.inters.Role, interceptors...) +} + +// Create returns a builder for creating a Role entity. +func (c *RoleClient) Create() *RoleCreate { + mutation := newRoleMutation(c.config, OpCreate) + return &RoleCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of Role entities. +func (c *RoleClient) CreateBulk(builders ...*RoleCreate) *RoleCreateBulk { + return &RoleCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *RoleClient) MapCreateBulk(slice any, setFunc func(*RoleCreate, int)) *RoleCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &RoleCreateBulk{err: fmt.Errorf("calling to RoleClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*RoleCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &RoleCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for Role. +func (c *RoleClient) Update() *RoleUpdate { + mutation := newRoleMutation(c.config, OpUpdate) + return &RoleUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *RoleClient) UpdateOne(_m *Role) *RoleUpdateOne { + mutation := newRoleMutation(c.config, OpUpdateOne, withRole(_m)) + return &RoleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *RoleClient) UpdateOneID(id string) *RoleUpdateOne { + mutation := newRoleMutation(c.config, OpUpdateOne, withRoleID(id)) + return &RoleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for Role. +func (c *RoleClient) Delete() *RoleDelete { + mutation := newRoleMutation(c.config, OpDelete) + return &RoleDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *RoleClient) DeleteOne(_m *Role) *RoleDeleteOne { + return c.DeleteOneID(_m.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *RoleClient) DeleteOneID(id string) *RoleDeleteOne { + builder := c.Delete().Where(role.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &RoleDeleteOne{builder} +} + +// Query returns a query builder for Role. +func (c *RoleClient) Query() *RoleQuery { + return &RoleQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeRole}, + inters: c.Interceptors(), + } +} + +// Get returns a Role entity by its id. +func (c *RoleClient) Get(ctx context.Context, id string) (*Role, error) { + return c.Query().Where(role.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *RoleClient) GetX(ctx context.Context, id string) *Role { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryRolePermissions queries the role_permissions edge of a Role. +func (c *RoleClient) QueryRolePermissions(_m *Role) *RolePermissionQuery { + query := (&RolePermissionClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(role.Table, role.FieldID, id), + sqlgraph.To(rolepermission.Table, rolepermission.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, role.RolePermissionsTable, role.RolePermissionsColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// QueryUserRoles queries the user_roles edge of a Role. +func (c *RoleClient) QueryUserRoles(_m *Role) *UserRoleQuery { + query := (&UserRoleClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(role.Table, role.FieldID, id), + sqlgraph.To(userrole.Table, userrole.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, role.UserRolesTable, role.UserRolesColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *RoleClient) Hooks() []Hook { + return c.hooks.Role +} + +// Interceptors returns the client interceptors. +func (c *RoleClient) Interceptors() []Interceptor { + return c.inters.Role +} + +func (c *RoleClient) mutate(ctx context.Context, m *RoleMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&RoleCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&RoleUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&RoleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&RoleDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown Role mutation op: %q", m.Op()) + } +} + +// RolePermissionClient is a client for the RolePermission schema. +type RolePermissionClient struct { + config +} + +// NewRolePermissionClient returns a client for the RolePermission from the given config. +func NewRolePermissionClient(c config) *RolePermissionClient { + return &RolePermissionClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `rolepermission.Hooks(f(g(h())))`. +func (c *RolePermissionClient) Use(hooks ...Hook) { + c.hooks.RolePermission = append(c.hooks.RolePermission, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `rolepermission.Intercept(f(g(h())))`. +func (c *RolePermissionClient) Intercept(interceptors ...Interceptor) { + c.inters.RolePermission = append(c.inters.RolePermission, interceptors...) +} + +// Create returns a builder for creating a RolePermission entity. +func (c *RolePermissionClient) Create() *RolePermissionCreate { + mutation := newRolePermissionMutation(c.config, OpCreate) + return &RolePermissionCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of RolePermission entities. +func (c *RolePermissionClient) CreateBulk(builders ...*RolePermissionCreate) *RolePermissionCreateBulk { + return &RolePermissionCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *RolePermissionClient) MapCreateBulk(slice any, setFunc func(*RolePermissionCreate, int)) *RolePermissionCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &RolePermissionCreateBulk{err: fmt.Errorf("calling to RolePermissionClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*RolePermissionCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &RolePermissionCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for RolePermission. +func (c *RolePermissionClient) Update() *RolePermissionUpdate { + mutation := newRolePermissionMutation(c.config, OpUpdate) + return &RolePermissionUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *RolePermissionClient) UpdateOne(_m *RolePermission) *RolePermissionUpdateOne { + mutation := newRolePermissionMutation(c.config, OpUpdateOne, withRolePermission(_m)) + return &RolePermissionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *RolePermissionClient) UpdateOneID(id int) *RolePermissionUpdateOne { + mutation := newRolePermissionMutation(c.config, OpUpdateOne, withRolePermissionID(id)) + return &RolePermissionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for RolePermission. +func (c *RolePermissionClient) Delete() *RolePermissionDelete { + mutation := newRolePermissionMutation(c.config, OpDelete) + return &RolePermissionDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *RolePermissionClient) DeleteOne(_m *RolePermission) *RolePermissionDeleteOne { + return c.DeleteOneID(_m.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *RolePermissionClient) DeleteOneID(id int) *RolePermissionDeleteOne { + builder := c.Delete().Where(rolepermission.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &RolePermissionDeleteOne{builder} +} + +// Query returns a query builder for RolePermission. +func (c *RolePermissionClient) Query() *RolePermissionQuery { + return &RolePermissionQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeRolePermission}, + inters: c.Interceptors(), + } +} + +// Get returns a RolePermission entity by its id. +func (c *RolePermissionClient) Get(ctx context.Context, id int) (*RolePermission, error) { + return c.Query().Where(rolepermission.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *RolePermissionClient) GetX(ctx context.Context, id int) *RolePermission { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryRole queries the role edge of a RolePermission. +func (c *RolePermissionClient) QueryRole(_m *RolePermission) *RoleQuery { + query := (&RoleClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(rolepermission.Table, rolepermission.FieldID, id), + sqlgraph.To(role.Table, role.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, rolepermission.RoleTable, rolepermission.RoleColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// QueryPermission queries the permission edge of a RolePermission. +func (c *RolePermissionClient) QueryPermission(_m *RolePermission) *PermissionQuery { + query := (&PermissionClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(rolepermission.Table, rolepermission.FieldID, id), + sqlgraph.To(permission.Table, permission.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, rolepermission.PermissionTable, rolepermission.PermissionColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *RolePermissionClient) Hooks() []Hook { + return c.hooks.RolePermission +} + +// Interceptors returns the client interceptors. +func (c *RolePermissionClient) Interceptors() []Interceptor { + return c.inters.RolePermission +} + +func (c *RolePermissionClient) mutate(ctx context.Context, m *RolePermissionMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&RolePermissionCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&RolePermissionUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&RolePermissionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&RolePermissionDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown RolePermission mutation op: %q", m.Op()) + } +} + +// UserClient is a client for the User schema. +type UserClient struct { + config +} + +// NewUserClient returns a client for the User from the given config. +func NewUserClient(c config) *UserClient { + return &UserClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `user.Hooks(f(g(h())))`. +func (c *UserClient) Use(hooks ...Hook) { + c.hooks.User = append(c.hooks.User, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `user.Intercept(f(g(h())))`. +func (c *UserClient) Intercept(interceptors ...Interceptor) { + c.inters.User = append(c.inters.User, interceptors...) +} + +// Create returns a builder for creating a User entity. +func (c *UserClient) Create() *UserCreate { + mutation := newUserMutation(c.config, OpCreate) + return &UserCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of User entities. +func (c *UserClient) CreateBulk(builders ...*UserCreate) *UserCreateBulk { + return &UserCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *UserClient) MapCreateBulk(slice any, setFunc func(*UserCreate, int)) *UserCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &UserCreateBulk{err: fmt.Errorf("calling to UserClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*UserCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &UserCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for User. +func (c *UserClient) Update() *UserUpdate { + mutation := newUserMutation(c.config, OpUpdate) + return &UserUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *UserClient) UpdateOne(_m *User) *UserUpdateOne { + mutation := newUserMutation(c.config, OpUpdateOne, withUser(_m)) + return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *UserClient) UpdateOneID(id string) *UserUpdateOne { + mutation := newUserMutation(c.config, OpUpdateOne, withUserID(id)) + return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for User. +func (c *UserClient) Delete() *UserDelete { + mutation := newUserMutation(c.config, OpDelete) + return &UserDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *UserClient) DeleteOne(_m *User) *UserDeleteOne { + return c.DeleteOneID(_m.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *UserClient) DeleteOneID(id string) *UserDeleteOne { + builder := c.Delete().Where(user.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &UserDeleteOne{builder} +} + +// Query returns a query builder for User. +func (c *UserClient) Query() *UserQuery { + return &UserQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeUser}, + inters: c.Interceptors(), + } +} + +// Get returns a User entity by its id. +func (c *UserClient) Get(ctx context.Context, id string) (*User, error) { + return c.Query().Where(user.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *UserClient) GetX(ctx context.Context, id string) *User { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryUserRoles queries the user_roles edge of a User. +func (c *UserClient) QueryUserRoles(_m *User) *UserRoleQuery { + query := (&UserRoleClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(user.Table, user.FieldID, id), + sqlgraph.To(userrole.Table, userrole.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, user.UserRolesTable, user.UserRolesColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *UserClient) Hooks() []Hook { + return c.hooks.User +} + +// Interceptors returns the client interceptors. +func (c *UserClient) Interceptors() []Interceptor { + return c.inters.User +} + +func (c *UserClient) mutate(ctx context.Context, m *UserMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&UserCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&UserUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&UserDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown User mutation op: %q", m.Op()) + } +} + +// UserRoleClient is a client for the UserRole schema. +type UserRoleClient struct { + config +} + +// NewUserRoleClient returns a client for the UserRole from the given config. +func NewUserRoleClient(c config) *UserRoleClient { + return &UserRoleClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `userrole.Hooks(f(g(h())))`. +func (c *UserRoleClient) Use(hooks ...Hook) { + c.hooks.UserRole = append(c.hooks.UserRole, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `userrole.Intercept(f(g(h())))`. +func (c *UserRoleClient) Intercept(interceptors ...Interceptor) { + c.inters.UserRole = append(c.inters.UserRole, interceptors...) +} + +// Create returns a builder for creating a UserRole entity. +func (c *UserRoleClient) Create() *UserRoleCreate { + mutation := newUserRoleMutation(c.config, OpCreate) + return &UserRoleCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of UserRole entities. +func (c *UserRoleClient) CreateBulk(builders ...*UserRoleCreate) *UserRoleCreateBulk { + return &UserRoleCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *UserRoleClient) MapCreateBulk(slice any, setFunc func(*UserRoleCreate, int)) *UserRoleCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &UserRoleCreateBulk{err: fmt.Errorf("calling to UserRoleClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*UserRoleCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &UserRoleCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for UserRole. +func (c *UserRoleClient) Update() *UserRoleUpdate { + mutation := newUserRoleMutation(c.config, OpUpdate) + return &UserRoleUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *UserRoleClient) UpdateOne(_m *UserRole) *UserRoleUpdateOne { + mutation := newUserRoleMutation(c.config, OpUpdateOne, withUserRole(_m)) + return &UserRoleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *UserRoleClient) UpdateOneID(id int) *UserRoleUpdateOne { + mutation := newUserRoleMutation(c.config, OpUpdateOne, withUserRoleID(id)) + return &UserRoleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for UserRole. +func (c *UserRoleClient) Delete() *UserRoleDelete { + mutation := newUserRoleMutation(c.config, OpDelete) + return &UserRoleDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *UserRoleClient) DeleteOne(_m *UserRole) *UserRoleDeleteOne { + return c.DeleteOneID(_m.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *UserRoleClient) DeleteOneID(id int) *UserRoleDeleteOne { + builder := c.Delete().Where(userrole.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &UserRoleDeleteOne{builder} +} + +// Query returns a query builder for UserRole. +func (c *UserRoleClient) Query() *UserRoleQuery { + return &UserRoleQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeUserRole}, + inters: c.Interceptors(), + } +} + +// Get returns a UserRole entity by its id. +func (c *UserRoleClient) Get(ctx context.Context, id int) (*UserRole, error) { + return c.Query().Where(userrole.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *UserRoleClient) GetX(ctx context.Context, id int) *UserRole { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryUser queries the user edge of a UserRole. +func (c *UserRoleClient) QueryUser(_m *UserRole) *UserQuery { + query := (&UserClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(userrole.Table, userrole.FieldID, id), + sqlgraph.To(user.Table, user.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, userrole.UserTable, userrole.UserColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// QueryRole queries the role edge of a UserRole. +func (c *UserRoleClient) QueryRole(_m *UserRole) *RoleQuery { + query := (&RoleClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := _m.ID + step := sqlgraph.NewStep( + sqlgraph.From(userrole.Table, userrole.FieldID, id), + sqlgraph.To(role.Table, role.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, userrole.RoleTable, userrole.RoleColumn), + ) + fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *UserRoleClient) Hooks() []Hook { + return c.hooks.UserRole +} + +// Interceptors returns the client interceptors. +func (c *UserRoleClient) Interceptors() []Interceptor { + return c.inters.UserRole +} + +func (c *UserRoleClient) mutate(ctx context.Context, m *UserRoleMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&UserRoleCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&UserRoleUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&UserRoleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&UserRoleDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown UserRole mutation op: %q", m.Op()) + } +} + +// hooks and interceptors per client, for fast access. +type ( + hooks struct { + AuditLog, Permission, Role, RolePermission, User, UserRole []ent.Hook + } + inters struct { + AuditLog, Permission, Role, RolePermission, User, UserRole []ent.Interceptor + } +) diff --git a/internal/ent/ent.go b/internal/ent/ent.go new file mode 100644 index 0000000..88eb1ea --- /dev/null +++ b/internal/ent/ent.go @@ -0,0 +1,618 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + "reflect" + "sync" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// ent aliases to avoid import conflicts in user's code. +type ( + Op = ent.Op + Hook = ent.Hook + Value = ent.Value + Query = ent.Query + QueryContext = ent.QueryContext + Querier = ent.Querier + QuerierFunc = ent.QuerierFunc + Interceptor = ent.Interceptor + InterceptFunc = ent.InterceptFunc + Traverser = ent.Traverser + TraverseFunc = ent.TraverseFunc + Policy = ent.Policy + Mutator = ent.Mutator + Mutation = ent.Mutation + MutateFunc = ent.MutateFunc +) + +type clientCtxKey struct{} + +// FromContext returns a Client stored inside a context, or nil if there isn't one. +func FromContext(ctx context.Context) *Client { + c, _ := ctx.Value(clientCtxKey{}).(*Client) + return c +} + +// NewContext returns a new context with the given Client attached. +func NewContext(parent context.Context, c *Client) context.Context { + return context.WithValue(parent, clientCtxKey{}, c) +} + +type txCtxKey struct{} + +// TxFromContext returns a Tx stored inside a context, or nil if there isn't one. +func TxFromContext(ctx context.Context) *Tx { + tx, _ := ctx.Value(txCtxKey{}).(*Tx) + return tx +} + +// NewTxContext returns a new context with the given Tx attached. +func NewTxContext(parent context.Context, tx *Tx) context.Context { + return context.WithValue(parent, txCtxKey{}, tx) +} + +// OrderFunc applies an ordering on the sql selector. +// Deprecated: Use Asc/Desc functions or the package builders instead. +type OrderFunc func(*sql.Selector) + +var ( + initCheck sync.Once + columnCheck sql.ColumnCheck +) + +// checkColumn checks if the column exists in the given table. +func checkColumn(t, c string) error { + initCheck.Do(func() { + columnCheck = sql.NewColumnCheck(map[string]func(string) bool{ + auditlog.Table: auditlog.ValidColumn, + permission.Table: permission.ValidColumn, + role.Table: role.ValidColumn, + rolepermission.Table: rolepermission.ValidColumn, + user.Table: user.ValidColumn, + userrole.Table: userrole.ValidColumn, + }) + }) + return columnCheck(t, c) +} + +// Asc applies the given fields in ASC order. +func Asc(fields ...string) func(*sql.Selector) { + return func(s *sql.Selector) { + for _, f := range fields { + if err := checkColumn(s.TableName(), f); err != nil { + s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)}) + } + s.OrderBy(sql.Asc(s.C(f))) + } + } +} + +// Desc applies the given fields in DESC order. +func Desc(fields ...string) func(*sql.Selector) { + return func(s *sql.Selector) { + for _, f := range fields { + if err := checkColumn(s.TableName(), f); err != nil { + s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)}) + } + s.OrderBy(sql.Desc(s.C(f))) + } + } +} + +// AggregateFunc applies an aggregation step on the group-by traversal/selector. +type AggregateFunc func(*sql.Selector) string + +// As is a pseudo aggregation function for renaming another other functions with custom names. For example: +// +// GroupBy(field1, field2). +// Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")). +// Scan(ctx, &v) +func As(fn AggregateFunc, end string) AggregateFunc { + return func(s *sql.Selector) string { + return sql.As(fn(s), end) + } +} + +// Count applies the "count" aggregation function on each group. +func Count() AggregateFunc { + return func(s *sql.Selector) string { + return sql.Count("*") + } +} + +// Max applies the "max" aggregation function on the given field of each group. +func Max(field string) AggregateFunc { + return func(s *sql.Selector) string { + if err := checkColumn(s.TableName(), field); err != nil { + s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) + return "" + } + return sql.Max(s.C(field)) + } +} + +// Mean applies the "mean" aggregation function on the given field of each group. +func Mean(field string) AggregateFunc { + return func(s *sql.Selector) string { + if err := checkColumn(s.TableName(), field); err != nil { + s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) + return "" + } + return sql.Avg(s.C(field)) + } +} + +// Min applies the "min" aggregation function on the given field of each group. +func Min(field string) AggregateFunc { + return func(s *sql.Selector) string { + if err := checkColumn(s.TableName(), field); err != nil { + s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) + return "" + } + return sql.Min(s.C(field)) + } +} + +// Sum applies the "sum" aggregation function on the given field of each group. +func Sum(field string) AggregateFunc { + return func(s *sql.Selector) string { + if err := checkColumn(s.TableName(), field); err != nil { + s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) + return "" + } + return sql.Sum(s.C(field)) + } +} + +// ValidationError returns when validating a field or edge fails. +type ValidationError struct { + Name string // Field or edge name. + err error +} + +// Error implements the error interface. +func (e *ValidationError) Error() string { + return e.err.Error() +} + +// Unwrap implements the errors.Wrapper interface. +func (e *ValidationError) Unwrap() error { + return e.err +} + +// IsValidationError returns a boolean indicating whether the error is a validation error. +func IsValidationError(err error) bool { + if err == nil { + return false + } + var e *ValidationError + return errors.As(err, &e) +} + +// NotFoundError returns when trying to fetch a specific entity and it was not found in the database. +type NotFoundError struct { + label string +} + +// Error implements the error interface. +func (e *NotFoundError) Error() string { + return "ent: " + e.label + " not found" +} + +// IsNotFound returns a boolean indicating whether the error is a not found error. +func IsNotFound(err error) bool { + if err == nil { + return false + } + var e *NotFoundError + return errors.As(err, &e) +} + +// MaskNotFound masks not found error. +func MaskNotFound(err error) error { + if IsNotFound(err) { + return nil + } + return err +} + +// NotSingularError returns when trying to fetch a singular entity and more then one was found in the database. +type NotSingularError struct { + label string +} + +// Error implements the error interface. +func (e *NotSingularError) Error() string { + return "ent: " + e.label + " not singular" +} + +// IsNotSingular returns a boolean indicating whether the error is a not singular error. +func IsNotSingular(err error) bool { + if err == nil { + return false + } + var e *NotSingularError + return errors.As(err, &e) +} + +// NotLoadedError returns when trying to get a node that was not loaded by the query. +type NotLoadedError struct { + edge string +} + +// Error implements the error interface. +func (e *NotLoadedError) Error() string { + return "ent: " + e.edge + " edge was not loaded" +} + +// IsNotLoaded returns a boolean indicating whether the error is a not loaded error. +func IsNotLoaded(err error) bool { + if err == nil { + return false + } + var e *NotLoadedError + return errors.As(err, &e) +} + +// ConstraintError returns when trying to create/update one or more entities and +// one or more of their constraints failed. For example, violation of edge or +// field uniqueness. +type ConstraintError struct { + msg string + wrap error +} + +// Error implements the error interface. +func (e ConstraintError) Error() string { + return "ent: constraint failed: " + e.msg +} + +// Unwrap implements the errors.Wrapper interface. +func (e *ConstraintError) Unwrap() error { + return e.wrap +} + +// IsConstraintError returns a boolean indicating whether the error is a constraint failure. +func IsConstraintError(err error) bool { + if err == nil { + return false + } + var e *ConstraintError + return errors.As(err, &e) +} + +// selector embedded by the different Select/GroupBy builders. +type selector struct { + label string + flds *[]string + fns []AggregateFunc + scan func(context.Context, any) error +} + +// ScanX is like Scan, but panics if an error occurs. +func (s *selector) ScanX(ctx context.Context, v any) { + if err := s.scan(ctx, v); err != nil { + panic(err) + } +} + +// Strings returns list of strings from a selector. It is only allowed when selecting one field. +func (s *selector) Strings(ctx context.Context) ([]string, error) { + if len(*s.flds) > 1 { + return nil, errors.New("ent: Strings is not achievable when selecting more than 1 field") + } + var v []string + if err := s.scan(ctx, &v); err != nil { + return nil, err + } + return v, nil +} + +// StringsX is like Strings, but panics if an error occurs. +func (s *selector) StringsX(ctx context.Context) []string { + v, err := s.Strings(ctx) + if err != nil { + panic(err) + } + return v +} + +// String returns a single string from a selector. It is only allowed when selecting one field. +func (s *selector) String(ctx context.Context) (_ string, err error) { + var v []string + if v, err = s.Strings(ctx); err != nil { + return + } + switch len(v) { + case 1: + return v[0], nil + case 0: + err = &NotFoundError{s.label} + default: + err = fmt.Errorf("ent: Strings returned %d results when one was expected", len(v)) + } + return +} + +// StringX is like String, but panics if an error occurs. +func (s *selector) StringX(ctx context.Context) string { + v, err := s.String(ctx) + if err != nil { + panic(err) + } + return v +} + +// Ints returns list of ints from a selector. It is only allowed when selecting one field. +func (s *selector) Ints(ctx context.Context) ([]int, error) { + if len(*s.flds) > 1 { + return nil, errors.New("ent: Ints is not achievable when selecting more than 1 field") + } + var v []int + if err := s.scan(ctx, &v); err != nil { + return nil, err + } + return v, nil +} + +// IntsX is like Ints, but panics if an error occurs. +func (s *selector) IntsX(ctx context.Context) []int { + v, err := s.Ints(ctx) + if err != nil { + panic(err) + } + return v +} + +// Int returns a single int from a selector. It is only allowed when selecting one field. +func (s *selector) Int(ctx context.Context) (_ int, err error) { + var v []int + if v, err = s.Ints(ctx); err != nil { + return + } + switch len(v) { + case 1: + return v[0], nil + case 0: + err = &NotFoundError{s.label} + default: + err = fmt.Errorf("ent: Ints returned %d results when one was expected", len(v)) + } + return +} + +// IntX is like Int, but panics if an error occurs. +func (s *selector) IntX(ctx context.Context) int { + v, err := s.Int(ctx) + if err != nil { + panic(err) + } + return v +} + +// Float64s returns list of float64s from a selector. It is only allowed when selecting one field. +func (s *selector) Float64s(ctx context.Context) ([]float64, error) { + if len(*s.flds) > 1 { + return nil, errors.New("ent: Float64s is not achievable when selecting more than 1 field") + } + var v []float64 + if err := s.scan(ctx, &v); err != nil { + return nil, err + } + return v, nil +} + +// Float64sX is like Float64s, but panics if an error occurs. +func (s *selector) Float64sX(ctx context.Context) []float64 { + v, err := s.Float64s(ctx) + if err != nil { + panic(err) + } + return v +} + +// Float64 returns a single float64 from a selector. It is only allowed when selecting one field. +func (s *selector) Float64(ctx context.Context) (_ float64, err error) { + var v []float64 + if v, err = s.Float64s(ctx); err != nil { + return + } + switch len(v) { + case 1: + return v[0], nil + case 0: + err = &NotFoundError{s.label} + default: + err = fmt.Errorf("ent: Float64s returned %d results when one was expected", len(v)) + } + return +} + +// Float64X is like Float64, but panics if an error occurs. +func (s *selector) Float64X(ctx context.Context) float64 { + v, err := s.Float64(ctx) + if err != nil { + panic(err) + } + return v +} + +// Bools returns list of bools from a selector. It is only allowed when selecting one field. +func (s *selector) Bools(ctx context.Context) ([]bool, error) { + if len(*s.flds) > 1 { + return nil, errors.New("ent: Bools is not achievable when selecting more than 1 field") + } + var v []bool + if err := s.scan(ctx, &v); err != nil { + return nil, err + } + return v, nil +} + +// BoolsX is like Bools, but panics if an error occurs. +func (s *selector) BoolsX(ctx context.Context) []bool { + v, err := s.Bools(ctx) + if err != nil { + panic(err) + } + return v +} + +// Bool returns a single bool from a selector. It is only allowed when selecting one field. +func (s *selector) Bool(ctx context.Context) (_ bool, err error) { + var v []bool + if v, err = s.Bools(ctx); err != nil { + return + } + switch len(v) { + case 1: + return v[0], nil + case 0: + err = &NotFoundError{s.label} + default: + err = fmt.Errorf("ent: Bools returned %d results when one was expected", len(v)) + } + return +} + +// BoolX is like Bool, but panics if an error occurs. +func (s *selector) BoolX(ctx context.Context) bool { + v, err := s.Bool(ctx) + if err != nil { + panic(err) + } + return v +} + +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := any(m).(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + +// setContextOp returns a new context with the given QueryContext attached (including its op) in case it does not exist. +func setContextOp(ctx context.Context, qc *QueryContext, op string) context.Context { + if ent.QueryFromContext(ctx) == nil { + qc.Op = op + ctx = ent.NewQueryContext(ctx, qc) + } + return ctx +} + +func querierAll[V Value, Q interface { + sqlAll(context.Context, ...queryHook) (V, error) +}]() Querier { + return QuerierFunc(func(ctx context.Context, q Query) (Value, error) { + query, ok := q.(Q) + if !ok { + return nil, fmt.Errorf("unexpected query type %T", q) + } + return query.sqlAll(ctx) + }) +} + +func querierCount[Q interface { + sqlCount(context.Context) (int, error) +}]() Querier { + return QuerierFunc(func(ctx context.Context, q Query) (Value, error) { + query, ok := q.(Q) + if !ok { + return nil, fmt.Errorf("unexpected query type %T", q) + } + return query.sqlCount(ctx) + }) +} + +func withInterceptors[V Value](ctx context.Context, q Query, qr Querier, inters []Interceptor) (v V, err error) { + for i := len(inters) - 1; i >= 0; i-- { + qr = inters[i].Intercept(qr) + } + rv, err := qr.Query(ctx, q) + if err != nil { + return v, err + } + vt, ok := rv.(V) + if !ok { + return v, fmt.Errorf("unexpected type %T returned from %T. expected type: %T", vt, q, v) + } + return vt, nil +} + +func scanWithInterceptors[Q1 ent.Query, Q2 interface { + sqlScan(context.Context, Q1, any) error +}](ctx context.Context, rootQuery Q1, selectOrGroup Q2, inters []Interceptor, v any) error { + rv := reflect.ValueOf(v) + var qr Querier = QuerierFunc(func(ctx context.Context, q Query) (Value, error) { + query, ok := q.(Q1) + if !ok { + return nil, fmt.Errorf("unexpected query type %T", q) + } + if err := selectOrGroup.sqlScan(ctx, query, v); err != nil { + return nil, err + } + if k := rv.Kind(); k == reflect.Pointer && rv.Elem().CanInterface() { + return rv.Elem().Interface(), nil + } + return v, nil + }) + for i := len(inters) - 1; i >= 0; i-- { + qr = inters[i].Intercept(qr) + } + vv, err := qr.Query(ctx, rootQuery) + if err != nil { + return err + } + switch rv2 := reflect.ValueOf(vv); { + case rv.IsNil(), rv2.IsNil(), rv.Kind() != reflect.Pointer: + case rv.Type() == rv2.Type(): + rv.Elem().Set(rv2.Elem()) + case rv.Elem().Type() == rv2.Type(): + rv.Elem().Set(rv2) + } + return nil +} + +// queryHook describes an internal hook for the different sqlAll methods. +type queryHook func(context.Context, *sqlgraph.QuerySpec) diff --git a/internal/ent/enttest/enttest.go b/internal/ent/enttest/enttest.go new file mode 100644 index 0000000..7e3c6e6 --- /dev/null +++ b/internal/ent/enttest/enttest.go @@ -0,0 +1,84 @@ +// Code generated by ent, DO NOT EDIT. + +package enttest + +import ( + "context" + + "git.dcentral.systems/toolz/goplt/internal/ent" + // required by schema hooks. + _ "git.dcentral.systems/toolz/goplt/internal/ent/runtime" + + "entgo.io/ent/dialect/sql/schema" + "git.dcentral.systems/toolz/goplt/internal/ent/migrate" +) + +type ( + // TestingT is the interface that is shared between + // testing.T and testing.B and used by enttest. + TestingT interface { + FailNow() + Error(...any) + } + + // Option configures client creation. + Option func(*options) + + options struct { + opts []ent.Option + migrateOpts []schema.MigrateOption + } +) + +// WithOptions forwards options to client creation. +func WithOptions(opts ...ent.Option) Option { + return func(o *options) { + o.opts = append(o.opts, opts...) + } +} + +// WithMigrateOptions forwards options to auto migration. +func WithMigrateOptions(opts ...schema.MigrateOption) Option { + return func(o *options) { + o.migrateOpts = append(o.migrateOpts, opts...) + } +} + +func newOptions(opts []Option) *options { + o := &options{} + for _, opt := range opts { + opt(o) + } + return o +} + +// Open calls ent.Open and auto-run migration. +func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Client { + o := newOptions(opts) + c, err := ent.Open(driverName, dataSourceName, o.opts...) + if err != nil { + t.Error(err) + t.FailNow() + } + migrateSchema(t, c, o) + return c +} + +// NewClient calls ent.NewClient and auto-run migration. +func NewClient(t TestingT, opts ...Option) *ent.Client { + o := newOptions(opts) + c := ent.NewClient(o.opts...) + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { + t.Error(err) + t.FailNow() + } +} diff --git a/internal/ent/hook/hook.go b/internal/ent/hook/hook.go new file mode 100644 index 0000000..3fc0288 --- /dev/null +++ b/internal/ent/hook/hook.go @@ -0,0 +1,259 @@ +// Code generated by ent, DO NOT EDIT. + +package hook + +import ( + "context" + "fmt" + + "git.dcentral.systems/toolz/goplt/internal/ent" +) + +// The AuditLogFunc type is an adapter to allow the use of ordinary +// function as AuditLog mutator. +type AuditLogFunc func(context.Context, *ent.AuditLogMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f AuditLogFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.AuditLogMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AuditLogMutation", m) +} + +// The PermissionFunc type is an adapter to allow the use of ordinary +// function as Permission mutator. +type PermissionFunc func(context.Context, *ent.PermissionMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f PermissionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.PermissionMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PermissionMutation", m) +} + +// The RoleFunc type is an adapter to allow the use of ordinary +// function as Role mutator. +type RoleFunc func(context.Context, *ent.RoleMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f RoleFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.RoleMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.RoleMutation", m) +} + +// The RolePermissionFunc type is an adapter to allow the use of ordinary +// function as RolePermission mutator. +type RolePermissionFunc func(context.Context, *ent.RolePermissionMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f RolePermissionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.RolePermissionMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.RolePermissionMutation", 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) + +// Mutate calls f(ctx, m). +func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.UserMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m) +} + +// The UserRoleFunc type is an adapter to allow the use of ordinary +// function as UserRole mutator. +type UserRoleFunc func(context.Context, *ent.UserRoleMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f UserRoleFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.UserRoleMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserRoleMutation", m) +} + +// Condition is a hook condition function. +type Condition func(context.Context, ent.Mutation) bool + +// And groups conditions with the AND operator. +func And(first, second Condition, rest ...Condition) Condition { + return func(ctx context.Context, m ent.Mutation) bool { + if !first(ctx, m) || !second(ctx, m) { + return false + } + for _, cond := range rest { + if !cond(ctx, m) { + return false + } + } + return true + } +} + +// Or groups conditions with the OR operator. +func Or(first, second Condition, rest ...Condition) Condition { + return func(ctx context.Context, m ent.Mutation) bool { + if first(ctx, m) || second(ctx, m) { + return true + } + for _, cond := range rest { + if cond(ctx, m) { + return true + } + } + return false + } +} + +// Not negates a given condition. +func Not(cond Condition) Condition { + return func(ctx context.Context, m ent.Mutation) bool { + return !cond(ctx, m) + } +} + +// HasOp is a condition testing mutation operation. +func HasOp(op ent.Op) Condition { + return func(_ context.Context, m ent.Mutation) bool { + return m.Op().Is(op) + } +} + +// HasAddedFields is a condition validating `.AddedField` on fields. +func HasAddedFields(field string, fields ...string) Condition { + return func(_ context.Context, m ent.Mutation) bool { + if _, exists := m.AddedField(field); !exists { + return false + } + for _, field := range fields { + if _, exists := m.AddedField(field); !exists { + return false + } + } + return true + } +} + +// HasClearedFields is a condition validating `.FieldCleared` on fields. +func HasClearedFields(field string, fields ...string) Condition { + return func(_ context.Context, m ent.Mutation) bool { + if exists := m.FieldCleared(field); !exists { + return false + } + for _, field := range fields { + if exists := m.FieldCleared(field); !exists { + return false + } + } + return true + } +} + +// HasFields is a condition validating `.Field` on fields. +func HasFields(field string, fields ...string) Condition { + return func(_ context.Context, m ent.Mutation) bool { + if _, exists := m.Field(field); !exists { + return false + } + for _, field := range fields { + if _, exists := m.Field(field); !exists { + return false + } + } + return true + } +} + +// If executes the given hook under condition. +// +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +func If(hk ent.Hook, cond Condition) ent.Hook { + return func(next ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if cond(ctx, m) { + return hk(next).Mutate(ctx, m) + } + return next.Mutate(ctx, m) + }) + } +} + +// On executes the given hook only for the given operation. +// +// hook.On(Log, ent.Delete|ent.Create) +func On(hk ent.Hook, op ent.Op) ent.Hook { + return If(hk, HasOp(op)) +} + +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return If(hk, Not(HasOp(op))) +} + +// FixedError is a hook returning a fixed error. +func FixedError(err error) ent.Hook { + return func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) { + return nil, err + }) + } +} + +// Reject returns a hook that rejects all operations that match op. +// +// func (T) Hooks() []ent.Hook { +// return []ent.Hook{ +// Reject(ent.Delete|ent.Update), +// } +// } +func Reject(op ent.Op) ent.Hook { + hk := FixedError(fmt.Errorf("%s operation is not allowed", op)) + return On(hk, op) +} + +// Chain acts as a list of hooks and is effectively immutable. +// Once created, it will always hold the same set of hooks in the same order. +type Chain struct { + hooks []ent.Hook +} + +// NewChain creates a new chain of hooks. +func NewChain(hooks ...ent.Hook) Chain { + return Chain{append([]ent.Hook(nil), hooks...)} +} + +// Hook chains the list of hooks and returns the final hook. +func (c Chain) Hook() ent.Hook { + return func(mutator ent.Mutator) ent.Mutator { + for i := len(c.hooks) - 1; i >= 0; i-- { + mutator = c.hooks[i](mutator) + } + return mutator + } +} + +// Append extends a chain, adding the specified hook +// as the last ones in the mutation flow. +func (c Chain) Append(hooks ...ent.Hook) Chain { + newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks)) + newHooks = append(newHooks, c.hooks...) + newHooks = append(newHooks, hooks...) + return Chain{newHooks} +} + +// Extend extends a chain, adding the specified chain +// as the last ones in the mutation flow. +func (c Chain) Extend(chain Chain) Chain { + return c.Append(chain.hooks...) +} diff --git a/internal/ent/migrate/migrate.go b/internal/ent/migrate/migrate.go new file mode 100644 index 0000000..1956a6b --- /dev/null +++ b/internal/ent/migrate/migrate.go @@ -0,0 +1,64 @@ +// Code generated by ent, DO NOT EDIT. + +package migrate + +import ( + "context" + "fmt" + "io" + + "entgo.io/ent/dialect" + "entgo.io/ent/dialect/sql/schema" +) + +var ( + // WithGlobalUniqueID sets the universal ids options to the migration. + // If this option is enabled, ent migration will allocate a 1<<32 range + // for the ids of each entity (table). + // Note that this option cannot be applied on tables that already exist. + WithGlobalUniqueID = schema.WithGlobalUniqueID + // WithDropColumn sets the drop column option to the migration. + // If this option is enabled, ent migration will drop old columns + // that were used for both fields and edges. This defaults to false. + WithDropColumn = schema.WithDropColumn + // WithDropIndex sets the drop index option to the migration. + // If this option is enabled, ent migration will drop old indexes + // that were defined in the schema. This defaults to false. + // Note that unique constraints are defined using `UNIQUE INDEX`, + // and therefore, it's recommended to enable this option to get more + // flexibility in the schema changes. + WithDropIndex = schema.WithDropIndex + // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. + WithForeignKeys = schema.WithForeignKeys +) + +// Schema is the API for creating, migrating and dropping a schema. +type Schema struct { + drv dialect.Driver +} + +// NewSchema creates a new schema client. +func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } + +// Create creates all schema resources. +func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { + migrate, err := schema.NewMigrate(s.drv, opts...) + if err != nil { + return fmt.Errorf("ent/migrate: %w", err) + } + return migrate.Create(ctx, tables...) +} + +// WriteTo writes the schema changes to w instead of running them against the database. +// +// if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil { +// log.Fatal(err) +// } +func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) +} diff --git a/internal/ent/migrate/schema.go b/internal/ent/migrate/schema.go new file mode 100644 index 0000000..e9fe845 --- /dev/null +++ b/internal/ent/migrate/schema.go @@ -0,0 +1,187 @@ +// Code generated by ent, DO NOT EDIT. + +package migrate + +import ( + "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/schema/field" +) + +var ( + // AuditLogsColumns holds the columns for the "audit_logs" table. + AuditLogsColumns = []*schema.Column{ + {Name: "id", Type: field.TypeString, Unique: true}, + {Name: "actor_id", Type: field.TypeString}, + {Name: "action", Type: field.TypeString}, + {Name: "target_id", Type: field.TypeString, Nullable: true}, + {Name: "metadata", Type: field.TypeJSON, Nullable: true}, + {Name: "timestamp", Type: field.TypeTime}, + } + // AuditLogsTable holds the schema information for the "audit_logs" table. + AuditLogsTable = &schema.Table{ + Name: "audit_logs", + Columns: AuditLogsColumns, + PrimaryKey: []*schema.Column{AuditLogsColumns[0]}, + Indexes: []*schema.Index{ + { + Name: "auditlog_actor_id", + Unique: false, + Columns: []*schema.Column{AuditLogsColumns[1]}, + }, + { + Name: "auditlog_target_id", + Unique: false, + Columns: []*schema.Column{AuditLogsColumns[3]}, + }, + { + Name: "auditlog_timestamp", + Unique: false, + Columns: []*schema.Column{AuditLogsColumns[5]}, + }, + { + Name: "auditlog_action", + Unique: false, + Columns: []*schema.Column{AuditLogsColumns[2]}, + }, + }, + } + // PermissionsColumns holds the columns for the "permissions" table. + PermissionsColumns = []*schema.Column{ + {Name: "id", Type: field.TypeString, Unique: true}, + {Name: "name", Type: field.TypeString, Unique: true}, + } + // PermissionsTable holds the schema information for the "permissions" table. + PermissionsTable = &schema.Table{ + Name: "permissions", + Columns: PermissionsColumns, + PrimaryKey: []*schema.Column{PermissionsColumns[0]}, + } + // RolesColumns holds the columns for the "roles" table. + RolesColumns = []*schema.Column{ + {Name: "id", Type: field.TypeString, Unique: true}, + {Name: "name", Type: field.TypeString, Unique: true}, + {Name: "description", Type: field.TypeString, Nullable: true}, + {Name: "created_at", Type: field.TypeTime}, + } + // RolesTable holds the schema information for the "roles" table. + RolesTable = &schema.Table{ + Name: "roles", + Columns: RolesColumns, + PrimaryKey: []*schema.Column{RolesColumns[0]}, + } + // RolePermissionsColumns holds the columns for the "role_permissions" table. + RolePermissionsColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "permission_role_permissions", Type: field.TypeString, Nullable: true}, + {Name: "role_role_permissions", Type: field.TypeString, Nullable: true}, + {Name: "role_id", Type: field.TypeString}, + {Name: "permission_id", Type: field.TypeString}, + } + // RolePermissionsTable holds the schema information for the "role_permissions" table. + RolePermissionsTable = &schema.Table{ + Name: "role_permissions", + Columns: RolePermissionsColumns, + PrimaryKey: []*schema.Column{RolePermissionsColumns[0]}, + ForeignKeys: []*schema.ForeignKey{ + { + Symbol: "role_permissions_permissions_role_permissions", + Columns: []*schema.Column{RolePermissionsColumns[1]}, + RefColumns: []*schema.Column{PermissionsColumns[0]}, + OnDelete: schema.SetNull, + }, + { + Symbol: "role_permissions_roles_role_permissions", + Columns: []*schema.Column{RolePermissionsColumns[2]}, + RefColumns: []*schema.Column{RolesColumns[0]}, + OnDelete: schema.SetNull, + }, + { + Symbol: "role_permissions_roles_role", + Columns: []*schema.Column{RolePermissionsColumns[3]}, + RefColumns: []*schema.Column{RolesColumns[0]}, + OnDelete: schema.NoAction, + }, + { + Symbol: "role_permissions_permissions_permission", + Columns: []*schema.Column{RolePermissionsColumns[4]}, + RefColumns: []*schema.Column{PermissionsColumns[0]}, + OnDelete: schema.NoAction, + }, + }, + } + // UsersColumns holds the columns for the "users" table. + UsersColumns = []*schema.Column{ + {Name: "id", Type: field.TypeString, Unique: true}, + {Name: "email", Type: field.TypeString, Unique: true}, + {Name: "password_hash", Type: field.TypeString}, + {Name: "verified", Type: field.TypeBool, Default: false}, + {Name: "created_at", Type: field.TypeTime}, + {Name: "updated_at", Type: field.TypeTime}, + } + // UsersTable holds the schema information for the "users" table. + UsersTable = &schema.Table{ + Name: "users", + Columns: UsersColumns, + PrimaryKey: []*schema.Column{UsersColumns[0]}, + } + // UserRolesColumns holds the columns for the "user_roles" table. + UserRolesColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "role_user_roles", Type: field.TypeString, Nullable: true}, + {Name: "user_user_roles", Type: field.TypeString, Nullable: true}, + {Name: "user_id", Type: field.TypeString}, + {Name: "role_id", Type: field.TypeString}, + } + // UserRolesTable holds the schema information for the "user_roles" table. + UserRolesTable = &schema.Table{ + Name: "user_roles", + Columns: UserRolesColumns, + PrimaryKey: []*schema.Column{UserRolesColumns[0]}, + ForeignKeys: []*schema.ForeignKey{ + { + Symbol: "user_roles_roles_user_roles", + Columns: []*schema.Column{UserRolesColumns[1]}, + RefColumns: []*schema.Column{RolesColumns[0]}, + OnDelete: schema.SetNull, + }, + { + Symbol: "user_roles_users_user_roles", + Columns: []*schema.Column{UserRolesColumns[2]}, + RefColumns: []*schema.Column{UsersColumns[0]}, + OnDelete: schema.SetNull, + }, + { + Symbol: "user_roles_users_user", + Columns: []*schema.Column{UserRolesColumns[3]}, + RefColumns: []*schema.Column{UsersColumns[0]}, + OnDelete: schema.NoAction, + }, + { + Symbol: "user_roles_roles_role", + Columns: []*schema.Column{UserRolesColumns[4]}, + RefColumns: []*schema.Column{RolesColumns[0]}, + OnDelete: schema.NoAction, + }, + }, + } + // Tables holds all the tables in the schema. + Tables = []*schema.Table{ + AuditLogsTable, + PermissionsTable, + RolesTable, + RolePermissionsTable, + UsersTable, + UserRolesTable, + } +) + +func init() { + RolePermissionsTable.ForeignKeys[0].RefTable = PermissionsTable + RolePermissionsTable.ForeignKeys[1].RefTable = RolesTable + RolePermissionsTable.ForeignKeys[2].RefTable = RolesTable + RolePermissionsTable.ForeignKeys[3].RefTable = PermissionsTable + UserRolesTable.ForeignKeys[0].RefTable = RolesTable + UserRolesTable.ForeignKeys[1].RefTable = UsersTable + UserRolesTable.ForeignKeys[2].RefTable = UsersTable + UserRolesTable.ForeignKeys[3].RefTable = RolesTable +} diff --git a/internal/ent/mutation.go b/internal/ent/mutation.go new file mode 100644 index 0000000..4169d02 --- /dev/null +++ b/internal/ent/mutation.go @@ -0,0 +1,3291 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + "sync" + "time" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +const ( + // Operation types. + OpCreate = ent.OpCreate + OpDelete = ent.OpDelete + OpDeleteOne = ent.OpDeleteOne + OpUpdate = ent.OpUpdate + OpUpdateOne = ent.OpUpdateOne + + // Node types. + TypeAuditLog = "AuditLog" + TypePermission = "Permission" + TypeRole = "Role" + TypeRolePermission = "RolePermission" + TypeUser = "User" + TypeUserRole = "UserRole" +) + +// AuditLogMutation represents an operation that mutates the AuditLog nodes in the graph. +type AuditLogMutation struct { + config + op Op + typ string + id *string + actor_id *string + action *string + target_id *string + metadata *map[string]interface{} + timestamp *time.Time + clearedFields map[string]struct{} + done bool + oldValue func(context.Context) (*AuditLog, error) + predicates []predicate.AuditLog +} + +var _ ent.Mutation = (*AuditLogMutation)(nil) + +// auditlogOption allows management of the mutation configuration using functional options. +type auditlogOption func(*AuditLogMutation) + +// newAuditLogMutation creates new mutation for the AuditLog entity. +func newAuditLogMutation(c config, op Op, opts ...auditlogOption) *AuditLogMutation { + m := &AuditLogMutation{ + config: c, + op: op, + typ: TypeAuditLog, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withAuditLogID sets the ID field of the mutation. +func withAuditLogID(id string) auditlogOption { + return func(m *AuditLogMutation) { + var ( + err error + once sync.Once + value *AuditLog + ) + m.oldValue = func(ctx context.Context) (*AuditLog, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().AuditLog.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withAuditLog sets the old AuditLog of the mutation. +func withAuditLog(node *AuditLog) auditlogOption { + return func(m *AuditLogMutation) { + m.oldValue = func(context.Context) (*AuditLog, 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 AuditLogMutation) 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 AuditLogMutation) 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 AuditLog entities. +func (m *AuditLogMutation) SetID(id string) { + 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 *AuditLogMutation) ID() (id string, 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 *AuditLogMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().AuditLog.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetActorID sets the "actor_id" field. +func (m *AuditLogMutation) SetActorID(s string) { + m.actor_id = &s +} + +// ActorID returns the value of the "actor_id" field in the mutation. +func (m *AuditLogMutation) ActorID() (r string, exists bool) { + v := m.actor_id + if v == nil { + return + } + return *v, true +} + +// OldActorID returns the old "actor_id" field's value of the AuditLog entity. +// If the AuditLog 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 *AuditLogMutation) OldActorID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldActorID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldActorID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldActorID: %w", err) + } + return oldValue.ActorID, nil +} + +// ResetActorID resets all changes to the "actor_id" field. +func (m *AuditLogMutation) ResetActorID() { + m.actor_id = nil +} + +// SetAction sets the "action" field. +func (m *AuditLogMutation) SetAction(s string) { + m.action = &s +} + +// Action returns the value of the "action" field in the mutation. +func (m *AuditLogMutation) Action() (r string, exists bool) { + v := m.action + if v == nil { + return + } + return *v, true +} + +// OldAction returns the old "action" field's value of the AuditLog entity. +// If the AuditLog 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 *AuditLogMutation) OldAction(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldAction is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldAction requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldAction: %w", err) + } + return oldValue.Action, nil +} + +// ResetAction resets all changes to the "action" field. +func (m *AuditLogMutation) ResetAction() { + m.action = nil +} + +// SetTargetID sets the "target_id" field. +func (m *AuditLogMutation) SetTargetID(s string) { + m.target_id = &s +} + +// TargetID returns the value of the "target_id" field in the mutation. +func (m *AuditLogMutation) TargetID() (r string, exists bool) { + v := m.target_id + if v == nil { + return + } + return *v, true +} + +// OldTargetID returns the old "target_id" field's value of the AuditLog entity. +// If the AuditLog 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 *AuditLogMutation) OldTargetID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldTargetID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldTargetID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldTargetID: %w", err) + } + return oldValue.TargetID, nil +} + +// ClearTargetID clears the value of the "target_id" field. +func (m *AuditLogMutation) ClearTargetID() { + m.target_id = nil + m.clearedFields[auditlog.FieldTargetID] = struct{}{} +} + +// TargetIDCleared returns if the "target_id" field was cleared in this mutation. +func (m *AuditLogMutation) TargetIDCleared() bool { + _, ok := m.clearedFields[auditlog.FieldTargetID] + return ok +} + +// ResetTargetID resets all changes to the "target_id" field. +func (m *AuditLogMutation) ResetTargetID() { + m.target_id = nil + delete(m.clearedFields, auditlog.FieldTargetID) +} + +// SetMetadata sets the "metadata" field. +func (m *AuditLogMutation) SetMetadata(value map[string]interface{}) { + m.metadata = &value +} + +// Metadata returns the value of the "metadata" field in the mutation. +func (m *AuditLogMutation) Metadata() (r map[string]interface{}, exists bool) { + v := m.metadata + if v == nil { + return + } + return *v, true +} + +// OldMetadata returns the old "metadata" field's value of the AuditLog entity. +// If the AuditLog 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 *AuditLogMutation) OldMetadata(ctx context.Context) (v map[string]interface{}, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldMetadata is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldMetadata requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldMetadata: %w", err) + } + return oldValue.Metadata, nil +} + +// ClearMetadata clears the value of the "metadata" field. +func (m *AuditLogMutation) ClearMetadata() { + m.metadata = nil + m.clearedFields[auditlog.FieldMetadata] = struct{}{} +} + +// MetadataCleared returns if the "metadata" field was cleared in this mutation. +func (m *AuditLogMutation) MetadataCleared() bool { + _, ok := m.clearedFields[auditlog.FieldMetadata] + return ok +} + +// ResetMetadata resets all changes to the "metadata" field. +func (m *AuditLogMutation) ResetMetadata() { + m.metadata = nil + delete(m.clearedFields, auditlog.FieldMetadata) +} + +// SetTimestamp sets the "timestamp" field. +func (m *AuditLogMutation) SetTimestamp(t time.Time) { + m.timestamp = &t +} + +// Timestamp returns the value of the "timestamp" field in the mutation. +func (m *AuditLogMutation) Timestamp() (r time.Time, exists bool) { + v := m.timestamp + if v == nil { + return + } + return *v, true +} + +// OldTimestamp returns the old "timestamp" field's value of the AuditLog entity. +// If the AuditLog 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 *AuditLogMutation) OldTimestamp(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldTimestamp is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldTimestamp requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldTimestamp: %w", err) + } + return oldValue.Timestamp, nil +} + +// ResetTimestamp resets all changes to the "timestamp" field. +func (m *AuditLogMutation) ResetTimestamp() { + m.timestamp = nil +} + +// Where appends a list predicates to the AuditLogMutation builder. +func (m *AuditLogMutation) Where(ps ...predicate.AuditLog) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the AuditLogMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *AuditLogMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.AuditLog, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *AuditLogMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *AuditLogMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (AuditLog). +func (m *AuditLogMutation) 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 *AuditLogMutation) Fields() []string { + fields := make([]string, 0, 5) + if m.actor_id != nil { + fields = append(fields, auditlog.FieldActorID) + } + if m.action != nil { + fields = append(fields, auditlog.FieldAction) + } + if m.target_id != nil { + fields = append(fields, auditlog.FieldTargetID) + } + if m.metadata != nil { + fields = append(fields, auditlog.FieldMetadata) + } + if m.timestamp != nil { + fields = append(fields, auditlog.FieldTimestamp) + } + 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 *AuditLogMutation) Field(name string) (ent.Value, bool) { + switch name { + case auditlog.FieldActorID: + return m.ActorID() + case auditlog.FieldAction: + return m.Action() + case auditlog.FieldTargetID: + return m.TargetID() + case auditlog.FieldMetadata: + return m.Metadata() + case auditlog.FieldTimestamp: + return m.Timestamp() + } + 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 *AuditLogMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case auditlog.FieldActorID: + return m.OldActorID(ctx) + case auditlog.FieldAction: + return m.OldAction(ctx) + case auditlog.FieldTargetID: + return m.OldTargetID(ctx) + case auditlog.FieldMetadata: + return m.OldMetadata(ctx) + case auditlog.FieldTimestamp: + return m.OldTimestamp(ctx) + } + return nil, fmt.Errorf("unknown AuditLog 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 *AuditLogMutation) SetField(name string, value ent.Value) error { + switch name { + case auditlog.FieldActorID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetActorID(v) + return nil + case auditlog.FieldAction: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetAction(v) + return nil + case auditlog.FieldTargetID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetTargetID(v) + return nil + case auditlog.FieldMetadata: + v, ok := value.(map[string]interface{}) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetMetadata(v) + return nil + case auditlog.FieldTimestamp: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetTimestamp(v) + return nil + } + return fmt.Errorf("unknown AuditLog field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *AuditLogMutation) 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 *AuditLogMutation) 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 *AuditLogMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown AuditLog numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *AuditLogMutation) ClearedFields() []string { + var fields []string + if m.FieldCleared(auditlog.FieldTargetID) { + fields = append(fields, auditlog.FieldTargetID) + } + if m.FieldCleared(auditlog.FieldMetadata) { + fields = append(fields, auditlog.FieldMetadata) + } + return fields +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *AuditLogMutation) 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 *AuditLogMutation) ClearField(name string) error { + switch name { + case auditlog.FieldTargetID: + m.ClearTargetID() + return nil + case auditlog.FieldMetadata: + m.ClearMetadata() + return nil + } + return fmt.Errorf("unknown AuditLog 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 *AuditLogMutation) ResetField(name string) error { + switch name { + case auditlog.FieldActorID: + m.ResetActorID() + return nil + case auditlog.FieldAction: + m.ResetAction() + return nil + case auditlog.FieldTargetID: + m.ResetTargetID() + return nil + case auditlog.FieldMetadata: + m.ResetMetadata() + return nil + case auditlog.FieldTimestamp: + m.ResetTimestamp() + return nil + } + return fmt.Errorf("unknown AuditLog field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *AuditLogMutation) AddedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *AuditLogMutation) AddedIDs(name string) []ent.Value { + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *AuditLogMutation) RemovedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *AuditLogMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *AuditLogMutation) ClearedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *AuditLogMutation) EdgeCleared(name string) bool { + 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 *AuditLogMutation) ClearEdge(name string) error { + return fmt.Errorf("unknown AuditLog 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 *AuditLogMutation) ResetEdge(name string) error { + return fmt.Errorf("unknown AuditLog edge %s", name) +} + +// PermissionMutation represents an operation that mutates the Permission nodes in the graph. +type PermissionMutation struct { + config + op Op + typ string + id *string + name *string + clearedFields map[string]struct{} + role_permissions map[int]struct{} + removedrole_permissions map[int]struct{} + clearedrole_permissions bool + done bool + oldValue func(context.Context) (*Permission, error) + predicates []predicate.Permission +} + +var _ ent.Mutation = (*PermissionMutation)(nil) + +// permissionOption allows management of the mutation configuration using functional options. +type permissionOption func(*PermissionMutation) + +// newPermissionMutation creates new mutation for the Permission entity. +func newPermissionMutation(c config, op Op, opts ...permissionOption) *PermissionMutation { + m := &PermissionMutation{ + config: c, + op: op, + typ: TypePermission, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withPermissionID sets the ID field of the mutation. +func withPermissionID(id string) permissionOption { + return func(m *PermissionMutation) { + var ( + err error + once sync.Once + value *Permission + ) + m.oldValue = func(ctx context.Context) (*Permission, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().Permission.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withPermission sets the old Permission of the mutation. +func withPermission(node *Permission) permissionOption { + return func(m *PermissionMutation) { + m.oldValue = func(context.Context) (*Permission, 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 PermissionMutation) 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 PermissionMutation) 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 Permission entities. +func (m *PermissionMutation) SetID(id string) { + 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 *PermissionMutation) ID() (id string, 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 *PermissionMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Permission.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetName sets the "name" field. +func (m *PermissionMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *PermissionMutation) 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 Permission entity. +// If the Permission 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 *PermissionMutation) 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 *PermissionMutation) ResetName() { + m.name = nil +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by ids. +func (m *PermissionMutation) AddRolePermissionIDs(ids ...int) { + if m.role_permissions == nil { + m.role_permissions = make(map[int]struct{}) + } + for i := range ids { + m.role_permissions[ids[i]] = struct{}{} + } +} + +// ClearRolePermissions clears the "role_permissions" edge to the RolePermission entity. +func (m *PermissionMutation) ClearRolePermissions() { + m.clearedrole_permissions = true +} + +// RolePermissionsCleared reports if the "role_permissions" edge to the RolePermission entity was cleared. +func (m *PermissionMutation) RolePermissionsCleared() bool { + return m.clearedrole_permissions +} + +// RemoveRolePermissionIDs removes the "role_permissions" edge to the RolePermission entity by IDs. +func (m *PermissionMutation) RemoveRolePermissionIDs(ids ...int) { + if m.removedrole_permissions == nil { + m.removedrole_permissions = make(map[int]struct{}) + } + for i := range ids { + delete(m.role_permissions, ids[i]) + m.removedrole_permissions[ids[i]] = struct{}{} + } +} + +// RemovedRolePermissions returns the removed IDs of the "role_permissions" edge to the RolePermission entity. +func (m *PermissionMutation) RemovedRolePermissionsIDs() (ids []int) { + for id := range m.removedrole_permissions { + ids = append(ids, id) + } + return +} + +// RolePermissionsIDs returns the "role_permissions" edge IDs in the mutation. +func (m *PermissionMutation) RolePermissionsIDs() (ids []int) { + for id := range m.role_permissions { + ids = append(ids, id) + } + return +} + +// ResetRolePermissions resets all changes to the "role_permissions" edge. +func (m *PermissionMutation) ResetRolePermissions() { + m.role_permissions = nil + m.clearedrole_permissions = false + m.removedrole_permissions = nil +} + +// Where appends a list predicates to the PermissionMutation builder. +func (m *PermissionMutation) Where(ps ...predicate.Permission) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the PermissionMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *PermissionMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.Permission, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *PermissionMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *PermissionMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (Permission). +func (m *PermissionMutation) 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 *PermissionMutation) Fields() []string { + fields := make([]string, 0, 1) + if m.name != nil { + fields = append(fields, permission.FieldName) + } + 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 *PermissionMutation) Field(name string) (ent.Value, bool) { + switch name { + case permission.FieldName: + return m.Name() + } + 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 *PermissionMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case permission.FieldName: + return m.OldName(ctx) + } + return nil, fmt.Errorf("unknown Permission 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 *PermissionMutation) SetField(name string, value ent.Value) error { + switch name { + case permission.FieldName: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetName(v) + return nil + } + return fmt.Errorf("unknown Permission field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *PermissionMutation) 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 *PermissionMutation) 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 *PermissionMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown Permission numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *PermissionMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *PermissionMutation) 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 *PermissionMutation) ClearField(name string) error { + return fmt.Errorf("unknown Permission 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 *PermissionMutation) ResetField(name string) error { + switch name { + case permission.FieldName: + m.ResetName() + return nil + } + return fmt.Errorf("unknown Permission field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *PermissionMutation) AddedEdges() []string { + edges := make([]string, 0, 1) + if m.role_permissions != nil { + edges = append(edges, permission.EdgeRolePermissions) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *PermissionMutation) AddedIDs(name string) []ent.Value { + switch name { + case permission.EdgeRolePermissions: + ids := make([]ent.Value, 0, len(m.role_permissions)) + for id := range m.role_permissions { + ids = append(ids, id) + } + return ids + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *PermissionMutation) RemovedEdges() []string { + edges := make([]string, 0, 1) + if m.removedrole_permissions != nil { + edges = append(edges, permission.EdgeRolePermissions) + } + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *PermissionMutation) RemovedIDs(name string) []ent.Value { + switch name { + case permission.EdgeRolePermissions: + ids := make([]ent.Value, 0, len(m.removedrole_permissions)) + for id := range m.removedrole_permissions { + ids = append(ids, id) + } + return ids + } + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *PermissionMutation) ClearedEdges() []string { + edges := make([]string, 0, 1) + if m.clearedrole_permissions { + edges = append(edges, permission.EdgeRolePermissions) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *PermissionMutation) EdgeCleared(name string) bool { + switch name { + case permission.EdgeRolePermissions: + return m.clearedrole_permissions + } + 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 *PermissionMutation) ClearEdge(name string) error { + switch name { + } + return fmt.Errorf("unknown Permission 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 *PermissionMutation) ResetEdge(name string) error { + switch name { + case permission.EdgeRolePermissions: + m.ResetRolePermissions() + return nil + } + return fmt.Errorf("unknown Permission edge %s", name) +} + +// RoleMutation represents an operation that mutates the Role nodes in the graph. +type RoleMutation struct { + config + op Op + typ string + id *string + name *string + description *string + created_at *time.Time + clearedFields map[string]struct{} + role_permissions map[int]struct{} + removedrole_permissions map[int]struct{} + clearedrole_permissions bool + user_roles map[int]struct{} + removeduser_roles map[int]struct{} + cleareduser_roles bool + done bool + oldValue func(context.Context) (*Role, error) + predicates []predicate.Role +} + +var _ ent.Mutation = (*RoleMutation)(nil) + +// roleOption allows management of the mutation configuration using functional options. +type roleOption func(*RoleMutation) + +// newRoleMutation creates new mutation for the Role entity. +func newRoleMutation(c config, op Op, opts ...roleOption) *RoleMutation { + m := &RoleMutation{ + config: c, + op: op, + typ: TypeRole, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withRoleID sets the ID field of the mutation. +func withRoleID(id string) roleOption { + return func(m *RoleMutation) { + var ( + err error + once sync.Once + value *Role + ) + m.oldValue = func(ctx context.Context) (*Role, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().Role.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withRole sets the old Role of the mutation. +func withRole(node *Role) roleOption { + return func(m *RoleMutation) { + m.oldValue = func(context.Context) (*Role, 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 RoleMutation) 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 RoleMutation) 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 Role entities. +func (m *RoleMutation) SetID(id string) { + 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 *RoleMutation) ID() (id string, 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 *RoleMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Role.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetName sets the "name" field. +func (m *RoleMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *RoleMutation) 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 Role entity. +// If the Role 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 *RoleMutation) 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 *RoleMutation) ResetName() { + m.name = nil +} + +// SetDescription sets the "description" field. +func (m *RoleMutation) SetDescription(s string) { + m.description = &s +} + +// Description returns the value of the "description" field in the mutation. +func (m *RoleMutation) Description() (r string, exists bool) { + v := m.description + if v == nil { + return + } + return *v, true +} + +// OldDescription returns the old "description" field's value of the Role entity. +// If the Role 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 *RoleMutation) OldDescription(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldDescription is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldDescription requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldDescription: %w", err) + } + return oldValue.Description, nil +} + +// ClearDescription clears the value of the "description" field. +func (m *RoleMutation) ClearDescription() { + m.description = nil + m.clearedFields[role.FieldDescription] = struct{}{} +} + +// DescriptionCleared returns if the "description" field was cleared in this mutation. +func (m *RoleMutation) DescriptionCleared() bool { + _, ok := m.clearedFields[role.FieldDescription] + return ok +} + +// ResetDescription resets all changes to the "description" field. +func (m *RoleMutation) ResetDescription() { + m.description = nil + delete(m.clearedFields, role.FieldDescription) +} + +// SetCreatedAt sets the "created_at" field. +func (m *RoleMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *RoleMutation) 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 Role entity. +// If the Role 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 *RoleMutation) 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 *RoleMutation) ResetCreatedAt() { + m.created_at = nil +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by ids. +func (m *RoleMutation) AddRolePermissionIDs(ids ...int) { + if m.role_permissions == nil { + m.role_permissions = make(map[int]struct{}) + } + for i := range ids { + m.role_permissions[ids[i]] = struct{}{} + } +} + +// ClearRolePermissions clears the "role_permissions" edge to the RolePermission entity. +func (m *RoleMutation) ClearRolePermissions() { + m.clearedrole_permissions = true +} + +// RolePermissionsCleared reports if the "role_permissions" edge to the RolePermission entity was cleared. +func (m *RoleMutation) RolePermissionsCleared() bool { + return m.clearedrole_permissions +} + +// RemoveRolePermissionIDs removes the "role_permissions" edge to the RolePermission entity by IDs. +func (m *RoleMutation) RemoveRolePermissionIDs(ids ...int) { + if m.removedrole_permissions == nil { + m.removedrole_permissions = make(map[int]struct{}) + } + for i := range ids { + delete(m.role_permissions, ids[i]) + m.removedrole_permissions[ids[i]] = struct{}{} + } +} + +// RemovedRolePermissions returns the removed IDs of the "role_permissions" edge to the RolePermission entity. +func (m *RoleMutation) RemovedRolePermissionsIDs() (ids []int) { + for id := range m.removedrole_permissions { + ids = append(ids, id) + } + return +} + +// RolePermissionsIDs returns the "role_permissions" edge IDs in the mutation. +func (m *RoleMutation) RolePermissionsIDs() (ids []int) { + for id := range m.role_permissions { + ids = append(ids, id) + } + return +} + +// ResetRolePermissions resets all changes to the "role_permissions" edge. +func (m *RoleMutation) ResetRolePermissions() { + m.role_permissions = nil + m.clearedrole_permissions = false + m.removedrole_permissions = nil +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by ids. +func (m *RoleMutation) AddUserRoleIDs(ids ...int) { + if m.user_roles == nil { + m.user_roles = make(map[int]struct{}) + } + for i := range ids { + m.user_roles[ids[i]] = struct{}{} + } +} + +// ClearUserRoles clears the "user_roles" edge to the UserRole entity. +func (m *RoleMutation) ClearUserRoles() { + m.cleareduser_roles = true +} + +// UserRolesCleared reports if the "user_roles" edge to the UserRole entity was cleared. +func (m *RoleMutation) UserRolesCleared() bool { + return m.cleareduser_roles +} + +// RemoveUserRoleIDs removes the "user_roles" edge to the UserRole entity by IDs. +func (m *RoleMutation) RemoveUserRoleIDs(ids ...int) { + if m.removeduser_roles == nil { + m.removeduser_roles = make(map[int]struct{}) + } + for i := range ids { + delete(m.user_roles, ids[i]) + m.removeduser_roles[ids[i]] = struct{}{} + } +} + +// RemovedUserRoles returns the removed IDs of the "user_roles" edge to the UserRole entity. +func (m *RoleMutation) RemovedUserRolesIDs() (ids []int) { + for id := range m.removeduser_roles { + ids = append(ids, id) + } + return +} + +// UserRolesIDs returns the "user_roles" edge IDs in the mutation. +func (m *RoleMutation) UserRolesIDs() (ids []int) { + for id := range m.user_roles { + ids = append(ids, id) + } + return +} + +// ResetUserRoles resets all changes to the "user_roles" edge. +func (m *RoleMutation) ResetUserRoles() { + m.user_roles = nil + m.cleareduser_roles = false + m.removeduser_roles = nil +} + +// Where appends a list predicates to the RoleMutation builder. +func (m *RoleMutation) Where(ps ...predicate.Role) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the RoleMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *RoleMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.Role, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *RoleMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *RoleMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (Role). +func (m *RoleMutation) 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 *RoleMutation) Fields() []string { + fields := make([]string, 0, 3) + if m.name != nil { + fields = append(fields, role.FieldName) + } + if m.description != nil { + fields = append(fields, role.FieldDescription) + } + if m.created_at != nil { + fields = append(fields, role.FieldCreatedAt) + } + 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 *RoleMutation) Field(name string) (ent.Value, bool) { + switch name { + case role.FieldName: + return m.Name() + case role.FieldDescription: + return m.Description() + case role.FieldCreatedAt: + return m.CreatedAt() + } + 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 *RoleMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case role.FieldName: + return m.OldName(ctx) + case role.FieldDescription: + return m.OldDescription(ctx) + case role.FieldCreatedAt: + return m.OldCreatedAt(ctx) + } + return nil, fmt.Errorf("unknown Role 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 *RoleMutation) SetField(name string, value ent.Value) error { + switch name { + case role.FieldName: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetName(v) + return nil + case role.FieldDescription: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetDescription(v) + return nil + case role.FieldCreatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetCreatedAt(v) + return nil + } + return fmt.Errorf("unknown Role field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *RoleMutation) 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 *RoleMutation) 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 *RoleMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown Role numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *RoleMutation) ClearedFields() []string { + var fields []string + if m.FieldCleared(role.FieldDescription) { + fields = append(fields, role.FieldDescription) + } + return fields +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *RoleMutation) 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 *RoleMutation) ClearField(name string) error { + switch name { + case role.FieldDescription: + m.ClearDescription() + return nil + } + return fmt.Errorf("unknown Role 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 *RoleMutation) ResetField(name string) error { + switch name { + case role.FieldName: + m.ResetName() + return nil + case role.FieldDescription: + m.ResetDescription() + return nil + case role.FieldCreatedAt: + m.ResetCreatedAt() + return nil + } + return fmt.Errorf("unknown Role field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *RoleMutation) AddedEdges() []string { + edges := make([]string, 0, 2) + if m.role_permissions != nil { + edges = append(edges, role.EdgeRolePermissions) + } + if m.user_roles != nil { + edges = append(edges, role.EdgeUserRoles) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *RoleMutation) AddedIDs(name string) []ent.Value { + switch name { + case role.EdgeRolePermissions: + ids := make([]ent.Value, 0, len(m.role_permissions)) + for id := range m.role_permissions { + ids = append(ids, id) + } + return ids + case role.EdgeUserRoles: + ids := make([]ent.Value, 0, len(m.user_roles)) + for id := range m.user_roles { + ids = append(ids, id) + } + return ids + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *RoleMutation) RemovedEdges() []string { + edges := make([]string, 0, 2) + if m.removedrole_permissions != nil { + edges = append(edges, role.EdgeRolePermissions) + } + if m.removeduser_roles != nil { + edges = append(edges, role.EdgeUserRoles) + } + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *RoleMutation) RemovedIDs(name string) []ent.Value { + switch name { + case role.EdgeRolePermissions: + ids := make([]ent.Value, 0, len(m.removedrole_permissions)) + for id := range m.removedrole_permissions { + ids = append(ids, id) + } + return ids + case role.EdgeUserRoles: + ids := make([]ent.Value, 0, len(m.removeduser_roles)) + for id := range m.removeduser_roles { + ids = append(ids, id) + } + return ids + } + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *RoleMutation) ClearedEdges() []string { + edges := make([]string, 0, 2) + if m.clearedrole_permissions { + edges = append(edges, role.EdgeRolePermissions) + } + if m.cleareduser_roles { + edges = append(edges, role.EdgeUserRoles) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *RoleMutation) EdgeCleared(name string) bool { + switch name { + case role.EdgeRolePermissions: + return m.clearedrole_permissions + case role.EdgeUserRoles: + return m.cleareduser_roles + } + 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 *RoleMutation) ClearEdge(name string) error { + switch name { + } + return fmt.Errorf("unknown Role 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 *RoleMutation) ResetEdge(name string) error { + switch name { + case role.EdgeRolePermissions: + m.ResetRolePermissions() + return nil + case role.EdgeUserRoles: + m.ResetUserRoles() + return nil + } + return fmt.Errorf("unknown Role edge %s", name) +} + +// RolePermissionMutation represents an operation that mutates the RolePermission nodes in the graph. +type RolePermissionMutation struct { + config + op Op + typ string + id *int + clearedFields map[string]struct{} + role *string + clearedrole bool + permission *string + clearedpermission bool + done bool + oldValue func(context.Context) (*RolePermission, error) + predicates []predicate.RolePermission +} + +var _ ent.Mutation = (*RolePermissionMutation)(nil) + +// rolepermissionOption allows management of the mutation configuration using functional options. +type rolepermissionOption func(*RolePermissionMutation) + +// newRolePermissionMutation creates new mutation for the RolePermission entity. +func newRolePermissionMutation(c config, op Op, opts ...rolepermissionOption) *RolePermissionMutation { + m := &RolePermissionMutation{ + config: c, + op: op, + typ: TypeRolePermission, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withRolePermissionID sets the ID field of the mutation. +func withRolePermissionID(id int) rolepermissionOption { + return func(m *RolePermissionMutation) { + var ( + err error + once sync.Once + value *RolePermission + ) + m.oldValue = func(ctx context.Context) (*RolePermission, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().RolePermission.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withRolePermission sets the old RolePermission of the mutation. +func withRolePermission(node *RolePermission) rolepermissionOption { + return func(m *RolePermissionMutation) { + m.oldValue = func(context.Context) (*RolePermission, 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 RolePermissionMutation) 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 RolePermissionMutation) 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 +} + +// 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 *RolePermissionMutation) ID() (id int, 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 *RolePermissionMutation) IDs(ctx context.Context) ([]int, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []int{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().RolePermission.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetRoleID sets the "role_id" field. +func (m *RolePermissionMutation) SetRoleID(s string) { + m.role = &s +} + +// RoleID returns the value of the "role_id" field in the mutation. +func (m *RolePermissionMutation) RoleID() (r string, exists bool) { + v := m.role + if v == nil { + return + } + return *v, true +} + +// OldRoleID returns the old "role_id" field's value of the RolePermission entity. +// If the RolePermission 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 *RolePermissionMutation) OldRoleID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldRoleID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldRoleID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldRoleID: %w", err) + } + return oldValue.RoleID, nil +} + +// ResetRoleID resets all changes to the "role_id" field. +func (m *RolePermissionMutation) ResetRoleID() { + m.role = nil +} + +// SetPermissionID sets the "permission_id" field. +func (m *RolePermissionMutation) SetPermissionID(s string) { + m.permission = &s +} + +// PermissionID returns the value of the "permission_id" field in the mutation. +func (m *RolePermissionMutation) PermissionID() (r string, exists bool) { + v := m.permission + if v == nil { + return + } + return *v, true +} + +// OldPermissionID returns the old "permission_id" field's value of the RolePermission entity. +// If the RolePermission 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 *RolePermissionMutation) OldPermissionID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldPermissionID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldPermissionID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldPermissionID: %w", err) + } + return oldValue.PermissionID, nil +} + +// ResetPermissionID resets all changes to the "permission_id" field. +func (m *RolePermissionMutation) ResetPermissionID() { + m.permission = nil +} + +// ClearRole clears the "role" edge to the Role entity. +func (m *RolePermissionMutation) ClearRole() { + m.clearedrole = true + m.clearedFields[rolepermission.FieldRoleID] = struct{}{} +} + +// RoleCleared reports if the "role" edge to the Role entity was cleared. +func (m *RolePermissionMutation) RoleCleared() bool { + return m.clearedrole +} + +// RoleIDs returns the "role" edge IDs in the mutation. +// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use +// RoleID instead. It exists only for internal usage by the builders. +func (m *RolePermissionMutation) RoleIDs() (ids []string) { + if id := m.role; id != nil { + ids = append(ids, *id) + } + return +} + +// ResetRole resets all changes to the "role" edge. +func (m *RolePermissionMutation) ResetRole() { + m.role = nil + m.clearedrole = false +} + +// ClearPermission clears the "permission" edge to the Permission entity. +func (m *RolePermissionMutation) ClearPermission() { + m.clearedpermission = true + m.clearedFields[rolepermission.FieldPermissionID] = struct{}{} +} + +// PermissionCleared reports if the "permission" edge to the Permission entity was cleared. +func (m *RolePermissionMutation) PermissionCleared() bool { + return m.clearedpermission +} + +// PermissionIDs returns the "permission" edge IDs in the mutation. +// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use +// PermissionID instead. It exists only for internal usage by the builders. +func (m *RolePermissionMutation) PermissionIDs() (ids []string) { + if id := m.permission; id != nil { + ids = append(ids, *id) + } + return +} + +// ResetPermission resets all changes to the "permission" edge. +func (m *RolePermissionMutation) ResetPermission() { + m.permission = nil + m.clearedpermission = false +} + +// Where appends a list predicates to the RolePermissionMutation builder. +func (m *RolePermissionMutation) Where(ps ...predicate.RolePermission) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the RolePermissionMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *RolePermissionMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.RolePermission, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *RolePermissionMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *RolePermissionMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (RolePermission). +func (m *RolePermissionMutation) 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 *RolePermissionMutation) Fields() []string { + fields := make([]string, 0, 2) + if m.role != nil { + fields = append(fields, rolepermission.FieldRoleID) + } + if m.permission != nil { + fields = append(fields, rolepermission.FieldPermissionID) + } + 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 *RolePermissionMutation) Field(name string) (ent.Value, bool) { + switch name { + case rolepermission.FieldRoleID: + return m.RoleID() + case rolepermission.FieldPermissionID: + return m.PermissionID() + } + 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 *RolePermissionMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case rolepermission.FieldRoleID: + return m.OldRoleID(ctx) + case rolepermission.FieldPermissionID: + return m.OldPermissionID(ctx) + } + return nil, fmt.Errorf("unknown RolePermission 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 *RolePermissionMutation) SetField(name string, value ent.Value) error { + switch name { + case rolepermission.FieldRoleID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetRoleID(v) + return nil + case rolepermission.FieldPermissionID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetPermissionID(v) + return nil + } + return fmt.Errorf("unknown RolePermission field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *RolePermissionMutation) 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 *RolePermissionMutation) 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 *RolePermissionMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown RolePermission numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *RolePermissionMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *RolePermissionMutation) 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 *RolePermissionMutation) ClearField(name string) error { + return fmt.Errorf("unknown RolePermission 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 *RolePermissionMutation) ResetField(name string) error { + switch name { + case rolepermission.FieldRoleID: + m.ResetRoleID() + return nil + case rolepermission.FieldPermissionID: + m.ResetPermissionID() + return nil + } + return fmt.Errorf("unknown RolePermission field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *RolePermissionMutation) AddedEdges() []string { + edges := make([]string, 0, 2) + if m.role != nil { + edges = append(edges, rolepermission.EdgeRole) + } + if m.permission != nil { + edges = append(edges, rolepermission.EdgePermission) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *RolePermissionMutation) AddedIDs(name string) []ent.Value { + switch name { + case rolepermission.EdgeRole: + if id := m.role; id != nil { + return []ent.Value{*id} + } + case rolepermission.EdgePermission: + if id := m.permission; id != nil { + return []ent.Value{*id} + } + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *RolePermissionMutation) RemovedEdges() []string { + edges := make([]string, 0, 2) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *RolePermissionMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *RolePermissionMutation) ClearedEdges() []string { + edges := make([]string, 0, 2) + if m.clearedrole { + edges = append(edges, rolepermission.EdgeRole) + } + if m.clearedpermission { + edges = append(edges, rolepermission.EdgePermission) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *RolePermissionMutation) EdgeCleared(name string) bool { + switch name { + case rolepermission.EdgeRole: + return m.clearedrole + case rolepermission.EdgePermission: + return m.clearedpermission + } + 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 *RolePermissionMutation) ClearEdge(name string) error { + switch name { + case rolepermission.EdgeRole: + m.ClearRole() + return nil + case rolepermission.EdgePermission: + m.ClearPermission() + return nil + } + return fmt.Errorf("unknown RolePermission 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 *RolePermissionMutation) ResetEdge(name string) error { + switch name { + case rolepermission.EdgeRole: + m.ResetRole() + return nil + case rolepermission.EdgePermission: + m.ResetPermission() + return nil + } + return fmt.Errorf("unknown RolePermission edge %s", name) +} + +// UserMutation represents an operation that mutates the User nodes in the graph. +type UserMutation struct { + config + op Op + typ string + id *string + email *string + password_hash *string + verified *bool + created_at *time.Time + updated_at *time.Time + clearedFields map[string]struct{} + user_roles map[int]struct{} + removeduser_roles map[int]struct{} + cleareduser_roles bool + done bool + oldValue func(context.Context) (*User, error) + predicates []predicate.User +} + +var _ ent.Mutation = (*UserMutation)(nil) + +// userOption allows management of the mutation configuration using functional options. +type userOption func(*UserMutation) + +// newUserMutation creates new mutation for the User entity. +func newUserMutation(c config, op Op, opts ...userOption) *UserMutation { + m := &UserMutation{ + config: c, + op: op, + typ: TypeUser, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withUserID sets the ID field of the mutation. +func withUserID(id string) userOption { + return func(m *UserMutation) { + var ( + err error + once sync.Once + value *User + ) + m.oldValue = func(ctx context.Context) (*User, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().User.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withUser sets the old User of the mutation. +func withUser(node *User) userOption { + return func(m *UserMutation) { + m.oldValue = func(context.Context) (*User, 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 UserMutation) 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 UserMutation) 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 User entities. +func (m *UserMutation) SetID(id string) { + 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 *UserMutation) ID() (id string, 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 *UserMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().User.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetEmail sets the "email" field. +func (m *UserMutation) SetEmail(s string) { + m.email = &s +} + +// Email returns the value of the "email" field in the mutation. +func (m *UserMutation) Email() (r string, exists bool) { + v := m.email + if v == nil { + return + } + return *v, true +} + +// OldEmail returns the old "email" 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) OldEmail(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldEmail is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldEmail requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldEmail: %w", err) + } + return oldValue.Email, nil +} + +// ResetEmail resets all changes to the "email" field. +func (m *UserMutation) ResetEmail() { + m.email = nil +} + +// SetPasswordHash sets the "password_hash" field. +func (m *UserMutation) SetPasswordHash(s string) { + m.password_hash = &s +} + +// PasswordHash returns the value of the "password_hash" field in the mutation. +func (m *UserMutation) PasswordHash() (r string, exists bool) { + v := m.password_hash + if v == nil { + return + } + return *v, true +} + +// OldPasswordHash returns the old "password_hash" 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) OldPasswordHash(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldPasswordHash is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldPasswordHash requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldPasswordHash: %w", err) + } + return oldValue.PasswordHash, nil +} + +// ResetPasswordHash resets all changes to the "password_hash" field. +func (m *UserMutation) ResetPasswordHash() { + m.password_hash = nil +} + +// SetVerified sets the "verified" field. +func (m *UserMutation) SetVerified(b bool) { + m.verified = &b +} + +// Verified returns the value of the "verified" field in the mutation. +func (m *UserMutation) Verified() (r bool, exists bool) { + v := m.verified + if v == nil { + return + } + return *v, true +} + +// OldVerified returns the old "verified" 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) OldVerified(ctx context.Context) (v bool, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldVerified is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldVerified requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldVerified: %w", err) + } + return oldValue.Verified, nil +} + +// ResetVerified resets all changes to the "verified" field. +func (m *UserMutation) ResetVerified() { + m.verified = nil +} + +// SetCreatedAt sets the "created_at" field. +func (m *UserMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *UserMutation) 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 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) 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 *UserMutation) ResetCreatedAt() { + m.created_at = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *UserMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *UserMutation) 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 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) 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 *UserMutation) ResetUpdatedAt() { + m.updated_at = nil +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by ids. +func (m *UserMutation) AddUserRoleIDs(ids ...int) { + if m.user_roles == nil { + m.user_roles = make(map[int]struct{}) + } + for i := range ids { + m.user_roles[ids[i]] = struct{}{} + } +} + +// ClearUserRoles clears the "user_roles" edge to the UserRole entity. +func (m *UserMutation) ClearUserRoles() { + m.cleareduser_roles = true +} + +// UserRolesCleared reports if the "user_roles" edge to the UserRole entity was cleared. +func (m *UserMutation) UserRolesCleared() bool { + return m.cleareduser_roles +} + +// RemoveUserRoleIDs removes the "user_roles" edge to the UserRole entity by IDs. +func (m *UserMutation) RemoveUserRoleIDs(ids ...int) { + if m.removeduser_roles == nil { + m.removeduser_roles = make(map[int]struct{}) + } + for i := range ids { + delete(m.user_roles, ids[i]) + m.removeduser_roles[ids[i]] = struct{}{} + } +} + +// RemovedUserRoles returns the removed IDs of the "user_roles" edge to the UserRole entity. +func (m *UserMutation) RemovedUserRolesIDs() (ids []int) { + for id := range m.removeduser_roles { + ids = append(ids, id) + } + return +} + +// UserRolesIDs returns the "user_roles" edge IDs in the mutation. +func (m *UserMutation) UserRolesIDs() (ids []int) { + for id := range m.user_roles { + ids = append(ids, id) + } + return +} + +// ResetUserRoles resets all changes to the "user_roles" edge. +func (m *UserMutation) ResetUserRoles() { + m.user_roles = nil + m.cleareduser_roles = false + m.removeduser_roles = nil +} + +// Where appends a list predicates to the UserMutation builder. +func (m *UserMutation) Where(ps ...predicate.User) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the UserMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *UserMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.User, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *UserMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *UserMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (User). +func (m *UserMutation) 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 *UserMutation) Fields() []string { + fields := make([]string, 0, 5) + if m.email != nil { + fields = append(fields, user.FieldEmail) + } + if m.password_hash != nil { + fields = append(fields, user.FieldPasswordHash) + } + if m.verified != nil { + fields = append(fields, user.FieldVerified) + } + if m.created_at != nil { + fields = append(fields, user.FieldCreatedAt) + } + if m.updated_at != nil { + fields = append(fields, user.FieldUpdatedAt) + } + 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 *UserMutation) Field(name string) (ent.Value, bool) { + switch name { + case user.FieldEmail: + return m.Email() + case user.FieldPasswordHash: + return m.PasswordHash() + case user.FieldVerified: + return m.Verified() + case user.FieldCreatedAt: + return m.CreatedAt() + case user.FieldUpdatedAt: + return m.UpdatedAt() + } + 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 *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case user.FieldEmail: + return m.OldEmail(ctx) + case user.FieldPasswordHash: + return m.OldPasswordHash(ctx) + case user.FieldVerified: + return m.OldVerified(ctx) + case user.FieldCreatedAt: + return m.OldCreatedAt(ctx) + case user.FieldUpdatedAt: + return m.OldUpdatedAt(ctx) + } + return nil, fmt.Errorf("unknown User 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 *UserMutation) SetField(name string, value ent.Value) error { + switch name { + case user.FieldEmail: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetEmail(v) + return nil + case user.FieldPasswordHash: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetPasswordHash(v) + return nil + case user.FieldVerified: + v, ok := value.(bool) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetVerified(v) + return nil + case user.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 user.FieldUpdatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUpdatedAt(v) + return nil + } + return fmt.Errorf("unknown User field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *UserMutation) 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 *UserMutation) 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 *UserMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown User numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *UserMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *UserMutation) 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 *UserMutation) ClearField(name string) error { + return fmt.Errorf("unknown User 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 *UserMutation) ResetField(name string) error { + switch name { + case user.FieldEmail: + m.ResetEmail() + return nil + case user.FieldPasswordHash: + m.ResetPasswordHash() + return nil + case user.FieldVerified: + m.ResetVerified() + return nil + case user.FieldCreatedAt: + m.ResetCreatedAt() + return nil + case user.FieldUpdatedAt: + m.ResetUpdatedAt() + return nil + } + return fmt.Errorf("unknown User field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *UserMutation) AddedEdges() []string { + edges := make([]string, 0, 1) + if m.user_roles != nil { + edges = append(edges, user.EdgeUserRoles) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *UserMutation) AddedIDs(name string) []ent.Value { + switch name { + case user.EdgeUserRoles: + ids := make([]ent.Value, 0, len(m.user_roles)) + for id := range m.user_roles { + 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, 1) + if m.removeduser_roles != nil { + edges = append(edges, user.EdgeUserRoles) + } + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *UserMutation) RemovedIDs(name string) []ent.Value { + switch name { + case user.EdgeUserRoles: + ids := make([]ent.Value, 0, len(m.removeduser_roles)) + for id := range m.removeduser_roles { + 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, 1) + if m.cleareduser_roles { + edges = append(edges, user.EdgeUserRoles) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *UserMutation) EdgeCleared(name string) bool { + switch name { + case user.EdgeUserRoles: + return m.cleareduser_roles + } + 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 *UserMutation) ClearEdge(name string) error { + switch name { + } + return fmt.Errorf("unknown User 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 *UserMutation) ResetEdge(name string) error { + switch name { + case user.EdgeUserRoles: + m.ResetUserRoles() + return nil + } + return fmt.Errorf("unknown User edge %s", name) +} + +// UserRoleMutation represents an operation that mutates the UserRole nodes in the graph. +type UserRoleMutation struct { + config + op Op + typ string + id *int + clearedFields map[string]struct{} + user *string + cleareduser bool + role *string + clearedrole bool + done bool + oldValue func(context.Context) (*UserRole, error) + predicates []predicate.UserRole +} + +var _ ent.Mutation = (*UserRoleMutation)(nil) + +// userroleOption allows management of the mutation configuration using functional options. +type userroleOption func(*UserRoleMutation) + +// newUserRoleMutation creates new mutation for the UserRole entity. +func newUserRoleMutation(c config, op Op, opts ...userroleOption) *UserRoleMutation { + m := &UserRoleMutation{ + config: c, + op: op, + typ: TypeUserRole, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withUserRoleID sets the ID field of the mutation. +func withUserRoleID(id int) userroleOption { + return func(m *UserRoleMutation) { + var ( + err error + once sync.Once + value *UserRole + ) + m.oldValue = func(ctx context.Context) (*UserRole, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().UserRole.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withUserRole sets the old UserRole of the mutation. +func withUserRole(node *UserRole) userroleOption { + return func(m *UserRoleMutation) { + m.oldValue = func(context.Context) (*UserRole, 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 UserRoleMutation) 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 UserRoleMutation) 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 +} + +// 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 *UserRoleMutation) ID() (id int, 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 *UserRoleMutation) IDs(ctx context.Context) ([]int, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []int{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().UserRole.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetUserID sets the "user_id" field. +func (m *UserRoleMutation) SetUserID(s string) { + m.user = &s +} + +// UserID returns the value of the "user_id" field in the mutation. +func (m *UserRoleMutation) UserID() (r string, exists bool) { + v := m.user + if v == nil { + return + } + return *v, true +} + +// OldUserID returns the old "user_id" field's value of the UserRole entity. +// If the UserRole 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 *UserRoleMutation) OldUserID(ctx context.Context) (v string, 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 *UserRoleMutation) ResetUserID() { + m.user = nil +} + +// SetRoleID sets the "role_id" field. +func (m *UserRoleMutation) SetRoleID(s string) { + m.role = &s +} + +// RoleID returns the value of the "role_id" field in the mutation. +func (m *UserRoleMutation) RoleID() (r string, exists bool) { + v := m.role + if v == nil { + return + } + return *v, true +} + +// OldRoleID returns the old "role_id" field's value of the UserRole entity. +// If the UserRole 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 *UserRoleMutation) OldRoleID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldRoleID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldRoleID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldRoleID: %w", err) + } + return oldValue.RoleID, nil +} + +// ResetRoleID resets all changes to the "role_id" field. +func (m *UserRoleMutation) ResetRoleID() { + m.role = nil +} + +// ClearUser clears the "user" edge to the User entity. +func (m *UserRoleMutation) ClearUser() { + m.cleareduser = true + m.clearedFields[userrole.FieldUserID] = struct{}{} +} + +// UserCleared reports if the "user" edge to the User entity was cleared. +func (m *UserRoleMutation) 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 *UserRoleMutation) UserIDs() (ids []string) { + if id := m.user; id != nil { + ids = append(ids, *id) + } + return +} + +// ResetUser resets all changes to the "user" edge. +func (m *UserRoleMutation) ResetUser() { + m.user = nil + m.cleareduser = false +} + +// ClearRole clears the "role" edge to the Role entity. +func (m *UserRoleMutation) ClearRole() { + m.clearedrole = true + m.clearedFields[userrole.FieldRoleID] = struct{}{} +} + +// RoleCleared reports if the "role" edge to the Role entity was cleared. +func (m *UserRoleMutation) RoleCleared() bool { + return m.clearedrole +} + +// RoleIDs returns the "role" edge IDs in the mutation. +// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use +// RoleID instead. It exists only for internal usage by the builders. +func (m *UserRoleMutation) RoleIDs() (ids []string) { + if id := m.role; id != nil { + ids = append(ids, *id) + } + return +} + +// ResetRole resets all changes to the "role" edge. +func (m *UserRoleMutation) ResetRole() { + m.role = nil + m.clearedrole = false +} + +// Where appends a list predicates to the UserRoleMutation builder. +func (m *UserRoleMutation) Where(ps ...predicate.UserRole) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the UserRoleMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *UserRoleMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.UserRole, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *UserRoleMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *UserRoleMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (UserRole). +func (m *UserRoleMutation) 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 *UserRoleMutation) Fields() []string { + fields := make([]string, 0, 2) + if m.user != nil { + fields = append(fields, userrole.FieldUserID) + } + if m.role != nil { + fields = append(fields, userrole.FieldRoleID) + } + 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 *UserRoleMutation) Field(name string) (ent.Value, bool) { + switch name { + case userrole.FieldUserID: + return m.UserID() + case userrole.FieldRoleID: + return m.RoleID() + } + 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 *UserRoleMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case userrole.FieldUserID: + return m.OldUserID(ctx) + case userrole.FieldRoleID: + return m.OldRoleID(ctx) + } + return nil, fmt.Errorf("unknown UserRole 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 *UserRoleMutation) SetField(name string, value ent.Value) error { + switch name { + case userrole.FieldUserID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUserID(v) + return nil + case userrole.FieldRoleID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetRoleID(v) + return nil + } + return fmt.Errorf("unknown UserRole field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *UserRoleMutation) 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 *UserRoleMutation) 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 *UserRoleMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown UserRole numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *UserRoleMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *UserRoleMutation) 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 *UserRoleMutation) ClearField(name string) error { + return fmt.Errorf("unknown UserRole 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 *UserRoleMutation) ResetField(name string) error { + switch name { + case userrole.FieldUserID: + m.ResetUserID() + return nil + case userrole.FieldRoleID: + m.ResetRoleID() + return nil + } + return fmt.Errorf("unknown UserRole field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *UserRoleMutation) AddedEdges() []string { + edges := make([]string, 0, 2) + if m.user != nil { + edges = append(edges, userrole.EdgeUser) + } + if m.role != nil { + edges = append(edges, userrole.EdgeRole) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *UserRoleMutation) AddedIDs(name string) []ent.Value { + switch name { + case userrole.EdgeUser: + if id := m.user; id != nil { + return []ent.Value{*id} + } + case userrole.EdgeRole: + if id := m.role; id != nil { + return []ent.Value{*id} + } + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *UserRoleMutation) RemovedEdges() []string { + edges := make([]string, 0, 2) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *UserRoleMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *UserRoleMutation) ClearedEdges() []string { + edges := make([]string, 0, 2) + if m.cleareduser { + edges = append(edges, userrole.EdgeUser) + } + if m.clearedrole { + edges = append(edges, userrole.EdgeRole) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *UserRoleMutation) EdgeCleared(name string) bool { + switch name { + case userrole.EdgeUser: + return m.cleareduser + case userrole.EdgeRole: + return m.clearedrole + } + 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 *UserRoleMutation) ClearEdge(name string) error { + switch name { + case userrole.EdgeUser: + m.ClearUser() + return nil + case userrole.EdgeRole: + m.ClearRole() + return nil + } + return fmt.Errorf("unknown UserRole 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 *UserRoleMutation) ResetEdge(name string) error { + switch name { + case userrole.EdgeUser: + m.ResetUser() + return nil + case userrole.EdgeRole: + m.ResetRole() + return nil + } + return fmt.Errorf("unknown UserRole edge %s", name) +} diff --git a/internal/ent/permission.go b/internal/ent/permission.go new file mode 100644 index 0000000..aced3d0 --- /dev/null +++ b/internal/ent/permission.go @@ -0,0 +1,127 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" +) + +// Permission is the model entity for the Permission schema. +type Permission struct { + config `json:"-"` + // ID of the ent. + ID string `json:"id,omitempty"` + // Format: module.resource.action + Name string `json:"name,omitempty"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the PermissionQuery when eager-loading is set. + Edges PermissionEdges `json:"edges"` + selectValues sql.SelectValues +} + +// PermissionEdges holds the relations/edges for other nodes in the graph. +type PermissionEdges struct { + // RolePermissions holds the value of the role_permissions edge. + RolePermissions []*RolePermission `json:"role_permissions,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [1]bool +} + +// RolePermissionsOrErr returns the RolePermissions value or an error if the edge +// was not loaded in eager-loading. +func (e PermissionEdges) RolePermissionsOrErr() ([]*RolePermission, error) { + if e.loadedTypes[0] { + return e.RolePermissions, nil + } + return nil, &NotLoadedError{edge: "role_permissions"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*Permission) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case permission.FieldID, permission.FieldName: + values[i] = new(sql.NullString) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the Permission fields. +func (_m *Permission) 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 permission.FieldID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value.Valid { + _m.ID = value.String + } + case permission.FieldName: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field name", values[i]) + } else if value.Valid { + _m.Name = value.String + } + default: + _m.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the Permission. +// This includes values selected through modifiers, order, etc. +func (_m *Permission) Value(name string) (ent.Value, error) { + return _m.selectValues.Get(name) +} + +// QueryRolePermissions queries the "role_permissions" edge of the Permission entity. +func (_m *Permission) QueryRolePermissions() *RolePermissionQuery { + return NewPermissionClient(_m.config).QueryRolePermissions(_m) +} + +// Update returns a builder for updating this Permission. +// Note that you need to call Permission.Unwrap() before calling this method if this Permission +// was returned from a transaction, and the transaction was committed or rolled back. +func (_m *Permission) Update() *PermissionUpdateOne { + return NewPermissionClient(_m.config).UpdateOne(_m) +} + +// Unwrap unwraps the Permission 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 (_m *Permission) Unwrap() *Permission { + _tx, ok := _m.config.driver.(*txDriver) + if !ok { + panic("ent: Permission is not a transactional entity") + } + _m.config.driver = _tx.drv + return _m +} + +// String implements the fmt.Stringer. +func (_m *Permission) String() string { + var builder strings.Builder + builder.WriteString("Permission(") + builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("name=") + builder.WriteString(_m.Name) + builder.WriteByte(')') + return builder.String() +} + +// Permissions is a parsable slice of Permission. +type Permissions []*Permission diff --git a/internal/ent/permission/permission.go b/internal/ent/permission/permission.go new file mode 100644 index 0000000..2350fa4 --- /dev/null +++ b/internal/ent/permission/permission.go @@ -0,0 +1,83 @@ +// Code generated by ent, DO NOT EDIT. + +package permission + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" +) + +const ( + // Label holds the string label denoting the permission type in the database. + Label = "permission" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldName holds the string denoting the name field in the database. + FieldName = "name" + // EdgeRolePermissions holds the string denoting the role_permissions edge name in mutations. + EdgeRolePermissions = "role_permissions" + // Table holds the table name of the permission in the database. + Table = "permissions" + // RolePermissionsTable is the table that holds the role_permissions relation/edge. + RolePermissionsTable = "role_permissions" + // RolePermissionsInverseTable is the table name for the RolePermission entity. + // It exists in this package in order to avoid circular dependency with the "rolepermission" package. + RolePermissionsInverseTable = "role_permissions" + // RolePermissionsColumn is the table column denoting the role_permissions relation/edge. + RolePermissionsColumn = "permission_role_permissions" +) + +// Columns holds all SQL columns for permission fields. +var Columns = []string{ + FieldID, + FieldName, +} + +// 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 ( + // NameValidator is a validator for the "name" field. It is called by the builders before save. + NameValidator func(string) error +) + +// OrderOption defines the ordering options for the Permission queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByName orders the results by the name field. +func ByName(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldName, opts...).ToFunc() +} + +// ByRolePermissionsCount orders the results by role_permissions count. +func ByRolePermissionsCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newRolePermissionsStep(), opts...) + } +} + +// ByRolePermissions orders the results by role_permissions terms. +func ByRolePermissions(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newRolePermissionsStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} +func newRolePermissionsStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(RolePermissionsInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, RolePermissionsTable, RolePermissionsColumn), + ) +} diff --git a/internal/ent/permission/where.go b/internal/ent/permission/where.go new file mode 100644 index 0000000..35d0594 --- /dev/null +++ b/internal/ent/permission/where.go @@ -0,0 +1,172 @@ +// Code generated by ent, DO NOT EDIT. + +package permission + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id string) predicate.Permission { + return predicate.Permission(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id string) predicate.Permission { + return predicate.Permission(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id string) predicate.Permission { + return predicate.Permission(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...string) predicate.Permission { + return predicate.Permission(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...string) predicate.Permission { + return predicate.Permission(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id string) predicate.Permission { + return predicate.Permission(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id string) predicate.Permission { + return predicate.Permission(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id string) predicate.Permission { + return predicate.Permission(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id string) predicate.Permission { + return predicate.Permission(sql.FieldLTE(FieldID, id)) +} + +// IDEqualFold applies the EqualFold predicate on the ID field. +func IDEqualFold(id string) predicate.Permission { + return predicate.Permission(sql.FieldEqualFold(FieldID, id)) +} + +// IDContainsFold applies the ContainsFold predicate on the ID field. +func IDContainsFold(id string) predicate.Permission { + return predicate.Permission(sql.FieldContainsFold(FieldID, id)) +} + +// Name applies equality check predicate on the "name" field. It's identical to NameEQ. +func Name(v string) predicate.Permission { + return predicate.Permission(sql.FieldEQ(FieldName, v)) +} + +// NameEQ applies the EQ predicate on the "name" field. +func NameEQ(v string) predicate.Permission { + return predicate.Permission(sql.FieldEQ(FieldName, v)) +} + +// NameNEQ applies the NEQ predicate on the "name" field. +func NameNEQ(v string) predicate.Permission { + return predicate.Permission(sql.FieldNEQ(FieldName, v)) +} + +// NameIn applies the In predicate on the "name" field. +func NameIn(vs ...string) predicate.Permission { + return predicate.Permission(sql.FieldIn(FieldName, vs...)) +} + +// NameNotIn applies the NotIn predicate on the "name" field. +func NameNotIn(vs ...string) predicate.Permission { + return predicate.Permission(sql.FieldNotIn(FieldName, vs...)) +} + +// NameGT applies the GT predicate on the "name" field. +func NameGT(v string) predicate.Permission { + return predicate.Permission(sql.FieldGT(FieldName, v)) +} + +// NameGTE applies the GTE predicate on the "name" field. +func NameGTE(v string) predicate.Permission { + return predicate.Permission(sql.FieldGTE(FieldName, v)) +} + +// NameLT applies the LT predicate on the "name" field. +func NameLT(v string) predicate.Permission { + return predicate.Permission(sql.FieldLT(FieldName, v)) +} + +// NameLTE applies the LTE predicate on the "name" field. +func NameLTE(v string) predicate.Permission { + return predicate.Permission(sql.FieldLTE(FieldName, v)) +} + +// NameContains applies the Contains predicate on the "name" field. +func NameContains(v string) predicate.Permission { + return predicate.Permission(sql.FieldContains(FieldName, v)) +} + +// NameHasPrefix applies the HasPrefix predicate on the "name" field. +func NameHasPrefix(v string) predicate.Permission { + return predicate.Permission(sql.FieldHasPrefix(FieldName, v)) +} + +// NameHasSuffix applies the HasSuffix predicate on the "name" field. +func NameHasSuffix(v string) predicate.Permission { + return predicate.Permission(sql.FieldHasSuffix(FieldName, v)) +} + +// NameEqualFold applies the EqualFold predicate on the "name" field. +func NameEqualFold(v string) predicate.Permission { + return predicate.Permission(sql.FieldEqualFold(FieldName, v)) +} + +// NameContainsFold applies the ContainsFold predicate on the "name" field. +func NameContainsFold(v string) predicate.Permission { + return predicate.Permission(sql.FieldContainsFold(FieldName, v)) +} + +// HasRolePermissions applies the HasEdge predicate on the "role_permissions" edge. +func HasRolePermissions() predicate.Permission { + return predicate.Permission(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, RolePermissionsTable, RolePermissionsColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasRolePermissionsWith applies the HasEdge predicate on the "role_permissions" edge with a given conditions (other predicates). +func HasRolePermissionsWith(preds ...predicate.RolePermission) predicate.Permission { + return predicate.Permission(func(s *sql.Selector) { + step := newRolePermissionsStep() + 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.Permission) predicate.Permission { + return predicate.Permission(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.Permission) predicate.Permission { + return predicate.Permission(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.Permission) predicate.Permission { + return predicate.Permission(sql.NotPredicates(p)) +} diff --git a/internal/ent/permission_create.go b/internal/ent/permission_create.go new file mode 100644 index 0000000..db05dc1 --- /dev/null +++ b/internal/ent/permission_create.go @@ -0,0 +1,231 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// PermissionCreate is the builder for creating a Permission entity. +type PermissionCreate struct { + config + mutation *PermissionMutation + hooks []Hook +} + +// SetName sets the "name" field. +func (_c *PermissionCreate) SetName(v string) *PermissionCreate { + _c.mutation.SetName(v) + return _c +} + +// SetID sets the "id" field. +func (_c *PermissionCreate) SetID(v string) *PermissionCreate { + _c.mutation.SetID(v) + return _c +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by IDs. +func (_c *PermissionCreate) AddRolePermissionIDs(ids ...int) *PermissionCreate { + _c.mutation.AddRolePermissionIDs(ids...) + return _c +} + +// AddRolePermissions adds the "role_permissions" edges to the RolePermission entity. +func (_c *PermissionCreate) AddRolePermissions(v ...*RolePermission) *PermissionCreate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _c.AddRolePermissionIDs(ids...) +} + +// Mutation returns the PermissionMutation object of the builder. +func (_c *PermissionCreate) Mutation() *PermissionMutation { + return _c.mutation +} + +// Save creates the Permission in the database. +func (_c *PermissionCreate) Save(ctx context.Context) (*Permission, error) { + return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (_c *PermissionCreate) SaveX(ctx context.Context) *Permission { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *PermissionCreate) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *PermissionCreate) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_c *PermissionCreate) check() error { + if _, ok := _c.mutation.Name(); !ok { + return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Permission.name"`)} + } + if v, ok := _c.mutation.Name(); ok { + if err := permission.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Permission.name": %w`, err)} + } + } + return nil +} + +func (_c *PermissionCreate) sqlSave(ctx context.Context) (*Permission, 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 Permission.ID type: %T", _spec.ID.Value) + } + } + _c.mutation.id = &_node.ID + _c.mutation.done = true + return _node, nil +} + +func (_c *PermissionCreate) createSpec() (*Permission, *sqlgraph.CreateSpec) { + var ( + _node = &Permission{config: _c.config} + _spec = sqlgraph.NewCreateSpec(permission.Table, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString)) + ) + if id, ok := _c.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = id + } + if value, ok := _c.mutation.Name(); ok { + _spec.SetField(permission.FieldName, field.TypeString, value) + _node.Name = value + } + if nodes := _c.mutation.RolePermissionsIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } + return _node, _spec +} + +// PermissionCreateBulk is the builder for creating many Permission entities in bulk. +type PermissionCreateBulk struct { + config + err error + builders []*PermissionCreate +} + +// Save creates the Permission entities in the database. +func (_c *PermissionCreateBulk) Save(ctx context.Context) ([]*Permission, error) { + if _c.err != nil { + return nil, _c.err + } + specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) + nodes := make([]*Permission, len(_c.builders)) + mutators := make([]Mutator, len(_c.builders)) + for i := range _c.builders { + func(i int, root context.Context) { + builder := _c.builders[i] + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*PermissionMutation) + 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 *PermissionCreateBulk) SaveX(ctx context.Context) []*Permission { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *PermissionCreateBulk) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *PermissionCreateBulk) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/permission_delete.go b/internal/ent/permission_delete.go new file mode 100644 index 0000000..efb3360 --- /dev/null +++ b/internal/ent/permission_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" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// PermissionDelete is the builder for deleting a Permission entity. +type PermissionDelete struct { + config + hooks []Hook + mutation *PermissionMutation +} + +// Where appends a list predicates to the PermissionDelete builder. +func (_d *PermissionDelete) Where(ps ...predicate.Permission) *PermissionDelete { + _d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (_d *PermissionDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *PermissionDelete) ExecX(ctx context.Context) int { + n, err := _d.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (_d *PermissionDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(permission.Table, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString)) + if ps := _d.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + _d.mutation.done = true + return affected, err +} + +// PermissionDeleteOne is the builder for deleting a single Permission entity. +type PermissionDeleteOne struct { + _d *PermissionDelete +} + +// Where appends a list predicates to the PermissionDelete builder. +func (_d *PermissionDeleteOne) Where(ps ...predicate.Permission) *PermissionDeleteOne { + _d._d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query. +func (_d *PermissionDeleteOne) Exec(ctx context.Context) error { + n, err := _d._d.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{permission.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *PermissionDeleteOne) ExecX(ctx context.Context) { + if err := _d.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/permission_query.go b/internal/ent/permission_query.go new file mode 100644 index 0000000..8e13ee1 --- /dev/null +++ b/internal/ent/permission_query.go @@ -0,0 +1,607 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "database/sql/driver" + "fmt" + "math" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// PermissionQuery is the builder for querying Permission entities. +type PermissionQuery struct { + config + ctx *QueryContext + order []permission.OrderOption + inters []Interceptor + predicates []predicate.Permission + withRolePermissions *RolePermissionQuery + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the PermissionQuery builder. +func (_q *PermissionQuery) Where(ps ...predicate.Permission) *PermissionQuery { + _q.predicates = append(_q.predicates, ps...) + return _q +} + +// Limit the number of records to be returned by this query. +func (_q *PermissionQuery) Limit(limit int) *PermissionQuery { + _q.ctx.Limit = &limit + return _q +} + +// Offset to start from. +func (_q *PermissionQuery) Offset(offset int) *PermissionQuery { + _q.ctx.Offset = &offset + return _q +} + +// 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 (_q *PermissionQuery) Unique(unique bool) *PermissionQuery { + _q.ctx.Unique = &unique + return _q +} + +// Order specifies how the records should be ordered. +func (_q *PermissionQuery) Order(o ...permission.OrderOption) *PermissionQuery { + _q.order = append(_q.order, o...) + return _q +} + +// QueryRolePermissions chains the current query on the "role_permissions" edge. +func (_q *PermissionQuery) QueryRolePermissions() *RolePermissionQuery { + query := (&RolePermissionClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(permission.Table, permission.FieldID, selector), + sqlgraph.To(rolepermission.Table, rolepermission.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, permission.RolePermissionsTable, permission.RolePermissionsColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// First returns the first Permission entity from the query. +// Returns a *NotFoundError when no Permission was found. +func (_q *PermissionQuery) First(ctx context.Context) (*Permission, error) { + nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{permission.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (_q *PermissionQuery) FirstX(ctx context.Context) *Permission { + node, err := _q.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first Permission ID from the query. +// Returns a *NotFoundError when no Permission ID was found. +func (_q *PermissionQuery) FirstID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{permission.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (_q *PermissionQuery) FirstIDX(ctx context.Context) string { + id, err := _q.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single Permission entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one Permission entity is found. +// Returns a *NotFoundError when no Permission entities are found. +func (_q *PermissionQuery) Only(ctx context.Context) (*Permission, error) { + nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{permission.Label} + default: + return nil, &NotSingularError{permission.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (_q *PermissionQuery) OnlyX(ctx context.Context) *Permission { + node, err := _q.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only Permission ID in the query. +// Returns a *NotSingularError when more than one Permission ID is found. +// Returns a *NotFoundError when no entities are found. +func (_q *PermissionQuery) OnlyID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{permission.Label} + default: + err = &NotSingularError{permission.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (_q *PermissionQuery) OnlyIDX(ctx context.Context) string { + id, err := _q.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of Permissions. +func (_q *PermissionQuery) All(ctx context.Context) ([]*Permission, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll) + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*Permission, *PermissionQuery]() + return withInterceptors[[]*Permission](ctx, _q, qr, _q.inters) +} + +// AllX is like All, but panics if an error occurs. +func (_q *PermissionQuery) AllX(ctx context.Context) []*Permission { + nodes, err := _q.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of Permission IDs. +func (_q *PermissionQuery) IDs(ctx context.Context) (ids []string, err error) { + if _q.ctx.Unique == nil && _q.path != nil { + _q.Unique(true) + } + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs) + if err = _q.Select(permission.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (_q *PermissionQuery) IDsX(ctx context.Context) []string { + ids, err := _q.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (_q *PermissionQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount) + if err := _q.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, _q, querierCount[*PermissionQuery](), _q.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (_q *PermissionQuery) CountX(ctx context.Context) int { + count, err := _q.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (_q *PermissionQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist) + switch _, err := _q.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 (_q *PermissionQuery) ExistX(ctx context.Context) bool { + exist, err := _q.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the PermissionQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (_q *PermissionQuery) Clone() *PermissionQuery { + if _q == nil { + return nil + } + return &PermissionQuery{ + config: _q.config, + ctx: _q.ctx.Clone(), + order: append([]permission.OrderOption{}, _q.order...), + inters: append([]Interceptor{}, _q.inters...), + predicates: append([]predicate.Permission{}, _q.predicates...), + withRolePermissions: _q.withRolePermissions.Clone(), + // clone intermediate query. + sql: _q.sql.Clone(), + path: _q.path, + } +} + +// WithRolePermissions tells the query-builder to eager-load the nodes that are connected to +// the "role_permissions" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *PermissionQuery) WithRolePermissions(opts ...func(*RolePermissionQuery)) *PermissionQuery { + query := (&RolePermissionClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withRolePermissions = query + return _q +} + +// 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 { +// Name string `json:"name,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.Permission.Query(). +// GroupBy(permission.FieldName). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (_q *PermissionQuery) GroupBy(field string, fields ...string) *PermissionGroupBy { + _q.ctx.Fields = append([]string{field}, fields...) + grbuild := &PermissionGroupBy{build: _q} + grbuild.flds = &_q.ctx.Fields + grbuild.label = permission.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 { +// Name string `json:"name,omitempty"` +// } +// +// client.Permission.Query(). +// Select(permission.FieldName). +// Scan(ctx, &v) +func (_q *PermissionQuery) Select(fields ...string) *PermissionSelect { + _q.ctx.Fields = append(_q.ctx.Fields, fields...) + sbuild := &PermissionSelect{PermissionQuery: _q} + sbuild.label = permission.Label + sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a PermissionSelect configured with the given aggregations. +func (_q *PermissionQuery) Aggregate(fns ...AggregateFunc) *PermissionSelect { + return _q.Select().Aggregate(fns...) +} + +func (_q *PermissionQuery) prepareQuery(ctx context.Context) error { + for _, inter := range _q.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, _q); err != nil { + return err + } + } + } + for _, f := range _q.ctx.Fields { + if !permission.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if _q.path != nil { + prev, err := _q.path(ctx) + if err != nil { + return err + } + _q.sql = prev + } + return nil +} + +func (_q *PermissionQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Permission, error) { + var ( + nodes = []*Permission{} + _spec = _q.querySpec() + loadedTypes = [1]bool{ + _q.withRolePermissions != nil, + } + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*Permission).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &Permission{config: _q.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, _q.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := _q.withRolePermissions; query != nil { + if err := _q.loadRolePermissions(ctx, query, nodes, + func(n *Permission) { n.Edges.RolePermissions = []*RolePermission{} }, + func(n *Permission, e *RolePermission) { n.Edges.RolePermissions = append(n.Edges.RolePermissions, e) }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (_q *PermissionQuery) loadRolePermissions(ctx context.Context, query *RolePermissionQuery, nodes []*Permission, init func(*Permission), assign func(*Permission, *RolePermission)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[string]*Permission) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + query.withFKs = true + query.Where(predicate.RolePermission(func(s *sql.Selector) { + s.Where(sql.InValues(s.C(permission.RolePermissionsColumn), fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.permission_role_permissions + if fk == nil { + return fmt.Errorf(`foreign-key "permission_role_permissions" is nil for node %v`, n.ID) + } + node, ok := nodeids[*fk] + if !ok { + return fmt.Errorf(`unexpected referenced foreign-key "permission_role_permissions" returned %v for node %v`, *fk, n.ID) + } + assign(node, n) + } + return nil +} + +func (_q *PermissionQuery) sqlCount(ctx context.Context) (int, error) { + _spec := _q.querySpec() + _spec.Node.Columns = _q.ctx.Fields + if len(_q.ctx.Fields) > 0 { + _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique + } + return sqlgraph.CountNodes(ctx, _q.driver, _spec) +} + +func (_q *PermissionQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(permission.Table, permission.Columns, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString)) + _spec.From = _q.sql + if unique := _q.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if _q.path != nil { + _spec.Unique = true + } + if fields := _q.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, permission.FieldID) + for i := range fields { + if fields[i] != permission.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := _q.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := _q.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := _q.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := _q.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (_q *PermissionQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(_q.driver.Dialect()) + t1 := builder.Table(permission.Table) + columns := _q.ctx.Fields + if len(columns) == 0 { + columns = permission.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if _q.sql != nil { + selector = _q.sql + selector.Select(selector.Columns(columns...)...) + } + if _q.ctx.Unique != nil && *_q.ctx.Unique { + selector.Distinct() + } + for _, p := range _q.predicates { + p(selector) + } + for _, p := range _q.order { + p(selector) + } + if offset := _q.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 := _q.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// PermissionGroupBy is the group-by builder for Permission entities. +type PermissionGroupBy struct { + selector + build *PermissionQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (_g *PermissionGroupBy) Aggregate(fns ...AggregateFunc) *PermissionGroupBy { + _g.fns = append(_g.fns, fns...) + return _g +} + +// Scan applies the selector query and scans the result into the given value. +func (_g *PermissionGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*PermissionQuery, *PermissionGroupBy](ctx, _g.build, _g, _g.build.inters, v) +} + +func (_g *PermissionGroupBy) sqlScan(ctx context.Context, root *PermissionQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*_g.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// PermissionSelect is the builder for selecting fields of Permission entities. +type PermissionSelect struct { + *PermissionQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (_s *PermissionSelect) Aggregate(fns ...AggregateFunc) *PermissionSelect { + _s.fns = append(_s.fns, fns...) + return _s +} + +// Scan applies the selector query and scans the result into the given value. +func (_s *PermissionSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*PermissionQuery, *PermissionSelect](ctx, _s.PermissionQuery, _s, _s.inters, v) +} + +func (_s *PermissionSelect) sqlScan(ctx context.Context, root *PermissionQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/ent/permission_update.go b/internal/ent/permission_update.go new file mode 100644 index 0000000..b61d452 --- /dev/null +++ b/internal/ent/permission_update.go @@ -0,0 +1,398 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// PermissionUpdate is the builder for updating Permission entities. +type PermissionUpdate struct { + config + hooks []Hook + mutation *PermissionMutation +} + +// Where appends a list predicates to the PermissionUpdate builder. +func (_u *PermissionUpdate) Where(ps ...predicate.Permission) *PermissionUpdate { + _u.mutation.Where(ps...) + return _u +} + +// SetName sets the "name" field. +func (_u *PermissionUpdate) SetName(v string) *PermissionUpdate { + _u.mutation.SetName(v) + return _u +} + +// SetNillableName sets the "name" field if the given value is not nil. +func (_u *PermissionUpdate) SetNillableName(v *string) *PermissionUpdate { + if v != nil { + _u.SetName(*v) + } + return _u +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by IDs. +func (_u *PermissionUpdate) AddRolePermissionIDs(ids ...int) *PermissionUpdate { + _u.mutation.AddRolePermissionIDs(ids...) + return _u +} + +// AddRolePermissions adds the "role_permissions" edges to the RolePermission entity. +func (_u *PermissionUpdate) AddRolePermissions(v ...*RolePermission) *PermissionUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddRolePermissionIDs(ids...) +} + +// Mutation returns the PermissionMutation object of the builder. +func (_u *PermissionUpdate) Mutation() *PermissionMutation { + return _u.mutation +} + +// ClearRolePermissions clears all "role_permissions" edges to the RolePermission entity. +func (_u *PermissionUpdate) ClearRolePermissions() *PermissionUpdate { + _u.mutation.ClearRolePermissions() + return _u +} + +// RemoveRolePermissionIDs removes the "role_permissions" edge to RolePermission entities by IDs. +func (_u *PermissionUpdate) RemoveRolePermissionIDs(ids ...int) *PermissionUpdate { + _u.mutation.RemoveRolePermissionIDs(ids...) + return _u +} + +// RemoveRolePermissions removes "role_permissions" edges to RolePermission entities. +func (_u *PermissionUpdate) RemoveRolePermissions(v ...*RolePermission) *PermissionUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveRolePermissionIDs(ids...) +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (_u *PermissionUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *PermissionUpdate) SaveX(ctx context.Context) int { + affected, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (_u *PermissionUpdate) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *PermissionUpdate) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *PermissionUpdate) check() error { + if v, ok := _u.mutation.Name(); ok { + if err := permission.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Permission.name": %w`, err)} + } + } + return nil +} + +func (_u *PermissionUpdate) sqlSave(ctx context.Context) (_node int, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(permission.Table, permission.Columns, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString)) + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.Name(); ok { + _spec.SetField(permission.FieldName, field.TypeString, value) + } + if _u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedRolePermissionsIDs(); len(nodes) > 0 && !_u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RolePermissionsIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{permission.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + _u.mutation.done = true + return _node, nil +} + +// PermissionUpdateOne is the builder for updating a single Permission entity. +type PermissionUpdateOne struct { + config + fields []string + hooks []Hook + mutation *PermissionMutation +} + +// SetName sets the "name" field. +func (_u *PermissionUpdateOne) SetName(v string) *PermissionUpdateOne { + _u.mutation.SetName(v) + return _u +} + +// SetNillableName sets the "name" field if the given value is not nil. +func (_u *PermissionUpdateOne) SetNillableName(v *string) *PermissionUpdateOne { + if v != nil { + _u.SetName(*v) + } + return _u +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by IDs. +func (_u *PermissionUpdateOne) AddRolePermissionIDs(ids ...int) *PermissionUpdateOne { + _u.mutation.AddRolePermissionIDs(ids...) + return _u +} + +// AddRolePermissions adds the "role_permissions" edges to the RolePermission entity. +func (_u *PermissionUpdateOne) AddRolePermissions(v ...*RolePermission) *PermissionUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddRolePermissionIDs(ids...) +} + +// Mutation returns the PermissionMutation object of the builder. +func (_u *PermissionUpdateOne) Mutation() *PermissionMutation { + return _u.mutation +} + +// ClearRolePermissions clears all "role_permissions" edges to the RolePermission entity. +func (_u *PermissionUpdateOne) ClearRolePermissions() *PermissionUpdateOne { + _u.mutation.ClearRolePermissions() + return _u +} + +// RemoveRolePermissionIDs removes the "role_permissions" edge to RolePermission entities by IDs. +func (_u *PermissionUpdateOne) RemoveRolePermissionIDs(ids ...int) *PermissionUpdateOne { + _u.mutation.RemoveRolePermissionIDs(ids...) + return _u +} + +// RemoveRolePermissions removes "role_permissions" edges to RolePermission entities. +func (_u *PermissionUpdateOne) RemoveRolePermissions(v ...*RolePermission) *PermissionUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveRolePermissionIDs(ids...) +} + +// Where appends a list predicates to the PermissionUpdate builder. +func (_u *PermissionUpdateOne) Where(ps ...predicate.Permission) *PermissionUpdateOne { + _u.mutation.Where(ps...) + return _u +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (_u *PermissionUpdateOne) Select(field string, fields ...string) *PermissionUpdateOne { + _u.fields = append([]string{field}, fields...) + return _u +} + +// Save executes the query and returns the updated Permission entity. +func (_u *PermissionUpdateOne) Save(ctx context.Context) (*Permission, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *PermissionUpdateOne) SaveX(ctx context.Context) *Permission { + node, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (_u *PermissionUpdateOne) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *PermissionUpdateOne) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *PermissionUpdateOne) check() error { + if v, ok := _u.mutation.Name(); ok { + if err := permission.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Permission.name": %w`, err)} + } + } + return nil +} + +func (_u *PermissionUpdateOne) sqlSave(ctx context.Context) (_node *Permission, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(permission.Table, permission.Columns, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString)) + id, ok := _u.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Permission.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := _u.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, permission.FieldID) + for _, f := range fields { + if !permission.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != permission.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.Name(); ok { + _spec.SetField(permission.FieldName, field.TypeString, value) + } + if _u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedRolePermissionsIDs(); len(nodes) > 0 && !_u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RolePermissionsIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: permission.RolePermissionsTable, + Columns: []string{permission.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &Permission{config: _u.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{permission.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + _u.mutation.done = true + return _node, nil +} diff --git a/internal/ent/predicate/predicate.go b/internal/ent/predicate/predicate.go new file mode 100644 index 0000000..d1a17cf --- /dev/null +++ b/internal/ent/predicate/predicate.go @@ -0,0 +1,25 @@ +// Code generated by ent, DO NOT EDIT. + +package predicate + +import ( + "entgo.io/ent/dialect/sql" +) + +// AuditLog is the predicate function for auditlog builders. +type AuditLog func(*sql.Selector) + +// Permission is the predicate function for permission builders. +type Permission func(*sql.Selector) + +// Role is the predicate function for role builders. +type Role func(*sql.Selector) + +// RolePermission is the predicate function for rolepermission builders. +type RolePermission func(*sql.Selector) + +// User is the predicate function for user builders. +type User func(*sql.Selector) + +// UserRole is the predicate function for userrole builders. +type UserRole func(*sql.Selector) diff --git a/internal/ent/role.go b/internal/ent/role.go new file mode 100644 index 0000000..46d7e71 --- /dev/null +++ b/internal/ent/role.go @@ -0,0 +1,168 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + "time" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/role" +) + +// Role is the model entity for the Role schema. +type Role struct { + config `json:"-"` + // ID of the ent. + ID string `json:"id,omitempty"` + // Name holds the value of the "name" field. + Name string `json:"name,omitempty"` + // Description holds the value of the "description" field. + Description string `json:"description,omitempty"` + // CreatedAt holds the value of the "created_at" field. + CreatedAt time.Time `json:"created_at,omitempty"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the RoleQuery when eager-loading is set. + Edges RoleEdges `json:"edges"` + selectValues sql.SelectValues +} + +// RoleEdges holds the relations/edges for other nodes in the graph. +type RoleEdges struct { + // RolePermissions holds the value of the role_permissions edge. + RolePermissions []*RolePermission `json:"role_permissions,omitempty"` + // UserRoles holds the value of the user_roles edge. + UserRoles []*UserRole `json:"user_roles,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [2]bool +} + +// RolePermissionsOrErr returns the RolePermissions value or an error if the edge +// was not loaded in eager-loading. +func (e RoleEdges) RolePermissionsOrErr() ([]*RolePermission, error) { + if e.loadedTypes[0] { + return e.RolePermissions, nil + } + return nil, &NotLoadedError{edge: "role_permissions"} +} + +// UserRolesOrErr returns the UserRoles value or an error if the edge +// was not loaded in eager-loading. +func (e RoleEdges) UserRolesOrErr() ([]*UserRole, error) { + if e.loadedTypes[1] { + return e.UserRoles, nil + } + return nil, &NotLoadedError{edge: "user_roles"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*Role) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case role.FieldID, role.FieldName, role.FieldDescription: + values[i] = new(sql.NullString) + case role.FieldCreatedAt: + values[i] = new(sql.NullTime) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the Role fields. +func (_m *Role) 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 role.FieldID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value.Valid { + _m.ID = value.String + } + case role.FieldName: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field name", values[i]) + } else if value.Valid { + _m.Name = value.String + } + case role.FieldDescription: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field description", values[i]) + } else if value.Valid { + _m.Description = value.String + } + case role.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 { + _m.CreatedAt = value.Time + } + default: + _m.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the Role. +// This includes values selected through modifiers, order, etc. +func (_m *Role) Value(name string) (ent.Value, error) { + return _m.selectValues.Get(name) +} + +// QueryRolePermissions queries the "role_permissions" edge of the Role entity. +func (_m *Role) QueryRolePermissions() *RolePermissionQuery { + return NewRoleClient(_m.config).QueryRolePermissions(_m) +} + +// QueryUserRoles queries the "user_roles" edge of the Role entity. +func (_m *Role) QueryUserRoles() *UserRoleQuery { + return NewRoleClient(_m.config).QueryUserRoles(_m) +} + +// Update returns a builder for updating this Role. +// Note that you need to call Role.Unwrap() before calling this method if this Role +// was returned from a transaction, and the transaction was committed or rolled back. +func (_m *Role) Update() *RoleUpdateOne { + return NewRoleClient(_m.config).UpdateOne(_m) +} + +// Unwrap unwraps the Role 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 (_m *Role) Unwrap() *Role { + _tx, ok := _m.config.driver.(*txDriver) + if !ok { + panic("ent: Role is not a transactional entity") + } + _m.config.driver = _tx.drv + return _m +} + +// String implements the fmt.Stringer. +func (_m *Role) String() string { + var builder strings.Builder + builder.WriteString("Role(") + builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("name=") + builder.WriteString(_m.Name) + builder.WriteString(", ") + builder.WriteString("description=") + builder.WriteString(_m.Description) + builder.WriteString(", ") + builder.WriteString("created_at=") + builder.WriteString(_m.CreatedAt.Format(time.ANSIC)) + builder.WriteByte(')') + return builder.String() +} + +// Roles is a parsable slice of Role. +type Roles []*Role diff --git a/internal/ent/role/role.go b/internal/ent/role/role.go new file mode 100644 index 0000000..6f62145 --- /dev/null +++ b/internal/ent/role/role.go @@ -0,0 +1,133 @@ +// Code generated by ent, DO NOT EDIT. + +package role + +import ( + "time" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" +) + +const ( + // Label holds the string label denoting the role type in the database. + Label = "role" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldName holds the string denoting the name field in the database. + FieldName = "name" + // FieldDescription holds the string denoting the description field in the database. + FieldDescription = "description" + // FieldCreatedAt holds the string denoting the created_at field in the database. + FieldCreatedAt = "created_at" + // EdgeRolePermissions holds the string denoting the role_permissions edge name in mutations. + EdgeRolePermissions = "role_permissions" + // EdgeUserRoles holds the string denoting the user_roles edge name in mutations. + EdgeUserRoles = "user_roles" + // Table holds the table name of the role in the database. + Table = "roles" + // RolePermissionsTable is the table that holds the role_permissions relation/edge. + RolePermissionsTable = "role_permissions" + // RolePermissionsInverseTable is the table name for the RolePermission entity. + // It exists in this package in order to avoid circular dependency with the "rolepermission" package. + RolePermissionsInverseTable = "role_permissions" + // RolePermissionsColumn is the table column denoting the role_permissions relation/edge. + RolePermissionsColumn = "role_role_permissions" + // UserRolesTable is the table that holds the user_roles relation/edge. + UserRolesTable = "user_roles" + // UserRolesInverseTable is the table name for the UserRole entity. + // It exists in this package in order to avoid circular dependency with the "userrole" package. + UserRolesInverseTable = "user_roles" + // UserRolesColumn is the table column denoting the user_roles relation/edge. + UserRolesColumn = "role_user_roles" +) + +// Columns holds all SQL columns for role fields. +var Columns = []string{ + FieldID, + FieldName, + FieldDescription, + FieldCreatedAt, +} + +// 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 ( + // NameValidator is a validator for the "name" field. It is called by the builders before save. + NameValidator func(string) error + // DefaultCreatedAt holds the default value on creation for the "created_at" field. + DefaultCreatedAt func() time.Time +) + +// OrderOption defines the ordering options for the Role queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByName orders the results by the name field. +func ByName(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldName, opts...).ToFunc() +} + +// ByDescription orders the results by the description field. +func ByDescription(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldDescription, opts...).ToFunc() +} + +// ByCreatedAt orders the results by the created_at field. +func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() +} + +// ByRolePermissionsCount orders the results by role_permissions count. +func ByRolePermissionsCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newRolePermissionsStep(), opts...) + } +} + +// ByRolePermissions orders the results by role_permissions terms. +func ByRolePermissions(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newRolePermissionsStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} + +// ByUserRolesCount orders the results by user_roles count. +func ByUserRolesCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newUserRolesStep(), opts...) + } +} + +// ByUserRoles orders the results by user_roles terms. +func ByUserRoles(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newUserRolesStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} +func newRolePermissionsStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(RolePermissionsInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, RolePermissionsTable, RolePermissionsColumn), + ) +} +func newUserRolesStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(UserRolesInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, UserRolesTable, UserRolesColumn), + ) +} diff --git a/internal/ent/role/where.go b/internal/ent/role/where.go new file mode 100644 index 0000000..086045c --- /dev/null +++ b/internal/ent/role/where.go @@ -0,0 +1,322 @@ +// Code generated by ent, DO NOT EDIT. + +package role + +import ( + "time" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id string) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id string) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id string) predicate.Role { + return predicate.Role(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...string) predicate.Role { + return predicate.Role(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...string) predicate.Role { + return predicate.Role(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id string) predicate.Role { + return predicate.Role(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id string) predicate.Role { + return predicate.Role(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id string) predicate.Role { + return predicate.Role(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id string) predicate.Role { + return predicate.Role(sql.FieldLTE(FieldID, id)) +} + +// IDEqualFold applies the EqualFold predicate on the ID field. +func IDEqualFold(id string) predicate.Role { + return predicate.Role(sql.FieldEqualFold(FieldID, id)) +} + +// IDContainsFold applies the ContainsFold predicate on the ID field. +func IDContainsFold(id string) predicate.Role { + return predicate.Role(sql.FieldContainsFold(FieldID, id)) +} + +// Name applies equality check predicate on the "name" field. It's identical to NameEQ. +func Name(v string) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldName, v)) +} + +// Description applies equality check predicate on the "description" field. It's identical to DescriptionEQ. +func Description(v string) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldDescription, v)) +} + +// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. +func CreatedAt(v time.Time) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldCreatedAt, v)) +} + +// NameEQ applies the EQ predicate on the "name" field. +func NameEQ(v string) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldName, v)) +} + +// NameNEQ applies the NEQ predicate on the "name" field. +func NameNEQ(v string) predicate.Role { + return predicate.Role(sql.FieldNEQ(FieldName, v)) +} + +// NameIn applies the In predicate on the "name" field. +func NameIn(vs ...string) predicate.Role { + return predicate.Role(sql.FieldIn(FieldName, vs...)) +} + +// NameNotIn applies the NotIn predicate on the "name" field. +func NameNotIn(vs ...string) predicate.Role { + return predicate.Role(sql.FieldNotIn(FieldName, vs...)) +} + +// NameGT applies the GT predicate on the "name" field. +func NameGT(v string) predicate.Role { + return predicate.Role(sql.FieldGT(FieldName, v)) +} + +// NameGTE applies the GTE predicate on the "name" field. +func NameGTE(v string) predicate.Role { + return predicate.Role(sql.FieldGTE(FieldName, v)) +} + +// NameLT applies the LT predicate on the "name" field. +func NameLT(v string) predicate.Role { + return predicate.Role(sql.FieldLT(FieldName, v)) +} + +// NameLTE applies the LTE predicate on the "name" field. +func NameLTE(v string) predicate.Role { + return predicate.Role(sql.FieldLTE(FieldName, v)) +} + +// NameContains applies the Contains predicate on the "name" field. +func NameContains(v string) predicate.Role { + return predicate.Role(sql.FieldContains(FieldName, v)) +} + +// NameHasPrefix applies the HasPrefix predicate on the "name" field. +func NameHasPrefix(v string) predicate.Role { + return predicate.Role(sql.FieldHasPrefix(FieldName, v)) +} + +// NameHasSuffix applies the HasSuffix predicate on the "name" field. +func NameHasSuffix(v string) predicate.Role { + return predicate.Role(sql.FieldHasSuffix(FieldName, v)) +} + +// NameEqualFold applies the EqualFold predicate on the "name" field. +func NameEqualFold(v string) predicate.Role { + return predicate.Role(sql.FieldEqualFold(FieldName, v)) +} + +// NameContainsFold applies the ContainsFold predicate on the "name" field. +func NameContainsFold(v string) predicate.Role { + return predicate.Role(sql.FieldContainsFold(FieldName, v)) +} + +// DescriptionEQ applies the EQ predicate on the "description" field. +func DescriptionEQ(v string) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldDescription, v)) +} + +// DescriptionNEQ applies the NEQ predicate on the "description" field. +func DescriptionNEQ(v string) predicate.Role { + return predicate.Role(sql.FieldNEQ(FieldDescription, v)) +} + +// DescriptionIn applies the In predicate on the "description" field. +func DescriptionIn(vs ...string) predicate.Role { + return predicate.Role(sql.FieldIn(FieldDescription, vs...)) +} + +// DescriptionNotIn applies the NotIn predicate on the "description" field. +func DescriptionNotIn(vs ...string) predicate.Role { + return predicate.Role(sql.FieldNotIn(FieldDescription, vs...)) +} + +// DescriptionGT applies the GT predicate on the "description" field. +func DescriptionGT(v string) predicate.Role { + return predicate.Role(sql.FieldGT(FieldDescription, v)) +} + +// DescriptionGTE applies the GTE predicate on the "description" field. +func DescriptionGTE(v string) predicate.Role { + return predicate.Role(sql.FieldGTE(FieldDescription, v)) +} + +// DescriptionLT applies the LT predicate on the "description" field. +func DescriptionLT(v string) predicate.Role { + return predicate.Role(sql.FieldLT(FieldDescription, v)) +} + +// DescriptionLTE applies the LTE predicate on the "description" field. +func DescriptionLTE(v string) predicate.Role { + return predicate.Role(sql.FieldLTE(FieldDescription, v)) +} + +// DescriptionContains applies the Contains predicate on the "description" field. +func DescriptionContains(v string) predicate.Role { + return predicate.Role(sql.FieldContains(FieldDescription, v)) +} + +// DescriptionHasPrefix applies the HasPrefix predicate on the "description" field. +func DescriptionHasPrefix(v string) predicate.Role { + return predicate.Role(sql.FieldHasPrefix(FieldDescription, v)) +} + +// DescriptionHasSuffix applies the HasSuffix predicate on the "description" field. +func DescriptionHasSuffix(v string) predicate.Role { + return predicate.Role(sql.FieldHasSuffix(FieldDescription, v)) +} + +// DescriptionIsNil applies the IsNil predicate on the "description" field. +func DescriptionIsNil() predicate.Role { + return predicate.Role(sql.FieldIsNull(FieldDescription)) +} + +// DescriptionNotNil applies the NotNil predicate on the "description" field. +func DescriptionNotNil() predicate.Role { + return predicate.Role(sql.FieldNotNull(FieldDescription)) +} + +// DescriptionEqualFold applies the EqualFold predicate on the "description" field. +func DescriptionEqualFold(v string) predicate.Role { + return predicate.Role(sql.FieldEqualFold(FieldDescription, v)) +} + +// DescriptionContainsFold applies the ContainsFold predicate on the "description" field. +func DescriptionContainsFold(v string) predicate.Role { + return predicate.Role(sql.FieldContainsFold(FieldDescription, v)) +} + +// CreatedAtEQ applies the EQ predicate on the "created_at" field. +func CreatedAtEQ(v time.Time) predicate.Role { + return predicate.Role(sql.FieldEQ(FieldCreatedAt, v)) +} + +// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. +func CreatedAtNEQ(v time.Time) predicate.Role { + return predicate.Role(sql.FieldNEQ(FieldCreatedAt, v)) +} + +// CreatedAtIn applies the In predicate on the "created_at" field. +func CreatedAtIn(vs ...time.Time) predicate.Role { + return predicate.Role(sql.FieldIn(FieldCreatedAt, vs...)) +} + +// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. +func CreatedAtNotIn(vs ...time.Time) predicate.Role { + return predicate.Role(sql.FieldNotIn(FieldCreatedAt, vs...)) +} + +// CreatedAtGT applies the GT predicate on the "created_at" field. +func CreatedAtGT(v time.Time) predicate.Role { + return predicate.Role(sql.FieldGT(FieldCreatedAt, v)) +} + +// CreatedAtGTE applies the GTE predicate on the "created_at" field. +func CreatedAtGTE(v time.Time) predicate.Role { + return predicate.Role(sql.FieldGTE(FieldCreatedAt, v)) +} + +// CreatedAtLT applies the LT predicate on the "created_at" field. +func CreatedAtLT(v time.Time) predicate.Role { + return predicate.Role(sql.FieldLT(FieldCreatedAt, v)) +} + +// CreatedAtLTE applies the LTE predicate on the "created_at" field. +func CreatedAtLTE(v time.Time) predicate.Role { + return predicate.Role(sql.FieldLTE(FieldCreatedAt, v)) +} + +// HasRolePermissions applies the HasEdge predicate on the "role_permissions" edge. +func HasRolePermissions() predicate.Role { + return predicate.Role(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, RolePermissionsTable, RolePermissionsColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasRolePermissionsWith applies the HasEdge predicate on the "role_permissions" edge with a given conditions (other predicates). +func HasRolePermissionsWith(preds ...predicate.RolePermission) predicate.Role { + return predicate.Role(func(s *sql.Selector) { + step := newRolePermissionsStep() + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// HasUserRoles applies the HasEdge predicate on the "user_roles" edge. +func HasUserRoles() predicate.Role { + return predicate.Role(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, UserRolesTable, UserRolesColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasUserRolesWith applies the HasEdge predicate on the "user_roles" edge with a given conditions (other predicates). +func HasUserRolesWith(preds ...predicate.UserRole) predicate.Role { + return predicate.Role(func(s *sql.Selector) { + step := newUserRolesStep() + 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.Role) predicate.Role { + return predicate.Role(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.Role) predicate.Role { + return predicate.Role(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.Role) predicate.Role { + return predicate.Role(sql.NotPredicates(p)) +} diff --git a/internal/ent/role_create.go b/internal/ent/role_create.go new file mode 100644 index 0000000..221dadc --- /dev/null +++ b/internal/ent/role_create.go @@ -0,0 +1,313 @@ +// 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/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// RoleCreate is the builder for creating a Role entity. +type RoleCreate struct { + config + mutation *RoleMutation + hooks []Hook +} + +// SetName sets the "name" field. +func (_c *RoleCreate) SetName(v string) *RoleCreate { + _c.mutation.SetName(v) + return _c +} + +// SetDescription sets the "description" field. +func (_c *RoleCreate) SetDescription(v string) *RoleCreate { + _c.mutation.SetDescription(v) + return _c +} + +// SetNillableDescription sets the "description" field if the given value is not nil. +func (_c *RoleCreate) SetNillableDescription(v *string) *RoleCreate { + if v != nil { + _c.SetDescription(*v) + } + return _c +} + +// SetCreatedAt sets the "created_at" field. +func (_c *RoleCreate) SetCreatedAt(v time.Time) *RoleCreate { + _c.mutation.SetCreatedAt(v) + return _c +} + +// SetNillableCreatedAt sets the "created_at" field if the given value is not nil. +func (_c *RoleCreate) SetNillableCreatedAt(v *time.Time) *RoleCreate { + if v != nil { + _c.SetCreatedAt(*v) + } + return _c +} + +// SetID sets the "id" field. +func (_c *RoleCreate) SetID(v string) *RoleCreate { + _c.mutation.SetID(v) + return _c +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by IDs. +func (_c *RoleCreate) AddRolePermissionIDs(ids ...int) *RoleCreate { + _c.mutation.AddRolePermissionIDs(ids...) + return _c +} + +// AddRolePermissions adds the "role_permissions" edges to the RolePermission entity. +func (_c *RoleCreate) AddRolePermissions(v ...*RolePermission) *RoleCreate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _c.AddRolePermissionIDs(ids...) +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs. +func (_c *RoleCreate) AddUserRoleIDs(ids ...int) *RoleCreate { + _c.mutation.AddUserRoleIDs(ids...) + return _c +} + +// AddUserRoles adds the "user_roles" edges to the UserRole entity. +func (_c *RoleCreate) AddUserRoles(v ...*UserRole) *RoleCreate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _c.AddUserRoleIDs(ids...) +} + +// Mutation returns the RoleMutation object of the builder. +func (_c *RoleCreate) Mutation() *RoleMutation { + return _c.mutation +} + +// Save creates the Role in the database. +func (_c *RoleCreate) Save(ctx context.Context) (*Role, error) { + _c.defaults() + return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (_c *RoleCreate) SaveX(ctx context.Context) *Role { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *RoleCreate) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *RoleCreate) 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 *RoleCreate) defaults() { + if _, ok := _c.mutation.CreatedAt(); !ok { + v := role.DefaultCreatedAt() + _c.mutation.SetCreatedAt(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_c *RoleCreate) check() error { + if _, ok := _c.mutation.Name(); !ok { + return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Role.name"`)} + } + if v, ok := _c.mutation.Name(); ok { + if err := role.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Role.name": %w`, err)} + } + } + if _, ok := _c.mutation.CreatedAt(); !ok { + return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Role.created_at"`)} + } + return nil +} + +func (_c *RoleCreate) sqlSave(ctx context.Context) (*Role, 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 Role.ID type: %T", _spec.ID.Value) + } + } + _c.mutation.id = &_node.ID + _c.mutation.done = true + return _node, nil +} + +func (_c *RoleCreate) createSpec() (*Role, *sqlgraph.CreateSpec) { + var ( + _node = &Role{config: _c.config} + _spec = sqlgraph.NewCreateSpec(role.Table, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString)) + ) + if id, ok := _c.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = id + } + if value, ok := _c.mutation.Name(); ok { + _spec.SetField(role.FieldName, field.TypeString, value) + _node.Name = value + } + if value, ok := _c.mutation.Description(); ok { + _spec.SetField(role.FieldDescription, field.TypeString, value) + _node.Description = value + } + if value, ok := _c.mutation.CreatedAt(); ok { + _spec.SetField(role.FieldCreatedAt, field.TypeTime, value) + _node.CreatedAt = value + } + if nodes := _c.mutation.RolePermissionsIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } + if nodes := _c.mutation.UserRolesIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } + return _node, _spec +} + +// RoleCreateBulk is the builder for creating many Role entities in bulk. +type RoleCreateBulk struct { + config + err error + builders []*RoleCreate +} + +// Save creates the Role entities in the database. +func (_c *RoleCreateBulk) Save(ctx context.Context) ([]*Role, error) { + if _c.err != nil { + return nil, _c.err + } + specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) + nodes := make([]*Role, 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.(*RoleMutation) + 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 *RoleCreateBulk) SaveX(ctx context.Context) []*Role { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *RoleCreateBulk) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *RoleCreateBulk) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/role_delete.go b/internal/ent/role_delete.go new file mode 100644 index 0000000..3688b06 --- /dev/null +++ b/internal/ent/role_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" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" +) + +// RoleDelete is the builder for deleting a Role entity. +type RoleDelete struct { + config + hooks []Hook + mutation *RoleMutation +} + +// Where appends a list predicates to the RoleDelete builder. +func (_d *RoleDelete) Where(ps ...predicate.Role) *RoleDelete { + _d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (_d *RoleDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *RoleDelete) ExecX(ctx context.Context) int { + n, err := _d.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (_d *RoleDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(role.Table, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString)) + if ps := _d.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + _d.mutation.done = true + return affected, err +} + +// RoleDeleteOne is the builder for deleting a single Role entity. +type RoleDeleteOne struct { + _d *RoleDelete +} + +// Where appends a list predicates to the RoleDelete builder. +func (_d *RoleDeleteOne) Where(ps ...predicate.Role) *RoleDeleteOne { + _d._d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query. +func (_d *RoleDeleteOne) Exec(ctx context.Context) error { + n, err := _d._d.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{role.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *RoleDeleteOne) ExecX(ctx context.Context) { + if err := _d.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/role_query.go b/internal/ent/role_query.go new file mode 100644 index 0000000..c4e8e42 --- /dev/null +++ b/internal/ent/role_query.go @@ -0,0 +1,682 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "database/sql/driver" + "fmt" + "math" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// RoleQuery is the builder for querying Role entities. +type RoleQuery struct { + config + ctx *QueryContext + order []role.OrderOption + inters []Interceptor + predicates []predicate.Role + withRolePermissions *RolePermissionQuery + withUserRoles *UserRoleQuery + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the RoleQuery builder. +func (_q *RoleQuery) Where(ps ...predicate.Role) *RoleQuery { + _q.predicates = append(_q.predicates, ps...) + return _q +} + +// Limit the number of records to be returned by this query. +func (_q *RoleQuery) Limit(limit int) *RoleQuery { + _q.ctx.Limit = &limit + return _q +} + +// Offset to start from. +func (_q *RoleQuery) Offset(offset int) *RoleQuery { + _q.ctx.Offset = &offset + return _q +} + +// 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 (_q *RoleQuery) Unique(unique bool) *RoleQuery { + _q.ctx.Unique = &unique + return _q +} + +// Order specifies how the records should be ordered. +func (_q *RoleQuery) Order(o ...role.OrderOption) *RoleQuery { + _q.order = append(_q.order, o...) + return _q +} + +// QueryRolePermissions chains the current query on the "role_permissions" edge. +func (_q *RoleQuery) QueryRolePermissions() *RolePermissionQuery { + query := (&RolePermissionClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(role.Table, role.FieldID, selector), + sqlgraph.To(rolepermission.Table, rolepermission.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, role.RolePermissionsTable, role.RolePermissionsColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// QueryUserRoles chains the current query on the "user_roles" edge. +func (_q *RoleQuery) QueryUserRoles() *UserRoleQuery { + query := (&UserRoleClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(role.Table, role.FieldID, selector), + sqlgraph.To(userrole.Table, userrole.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, role.UserRolesTable, role.UserRolesColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// First returns the first Role entity from the query. +// Returns a *NotFoundError when no Role was found. +func (_q *RoleQuery) First(ctx context.Context) (*Role, error) { + nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{role.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (_q *RoleQuery) FirstX(ctx context.Context) *Role { + node, err := _q.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first Role ID from the query. +// Returns a *NotFoundError when no Role ID was found. +func (_q *RoleQuery) FirstID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{role.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (_q *RoleQuery) FirstIDX(ctx context.Context) string { + id, err := _q.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single Role entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one Role entity is found. +// Returns a *NotFoundError when no Role entities are found. +func (_q *RoleQuery) Only(ctx context.Context) (*Role, error) { + nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{role.Label} + default: + return nil, &NotSingularError{role.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (_q *RoleQuery) OnlyX(ctx context.Context) *Role { + node, err := _q.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only Role ID in the query. +// Returns a *NotSingularError when more than one Role ID is found. +// Returns a *NotFoundError when no entities are found. +func (_q *RoleQuery) OnlyID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{role.Label} + default: + err = &NotSingularError{role.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (_q *RoleQuery) OnlyIDX(ctx context.Context) string { + id, err := _q.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of Roles. +func (_q *RoleQuery) All(ctx context.Context) ([]*Role, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll) + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*Role, *RoleQuery]() + return withInterceptors[[]*Role](ctx, _q, qr, _q.inters) +} + +// AllX is like All, but panics if an error occurs. +func (_q *RoleQuery) AllX(ctx context.Context) []*Role { + nodes, err := _q.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of Role IDs. +func (_q *RoleQuery) IDs(ctx context.Context) (ids []string, err error) { + if _q.ctx.Unique == nil && _q.path != nil { + _q.Unique(true) + } + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs) + if err = _q.Select(role.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (_q *RoleQuery) IDsX(ctx context.Context) []string { + ids, err := _q.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (_q *RoleQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount) + if err := _q.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, _q, querierCount[*RoleQuery](), _q.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (_q *RoleQuery) CountX(ctx context.Context) int { + count, err := _q.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (_q *RoleQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist) + switch _, err := _q.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 (_q *RoleQuery) ExistX(ctx context.Context) bool { + exist, err := _q.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the RoleQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (_q *RoleQuery) Clone() *RoleQuery { + if _q == nil { + return nil + } + return &RoleQuery{ + config: _q.config, + ctx: _q.ctx.Clone(), + order: append([]role.OrderOption{}, _q.order...), + inters: append([]Interceptor{}, _q.inters...), + predicates: append([]predicate.Role{}, _q.predicates...), + withRolePermissions: _q.withRolePermissions.Clone(), + withUserRoles: _q.withUserRoles.Clone(), + // clone intermediate query. + sql: _q.sql.Clone(), + path: _q.path, + } +} + +// WithRolePermissions tells the query-builder to eager-load the nodes that are connected to +// the "role_permissions" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *RoleQuery) WithRolePermissions(opts ...func(*RolePermissionQuery)) *RoleQuery { + query := (&RolePermissionClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withRolePermissions = query + return _q +} + +// WithUserRoles tells the query-builder to eager-load the nodes that are connected to +// the "user_roles" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *RoleQuery) WithUserRoles(opts ...func(*UserRoleQuery)) *RoleQuery { + query := (&UserRoleClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withUserRoles = query + return _q +} + +// 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 { +// Name string `json:"name,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.Role.Query(). +// GroupBy(role.FieldName). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (_q *RoleQuery) GroupBy(field string, fields ...string) *RoleGroupBy { + _q.ctx.Fields = append([]string{field}, fields...) + grbuild := &RoleGroupBy{build: _q} + grbuild.flds = &_q.ctx.Fields + grbuild.label = role.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 { +// Name string `json:"name,omitempty"` +// } +// +// client.Role.Query(). +// Select(role.FieldName). +// Scan(ctx, &v) +func (_q *RoleQuery) Select(fields ...string) *RoleSelect { + _q.ctx.Fields = append(_q.ctx.Fields, fields...) + sbuild := &RoleSelect{RoleQuery: _q} + sbuild.label = role.Label + sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a RoleSelect configured with the given aggregations. +func (_q *RoleQuery) Aggregate(fns ...AggregateFunc) *RoleSelect { + return _q.Select().Aggregate(fns...) +} + +func (_q *RoleQuery) prepareQuery(ctx context.Context) error { + for _, inter := range _q.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, _q); err != nil { + return err + } + } + } + for _, f := range _q.ctx.Fields { + if !role.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if _q.path != nil { + prev, err := _q.path(ctx) + if err != nil { + return err + } + _q.sql = prev + } + return nil +} + +func (_q *RoleQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Role, error) { + var ( + nodes = []*Role{} + _spec = _q.querySpec() + loadedTypes = [2]bool{ + _q.withRolePermissions != nil, + _q.withUserRoles != nil, + } + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*Role).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &Role{config: _q.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, _q.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := _q.withRolePermissions; query != nil { + if err := _q.loadRolePermissions(ctx, query, nodes, + func(n *Role) { n.Edges.RolePermissions = []*RolePermission{} }, + func(n *Role, e *RolePermission) { n.Edges.RolePermissions = append(n.Edges.RolePermissions, e) }); err != nil { + return nil, err + } + } + if query := _q.withUserRoles; query != nil { + if err := _q.loadUserRoles(ctx, query, nodes, + func(n *Role) { n.Edges.UserRoles = []*UserRole{} }, + func(n *Role, e *UserRole) { n.Edges.UserRoles = append(n.Edges.UserRoles, e) }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (_q *RoleQuery) loadRolePermissions(ctx context.Context, query *RolePermissionQuery, nodes []*Role, init func(*Role), assign func(*Role, *RolePermission)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[string]*Role) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + query.withFKs = true + query.Where(predicate.RolePermission(func(s *sql.Selector) { + s.Where(sql.InValues(s.C(role.RolePermissionsColumn), fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.role_role_permissions + if fk == nil { + return fmt.Errorf(`foreign-key "role_role_permissions" is nil for node %v`, n.ID) + } + node, ok := nodeids[*fk] + if !ok { + return fmt.Errorf(`unexpected referenced foreign-key "role_role_permissions" returned %v for node %v`, *fk, n.ID) + } + assign(node, n) + } + return nil +} +func (_q *RoleQuery) loadUserRoles(ctx context.Context, query *UserRoleQuery, nodes []*Role, init func(*Role), assign func(*Role, *UserRole)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[string]*Role) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + query.withFKs = true + query.Where(predicate.UserRole(func(s *sql.Selector) { + s.Where(sql.InValues(s.C(role.UserRolesColumn), fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.role_user_roles + if fk == nil { + return fmt.Errorf(`foreign-key "role_user_roles" is nil for node %v`, n.ID) + } + node, ok := nodeids[*fk] + if !ok { + return fmt.Errorf(`unexpected referenced foreign-key "role_user_roles" returned %v for node %v`, *fk, n.ID) + } + assign(node, n) + } + return nil +} + +func (_q *RoleQuery) sqlCount(ctx context.Context) (int, error) { + _spec := _q.querySpec() + _spec.Node.Columns = _q.ctx.Fields + if len(_q.ctx.Fields) > 0 { + _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique + } + return sqlgraph.CountNodes(ctx, _q.driver, _spec) +} + +func (_q *RoleQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(role.Table, role.Columns, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString)) + _spec.From = _q.sql + if unique := _q.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if _q.path != nil { + _spec.Unique = true + } + if fields := _q.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, role.FieldID) + for i := range fields { + if fields[i] != role.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := _q.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := _q.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := _q.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := _q.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (_q *RoleQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(_q.driver.Dialect()) + t1 := builder.Table(role.Table) + columns := _q.ctx.Fields + if len(columns) == 0 { + columns = role.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if _q.sql != nil { + selector = _q.sql + selector.Select(selector.Columns(columns...)...) + } + if _q.ctx.Unique != nil && *_q.ctx.Unique { + selector.Distinct() + } + for _, p := range _q.predicates { + p(selector) + } + for _, p := range _q.order { + p(selector) + } + if offset := _q.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 := _q.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// RoleGroupBy is the group-by builder for Role entities. +type RoleGroupBy struct { + selector + build *RoleQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (_g *RoleGroupBy) Aggregate(fns ...AggregateFunc) *RoleGroupBy { + _g.fns = append(_g.fns, fns...) + return _g +} + +// Scan applies the selector query and scans the result into the given value. +func (_g *RoleGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*RoleQuery, *RoleGroupBy](ctx, _g.build, _g, _g.build.inters, v) +} + +func (_g *RoleGroupBy) sqlScan(ctx context.Context, root *RoleQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*_g.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// RoleSelect is the builder for selecting fields of Role entities. +type RoleSelect struct { + *RoleQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (_s *RoleSelect) Aggregate(fns ...AggregateFunc) *RoleSelect { + _s.fns = append(_s.fns, fns...) + return _s +} + +// Scan applies the selector query and scans the result into the given value. +func (_s *RoleSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*RoleQuery, *RoleSelect](ctx, _s.RoleQuery, _s, _s.inters, v) +} + +func (_s *RoleSelect) sqlScan(ctx context.Context, root *RoleQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/ent/role_update.go b/internal/ent/role_update.go new file mode 100644 index 0000000..c100bcf --- /dev/null +++ b/internal/ent/role_update.go @@ -0,0 +1,613 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// RoleUpdate is the builder for updating Role entities. +type RoleUpdate struct { + config + hooks []Hook + mutation *RoleMutation +} + +// Where appends a list predicates to the RoleUpdate builder. +func (_u *RoleUpdate) Where(ps ...predicate.Role) *RoleUpdate { + _u.mutation.Where(ps...) + return _u +} + +// SetName sets the "name" field. +func (_u *RoleUpdate) SetName(v string) *RoleUpdate { + _u.mutation.SetName(v) + return _u +} + +// SetNillableName sets the "name" field if the given value is not nil. +func (_u *RoleUpdate) SetNillableName(v *string) *RoleUpdate { + if v != nil { + _u.SetName(*v) + } + return _u +} + +// SetDescription sets the "description" field. +func (_u *RoleUpdate) SetDescription(v string) *RoleUpdate { + _u.mutation.SetDescription(v) + return _u +} + +// SetNillableDescription sets the "description" field if the given value is not nil. +func (_u *RoleUpdate) SetNillableDescription(v *string) *RoleUpdate { + if v != nil { + _u.SetDescription(*v) + } + return _u +} + +// ClearDescription clears the value of the "description" field. +func (_u *RoleUpdate) ClearDescription() *RoleUpdate { + _u.mutation.ClearDescription() + return _u +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by IDs. +func (_u *RoleUpdate) AddRolePermissionIDs(ids ...int) *RoleUpdate { + _u.mutation.AddRolePermissionIDs(ids...) + return _u +} + +// AddRolePermissions adds the "role_permissions" edges to the RolePermission entity. +func (_u *RoleUpdate) AddRolePermissions(v ...*RolePermission) *RoleUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddRolePermissionIDs(ids...) +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs. +func (_u *RoleUpdate) AddUserRoleIDs(ids ...int) *RoleUpdate { + _u.mutation.AddUserRoleIDs(ids...) + return _u +} + +// AddUserRoles adds the "user_roles" edges to the UserRole entity. +func (_u *RoleUpdate) AddUserRoles(v ...*UserRole) *RoleUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddUserRoleIDs(ids...) +} + +// Mutation returns the RoleMutation object of the builder. +func (_u *RoleUpdate) Mutation() *RoleMutation { + return _u.mutation +} + +// ClearRolePermissions clears all "role_permissions" edges to the RolePermission entity. +func (_u *RoleUpdate) ClearRolePermissions() *RoleUpdate { + _u.mutation.ClearRolePermissions() + return _u +} + +// RemoveRolePermissionIDs removes the "role_permissions" edge to RolePermission entities by IDs. +func (_u *RoleUpdate) RemoveRolePermissionIDs(ids ...int) *RoleUpdate { + _u.mutation.RemoveRolePermissionIDs(ids...) + return _u +} + +// RemoveRolePermissions removes "role_permissions" edges to RolePermission entities. +func (_u *RoleUpdate) RemoveRolePermissions(v ...*RolePermission) *RoleUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveRolePermissionIDs(ids...) +} + +// ClearUserRoles clears all "user_roles" edges to the UserRole entity. +func (_u *RoleUpdate) ClearUserRoles() *RoleUpdate { + _u.mutation.ClearUserRoles() + return _u +} + +// RemoveUserRoleIDs removes the "user_roles" edge to UserRole entities by IDs. +func (_u *RoleUpdate) RemoveUserRoleIDs(ids ...int) *RoleUpdate { + _u.mutation.RemoveUserRoleIDs(ids...) + return _u +} + +// RemoveUserRoles removes "user_roles" edges to UserRole entities. +func (_u *RoleUpdate) RemoveUserRoles(v ...*UserRole) *RoleUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveUserRoleIDs(ids...) +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (_u *RoleUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *RoleUpdate) SaveX(ctx context.Context) int { + affected, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (_u *RoleUpdate) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *RoleUpdate) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *RoleUpdate) check() error { + if v, ok := _u.mutation.Name(); ok { + if err := role.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Role.name": %w`, err)} + } + } + return nil +} + +func (_u *RoleUpdate) sqlSave(ctx context.Context) (_node int, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(role.Table, role.Columns, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString)) + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.Name(); ok { + _spec.SetField(role.FieldName, field.TypeString, value) + } + if value, ok := _u.mutation.Description(); ok { + _spec.SetField(role.FieldDescription, field.TypeString, value) + } + if _u.mutation.DescriptionCleared() { + _spec.ClearField(role.FieldDescription, field.TypeString) + } + if _u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedRolePermissionsIDs(); len(nodes) > 0 && !_u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RolePermissionsIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedUserRolesIDs(); len(nodes) > 0 && !_u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.UserRolesIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{role.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + _u.mutation.done = true + return _node, nil +} + +// RoleUpdateOne is the builder for updating a single Role entity. +type RoleUpdateOne struct { + config + fields []string + hooks []Hook + mutation *RoleMutation +} + +// SetName sets the "name" field. +func (_u *RoleUpdateOne) SetName(v string) *RoleUpdateOne { + _u.mutation.SetName(v) + return _u +} + +// SetNillableName sets the "name" field if the given value is not nil. +func (_u *RoleUpdateOne) SetNillableName(v *string) *RoleUpdateOne { + if v != nil { + _u.SetName(*v) + } + return _u +} + +// SetDescription sets the "description" field. +func (_u *RoleUpdateOne) SetDescription(v string) *RoleUpdateOne { + _u.mutation.SetDescription(v) + return _u +} + +// SetNillableDescription sets the "description" field if the given value is not nil. +func (_u *RoleUpdateOne) SetNillableDescription(v *string) *RoleUpdateOne { + if v != nil { + _u.SetDescription(*v) + } + return _u +} + +// ClearDescription clears the value of the "description" field. +func (_u *RoleUpdateOne) ClearDescription() *RoleUpdateOne { + _u.mutation.ClearDescription() + return _u +} + +// AddRolePermissionIDs adds the "role_permissions" edge to the RolePermission entity by IDs. +func (_u *RoleUpdateOne) AddRolePermissionIDs(ids ...int) *RoleUpdateOne { + _u.mutation.AddRolePermissionIDs(ids...) + return _u +} + +// AddRolePermissions adds the "role_permissions" edges to the RolePermission entity. +func (_u *RoleUpdateOne) AddRolePermissions(v ...*RolePermission) *RoleUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddRolePermissionIDs(ids...) +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs. +func (_u *RoleUpdateOne) AddUserRoleIDs(ids ...int) *RoleUpdateOne { + _u.mutation.AddUserRoleIDs(ids...) + return _u +} + +// AddUserRoles adds the "user_roles" edges to the UserRole entity. +func (_u *RoleUpdateOne) AddUserRoles(v ...*UserRole) *RoleUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddUserRoleIDs(ids...) +} + +// Mutation returns the RoleMutation object of the builder. +func (_u *RoleUpdateOne) Mutation() *RoleMutation { + return _u.mutation +} + +// ClearRolePermissions clears all "role_permissions" edges to the RolePermission entity. +func (_u *RoleUpdateOne) ClearRolePermissions() *RoleUpdateOne { + _u.mutation.ClearRolePermissions() + return _u +} + +// RemoveRolePermissionIDs removes the "role_permissions" edge to RolePermission entities by IDs. +func (_u *RoleUpdateOne) RemoveRolePermissionIDs(ids ...int) *RoleUpdateOne { + _u.mutation.RemoveRolePermissionIDs(ids...) + return _u +} + +// RemoveRolePermissions removes "role_permissions" edges to RolePermission entities. +func (_u *RoleUpdateOne) RemoveRolePermissions(v ...*RolePermission) *RoleUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveRolePermissionIDs(ids...) +} + +// ClearUserRoles clears all "user_roles" edges to the UserRole entity. +func (_u *RoleUpdateOne) ClearUserRoles() *RoleUpdateOne { + _u.mutation.ClearUserRoles() + return _u +} + +// RemoveUserRoleIDs removes the "user_roles" edge to UserRole entities by IDs. +func (_u *RoleUpdateOne) RemoveUserRoleIDs(ids ...int) *RoleUpdateOne { + _u.mutation.RemoveUserRoleIDs(ids...) + return _u +} + +// RemoveUserRoles removes "user_roles" edges to UserRole entities. +func (_u *RoleUpdateOne) RemoveUserRoles(v ...*UserRole) *RoleUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveUserRoleIDs(ids...) +} + +// Where appends a list predicates to the RoleUpdate builder. +func (_u *RoleUpdateOne) Where(ps ...predicate.Role) *RoleUpdateOne { + _u.mutation.Where(ps...) + return _u +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (_u *RoleUpdateOne) Select(field string, fields ...string) *RoleUpdateOne { + _u.fields = append([]string{field}, fields...) + return _u +} + +// Save executes the query and returns the updated Role entity. +func (_u *RoleUpdateOne) Save(ctx context.Context) (*Role, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *RoleUpdateOne) SaveX(ctx context.Context) *Role { + node, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (_u *RoleUpdateOne) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *RoleUpdateOne) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *RoleUpdateOne) check() error { + if v, ok := _u.mutation.Name(); ok { + if err := role.NameValidator(v); err != nil { + return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Role.name": %w`, err)} + } + } + return nil +} + +func (_u *RoleUpdateOne) sqlSave(ctx context.Context) (_node *Role, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(role.Table, role.Columns, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString)) + id, ok := _u.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Role.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := _u.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, role.FieldID) + for _, f := range fields { + if !role.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != role.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.Name(); ok { + _spec.SetField(role.FieldName, field.TypeString, value) + } + if value, ok := _u.mutation.Description(); ok { + _spec.SetField(role.FieldDescription, field.TypeString, value) + } + if _u.mutation.DescriptionCleared() { + _spec.ClearField(role.FieldDescription, field.TypeString) + } + if _u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedRolePermissionsIDs(); len(nodes) > 0 && !_u.mutation.RolePermissionsCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RolePermissionsIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.RolePermissionsTable, + Columns: []string{role.RolePermissionsColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedUserRolesIDs(); len(nodes) > 0 && !_u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.UserRolesIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: role.UserRolesTable, + Columns: []string{role.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &Role{config: _u.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{role.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + _u.mutation.done = true + return _node, nil +} diff --git a/internal/ent/rolepermission.go b/internal/ent/rolepermission.go new file mode 100644 index 0000000..c92e26a --- /dev/null +++ b/internal/ent/rolepermission.go @@ -0,0 +1,182 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// RolePermission is the model entity for the RolePermission schema. +type RolePermission struct { + config `json:"-"` + // ID of the ent. + ID int `json:"id,omitempty"` + // RoleID holds the value of the "role_id" field. + RoleID string `json:"role_id,omitempty"` + // PermissionID holds the value of the "permission_id" field. + PermissionID string `json:"permission_id,omitempty"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the RolePermissionQuery when eager-loading is set. + Edges RolePermissionEdges `json:"edges"` + permission_role_permissions *string + role_role_permissions *string + selectValues sql.SelectValues +} + +// RolePermissionEdges holds the relations/edges for other nodes in the graph. +type RolePermissionEdges struct { + // Role holds the value of the role edge. + Role *Role `json:"role,omitempty"` + // Permission holds the value of the permission edge. + Permission *Permission `json:"permission,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [2]bool +} + +// RoleOrErr returns the Role value or an error if the edge +// was not loaded in eager-loading, or loaded but was not found. +func (e RolePermissionEdges) RoleOrErr() (*Role, error) { + if e.Role != nil { + return e.Role, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: role.Label} + } + return nil, &NotLoadedError{edge: "role"} +} + +// PermissionOrErr returns the Permission value or an error if the edge +// was not loaded in eager-loading, or loaded but was not found. +func (e RolePermissionEdges) PermissionOrErr() (*Permission, error) { + if e.Permission != nil { + return e.Permission, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: permission.Label} + } + return nil, &NotLoadedError{edge: "permission"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*RolePermission) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case rolepermission.FieldID: + values[i] = new(sql.NullInt64) + case rolepermission.FieldRoleID, rolepermission.FieldPermissionID: + values[i] = new(sql.NullString) + case rolepermission.ForeignKeys[0]: // permission_role_permissions + values[i] = new(sql.NullString) + case rolepermission.ForeignKeys[1]: // role_role_permissions + values[i] = new(sql.NullString) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the RolePermission fields. +func (_m *RolePermission) 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 rolepermission.FieldID: + value, ok := values[i].(*sql.NullInt64) + if !ok { + return fmt.Errorf("unexpected type %T for field id", value) + } + _m.ID = int(value.Int64) + case rolepermission.FieldRoleID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field role_id", values[i]) + } else if value.Valid { + _m.RoleID = value.String + } + case rolepermission.FieldPermissionID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field permission_id", values[i]) + } else if value.Valid { + _m.PermissionID = value.String + } + case rolepermission.ForeignKeys[0]: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field permission_role_permissions", values[i]) + } else if value.Valid { + _m.permission_role_permissions = new(string) + *_m.permission_role_permissions = value.String + } + case rolepermission.ForeignKeys[1]: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field role_role_permissions", values[i]) + } else if value.Valid { + _m.role_role_permissions = new(string) + *_m.role_role_permissions = value.String + } + default: + _m.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the RolePermission. +// This includes values selected through modifiers, order, etc. +func (_m *RolePermission) Value(name string) (ent.Value, error) { + return _m.selectValues.Get(name) +} + +// QueryRole queries the "role" edge of the RolePermission entity. +func (_m *RolePermission) QueryRole() *RoleQuery { + return NewRolePermissionClient(_m.config).QueryRole(_m) +} + +// QueryPermission queries the "permission" edge of the RolePermission entity. +func (_m *RolePermission) QueryPermission() *PermissionQuery { + return NewRolePermissionClient(_m.config).QueryPermission(_m) +} + +// Update returns a builder for updating this RolePermission. +// Note that you need to call RolePermission.Unwrap() before calling this method if this RolePermission +// was returned from a transaction, and the transaction was committed or rolled back. +func (_m *RolePermission) Update() *RolePermissionUpdateOne { + return NewRolePermissionClient(_m.config).UpdateOne(_m) +} + +// Unwrap unwraps the RolePermission 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 (_m *RolePermission) Unwrap() *RolePermission { + _tx, ok := _m.config.driver.(*txDriver) + if !ok { + panic("ent: RolePermission is not a transactional entity") + } + _m.config.driver = _tx.drv + return _m +} + +// String implements the fmt.Stringer. +func (_m *RolePermission) String() string { + var builder strings.Builder + builder.WriteString("RolePermission(") + builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("role_id=") + builder.WriteString(_m.RoleID) + builder.WriteString(", ") + builder.WriteString("permission_id=") + builder.WriteString(_m.PermissionID) + builder.WriteByte(')') + return builder.String() +} + +// RolePermissions is a parsable slice of RolePermission. +type RolePermissions []*RolePermission diff --git a/internal/ent/rolepermission/rolepermission.go b/internal/ent/rolepermission/rolepermission.go new file mode 100644 index 0000000..74260d3 --- /dev/null +++ b/internal/ent/rolepermission/rolepermission.go @@ -0,0 +1,114 @@ +// Code generated by ent, DO NOT EDIT. + +package rolepermission + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" +) + +const ( + // Label holds the string label denoting the rolepermission type in the database. + Label = "role_permission" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldRoleID holds the string denoting the role_id field in the database. + FieldRoleID = "role_id" + // FieldPermissionID holds the string denoting the permission_id field in the database. + FieldPermissionID = "permission_id" + // EdgeRole holds the string denoting the role edge name in mutations. + EdgeRole = "role" + // EdgePermission holds the string denoting the permission edge name in mutations. + EdgePermission = "permission" + // Table holds the table name of the rolepermission in the database. + Table = "role_permissions" + // RoleTable is the table that holds the role relation/edge. + RoleTable = "role_permissions" + // RoleInverseTable is the table name for the Role entity. + // It exists in this package in order to avoid circular dependency with the "role" package. + RoleInverseTable = "roles" + // RoleColumn is the table column denoting the role relation/edge. + RoleColumn = "role_id" + // PermissionTable is the table that holds the permission relation/edge. + PermissionTable = "role_permissions" + // PermissionInverseTable is the table name for the Permission entity. + // It exists in this package in order to avoid circular dependency with the "permission" package. + PermissionInverseTable = "permissions" + // PermissionColumn is the table column denoting the permission relation/edge. + PermissionColumn = "permission_id" +) + +// Columns holds all SQL columns for rolepermission fields. +var Columns = []string{ + FieldID, + FieldRoleID, + FieldPermissionID, +} + +// ForeignKeys holds the SQL foreign-keys that are owned by the "role_permissions" +// table and are not defined as standalone fields in the schema. +var ForeignKeys = []string{ + "permission_role_permissions", + "role_role_permissions", +} + +// 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 + } + } + for i := range ForeignKeys { + if column == ForeignKeys[i] { + return true + } + } + return false +} + +// OrderOption defines the ordering options for the RolePermission queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByRoleID orders the results by the role_id field. +func ByRoleID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldRoleID, opts...).ToFunc() +} + +// ByPermissionID orders the results by the permission_id field. +func ByPermissionID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldPermissionID, opts...).ToFunc() +} + +// ByRoleField orders the results by role field. +func ByRoleField(field string, opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newRoleStep(), sql.OrderByField(field, opts...)) + } +} + +// ByPermissionField orders the results by permission field. +func ByPermissionField(field string, opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newPermissionStep(), sql.OrderByField(field, opts...)) + } +} +func newRoleStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(RoleInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, RoleTable, RoleColumn), + ) +} +func newPermissionStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(PermissionInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, PermissionTable, PermissionColumn), + ) +} diff --git a/internal/ent/rolepermission/where.go b/internal/ent/rolepermission/where.go new file mode 100644 index 0000000..70539b8 --- /dev/null +++ b/internal/ent/rolepermission/where.go @@ -0,0 +1,255 @@ +// Code generated by ent, DO NOT EDIT. + +package rolepermission + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id int) predicate.RolePermission { + return predicate.RolePermission(sql.FieldLTE(FieldID, id)) +} + +// RoleID applies equality check predicate on the "role_id" field. It's identical to RoleIDEQ. +func RoleID(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEQ(FieldRoleID, v)) +} + +// PermissionID applies equality check predicate on the "permission_id" field. It's identical to PermissionIDEQ. +func PermissionID(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEQ(FieldPermissionID, v)) +} + +// RoleIDEQ applies the EQ predicate on the "role_id" field. +func RoleIDEQ(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEQ(FieldRoleID, v)) +} + +// RoleIDNEQ applies the NEQ predicate on the "role_id" field. +func RoleIDNEQ(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldNEQ(FieldRoleID, v)) +} + +// RoleIDIn applies the In predicate on the "role_id" field. +func RoleIDIn(vs ...string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldIn(FieldRoleID, vs...)) +} + +// RoleIDNotIn applies the NotIn predicate on the "role_id" field. +func RoleIDNotIn(vs ...string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldNotIn(FieldRoleID, vs...)) +} + +// RoleIDGT applies the GT predicate on the "role_id" field. +func RoleIDGT(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldGT(FieldRoleID, v)) +} + +// RoleIDGTE applies the GTE predicate on the "role_id" field. +func RoleIDGTE(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldGTE(FieldRoleID, v)) +} + +// RoleIDLT applies the LT predicate on the "role_id" field. +func RoleIDLT(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldLT(FieldRoleID, v)) +} + +// RoleIDLTE applies the LTE predicate on the "role_id" field. +func RoleIDLTE(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldLTE(FieldRoleID, v)) +} + +// RoleIDContains applies the Contains predicate on the "role_id" field. +func RoleIDContains(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldContains(FieldRoleID, v)) +} + +// RoleIDHasPrefix applies the HasPrefix predicate on the "role_id" field. +func RoleIDHasPrefix(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldHasPrefix(FieldRoleID, v)) +} + +// RoleIDHasSuffix applies the HasSuffix predicate on the "role_id" field. +func RoleIDHasSuffix(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldHasSuffix(FieldRoleID, v)) +} + +// RoleIDEqualFold applies the EqualFold predicate on the "role_id" field. +func RoleIDEqualFold(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEqualFold(FieldRoleID, v)) +} + +// RoleIDContainsFold applies the ContainsFold predicate on the "role_id" field. +func RoleIDContainsFold(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldContainsFold(FieldRoleID, v)) +} + +// PermissionIDEQ applies the EQ predicate on the "permission_id" field. +func PermissionIDEQ(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEQ(FieldPermissionID, v)) +} + +// PermissionIDNEQ applies the NEQ predicate on the "permission_id" field. +func PermissionIDNEQ(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldNEQ(FieldPermissionID, v)) +} + +// PermissionIDIn applies the In predicate on the "permission_id" field. +func PermissionIDIn(vs ...string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldIn(FieldPermissionID, vs...)) +} + +// PermissionIDNotIn applies the NotIn predicate on the "permission_id" field. +func PermissionIDNotIn(vs ...string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldNotIn(FieldPermissionID, vs...)) +} + +// PermissionIDGT applies the GT predicate on the "permission_id" field. +func PermissionIDGT(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldGT(FieldPermissionID, v)) +} + +// PermissionIDGTE applies the GTE predicate on the "permission_id" field. +func PermissionIDGTE(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldGTE(FieldPermissionID, v)) +} + +// PermissionIDLT applies the LT predicate on the "permission_id" field. +func PermissionIDLT(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldLT(FieldPermissionID, v)) +} + +// PermissionIDLTE applies the LTE predicate on the "permission_id" field. +func PermissionIDLTE(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldLTE(FieldPermissionID, v)) +} + +// PermissionIDContains applies the Contains predicate on the "permission_id" field. +func PermissionIDContains(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldContains(FieldPermissionID, v)) +} + +// PermissionIDHasPrefix applies the HasPrefix predicate on the "permission_id" field. +func PermissionIDHasPrefix(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldHasPrefix(FieldPermissionID, v)) +} + +// PermissionIDHasSuffix applies the HasSuffix predicate on the "permission_id" field. +func PermissionIDHasSuffix(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldHasSuffix(FieldPermissionID, v)) +} + +// PermissionIDEqualFold applies the EqualFold predicate on the "permission_id" field. +func PermissionIDEqualFold(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldEqualFold(FieldPermissionID, v)) +} + +// PermissionIDContainsFold applies the ContainsFold predicate on the "permission_id" field. +func PermissionIDContainsFold(v string) predicate.RolePermission { + return predicate.RolePermission(sql.FieldContainsFold(FieldPermissionID, v)) +} + +// HasRole applies the HasEdge predicate on the "role" edge. +func HasRole() predicate.RolePermission { + return predicate.RolePermission(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, RoleTable, RoleColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasRoleWith applies the HasEdge predicate on the "role" edge with a given conditions (other predicates). +func HasRoleWith(preds ...predicate.Role) predicate.RolePermission { + return predicate.RolePermission(func(s *sql.Selector) { + step := newRoleStep() + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// HasPermission applies the HasEdge predicate on the "permission" edge. +func HasPermission() predicate.RolePermission { + return predicate.RolePermission(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, PermissionTable, PermissionColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasPermissionWith applies the HasEdge predicate on the "permission" edge with a given conditions (other predicates). +func HasPermissionWith(preds ...predicate.Permission) predicate.RolePermission { + return predicate.RolePermission(func(s *sql.Selector) { + step := newPermissionStep() + 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.RolePermission) predicate.RolePermission { + return predicate.RolePermission(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.RolePermission) predicate.RolePermission { + return predicate.RolePermission(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.RolePermission) predicate.RolePermission { + return predicate.RolePermission(sql.NotPredicates(p)) +} diff --git a/internal/ent/rolepermission_create.go b/internal/ent/rolepermission_create.go new file mode 100644 index 0000000..dbc1ba3 --- /dev/null +++ b/internal/ent/rolepermission_create.go @@ -0,0 +1,240 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// RolePermissionCreate is the builder for creating a RolePermission entity. +type RolePermissionCreate struct { + config + mutation *RolePermissionMutation + hooks []Hook +} + +// SetRoleID sets the "role_id" field. +func (_c *RolePermissionCreate) SetRoleID(v string) *RolePermissionCreate { + _c.mutation.SetRoleID(v) + return _c +} + +// SetPermissionID sets the "permission_id" field. +func (_c *RolePermissionCreate) SetPermissionID(v string) *RolePermissionCreate { + _c.mutation.SetPermissionID(v) + return _c +} + +// SetRole sets the "role" edge to the Role entity. +func (_c *RolePermissionCreate) SetRole(v *Role) *RolePermissionCreate { + return _c.SetRoleID(v.ID) +} + +// SetPermission sets the "permission" edge to the Permission entity. +func (_c *RolePermissionCreate) SetPermission(v *Permission) *RolePermissionCreate { + return _c.SetPermissionID(v.ID) +} + +// Mutation returns the RolePermissionMutation object of the builder. +func (_c *RolePermissionCreate) Mutation() *RolePermissionMutation { + return _c.mutation +} + +// Save creates the RolePermission in the database. +func (_c *RolePermissionCreate) Save(ctx context.Context) (*RolePermission, error) { + return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (_c *RolePermissionCreate) SaveX(ctx context.Context) *RolePermission { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *RolePermissionCreate) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *RolePermissionCreate) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_c *RolePermissionCreate) check() error { + if _, ok := _c.mutation.RoleID(); !ok { + return &ValidationError{Name: "role_id", err: errors.New(`ent: missing required field "RolePermission.role_id"`)} + } + if _, ok := _c.mutation.PermissionID(); !ok { + return &ValidationError{Name: "permission_id", err: errors.New(`ent: missing required field "RolePermission.permission_id"`)} + } + if len(_c.mutation.RoleIDs()) == 0 { + return &ValidationError{Name: "role", err: errors.New(`ent: missing required edge "RolePermission.role"`)} + } + if len(_c.mutation.PermissionIDs()) == 0 { + return &ValidationError{Name: "permission", err: errors.New(`ent: missing required edge "RolePermission.permission"`)} + } + return nil +} + +func (_c *RolePermissionCreate) sqlSave(ctx context.Context) (*RolePermission, 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 + } + id := _spec.ID.Value.(int64) + _node.ID = int(id) + _c.mutation.id = &_node.ID + _c.mutation.done = true + return _node, nil +} + +func (_c *RolePermissionCreate) createSpec() (*RolePermission, *sqlgraph.CreateSpec) { + var ( + _node = &RolePermission{config: _c.config} + _spec = sqlgraph.NewCreateSpec(rolepermission.Table, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt)) + ) + if nodes := _c.mutation.RoleIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.RoleTable, + Columns: []string{rolepermission.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.RoleID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + if nodes := _c.mutation.PermissionIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.PermissionTable, + Columns: []string{rolepermission.PermissionColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.PermissionID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + return _node, _spec +} + +// RolePermissionCreateBulk is the builder for creating many RolePermission entities in bulk. +type RolePermissionCreateBulk struct { + config + err error + builders []*RolePermissionCreate +} + +// Save creates the RolePermission entities in the database. +func (_c *RolePermissionCreateBulk) Save(ctx context.Context) ([]*RolePermission, error) { + if _c.err != nil { + return nil, _c.err + } + specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) + nodes := make([]*RolePermission, len(_c.builders)) + mutators := make([]Mutator, len(_c.builders)) + for i := range _c.builders { + func(i int, root context.Context) { + builder := _c.builders[i] + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*RolePermissionMutation) + 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 + if specs[i].ID.Value != nil { + id := specs[i].ID.Value.(int64) + nodes[i].ID = int(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 *RolePermissionCreateBulk) SaveX(ctx context.Context) []*RolePermission { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *RolePermissionCreateBulk) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *RolePermissionCreateBulk) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/rolepermission_delete.go b/internal/ent/rolepermission_delete.go new file mode 100644 index 0000000..7ac0014 --- /dev/null +++ b/internal/ent/rolepermission_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" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// RolePermissionDelete is the builder for deleting a RolePermission entity. +type RolePermissionDelete struct { + config + hooks []Hook + mutation *RolePermissionMutation +} + +// Where appends a list predicates to the RolePermissionDelete builder. +func (_d *RolePermissionDelete) Where(ps ...predicate.RolePermission) *RolePermissionDelete { + _d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (_d *RolePermissionDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *RolePermissionDelete) ExecX(ctx context.Context) int { + n, err := _d.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (_d *RolePermissionDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(rolepermission.Table, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt)) + if ps := _d.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + _d.mutation.done = true + return affected, err +} + +// RolePermissionDeleteOne is the builder for deleting a single RolePermission entity. +type RolePermissionDeleteOne struct { + _d *RolePermissionDelete +} + +// Where appends a list predicates to the RolePermissionDelete builder. +func (_d *RolePermissionDeleteOne) Where(ps ...predicate.RolePermission) *RolePermissionDeleteOne { + _d._d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query. +func (_d *RolePermissionDeleteOne) Exec(ctx context.Context) error { + n, err := _d._d.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{rolepermission.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *RolePermissionDeleteOne) ExecX(ctx context.Context) { + if err := _d.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/rolepermission_query.go b/internal/ent/rolepermission_query.go new file mode 100644 index 0000000..da6d6aa --- /dev/null +++ b/internal/ent/rolepermission_query.go @@ -0,0 +1,686 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + "math" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// RolePermissionQuery is the builder for querying RolePermission entities. +type RolePermissionQuery struct { + config + ctx *QueryContext + order []rolepermission.OrderOption + inters []Interceptor + predicates []predicate.RolePermission + withRole *RoleQuery + withPermission *PermissionQuery + withFKs bool + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the RolePermissionQuery builder. +func (_q *RolePermissionQuery) Where(ps ...predicate.RolePermission) *RolePermissionQuery { + _q.predicates = append(_q.predicates, ps...) + return _q +} + +// Limit the number of records to be returned by this query. +func (_q *RolePermissionQuery) Limit(limit int) *RolePermissionQuery { + _q.ctx.Limit = &limit + return _q +} + +// Offset to start from. +func (_q *RolePermissionQuery) Offset(offset int) *RolePermissionQuery { + _q.ctx.Offset = &offset + return _q +} + +// 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 (_q *RolePermissionQuery) Unique(unique bool) *RolePermissionQuery { + _q.ctx.Unique = &unique + return _q +} + +// Order specifies how the records should be ordered. +func (_q *RolePermissionQuery) Order(o ...rolepermission.OrderOption) *RolePermissionQuery { + _q.order = append(_q.order, o...) + return _q +} + +// QueryRole chains the current query on the "role" edge. +func (_q *RolePermissionQuery) QueryRole() *RoleQuery { + query := (&RoleClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(rolepermission.Table, rolepermission.FieldID, selector), + sqlgraph.To(role.Table, role.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, rolepermission.RoleTable, rolepermission.RoleColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// QueryPermission chains the current query on the "permission" edge. +func (_q *RolePermissionQuery) QueryPermission() *PermissionQuery { + query := (&PermissionClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(rolepermission.Table, rolepermission.FieldID, selector), + sqlgraph.To(permission.Table, permission.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, rolepermission.PermissionTable, rolepermission.PermissionColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// First returns the first RolePermission entity from the query. +// Returns a *NotFoundError when no RolePermission was found. +func (_q *RolePermissionQuery) First(ctx context.Context) (*RolePermission, error) { + nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{rolepermission.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (_q *RolePermissionQuery) FirstX(ctx context.Context) *RolePermission { + node, err := _q.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first RolePermission ID from the query. +// Returns a *NotFoundError when no RolePermission ID was found. +func (_q *RolePermissionQuery) FirstID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{rolepermission.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (_q *RolePermissionQuery) FirstIDX(ctx context.Context) int { + id, err := _q.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single RolePermission entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one RolePermission entity is found. +// Returns a *NotFoundError when no RolePermission entities are found. +func (_q *RolePermissionQuery) Only(ctx context.Context) (*RolePermission, error) { + nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{rolepermission.Label} + default: + return nil, &NotSingularError{rolepermission.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (_q *RolePermissionQuery) OnlyX(ctx context.Context) *RolePermission { + node, err := _q.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only RolePermission ID in the query. +// Returns a *NotSingularError when more than one RolePermission ID is found. +// Returns a *NotFoundError when no entities are found. +func (_q *RolePermissionQuery) OnlyID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{rolepermission.Label} + default: + err = &NotSingularError{rolepermission.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (_q *RolePermissionQuery) OnlyIDX(ctx context.Context) int { + id, err := _q.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of RolePermissions. +func (_q *RolePermissionQuery) All(ctx context.Context) ([]*RolePermission, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll) + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*RolePermission, *RolePermissionQuery]() + return withInterceptors[[]*RolePermission](ctx, _q, qr, _q.inters) +} + +// AllX is like All, but panics if an error occurs. +func (_q *RolePermissionQuery) AllX(ctx context.Context) []*RolePermission { + nodes, err := _q.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of RolePermission IDs. +func (_q *RolePermissionQuery) IDs(ctx context.Context) (ids []int, err error) { + if _q.ctx.Unique == nil && _q.path != nil { + _q.Unique(true) + } + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs) + if err = _q.Select(rolepermission.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (_q *RolePermissionQuery) IDsX(ctx context.Context) []int { + ids, err := _q.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (_q *RolePermissionQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount) + if err := _q.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, _q, querierCount[*RolePermissionQuery](), _q.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (_q *RolePermissionQuery) CountX(ctx context.Context) int { + count, err := _q.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (_q *RolePermissionQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist) + switch _, err := _q.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 (_q *RolePermissionQuery) ExistX(ctx context.Context) bool { + exist, err := _q.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the RolePermissionQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (_q *RolePermissionQuery) Clone() *RolePermissionQuery { + if _q == nil { + return nil + } + return &RolePermissionQuery{ + config: _q.config, + ctx: _q.ctx.Clone(), + order: append([]rolepermission.OrderOption{}, _q.order...), + inters: append([]Interceptor{}, _q.inters...), + predicates: append([]predicate.RolePermission{}, _q.predicates...), + withRole: _q.withRole.Clone(), + withPermission: _q.withPermission.Clone(), + // clone intermediate query. + sql: _q.sql.Clone(), + path: _q.path, + } +} + +// WithRole tells the query-builder to eager-load the nodes that are connected to +// the "role" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *RolePermissionQuery) WithRole(opts ...func(*RoleQuery)) *RolePermissionQuery { + query := (&RoleClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withRole = query + return _q +} + +// WithPermission tells the query-builder to eager-load the nodes that are connected to +// the "permission" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *RolePermissionQuery) WithPermission(opts ...func(*PermissionQuery)) *RolePermissionQuery { + query := (&PermissionClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withPermission = query + return _q +} + +// 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 { +// RoleID string `json:"role_id,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.RolePermission.Query(). +// GroupBy(rolepermission.FieldRoleID). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (_q *RolePermissionQuery) GroupBy(field string, fields ...string) *RolePermissionGroupBy { + _q.ctx.Fields = append([]string{field}, fields...) + grbuild := &RolePermissionGroupBy{build: _q} + grbuild.flds = &_q.ctx.Fields + grbuild.label = rolepermission.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 { +// RoleID string `json:"role_id,omitempty"` +// } +// +// client.RolePermission.Query(). +// Select(rolepermission.FieldRoleID). +// Scan(ctx, &v) +func (_q *RolePermissionQuery) Select(fields ...string) *RolePermissionSelect { + _q.ctx.Fields = append(_q.ctx.Fields, fields...) + sbuild := &RolePermissionSelect{RolePermissionQuery: _q} + sbuild.label = rolepermission.Label + sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a RolePermissionSelect configured with the given aggregations. +func (_q *RolePermissionQuery) Aggregate(fns ...AggregateFunc) *RolePermissionSelect { + return _q.Select().Aggregate(fns...) +} + +func (_q *RolePermissionQuery) prepareQuery(ctx context.Context) error { + for _, inter := range _q.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, _q); err != nil { + return err + } + } + } + for _, f := range _q.ctx.Fields { + if !rolepermission.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if _q.path != nil { + prev, err := _q.path(ctx) + if err != nil { + return err + } + _q.sql = prev + } + return nil +} + +func (_q *RolePermissionQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*RolePermission, error) { + var ( + nodes = []*RolePermission{} + withFKs = _q.withFKs + _spec = _q.querySpec() + loadedTypes = [2]bool{ + _q.withRole != nil, + _q.withPermission != nil, + } + ) + if withFKs { + _spec.Node.Columns = append(_spec.Node.Columns, rolepermission.ForeignKeys...) + } + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*RolePermission).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &RolePermission{config: _q.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, _q.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := _q.withRole; query != nil { + if err := _q.loadRole(ctx, query, nodes, nil, + func(n *RolePermission, e *Role) { n.Edges.Role = e }); err != nil { + return nil, err + } + } + if query := _q.withPermission; query != nil { + if err := _q.loadPermission(ctx, query, nodes, nil, + func(n *RolePermission, e *Permission) { n.Edges.Permission = e }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (_q *RolePermissionQuery) loadRole(ctx context.Context, query *RoleQuery, nodes []*RolePermission, init func(*RolePermission), assign func(*RolePermission, *Role)) error { + ids := make([]string, 0, len(nodes)) + nodeids := make(map[string][]*RolePermission) + for i := range nodes { + fk := nodes[i].RoleID + if _, ok := nodeids[fk]; !ok { + ids = append(ids, fk) + } + nodeids[fk] = append(nodeids[fk], nodes[i]) + } + if len(ids) == 0 { + return nil + } + query.Where(role.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 "role_id" returned %v`, n.ID) + } + for i := range nodes { + assign(nodes[i], n) + } + } + return nil +} +func (_q *RolePermissionQuery) loadPermission(ctx context.Context, query *PermissionQuery, nodes []*RolePermission, init func(*RolePermission), assign func(*RolePermission, *Permission)) error { + ids := make([]string, 0, len(nodes)) + nodeids := make(map[string][]*RolePermission) + for i := range nodes { + fk := nodes[i].PermissionID + if _, ok := nodeids[fk]; !ok { + ids = append(ids, fk) + } + nodeids[fk] = append(nodeids[fk], nodes[i]) + } + if len(ids) == 0 { + return nil + } + query.Where(permission.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 "permission_id" returned %v`, n.ID) + } + for i := range nodes { + assign(nodes[i], n) + } + } + return nil +} + +func (_q *RolePermissionQuery) sqlCount(ctx context.Context) (int, error) { + _spec := _q.querySpec() + _spec.Node.Columns = _q.ctx.Fields + if len(_q.ctx.Fields) > 0 { + _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique + } + return sqlgraph.CountNodes(ctx, _q.driver, _spec) +} + +func (_q *RolePermissionQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(rolepermission.Table, rolepermission.Columns, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt)) + _spec.From = _q.sql + if unique := _q.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if _q.path != nil { + _spec.Unique = true + } + if fields := _q.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, rolepermission.FieldID) + for i := range fields { + if fields[i] != rolepermission.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + if _q.withRole != nil { + _spec.Node.AddColumnOnce(rolepermission.FieldRoleID) + } + if _q.withPermission != nil { + _spec.Node.AddColumnOnce(rolepermission.FieldPermissionID) + } + } + if ps := _q.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := _q.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := _q.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := _q.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (_q *RolePermissionQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(_q.driver.Dialect()) + t1 := builder.Table(rolepermission.Table) + columns := _q.ctx.Fields + if len(columns) == 0 { + columns = rolepermission.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if _q.sql != nil { + selector = _q.sql + selector.Select(selector.Columns(columns...)...) + } + if _q.ctx.Unique != nil && *_q.ctx.Unique { + selector.Distinct() + } + for _, p := range _q.predicates { + p(selector) + } + for _, p := range _q.order { + p(selector) + } + if offset := _q.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 := _q.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// RolePermissionGroupBy is the group-by builder for RolePermission entities. +type RolePermissionGroupBy struct { + selector + build *RolePermissionQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (_g *RolePermissionGroupBy) Aggregate(fns ...AggregateFunc) *RolePermissionGroupBy { + _g.fns = append(_g.fns, fns...) + return _g +} + +// Scan applies the selector query and scans the result into the given value. +func (_g *RolePermissionGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*RolePermissionQuery, *RolePermissionGroupBy](ctx, _g.build, _g, _g.build.inters, v) +} + +func (_g *RolePermissionGroupBy) sqlScan(ctx context.Context, root *RolePermissionQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*_g.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// RolePermissionSelect is the builder for selecting fields of RolePermission entities. +type RolePermissionSelect struct { + *RolePermissionQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (_s *RolePermissionSelect) Aggregate(fns ...AggregateFunc) *RolePermissionSelect { + _s.fns = append(_s.fns, fns...) + return _s +} + +// Scan applies the selector query and scans the result into the given value. +func (_s *RolePermissionSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*RolePermissionQuery, *RolePermissionSelect](ctx, _s.RolePermissionQuery, _s, _s.inters, v) +} + +func (_s *RolePermissionSelect) sqlScan(ctx context.Context, root *RolePermissionQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/ent/rolepermission_update.go b/internal/ent/rolepermission_update.go new file mode 100644 index 0000000..e240158 --- /dev/null +++ b/internal/ent/rolepermission_update.go @@ -0,0 +1,421 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/rolepermission" +) + +// RolePermissionUpdate is the builder for updating RolePermission entities. +type RolePermissionUpdate struct { + config + hooks []Hook + mutation *RolePermissionMutation +} + +// Where appends a list predicates to the RolePermissionUpdate builder. +func (_u *RolePermissionUpdate) Where(ps ...predicate.RolePermission) *RolePermissionUpdate { + _u.mutation.Where(ps...) + return _u +} + +// SetRoleID sets the "role_id" field. +func (_u *RolePermissionUpdate) SetRoleID(v string) *RolePermissionUpdate { + _u.mutation.SetRoleID(v) + return _u +} + +// SetNillableRoleID sets the "role_id" field if the given value is not nil. +func (_u *RolePermissionUpdate) SetNillableRoleID(v *string) *RolePermissionUpdate { + if v != nil { + _u.SetRoleID(*v) + } + return _u +} + +// SetPermissionID sets the "permission_id" field. +func (_u *RolePermissionUpdate) SetPermissionID(v string) *RolePermissionUpdate { + _u.mutation.SetPermissionID(v) + return _u +} + +// SetNillablePermissionID sets the "permission_id" field if the given value is not nil. +func (_u *RolePermissionUpdate) SetNillablePermissionID(v *string) *RolePermissionUpdate { + if v != nil { + _u.SetPermissionID(*v) + } + return _u +} + +// SetRole sets the "role" edge to the Role entity. +func (_u *RolePermissionUpdate) SetRole(v *Role) *RolePermissionUpdate { + return _u.SetRoleID(v.ID) +} + +// SetPermission sets the "permission" edge to the Permission entity. +func (_u *RolePermissionUpdate) SetPermission(v *Permission) *RolePermissionUpdate { + return _u.SetPermissionID(v.ID) +} + +// Mutation returns the RolePermissionMutation object of the builder. +func (_u *RolePermissionUpdate) Mutation() *RolePermissionMutation { + return _u.mutation +} + +// ClearRole clears the "role" edge to the Role entity. +func (_u *RolePermissionUpdate) ClearRole() *RolePermissionUpdate { + _u.mutation.ClearRole() + return _u +} + +// ClearPermission clears the "permission" edge to the Permission entity. +func (_u *RolePermissionUpdate) ClearPermission() *RolePermissionUpdate { + _u.mutation.ClearPermission() + return _u +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (_u *RolePermissionUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *RolePermissionUpdate) SaveX(ctx context.Context) int { + affected, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (_u *RolePermissionUpdate) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *RolePermissionUpdate) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *RolePermissionUpdate) check() error { + if _u.mutation.RoleCleared() && len(_u.mutation.RoleIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "RolePermission.role"`) + } + if _u.mutation.PermissionCleared() && len(_u.mutation.PermissionIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "RolePermission.permission"`) + } + return nil +} + +func (_u *RolePermissionUpdate) sqlSave(ctx context.Context) (_node int, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(rolepermission.Table, rolepermission.Columns, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt)) + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if _u.mutation.RoleCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.RoleTable, + Columns: []string{rolepermission.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RoleIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.RoleTable, + Columns: []string{rolepermission.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _u.mutation.PermissionCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.PermissionTable, + Columns: []string{rolepermission.PermissionColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.PermissionIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.PermissionTable, + Columns: []string{rolepermission.PermissionColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{rolepermission.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + _u.mutation.done = true + return _node, nil +} + +// RolePermissionUpdateOne is the builder for updating a single RolePermission entity. +type RolePermissionUpdateOne struct { + config + fields []string + hooks []Hook + mutation *RolePermissionMutation +} + +// SetRoleID sets the "role_id" field. +func (_u *RolePermissionUpdateOne) SetRoleID(v string) *RolePermissionUpdateOne { + _u.mutation.SetRoleID(v) + return _u +} + +// SetNillableRoleID sets the "role_id" field if the given value is not nil. +func (_u *RolePermissionUpdateOne) SetNillableRoleID(v *string) *RolePermissionUpdateOne { + if v != nil { + _u.SetRoleID(*v) + } + return _u +} + +// SetPermissionID sets the "permission_id" field. +func (_u *RolePermissionUpdateOne) SetPermissionID(v string) *RolePermissionUpdateOne { + _u.mutation.SetPermissionID(v) + return _u +} + +// SetNillablePermissionID sets the "permission_id" field if the given value is not nil. +func (_u *RolePermissionUpdateOne) SetNillablePermissionID(v *string) *RolePermissionUpdateOne { + if v != nil { + _u.SetPermissionID(*v) + } + return _u +} + +// SetRole sets the "role" edge to the Role entity. +func (_u *RolePermissionUpdateOne) SetRole(v *Role) *RolePermissionUpdateOne { + return _u.SetRoleID(v.ID) +} + +// SetPermission sets the "permission" edge to the Permission entity. +func (_u *RolePermissionUpdateOne) SetPermission(v *Permission) *RolePermissionUpdateOne { + return _u.SetPermissionID(v.ID) +} + +// Mutation returns the RolePermissionMutation object of the builder. +func (_u *RolePermissionUpdateOne) Mutation() *RolePermissionMutation { + return _u.mutation +} + +// ClearRole clears the "role" edge to the Role entity. +func (_u *RolePermissionUpdateOne) ClearRole() *RolePermissionUpdateOne { + _u.mutation.ClearRole() + return _u +} + +// ClearPermission clears the "permission" edge to the Permission entity. +func (_u *RolePermissionUpdateOne) ClearPermission() *RolePermissionUpdateOne { + _u.mutation.ClearPermission() + return _u +} + +// Where appends a list predicates to the RolePermissionUpdate builder. +func (_u *RolePermissionUpdateOne) Where(ps ...predicate.RolePermission) *RolePermissionUpdateOne { + _u.mutation.Where(ps...) + return _u +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (_u *RolePermissionUpdateOne) Select(field string, fields ...string) *RolePermissionUpdateOne { + _u.fields = append([]string{field}, fields...) + return _u +} + +// Save executes the query and returns the updated RolePermission entity. +func (_u *RolePermissionUpdateOne) Save(ctx context.Context) (*RolePermission, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *RolePermissionUpdateOne) SaveX(ctx context.Context) *RolePermission { + node, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (_u *RolePermissionUpdateOne) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *RolePermissionUpdateOne) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *RolePermissionUpdateOne) check() error { + if _u.mutation.RoleCleared() && len(_u.mutation.RoleIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "RolePermission.role"`) + } + if _u.mutation.PermissionCleared() && len(_u.mutation.PermissionIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "RolePermission.permission"`) + } + return nil +} + +func (_u *RolePermissionUpdateOne) sqlSave(ctx context.Context) (_node *RolePermission, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(rolepermission.Table, rolepermission.Columns, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt)) + id, ok := _u.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "RolePermission.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := _u.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, rolepermission.FieldID) + for _, f := range fields { + if !rolepermission.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != rolepermission.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if _u.mutation.RoleCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.RoleTable, + Columns: []string{rolepermission.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RoleIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.RoleTable, + Columns: []string{rolepermission.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _u.mutation.PermissionCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.PermissionTable, + Columns: []string{rolepermission.PermissionColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.PermissionIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: rolepermission.PermissionTable, + Columns: []string{rolepermission.PermissionColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &RolePermission{config: _u.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{rolepermission.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + _u.mutation.done = true + return _node, nil +} diff --git a/internal/ent/runtime.go b/internal/ent/runtime.go new file mode 100644 index 0000000..7cd49b0 --- /dev/null +++ b/internal/ent/runtime.go @@ -0,0 +1,73 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "time" + + "git.dcentral.systems/toolz/goplt/internal/ent/auditlog" + "git.dcentral.systems/toolz/goplt/internal/ent/permission" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/schema" + "git.dcentral.systems/toolz/goplt/internal/ent/user" +) + +// The init function reads all schema descriptors with runtime code +// (default values, validators, hooks and policies) and stitches it +// to their package variables. +func init() { + auditlogFields := schema.AuditLog{}.Fields() + _ = auditlogFields + // auditlogDescActorID is the schema descriptor for actor_id field. + auditlogDescActorID := auditlogFields[1].Descriptor() + // auditlog.ActorIDValidator is a validator for the "actor_id" field. It is called by the builders before save. + auditlog.ActorIDValidator = auditlogDescActorID.Validators[0].(func(string) error) + // auditlogDescAction is the schema descriptor for action field. + auditlogDescAction := auditlogFields[2].Descriptor() + // auditlog.ActionValidator is a validator for the "action" field. It is called by the builders before save. + auditlog.ActionValidator = auditlogDescAction.Validators[0].(func(string) error) + // auditlogDescTimestamp is the schema descriptor for timestamp field. + auditlogDescTimestamp := auditlogFields[5].Descriptor() + // auditlog.DefaultTimestamp holds the default value on creation for the timestamp field. + auditlog.DefaultTimestamp = auditlogDescTimestamp.Default.(func() time.Time) + permissionFields := schema.Permission{}.Fields() + _ = permissionFields + // permissionDescName is the schema descriptor for name field. + permissionDescName := permissionFields[1].Descriptor() + // permission.NameValidator is a validator for the "name" field. It is called by the builders before save. + permission.NameValidator = permissionDescName.Validators[0].(func(string) error) + roleFields := schema.Role{}.Fields() + _ = roleFields + // roleDescName is the schema descriptor for name field. + roleDescName := roleFields[1].Descriptor() + // role.NameValidator is a validator for the "name" field. It is called by the builders before save. + role.NameValidator = roleDescName.Validators[0].(func(string) error) + // roleDescCreatedAt is the schema descriptor for created_at field. + roleDescCreatedAt := roleFields[3].Descriptor() + // role.DefaultCreatedAt holds the default value on creation for the created_at field. + role.DefaultCreatedAt = roleDescCreatedAt.Default.(func() time.Time) + userFields := schema.User{}.Fields() + _ = userFields + // userDescEmail is the schema descriptor for email field. + userDescEmail := userFields[1].Descriptor() + // user.EmailValidator is a validator for the "email" field. It is called by the builders before save. + user.EmailValidator = userDescEmail.Validators[0].(func(string) error) + // userDescPasswordHash is the schema descriptor for password_hash field. + userDescPasswordHash := userFields[2].Descriptor() + // user.PasswordHashValidator is a validator for the "password_hash" field. It is called by the builders before save. + user.PasswordHashValidator = userDescPasswordHash.Validators[0].(func(string) error) + // userDescVerified is the schema descriptor for verified field. + userDescVerified := userFields[3].Descriptor() + // user.DefaultVerified holds the default value on creation for the verified field. + user.DefaultVerified = userDescVerified.Default.(bool) + // userDescCreatedAt is the schema descriptor for created_at field. + userDescCreatedAt := userFields[4].Descriptor() + // user.DefaultCreatedAt holds the default value on creation for the created_at field. + user.DefaultCreatedAt = userDescCreatedAt.Default.(func() time.Time) + // userDescUpdatedAt is the schema descriptor for updated_at field. + userDescUpdatedAt := userFields[5].Descriptor() + // user.DefaultUpdatedAt holds the default value on creation for the updated_at field. + user.DefaultUpdatedAt = userDescUpdatedAt.Default.(func() time.Time) + // user.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. + user.UpdateDefaultUpdatedAt = userDescUpdatedAt.UpdateDefault.(func() time.Time) +} diff --git a/internal/ent/runtime/runtime.go b/internal/ent/runtime/runtime.go new file mode 100644 index 0000000..0dd9113 --- /dev/null +++ b/internal/ent/runtime/runtime.go @@ -0,0 +1,10 @@ +// Code generated by ent, DO NOT EDIT. + +package runtime + +// The schema-stitching logic is generated in git.dcentral.systems/toolz/goplt/internal/ent/runtime.go + +const ( + Version = "v0.14.5" // Version of ent codegen. + Sum = "h1:Rj2WOYJtCkWyFo6a+5wB3EfBRP0rnx1fMk6gGA0UUe4=" // Sum of ent codegen. +) diff --git a/internal/ent/schema/audit_log.go b/internal/ent/schema/audit_log.go new file mode 100644 index 0000000..c5d231a --- /dev/null +++ b/internal/ent/schema/audit_log.go @@ -0,0 +1,49 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" +) + +// AuditLog holds the schema definition for the AuditLog entity. +type AuditLog struct { + ent.Schema +} + +// Fields of the AuditLog. +func (AuditLog) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("actor_id"). + NotEmpty(). + Comment("ID of the user/actor performing the action"), + field.String("action"). + NotEmpty(). + Comment("Action performed (e.g., create, update, delete)"), + field.String("target_id"). + Optional(). + Comment("ID of the target resource"), + field.JSON("metadata", map[string]interface{}{}). + Optional(). + Comment("Additional metadata as JSON"), + field.Time("timestamp"). + Default(time.Now). + Immutable(), + } +} + +// Indexes of the AuditLog. +func (AuditLog) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("actor_id"), + index.Fields("target_id"), + index.Fields("timestamp"), + index.Fields("action"), + } +} + diff --git a/internal/ent/schema/permission.go b/internal/ent/schema/permission.go new file mode 100644 index 0000000..e6fa0a5 --- /dev/null +++ b/internal/ent/schema/permission.go @@ -0,0 +1,33 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// Permission holds the schema definition for the Permission entity. +type Permission struct { + ent.Schema +} + +// Fields of the Permission. +func (Permission) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("name"). + Unique(). + NotEmpty(). + Comment("Format: module.resource.action"), + } +} + +// Edges of the Permission. +func (Permission) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role_permissions", RolePermission.Type), + } +} + diff --git a/internal/ent/schema/role.go b/internal/ent/schema/role.go new file mode 100644 index 0000000..78001a2 --- /dev/null +++ b/internal/ent/schema/role.go @@ -0,0 +1,40 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// Role holds the schema definition for the Role entity. +type Role struct { + ent.Schema +} + +// Fields of the Role. +func (Role) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("name"). + Unique(). + NotEmpty(), + field.String("description"). + Optional(), + field.Time("created_at"). + Default(time.Now). + Immutable(), + } +} + +// Edges of the Role. +func (Role) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role_permissions", RolePermission.Type), + edge.To("user_roles", UserRole.Type), + } +} + diff --git a/internal/ent/schema/role_permission.go b/internal/ent/schema/role_permission.go new file mode 100644 index 0000000..a50d39f --- /dev/null +++ b/internal/ent/schema/role_permission.go @@ -0,0 +1,35 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// RolePermission holds the schema definition for the RolePermission entity (many-to-many relationship). +type RolePermission struct { + ent.Schema +} + +// Fields of the RolePermission. +func (RolePermission) Fields() []ent.Field { + return []ent.Field{ + field.String("role_id"), + field.String("permission_id"), + } +} + +// Edges of the RolePermission. +func (RolePermission) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role", Role.Type). + Unique(). + Required(). + Field("role_id"), + edge.To("permission", Permission.Type). + Unique(). + Required(). + Field("permission_id"), + } +} + diff --git a/internal/ent/schema/user.go b/internal/ent/schema/user.go new file mode 100644 index 0000000..0cf998d --- /dev/null +++ b/internal/ent/schema/user.go @@ -0,0 +1,44 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// User holds the schema definition for the User entity. +type User struct { + ent.Schema +} + +// Fields of the User. +func (User) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("email"). + Unique(). + NotEmpty(), + field.String("password_hash"). + NotEmpty(), + field.Bool("verified"). + Default(false), + field.Time("created_at"). + Default(time.Now). + Immutable(), + field.Time("updated_at"). + Default(time.Now). + UpdateDefault(time.Now), + } +} + +// Edges of the User. +func (User) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("user_roles", UserRole.Type), + } +} + diff --git a/internal/ent/schema/user_role.go b/internal/ent/schema/user_role.go new file mode 100644 index 0000000..549f13c --- /dev/null +++ b/internal/ent/schema/user_role.go @@ -0,0 +1,35 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// UserRole holds the schema definition for the UserRole entity (many-to-many relationship). +type UserRole struct { + ent.Schema +} + +// Fields of the UserRole. +func (UserRole) Fields() []ent.Field { + return []ent.Field{ + field.String("user_id"), + field.String("role_id"), + } +} + +// Edges of the UserRole. +func (UserRole) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("user", User.Type). + Unique(). + Required(). + Field("user_id"), + edge.To("role", Role.Type). + Unique(). + Required(). + Field("role_id"), + } +} + diff --git a/internal/ent/tx.go b/internal/ent/tx.go new file mode 100644 index 0000000..36fd781 --- /dev/null +++ b/internal/ent/tx.go @@ -0,0 +1,225 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "sync" + + "entgo.io/ent/dialect" +) + +// Tx is a transactional client that is created by calling Client.Tx(). +type Tx struct { + config + // AuditLog is the client for interacting with the AuditLog builders. + AuditLog *AuditLogClient + // Permission is the client for interacting with the Permission builders. + Permission *PermissionClient + // Role is the client for interacting with the Role builders. + Role *RoleClient + // RolePermission is the client for interacting with the RolePermission builders. + RolePermission *RolePermissionClient + // User is the client for interacting with the User builders. + User *UserClient + // UserRole is the client for interacting with the UserRole builders. + UserRole *UserRoleClient + + // lazily loaded. + client *Client + clientOnce sync.Once + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Commit method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) +} + +// Commit commits the transaction. +func (tx *Tx) Commit() error { + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) + txDriver.mu.Lock() + hooks := append([]CommitHook(nil), txDriver.onCommit...) + txDriver.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Commit(tx.ctx, tx) +} + +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { + txDriver := tx.config.driver.(*txDriver) + txDriver.mu.Lock() + txDriver.onCommit = append(txDriver.onCommit, f) + txDriver.mu.Unlock() +} + +type ( + // Rollbacker is the interface that wraps the Rollback method. + Rollbacker interface { + Rollback(context.Context, *Tx) error + } + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) +} + +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + txDriver.mu.Lock() + hooks := append([]RollbackHook(nil), txDriver.onRollback...) + txDriver.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { + txDriver := tx.config.driver.(*txDriver) + txDriver.mu.Lock() + txDriver.onRollback = append(txDriver.onRollback, f) + txDriver.mu.Unlock() +} + +// Client returns a Client that binds to current transaction. +func (tx *Tx) Client() *Client { + tx.clientOnce.Do(func() { + tx.client = &Client{config: tx.config} + tx.client.init() + }) + return tx.client +} + +func (tx *Tx) init() { + tx.AuditLog = NewAuditLogClient(tx.config) + tx.Permission = NewPermissionClient(tx.config) + tx.Role = NewRoleClient(tx.config) + tx.RolePermission = NewRolePermissionClient(tx.config) + tx.User = NewUserClient(tx.config) + tx.UserRole = NewUserRoleClient(tx.config) +} + +// txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation. +// The idea is to support transactions without adding any extra code to the builders. +// When a builder calls to driver.Tx(), it gets the same dialect.Tx instance. +// Commit and Rollback are nop for the internal builders and the user must call one +// of them in order to commit or rollback the transaction. +// +// If a closed transaction is embedded in one of the generated entities, and the entity +// applies a query, for example: AuditLog.QueryXXX(), the query will be executed +// through the driver which created this transaction. +// +// Note that txDriver is not goroutine safe. +type txDriver struct { + // the driver we started the transaction from. + drv dialect.Driver + // tx is the underlying transaction. + tx dialect.Tx + // completion hooks. + mu sync.Mutex + onCommit []CommitHook + onRollback []RollbackHook +} + +// newTx creates a new transactional driver. +func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) { + tx, err := drv.Tx(ctx) + if err != nil { + return nil, err + } + return &txDriver{tx: tx, drv: drv}, nil +} + +// Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls +// from the internal builders. Should be called only by the internal builders. +func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil } + +// Dialect returns the dialect of the driver we started the transaction from. +func (tx *txDriver) Dialect() string { return tx.drv.Dialect() } + +// Close is a nop close. +func (*txDriver) Close() error { return nil } + +// Commit is a nop commit for the internal builders. +// User must call `Tx.Commit` in order to commit the transaction. +func (*txDriver) Commit() error { return nil } + +// Rollback is a nop rollback for the internal builders. +// User must call `Tx.Rollback` in order to rollback the transaction. +func (*txDriver) Rollback() error { return nil } + +// Exec calls tx.Exec. +func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error { + return tx.tx.Exec(ctx, query, args, v) +} + +// Query calls tx.Query. +func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error { + return tx.tx.Query(ctx, query, args, v) +} + +var _ dialect.Driver = (*txDriver)(nil) diff --git a/internal/ent/user.go b/internal/ent/user.go new file mode 100644 index 0000000..aa74feb --- /dev/null +++ b/internal/ent/user.go @@ -0,0 +1,176 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + "time" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/user" +) + +// User is the model entity for the User schema. +type User struct { + config `json:"-"` + // ID of the ent. + ID string `json:"id,omitempty"` + // Email holds the value of the "email" field. + Email string `json:"email,omitempty"` + // PasswordHash holds the value of the "password_hash" field. + PasswordHash string `json:"password_hash,omitempty"` + // Verified holds the value of the "verified" field. + Verified bool `json:"verified,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"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the UserQuery when eager-loading is set. + Edges UserEdges `json:"edges"` + selectValues sql.SelectValues +} + +// UserEdges holds the relations/edges for other nodes in the graph. +type UserEdges struct { + // UserRoles holds the value of the user_roles edge. + UserRoles []*UserRole `json:"user_roles,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [1]bool +} + +// UserRolesOrErr returns the UserRoles value or an error if the edge +// was not loaded in eager-loading. +func (e UserEdges) UserRolesOrErr() ([]*UserRole, error) { + if e.loadedTypes[0] { + return e.UserRoles, nil + } + return nil, &NotLoadedError{edge: "user_roles"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*User) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case user.FieldVerified: + values[i] = new(sql.NullBool) + case user.FieldID, user.FieldEmail, user.FieldPasswordHash: + values[i] = new(sql.NullString) + case user.FieldCreatedAt, user.FieldUpdatedAt: + values[i] = new(sql.NullTime) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the User fields. +func (_m *User) 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 user.FieldID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value.Valid { + _m.ID = value.String + } + case user.FieldEmail: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field email", values[i]) + } else if value.Valid { + _m.Email = value.String + } + case user.FieldPasswordHash: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field password_hash", values[i]) + } else if value.Valid { + _m.PasswordHash = value.String + } + case user.FieldVerified: + if value, ok := values[i].(*sql.NullBool); !ok { + return fmt.Errorf("unexpected type %T for field verified", values[i]) + } else if value.Valid { + _m.Verified = value.Bool + } + case user.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 { + _m.CreatedAt = value.Time + } + case user.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 { + _m.UpdatedAt = value.Time + } + default: + _m.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the User. +// This includes values selected through modifiers, order, etc. +func (_m *User) Value(name string) (ent.Value, error) { + return _m.selectValues.Get(name) +} + +// QueryUserRoles queries the "user_roles" edge of the User entity. +func (_m *User) QueryUserRoles() *UserRoleQuery { + return NewUserClient(_m.config).QueryUserRoles(_m) +} + +// 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. +func (_m *User) Update() *UserUpdateOne { + return NewUserClient(_m.config).UpdateOne(_m) +} + +// Unwrap unwraps the User 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 (_m *User) Unwrap() *User { + _tx, ok := _m.config.driver.(*txDriver) + if !ok { + panic("ent: User is not a transactional entity") + } + _m.config.driver = _tx.drv + return _m +} + +// String implements the fmt.Stringer. +func (_m *User) String() string { + var builder strings.Builder + builder.WriteString("User(") + builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("email=") + builder.WriteString(_m.Email) + builder.WriteString(", ") + builder.WriteString("password_hash=") + builder.WriteString(_m.PasswordHash) + builder.WriteString(", ") + builder.WriteString("verified=") + builder.WriteString(fmt.Sprintf("%v", _m.Verified)) + builder.WriteString(", ") + builder.WriteString("created_at=") + builder.WriteString(_m.CreatedAt.Format(time.ANSIC)) + builder.WriteString(", ") + builder.WriteString("updated_at=") + builder.WriteString(_m.UpdatedAt.Format(time.ANSIC)) + builder.WriteByte(')') + return builder.String() +} + +// Users is a parsable slice of User. +type Users []*User diff --git a/internal/ent/user/user.go b/internal/ent/user/user.go new file mode 100644 index 0000000..5000529 --- /dev/null +++ b/internal/ent/user/user.go @@ -0,0 +1,127 @@ +// Code generated by ent, DO NOT EDIT. + +package user + +import ( + "time" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" +) + +const ( + // Label holds the string label denoting the user type in the database. + Label = "user" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldEmail holds the string denoting the email field in the database. + FieldEmail = "email" + // FieldPasswordHash holds the string denoting the password_hash field in the database. + FieldPasswordHash = "password_hash" + // FieldVerified holds the string denoting the verified field in the database. + FieldVerified = "verified" + // 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" + // EdgeUserRoles holds the string denoting the user_roles edge name in mutations. + EdgeUserRoles = "user_roles" + // Table holds the table name of the user in the database. + Table = "users" + // UserRolesTable is the table that holds the user_roles relation/edge. + UserRolesTable = "user_roles" + // UserRolesInverseTable is the table name for the UserRole entity. + // It exists in this package in order to avoid circular dependency with the "userrole" package. + UserRolesInverseTable = "user_roles" + // UserRolesColumn is the table column denoting the user_roles relation/edge. + UserRolesColumn = "user_user_roles" +) + +// Columns holds all SQL columns for user fields. +var Columns = []string{ + FieldID, + FieldEmail, + FieldPasswordHash, + FieldVerified, + FieldCreatedAt, + FieldUpdatedAt, +} + +// 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 ( + // EmailValidator is a validator for the "email" field. It is called by the builders before save. + EmailValidator func(string) error + // PasswordHashValidator is a validator for the "password_hash" field. It is called by the builders before save. + PasswordHashValidator func(string) error + // DefaultVerified holds the default value on creation for the "verified" field. + DefaultVerified bool + // 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 +) + +// OrderOption defines the ordering options for the User queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByEmail orders the results by the email field. +func ByEmail(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldEmail, opts...).ToFunc() +} + +// ByPasswordHash orders the results by the password_hash field. +func ByPasswordHash(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldPasswordHash, opts...).ToFunc() +} + +// ByVerified orders the results by the verified field. +func ByVerified(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldVerified, opts...).ToFunc() +} + +// ByCreatedAt orders the results by the created_at field. +func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() +} + +// ByUpdatedAt orders the results by the updated_at field. +func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldUpdatedAt, opts...).ToFunc() +} + +// ByUserRolesCount orders the results by user_roles count. +func ByUserRolesCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newUserRolesStep(), opts...) + } +} + +// ByUserRoles orders the results by user_roles terms. +func ByUserRoles(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newUserRolesStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} +func newUserRolesStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(UserRolesInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, UserRolesTable, UserRolesColumn), + ) +} diff --git a/internal/ent/user/where.go b/internal/ent/user/where.go new file mode 100644 index 0000000..3d7e294 --- /dev/null +++ b/internal/ent/user/where.go @@ -0,0 +1,349 @@ +// Code generated by ent, DO NOT EDIT. + +package user + +import ( + "time" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id string) predicate.User { + return predicate.User(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id string) predicate.User { + return predicate.User(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id string) predicate.User { + return predicate.User(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...string) predicate.User { + return predicate.User(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...string) predicate.User { + return predicate.User(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id string) predicate.User { + return predicate.User(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id string) predicate.User { + return predicate.User(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id string) predicate.User { + return predicate.User(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id string) predicate.User { + return predicate.User(sql.FieldLTE(FieldID, id)) +} + +// IDEqualFold applies the EqualFold predicate on the ID field. +func IDEqualFold(id string) predicate.User { + return predicate.User(sql.FieldEqualFold(FieldID, id)) +} + +// IDContainsFold applies the ContainsFold predicate on the ID field. +func IDContainsFold(id string) predicate.User { + return predicate.User(sql.FieldContainsFold(FieldID, id)) +} + +// Email applies equality check predicate on the "email" field. It's identical to EmailEQ. +func Email(v string) predicate.User { + return predicate.User(sql.FieldEQ(FieldEmail, v)) +} + +// PasswordHash applies equality check predicate on the "password_hash" field. It's identical to PasswordHashEQ. +func PasswordHash(v string) predicate.User { + return predicate.User(sql.FieldEQ(FieldPasswordHash, v)) +} + +// Verified applies equality check predicate on the "verified" field. It's identical to VerifiedEQ. +func Verified(v bool) predicate.User { + return predicate.User(sql.FieldEQ(FieldVerified, v)) +} + +// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. +func CreatedAt(v time.Time) predicate.User { + return predicate.User(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.User { + return predicate.User(sql.FieldEQ(FieldUpdatedAt, v)) +} + +// EmailEQ applies the EQ predicate on the "email" field. +func EmailEQ(v string) predicate.User { + return predicate.User(sql.FieldEQ(FieldEmail, v)) +} + +// EmailNEQ applies the NEQ predicate on the "email" field. +func EmailNEQ(v string) predicate.User { + return predicate.User(sql.FieldNEQ(FieldEmail, v)) +} + +// EmailIn applies the In predicate on the "email" field. +func EmailIn(vs ...string) predicate.User { + return predicate.User(sql.FieldIn(FieldEmail, vs...)) +} + +// EmailNotIn applies the NotIn predicate on the "email" field. +func EmailNotIn(vs ...string) predicate.User { + return predicate.User(sql.FieldNotIn(FieldEmail, vs...)) +} + +// EmailGT applies the GT predicate on the "email" field. +func EmailGT(v string) predicate.User { + return predicate.User(sql.FieldGT(FieldEmail, v)) +} + +// EmailGTE applies the GTE predicate on the "email" field. +func EmailGTE(v string) predicate.User { + return predicate.User(sql.FieldGTE(FieldEmail, v)) +} + +// EmailLT applies the LT predicate on the "email" field. +func EmailLT(v string) predicate.User { + return predicate.User(sql.FieldLT(FieldEmail, v)) +} + +// EmailLTE applies the LTE predicate on the "email" field. +func EmailLTE(v string) predicate.User { + return predicate.User(sql.FieldLTE(FieldEmail, v)) +} + +// EmailContains applies the Contains predicate on the "email" field. +func EmailContains(v string) predicate.User { + return predicate.User(sql.FieldContains(FieldEmail, v)) +} + +// EmailHasPrefix applies the HasPrefix predicate on the "email" field. +func EmailHasPrefix(v string) predicate.User { + return predicate.User(sql.FieldHasPrefix(FieldEmail, v)) +} + +// EmailHasSuffix applies the HasSuffix predicate on the "email" field. +func EmailHasSuffix(v string) predicate.User { + return predicate.User(sql.FieldHasSuffix(FieldEmail, v)) +} + +// EmailEqualFold applies the EqualFold predicate on the "email" field. +func EmailEqualFold(v string) predicate.User { + return predicate.User(sql.FieldEqualFold(FieldEmail, v)) +} + +// EmailContainsFold applies the ContainsFold predicate on the "email" field. +func EmailContainsFold(v string) predicate.User { + return predicate.User(sql.FieldContainsFold(FieldEmail, v)) +} + +// PasswordHashEQ applies the EQ predicate on the "password_hash" field. +func PasswordHashEQ(v string) predicate.User { + return predicate.User(sql.FieldEQ(FieldPasswordHash, v)) +} + +// PasswordHashNEQ applies the NEQ predicate on the "password_hash" field. +func PasswordHashNEQ(v string) predicate.User { + return predicate.User(sql.FieldNEQ(FieldPasswordHash, v)) +} + +// PasswordHashIn applies the In predicate on the "password_hash" field. +func PasswordHashIn(vs ...string) predicate.User { + return predicate.User(sql.FieldIn(FieldPasswordHash, vs...)) +} + +// PasswordHashNotIn applies the NotIn predicate on the "password_hash" field. +func PasswordHashNotIn(vs ...string) predicate.User { + return predicate.User(sql.FieldNotIn(FieldPasswordHash, vs...)) +} + +// PasswordHashGT applies the GT predicate on the "password_hash" field. +func PasswordHashGT(v string) predicate.User { + return predicate.User(sql.FieldGT(FieldPasswordHash, v)) +} + +// PasswordHashGTE applies the GTE predicate on the "password_hash" field. +func PasswordHashGTE(v string) predicate.User { + return predicate.User(sql.FieldGTE(FieldPasswordHash, v)) +} + +// PasswordHashLT applies the LT predicate on the "password_hash" field. +func PasswordHashLT(v string) predicate.User { + return predicate.User(sql.FieldLT(FieldPasswordHash, v)) +} + +// PasswordHashLTE applies the LTE predicate on the "password_hash" field. +func PasswordHashLTE(v string) predicate.User { + return predicate.User(sql.FieldLTE(FieldPasswordHash, v)) +} + +// PasswordHashContains applies the Contains predicate on the "password_hash" field. +func PasswordHashContains(v string) predicate.User { + return predicate.User(sql.FieldContains(FieldPasswordHash, v)) +} + +// PasswordHashHasPrefix applies the HasPrefix predicate on the "password_hash" field. +func PasswordHashHasPrefix(v string) predicate.User { + return predicate.User(sql.FieldHasPrefix(FieldPasswordHash, v)) +} + +// PasswordHashHasSuffix applies the HasSuffix predicate on the "password_hash" field. +func PasswordHashHasSuffix(v string) predicate.User { + return predicate.User(sql.FieldHasSuffix(FieldPasswordHash, v)) +} + +// PasswordHashEqualFold applies the EqualFold predicate on the "password_hash" field. +func PasswordHashEqualFold(v string) predicate.User { + return predicate.User(sql.FieldEqualFold(FieldPasswordHash, v)) +} + +// PasswordHashContainsFold applies the ContainsFold predicate on the "password_hash" field. +func PasswordHashContainsFold(v string) predicate.User { + return predicate.User(sql.FieldContainsFold(FieldPasswordHash, v)) +} + +// VerifiedEQ applies the EQ predicate on the "verified" field. +func VerifiedEQ(v bool) predicate.User { + return predicate.User(sql.FieldEQ(FieldVerified, v)) +} + +// VerifiedNEQ applies the NEQ predicate on the "verified" field. +func VerifiedNEQ(v bool) predicate.User { + return predicate.User(sql.FieldNEQ(FieldVerified, v)) +} + +// CreatedAtEQ applies the EQ predicate on the "created_at" field. +func CreatedAtEQ(v time.Time) predicate.User { + return predicate.User(sql.FieldEQ(FieldCreatedAt, v)) +} + +// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. +func CreatedAtNEQ(v time.Time) predicate.User { + return predicate.User(sql.FieldNEQ(FieldCreatedAt, v)) +} + +// CreatedAtIn applies the In predicate on the "created_at" field. +func CreatedAtIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldIn(FieldCreatedAt, vs...)) +} + +// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. +func CreatedAtNotIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldNotIn(FieldCreatedAt, vs...)) +} + +// CreatedAtGT applies the GT predicate on the "created_at" field. +func CreatedAtGT(v time.Time) predicate.User { + return predicate.User(sql.FieldGT(FieldCreatedAt, v)) +} + +// CreatedAtGTE applies the GTE predicate on the "created_at" field. +func CreatedAtGTE(v time.Time) predicate.User { + return predicate.User(sql.FieldGTE(FieldCreatedAt, v)) +} + +// CreatedAtLT applies the LT predicate on the "created_at" field. +func CreatedAtLT(v time.Time) predicate.User { + return predicate.User(sql.FieldLT(FieldCreatedAt, v)) +} + +// CreatedAtLTE applies the LTE predicate on the "created_at" field. +func CreatedAtLTE(v time.Time) predicate.User { + return predicate.User(sql.FieldLTE(FieldCreatedAt, v)) +} + +// UpdatedAtEQ applies the EQ predicate on the "updated_at" field. +func UpdatedAtEQ(v time.Time) predicate.User { + return predicate.User(sql.FieldEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field. +func UpdatedAtNEQ(v time.Time) predicate.User { + return predicate.User(sql.FieldNEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtIn applies the In predicate on the "updated_at" field. +func UpdatedAtIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field. +func UpdatedAtNotIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldNotIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtGT applies the GT predicate on the "updated_at" field. +func UpdatedAtGT(v time.Time) predicate.User { + return predicate.User(sql.FieldGT(FieldUpdatedAt, v)) +} + +// UpdatedAtGTE applies the GTE predicate on the "updated_at" field. +func UpdatedAtGTE(v time.Time) predicate.User { + return predicate.User(sql.FieldGTE(FieldUpdatedAt, v)) +} + +// UpdatedAtLT applies the LT predicate on the "updated_at" field. +func UpdatedAtLT(v time.Time) predicate.User { + return predicate.User(sql.FieldLT(FieldUpdatedAt, v)) +} + +// UpdatedAtLTE applies the LTE predicate on the "updated_at" field. +func UpdatedAtLTE(v time.Time) predicate.User { + return predicate.User(sql.FieldLTE(FieldUpdatedAt, v)) +} + +// HasUserRoles applies the HasEdge predicate on the "user_roles" edge. +func HasUserRoles() predicate.User { + return predicate.User(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, UserRolesTable, UserRolesColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasUserRolesWith applies the HasEdge predicate on the "user_roles" edge with a given conditions (other predicates). +func HasUserRolesWith(preds ...predicate.UserRole) predicate.User { + return predicate.User(func(s *sql.Selector) { + step := newUserRolesStep() + 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(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.User) predicate.User { + return predicate.User(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.User) predicate.User { + return predicate.User(sql.NotPredicates(p)) +} diff --git a/internal/ent/user_create.go b/internal/ent/user_create.go new file mode 100644 index 0000000..e851a16 --- /dev/null +++ b/internal/ent/user_create.go @@ -0,0 +1,331 @@ +// 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/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserCreate is the builder for creating a User entity. +type UserCreate struct { + config + mutation *UserMutation + hooks []Hook +} + +// SetEmail sets the "email" field. +func (_c *UserCreate) SetEmail(v string) *UserCreate { + _c.mutation.SetEmail(v) + return _c +} + +// SetPasswordHash sets the "password_hash" field. +func (_c *UserCreate) SetPasswordHash(v string) *UserCreate { + _c.mutation.SetPasswordHash(v) + return _c +} + +// SetVerified sets the "verified" field. +func (_c *UserCreate) SetVerified(v bool) *UserCreate { + _c.mutation.SetVerified(v) + return _c +} + +// SetNillableVerified sets the "verified" field if the given value is not nil. +func (_c *UserCreate) SetNillableVerified(v *bool) *UserCreate { + if v != nil { + _c.SetVerified(*v) + } + return _c +} + +// SetCreatedAt sets the "created_at" field. +func (_c *UserCreate) SetCreatedAt(v time.Time) *UserCreate { + _c.mutation.SetCreatedAt(v) + return _c +} + +// SetNillableCreatedAt sets the "created_at" field if the given value is not nil. +func (_c *UserCreate) SetNillableCreatedAt(v *time.Time) *UserCreate { + if v != nil { + _c.SetCreatedAt(*v) + } + return _c +} + +// SetUpdatedAt sets the "updated_at" field. +func (_c *UserCreate) SetUpdatedAt(v time.Time) *UserCreate { + _c.mutation.SetUpdatedAt(v) + return _c +} + +// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. +func (_c *UserCreate) SetNillableUpdatedAt(v *time.Time) *UserCreate { + if v != nil { + _c.SetUpdatedAt(*v) + } + return _c +} + +// SetID sets the "id" field. +func (_c *UserCreate) SetID(v string) *UserCreate { + _c.mutation.SetID(v) + return _c +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs. +func (_c *UserCreate) AddUserRoleIDs(ids ...int) *UserCreate { + _c.mutation.AddUserRoleIDs(ids...) + return _c +} + +// AddUserRoles adds the "user_roles" edges to the UserRole entity. +func (_c *UserCreate) AddUserRoles(v ...*UserRole) *UserCreate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _c.AddUserRoleIDs(ids...) +} + +// Mutation returns the UserMutation object of the builder. +func (_c *UserCreate) Mutation() *UserMutation { + return _c.mutation +} + +// Save creates the User in the database. +func (_c *UserCreate) Save(ctx context.Context) (*User, error) { + _c.defaults() + return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (_c *UserCreate) SaveX(ctx context.Context) *User { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *UserCreate) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *UserCreate) 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 *UserCreate) defaults() { + if _, ok := _c.mutation.Verified(); !ok { + v := user.DefaultVerified + _c.mutation.SetVerified(v) + } + if _, ok := _c.mutation.CreatedAt(); !ok { + v := user.DefaultCreatedAt() + _c.mutation.SetCreatedAt(v) + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + v := user.DefaultUpdatedAt() + _c.mutation.SetUpdatedAt(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_c *UserCreate) check() error { + if _, ok := _c.mutation.Email(); !ok { + return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)} + } + if v, ok := _c.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 := _c.mutation.PasswordHash(); !ok { + return &ValidationError{Name: "password_hash", err: errors.New(`ent: missing required field "User.password_hash"`)} + } + if v, ok := _c.mutation.PasswordHash(); ok { + if err := user.PasswordHashValidator(v); err != nil { + return &ValidationError{Name: "password_hash", err: fmt.Errorf(`ent: validator failed for field "User.password_hash": %w`, err)} + } + } + if _, ok := _c.mutation.Verified(); !ok { + return &ValidationError{Name: "verified", err: errors.New(`ent: missing required field "User.verified"`)} + } + if _, ok := _c.mutation.CreatedAt(); !ok { + return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)} + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)} + } + return nil +} + +func (_c *UserCreate) sqlSave(ctx context.Context) (*User, 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 User.ID type: %T", _spec.ID.Value) + } + } + _c.mutation.id = &_node.ID + _c.mutation.done = true + return _node, nil +} + +func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { + var ( + _node = &User{config: _c.config} + _spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString)) + ) + if id, ok := _c.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = id + } + if value, ok := _c.mutation.Email(); ok { + _spec.SetField(user.FieldEmail, field.TypeString, value) + _node.Email = value + } + if value, ok := _c.mutation.PasswordHash(); ok { + _spec.SetField(user.FieldPasswordHash, field.TypeString, value) + _node.PasswordHash = value + } + if value, ok := _c.mutation.Verified(); ok { + _spec.SetField(user.FieldVerified, field.TypeBool, value) + _node.Verified = value + } + if value, ok := _c.mutation.CreatedAt(); ok { + _spec.SetField(user.FieldCreatedAt, field.TypeTime, value) + _node.CreatedAt = value + } + if value, ok := _c.mutation.UpdatedAt(); ok { + _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) + _node.UpdatedAt = value + } + if nodes := _c.mutation.UserRolesIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + 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 + err error + builders []*UserCreate +} + +// Save creates the User entities in the database. +func (_c *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { + if _c.err != nil { + return nil, _c.err + } + specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) + nodes := make([]*User, 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.(*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, _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 *UserCreateBulk) SaveX(ctx context.Context) []*User { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *UserCreateBulk) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *UserCreateBulk) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/user_delete.go b/internal/ent/user_delete.go new file mode 100644 index 0000000..e264ee0 --- /dev/null +++ b/internal/ent/user_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" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/user" +) + +// UserDelete is the builder for deleting a User entity. +type UserDelete struct { + config + hooks []Hook + mutation *UserMutation +} + +// Where appends a list predicates to the UserDelete builder. +func (_d *UserDelete) Where(ps ...predicate.User) *UserDelete { + _d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (_d *UserDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *UserDelete) ExecX(ctx context.Context) int { + n, err := _d.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (_d *UserDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString)) + if ps := _d.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + _d.mutation.done = true + return affected, err +} + +// UserDeleteOne is the builder for deleting a single User entity. +type UserDeleteOne struct { + _d *UserDelete +} + +// Where appends a list predicates to the UserDelete builder. +func (_d *UserDeleteOne) Where(ps ...predicate.User) *UserDeleteOne { + _d._d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query. +func (_d *UserDeleteOne) Exec(ctx context.Context) error { + n, err := _d._d.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{user.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *UserDeleteOne) ExecX(ctx context.Context) { + if err := _d.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/user_query.go b/internal/ent/user_query.go new file mode 100644 index 0000000..87cac0a --- /dev/null +++ b/internal/ent/user_query.go @@ -0,0 +1,607 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "database/sql/driver" + "fmt" + "math" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserQuery is the builder for querying User entities. +type UserQuery struct { + config + ctx *QueryContext + order []user.OrderOption + inters []Interceptor + predicates []predicate.User + withUserRoles *UserRoleQuery + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the UserQuery builder. +func (_q *UserQuery) Where(ps ...predicate.User) *UserQuery { + _q.predicates = append(_q.predicates, ps...) + return _q +} + +// Limit the number of records to be returned by this query. +func (_q *UserQuery) Limit(limit int) *UserQuery { + _q.ctx.Limit = &limit + return _q +} + +// Offset to start from. +func (_q *UserQuery) Offset(offset int) *UserQuery { + _q.ctx.Offset = &offset + return _q +} + +// 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 (_q *UserQuery) Unique(unique bool) *UserQuery { + _q.ctx.Unique = &unique + return _q +} + +// Order specifies how the records should be ordered. +func (_q *UserQuery) Order(o ...user.OrderOption) *UserQuery { + _q.order = append(_q.order, o...) + return _q +} + +// QueryUserRoles chains the current query on the "user_roles" edge. +func (_q *UserQuery) QueryUserRoles() *UserRoleQuery { + query := (&UserRoleClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(user.Table, user.FieldID, selector), + sqlgraph.To(userrole.Table, userrole.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, user.UserRolesTable, user.UserRolesColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.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 (_q *UserQuery) First(ctx context.Context) (*User, error) { + nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{user.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (_q *UserQuery) FirstX(ctx context.Context) *User { + node, err := _q.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first User ID from the query. +// Returns a *NotFoundError when no User ID was found. +func (_q *UserQuery) FirstID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{user.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (_q *UserQuery) FirstIDX(ctx context.Context) string { + id, err := _q.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single User entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one User entity is found. +// Returns a *NotFoundError when no User entities are found. +func (_q *UserQuery) Only(ctx context.Context) (*User, error) { + nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{user.Label} + default: + return nil, &NotSingularError{user.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (_q *UserQuery) OnlyX(ctx context.Context) *User { + node, err := _q.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only User ID in the query. +// Returns a *NotSingularError when more than one User ID is found. +// Returns a *NotFoundError when no entities are found. +func (_q *UserQuery) OnlyID(ctx context.Context) (id string, err error) { + var ids []string + if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{user.Label} + default: + err = &NotSingularError{user.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (_q *UserQuery) OnlyIDX(ctx context.Context) string { + id, err := _q.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of Users. +func (_q *UserQuery) All(ctx context.Context) ([]*User, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll) + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*User, *UserQuery]() + return withInterceptors[[]*User](ctx, _q, qr, _q.inters) +} + +// AllX is like All, but panics if an error occurs. +func (_q *UserQuery) AllX(ctx context.Context) []*User { + nodes, err := _q.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of User IDs. +func (_q *UserQuery) IDs(ctx context.Context) (ids []string, err error) { + if _q.ctx.Unique == nil && _q.path != nil { + _q.Unique(true) + } + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs) + if err = _q.Select(user.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (_q *UserQuery) IDsX(ctx context.Context) []string { + ids, err := _q.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (_q *UserQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount) + if err := _q.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, _q, querierCount[*UserQuery](), _q.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (_q *UserQuery) CountX(ctx context.Context) int { + count, err := _q.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (_q *UserQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist) + switch _, err := _q.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 (_q *UserQuery) ExistX(ctx context.Context) bool { + exist, err := _q.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (_q *UserQuery) Clone() *UserQuery { + if _q == nil { + return nil + } + return &UserQuery{ + config: _q.config, + ctx: _q.ctx.Clone(), + order: append([]user.OrderOption{}, _q.order...), + inters: append([]Interceptor{}, _q.inters...), + predicates: append([]predicate.User{}, _q.predicates...), + withUserRoles: _q.withUserRoles.Clone(), + // clone intermediate query. + sql: _q.sql.Clone(), + path: _q.path, + } +} + +// WithUserRoles tells the query-builder to eager-load the nodes that are connected to +// the "user_roles" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *UserQuery) WithUserRoles(opts ...func(*UserRoleQuery)) *UserQuery { + query := (&UserRoleClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withUserRoles = query + return _q +} + +// 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 { +// Email string `json:"email,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.User.Query(). +// GroupBy(user.FieldEmail). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { + _q.ctx.Fields = append([]string{field}, fields...) + grbuild := &UserGroupBy{build: _q} + grbuild.flds = &_q.ctx.Fields + grbuild.label = user.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 { +// Email string `json:"email,omitempty"` +// } +// +// client.User.Query(). +// Select(user.FieldEmail). +// Scan(ctx, &v) +func (_q *UserQuery) Select(fields ...string) *UserSelect { + _q.ctx.Fields = append(_q.ctx.Fields, fields...) + sbuild := &UserSelect{UserQuery: _q} + sbuild.label = user.Label + sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a UserSelect configured with the given aggregations. +func (_q *UserQuery) Aggregate(fns ...AggregateFunc) *UserSelect { + return _q.Select().Aggregate(fns...) +} + +func (_q *UserQuery) prepareQuery(ctx context.Context) error { + for _, inter := range _q.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, _q); err != nil { + return err + } + } + } + for _, f := range _q.ctx.Fields { + if !user.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if _q.path != nil { + prev, err := _q.path(ctx) + if err != nil { + return err + } + _q.sql = prev + } + return nil +} + +func (_q *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, error) { + var ( + nodes = []*User{} + _spec = _q.querySpec() + loadedTypes = [1]bool{ + _q.withUserRoles != nil, + } + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*User).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &User{config: _q.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, _q.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := _q.withUserRoles; query != nil { + if err := _q.loadUserRoles(ctx, query, nodes, + func(n *User) { n.Edges.UserRoles = []*UserRole{} }, + func(n *User, e *UserRole) { n.Edges.UserRoles = append(n.Edges.UserRoles, e) }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (_q *UserQuery) loadUserRoles(ctx context.Context, query *UserRoleQuery, nodes []*User, init func(*User), assign func(*User, *UserRole)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[string]*User) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + query.withFKs = true + query.Where(predicate.UserRole(func(s *sql.Selector) { + s.Where(sql.InValues(s.C(user.UserRolesColumn), fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.user_user_roles + if fk == nil { + return fmt.Errorf(`foreign-key "user_user_roles" is nil for node %v`, n.ID) + } + node, ok := nodeids[*fk] + if !ok { + return fmt.Errorf(`unexpected referenced foreign-key "user_user_roles" returned %v for node %v`, *fk, n.ID) + } + assign(node, n) + } + return nil +} + +func (_q *UserQuery) sqlCount(ctx context.Context) (int, error) { + _spec := _q.querySpec() + _spec.Node.Columns = _q.ctx.Fields + if len(_q.ctx.Fields) > 0 { + _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique + } + return sqlgraph.CountNodes(ctx, _q.driver, _spec) +} + +func (_q *UserQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString)) + _spec.From = _q.sql + if unique := _q.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if _q.path != nil { + _spec.Unique = true + } + if fields := _q.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, user.FieldID) + for i := range fields { + if fields[i] != user.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := _q.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := _q.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := _q.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := _q.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (_q *UserQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(_q.driver.Dialect()) + t1 := builder.Table(user.Table) + columns := _q.ctx.Fields + if len(columns) == 0 { + columns = user.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if _q.sql != nil { + selector = _q.sql + selector.Select(selector.Columns(columns...)...) + } + if _q.ctx.Unique != nil && *_q.ctx.Unique { + selector.Distinct() + } + for _, p := range _q.predicates { + p(selector) + } + for _, p := range _q.order { + p(selector) + } + if offset := _q.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 := _q.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// UserGroupBy is the group-by builder for User entities. +type UserGroupBy struct { + selector + build *UserQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (_g *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy { + _g.fns = append(_g.fns, fns...) + return _g +} + +// Scan applies the selector query and scans the result into the given value. +func (_g *UserGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*UserQuery, *UserGroupBy](ctx, _g.build, _g, _g.build.inters, v) +} + +func (_g *UserGroupBy) sqlScan(ctx context.Context, root *UserQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*_g.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// UserSelect is the builder for selecting fields of User entities. +type UserSelect struct { + *UserQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (_s *UserSelect) Aggregate(fns ...AggregateFunc) *UserSelect { + _s.fns = append(_s.fns, fns...) + return _s +} + +// Scan applies the selector query and scans the result into the given value. +func (_s *UserSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*UserQuery, *UserSelect](ctx, _s.UserQuery, _s, _s.inters, v) +} + +func (_s *UserSelect) sqlScan(ctx context.Context, root *UserQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/ent/user_update.go b/internal/ent/user_update.go new file mode 100644 index 0000000..d428d89 --- /dev/null +++ b/internal/ent/user_update.go @@ -0,0 +1,513 @@ +// 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" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// 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 (_u *UserUpdate) Where(ps ...predicate.User) *UserUpdate { + _u.mutation.Where(ps...) + return _u +} + +// SetEmail sets the "email" field. +func (_u *UserUpdate) SetEmail(v string) *UserUpdate { + _u.mutation.SetEmail(v) + return _u +} + +// SetNillableEmail sets the "email" field if the given value is not nil. +func (_u *UserUpdate) SetNillableEmail(v *string) *UserUpdate { + if v != nil { + _u.SetEmail(*v) + } + return _u +} + +// SetPasswordHash sets the "password_hash" field. +func (_u *UserUpdate) SetPasswordHash(v string) *UserUpdate { + _u.mutation.SetPasswordHash(v) + return _u +} + +// SetNillablePasswordHash sets the "password_hash" field if the given value is not nil. +func (_u *UserUpdate) SetNillablePasswordHash(v *string) *UserUpdate { + if v != nil { + _u.SetPasswordHash(*v) + } + return _u +} + +// SetVerified sets the "verified" field. +func (_u *UserUpdate) SetVerified(v bool) *UserUpdate { + _u.mutation.SetVerified(v) + return _u +} + +// SetNillableVerified sets the "verified" field if the given value is not nil. +func (_u *UserUpdate) SetNillableVerified(v *bool) *UserUpdate { + if v != nil { + _u.SetVerified(*v) + } + return _u +} + +// SetUpdatedAt sets the "updated_at" field. +func (_u *UserUpdate) SetUpdatedAt(v time.Time) *UserUpdate { + _u.mutation.SetUpdatedAt(v) + return _u +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs. +func (_u *UserUpdate) AddUserRoleIDs(ids ...int) *UserUpdate { + _u.mutation.AddUserRoleIDs(ids...) + return _u +} + +// AddUserRoles adds the "user_roles" edges to the UserRole entity. +func (_u *UserUpdate) AddUserRoles(v ...*UserRole) *UserUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddUserRoleIDs(ids...) +} + +// Mutation returns the UserMutation object of the builder. +func (_u *UserUpdate) Mutation() *UserMutation { + return _u.mutation +} + +// ClearUserRoles clears all "user_roles" edges to the UserRole entity. +func (_u *UserUpdate) ClearUserRoles() *UserUpdate { + _u.mutation.ClearUserRoles() + return _u +} + +// RemoveUserRoleIDs removes the "user_roles" edge to UserRole entities by IDs. +func (_u *UserUpdate) RemoveUserRoleIDs(ids ...int) *UserUpdate { + _u.mutation.RemoveUserRoleIDs(ids...) + return _u +} + +// RemoveUserRoles removes "user_roles" edges to UserRole entities. +func (_u *UserUpdate) RemoveUserRoles(v ...*UserRole) *UserUpdate { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveUserRoleIDs(ids...) +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (_u *UserUpdate) Save(ctx context.Context) (int, error) { + _u.defaults() + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *UserUpdate) SaveX(ctx context.Context) int { + affected, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (_u *UserUpdate) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *UserUpdate) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// defaults sets the default values of the builder before save. +func (_u *UserUpdate) defaults() { + if _, ok := _u.mutation.UpdatedAt(); !ok { + v := user.UpdateDefaultUpdatedAt() + _u.mutation.SetUpdatedAt(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *UserUpdate) check() error { + if v, ok := _u.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 v, ok := _u.mutation.PasswordHash(); ok { + if err := user.PasswordHashValidator(v); err != nil { + return &ValidationError{Name: "password_hash", err: fmt.Errorf(`ent: validator failed for field "User.password_hash": %w`, err)} + } + } + return nil +} + +func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString)) + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.Email(); ok { + _spec.SetField(user.FieldEmail, field.TypeString, value) + } + if value, ok := _u.mutation.PasswordHash(); ok { + _spec.SetField(user.FieldPasswordHash, field.TypeString, value) + } + if value, ok := _u.mutation.Verified(); ok { + _spec.SetField(user.FieldVerified, field.TypeBool, value) + } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) + } + if _u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedUserRolesIDs(); len(nodes) > 0 && !_u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.UserRolesIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _node, err = sqlgraph.UpdateNodes(ctx, _u.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 + } + _u.mutation.done = true + return _node, nil +} + +// UserUpdateOne is the builder for updating a single User entity. +type UserUpdateOne struct { + config + fields []string + hooks []Hook + mutation *UserMutation +} + +// SetEmail sets the "email" field. +func (_u *UserUpdateOne) SetEmail(v string) *UserUpdateOne { + _u.mutation.SetEmail(v) + return _u +} + +// SetNillableEmail sets the "email" field if the given value is not nil. +func (_u *UserUpdateOne) SetNillableEmail(v *string) *UserUpdateOne { + if v != nil { + _u.SetEmail(*v) + } + return _u +} + +// SetPasswordHash sets the "password_hash" field. +func (_u *UserUpdateOne) SetPasswordHash(v string) *UserUpdateOne { + _u.mutation.SetPasswordHash(v) + return _u +} + +// SetNillablePasswordHash sets the "password_hash" field if the given value is not nil. +func (_u *UserUpdateOne) SetNillablePasswordHash(v *string) *UserUpdateOne { + if v != nil { + _u.SetPasswordHash(*v) + } + return _u +} + +// SetVerified sets the "verified" field. +func (_u *UserUpdateOne) SetVerified(v bool) *UserUpdateOne { + _u.mutation.SetVerified(v) + return _u +} + +// SetNillableVerified sets the "verified" field if the given value is not nil. +func (_u *UserUpdateOne) SetNillableVerified(v *bool) *UserUpdateOne { + if v != nil { + _u.SetVerified(*v) + } + return _u +} + +// SetUpdatedAt sets the "updated_at" field. +func (_u *UserUpdateOne) SetUpdatedAt(v time.Time) *UserUpdateOne { + _u.mutation.SetUpdatedAt(v) + return _u +} + +// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs. +func (_u *UserUpdateOne) AddUserRoleIDs(ids ...int) *UserUpdateOne { + _u.mutation.AddUserRoleIDs(ids...) + return _u +} + +// AddUserRoles adds the "user_roles" edges to the UserRole entity. +func (_u *UserUpdateOne) AddUserRoles(v ...*UserRole) *UserUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.AddUserRoleIDs(ids...) +} + +// Mutation returns the UserMutation object of the builder. +func (_u *UserUpdateOne) Mutation() *UserMutation { + return _u.mutation +} + +// ClearUserRoles clears all "user_roles" edges to the UserRole entity. +func (_u *UserUpdateOne) ClearUserRoles() *UserUpdateOne { + _u.mutation.ClearUserRoles() + return _u +} + +// RemoveUserRoleIDs removes the "user_roles" edge to UserRole entities by IDs. +func (_u *UserUpdateOne) RemoveUserRoleIDs(ids ...int) *UserUpdateOne { + _u.mutation.RemoveUserRoleIDs(ids...) + return _u +} + +// RemoveUserRoles removes "user_roles" edges to UserRole entities. +func (_u *UserUpdateOne) RemoveUserRoles(v ...*UserRole) *UserUpdateOne { + ids := make([]int, len(v)) + for i := range v { + ids[i] = v[i].ID + } + return _u.RemoveUserRoleIDs(ids...) +} + +// Where appends a list predicates to the UserUpdate builder. +func (_u *UserUpdateOne) Where(ps ...predicate.User) *UserUpdateOne { + _u.mutation.Where(ps...) + return _u +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (_u *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne { + _u.fields = append([]string{field}, fields...) + return _u +} + +// Save executes the query and returns the updated User entity. +func (_u *UserUpdateOne) Save(ctx context.Context) (*User, error) { + _u.defaults() + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *UserUpdateOne) SaveX(ctx context.Context) *User { + node, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (_u *UserUpdateOne) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *UserUpdateOne) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// defaults sets the default values of the builder before save. +func (_u *UserUpdateOne) defaults() { + if _, ok := _u.mutation.UpdatedAt(); !ok { + v := user.UpdateDefaultUpdatedAt() + _u.mutation.SetUpdatedAt(v) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *UserUpdateOne) check() error { + if v, ok := _u.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 v, ok := _u.mutation.PasswordHash(); ok { + if err := user.PasswordHashValidator(v); err != nil { + return &ValidationError{Name: "password_hash", err: fmt.Errorf(`ent: validator failed for field "User.password_hash": %w`, err)} + } + } + return nil +} + +func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString)) + id, ok := _u.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 := _u.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 := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := _u.mutation.Email(); ok { + _spec.SetField(user.FieldEmail, field.TypeString, value) + } + if value, ok := _u.mutation.PasswordHash(); ok { + _spec.SetField(user.FieldPasswordHash, field.TypeString, value) + } + if value, ok := _u.mutation.Verified(); ok { + _spec.SetField(user.FieldVerified, field.TypeBool, value) + } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) + } + if _u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RemovedUserRolesIDs(); len(nodes) > 0 && !_u.mutation.UserRolesCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.UserRolesIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: false, + Table: user.UserRolesTable, + Columns: []string{user.UserRolesColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &User{config: _u.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, _u.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 + } + _u.mutation.done = true + return _node, nil +} diff --git a/internal/ent/userrole.go b/internal/ent/userrole.go new file mode 100644 index 0000000..2bb026e --- /dev/null +++ b/internal/ent/userrole.go @@ -0,0 +1,182 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserRole is the model entity for the UserRole schema. +type UserRole struct { + config `json:"-"` + // ID of the ent. + ID int `json:"id,omitempty"` + // UserID holds the value of the "user_id" field. + UserID string `json:"user_id,omitempty"` + // RoleID holds the value of the "role_id" field. + RoleID string `json:"role_id,omitempty"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the UserRoleQuery when eager-loading is set. + Edges UserRoleEdges `json:"edges"` + role_user_roles *string + user_user_roles *string + selectValues sql.SelectValues +} + +// UserRoleEdges holds the relations/edges for other nodes in the graph. +type UserRoleEdges struct { + // User holds the value of the user edge. + User *User `json:"user,omitempty"` + // Role holds the value of the role edge. + Role *Role `json:"role,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [2]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 UserRoleEdges) UserOrErr() (*User, error) { + if e.User != nil { + return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} + } + return nil, &NotLoadedError{edge: "user"} +} + +// RoleOrErr returns the Role value or an error if the edge +// was not loaded in eager-loading, or loaded but was not found. +func (e UserRoleEdges) RoleOrErr() (*Role, error) { + if e.Role != nil { + return e.Role, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: role.Label} + } + return nil, &NotLoadedError{edge: "role"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*UserRole) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case userrole.FieldID: + values[i] = new(sql.NullInt64) + case userrole.FieldUserID, userrole.FieldRoleID: + values[i] = new(sql.NullString) + case userrole.ForeignKeys[0]: // role_user_roles + values[i] = new(sql.NullString) + case userrole.ForeignKeys[1]: // user_user_roles + values[i] = new(sql.NullString) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the UserRole fields. +func (_m *UserRole) 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 userrole.FieldID: + value, ok := values[i].(*sql.NullInt64) + if !ok { + return fmt.Errorf("unexpected type %T for field id", value) + } + _m.ID = int(value.Int64) + case userrole.FieldUserID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field user_id", values[i]) + } else if value.Valid { + _m.UserID = value.String + } + case userrole.FieldRoleID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field role_id", values[i]) + } else if value.Valid { + _m.RoleID = value.String + } + case userrole.ForeignKeys[0]: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field role_user_roles", values[i]) + } else if value.Valid { + _m.role_user_roles = new(string) + *_m.role_user_roles = value.String + } + case userrole.ForeignKeys[1]: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field user_user_roles", values[i]) + } else if value.Valid { + _m.user_user_roles = new(string) + *_m.user_user_roles = value.String + } + default: + _m.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the UserRole. +// This includes values selected through modifiers, order, etc. +func (_m *UserRole) Value(name string) (ent.Value, error) { + return _m.selectValues.Get(name) +} + +// QueryUser queries the "user" edge of the UserRole entity. +func (_m *UserRole) QueryUser() *UserQuery { + return NewUserRoleClient(_m.config).QueryUser(_m) +} + +// QueryRole queries the "role" edge of the UserRole entity. +func (_m *UserRole) QueryRole() *RoleQuery { + return NewUserRoleClient(_m.config).QueryRole(_m) +} + +// Update returns a builder for updating this UserRole. +// Note that you need to call UserRole.Unwrap() before calling this method if this UserRole +// was returned from a transaction, and the transaction was committed or rolled back. +func (_m *UserRole) Update() *UserRoleUpdateOne { + return NewUserRoleClient(_m.config).UpdateOne(_m) +} + +// Unwrap unwraps the UserRole 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 (_m *UserRole) Unwrap() *UserRole { + _tx, ok := _m.config.driver.(*txDriver) + if !ok { + panic("ent: UserRole is not a transactional entity") + } + _m.config.driver = _tx.drv + return _m +} + +// String implements the fmt.Stringer. +func (_m *UserRole) String() string { + var builder strings.Builder + builder.WriteString("UserRole(") + builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("user_id=") + builder.WriteString(_m.UserID) + builder.WriteString(", ") + builder.WriteString("role_id=") + builder.WriteString(_m.RoleID) + builder.WriteByte(')') + return builder.String() +} + +// UserRoles is a parsable slice of UserRole. +type UserRoles []*UserRole diff --git a/internal/ent/userrole/userrole.go b/internal/ent/userrole/userrole.go new file mode 100644 index 0000000..23b8734 --- /dev/null +++ b/internal/ent/userrole/userrole.go @@ -0,0 +1,114 @@ +// Code generated by ent, DO NOT EDIT. + +package userrole + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" +) + +const ( + // Label holds the string label denoting the userrole type in the database. + Label = "user_role" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldUserID holds the string denoting the user_id field in the database. + FieldUserID = "user_id" + // FieldRoleID holds the string denoting the role_id field in the database. + FieldRoleID = "role_id" + // EdgeUser holds the string denoting the user edge name in mutations. + EdgeUser = "user" + // EdgeRole holds the string denoting the role edge name in mutations. + EdgeRole = "role" + // Table holds the table name of the userrole in the database. + Table = "user_roles" + // UserTable is the table that holds the user relation/edge. + UserTable = "user_roles" + // 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" + // RoleTable is the table that holds the role relation/edge. + RoleTable = "user_roles" + // RoleInverseTable is the table name for the Role entity. + // It exists in this package in order to avoid circular dependency with the "role" package. + RoleInverseTable = "roles" + // RoleColumn is the table column denoting the role relation/edge. + RoleColumn = "role_id" +) + +// Columns holds all SQL columns for userrole fields. +var Columns = []string{ + FieldID, + FieldUserID, + FieldRoleID, +} + +// ForeignKeys holds the SQL foreign-keys that are owned by the "user_roles" +// table and are not defined as standalone fields in the schema. +var ForeignKeys = []string{ + "role_user_roles", + "user_user_roles", +} + +// 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 + } + } + for i := range ForeignKeys { + if column == ForeignKeys[i] { + return true + } + } + return false +} + +// OrderOption defines the ordering options for the UserRole queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByUserID orders the results by the user_id field. +func ByUserID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldUserID, opts...).ToFunc() +} + +// ByRoleID orders the results by the role_id field. +func ByRoleID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldRoleID, opts...).ToFunc() +} + +// ByUserField orders the results by user field. +func ByUserField(field string, opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newUserStep(), sql.OrderByField(field, opts...)) + } +} + +// ByRoleField orders the results by role field. +func ByRoleField(field string, opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newRoleStep(), sql.OrderByField(field, opts...)) + } +} +func newUserStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(UserInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, UserTable, UserColumn), + ) +} +func newRoleStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(RoleInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, RoleTable, RoleColumn), + ) +} diff --git a/internal/ent/userrole/where.go b/internal/ent/userrole/where.go new file mode 100644 index 0000000..cc68d90 --- /dev/null +++ b/internal/ent/userrole/where.go @@ -0,0 +1,255 @@ +// Code generated by ent, DO NOT EDIT. + +package userrole + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...int) predicate.UserRole { + return predicate.UserRole(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...int) predicate.UserRole { + return predicate.UserRole(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id int) predicate.UserRole { + return predicate.UserRole(sql.FieldLTE(FieldID, id)) +} + +// UserID applies equality check predicate on the "user_id" field. It's identical to UserIDEQ. +func UserID(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldEQ(FieldUserID, v)) +} + +// RoleID applies equality check predicate on the "role_id" field. It's identical to RoleIDEQ. +func RoleID(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldEQ(FieldRoleID, v)) +} + +// UserIDEQ applies the EQ predicate on the "user_id" field. +func UserIDEQ(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldEQ(FieldUserID, v)) +} + +// UserIDNEQ applies the NEQ predicate on the "user_id" field. +func UserIDNEQ(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldNEQ(FieldUserID, v)) +} + +// UserIDIn applies the In predicate on the "user_id" field. +func UserIDIn(vs ...string) predicate.UserRole { + return predicate.UserRole(sql.FieldIn(FieldUserID, vs...)) +} + +// UserIDNotIn applies the NotIn predicate on the "user_id" field. +func UserIDNotIn(vs ...string) predicate.UserRole { + return predicate.UserRole(sql.FieldNotIn(FieldUserID, vs...)) +} + +// UserIDGT applies the GT predicate on the "user_id" field. +func UserIDGT(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldGT(FieldUserID, v)) +} + +// UserIDGTE applies the GTE predicate on the "user_id" field. +func UserIDGTE(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldGTE(FieldUserID, v)) +} + +// UserIDLT applies the LT predicate on the "user_id" field. +func UserIDLT(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldLT(FieldUserID, v)) +} + +// UserIDLTE applies the LTE predicate on the "user_id" field. +func UserIDLTE(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldLTE(FieldUserID, v)) +} + +// UserIDContains applies the Contains predicate on the "user_id" field. +func UserIDContains(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldContains(FieldUserID, v)) +} + +// UserIDHasPrefix applies the HasPrefix predicate on the "user_id" field. +func UserIDHasPrefix(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldHasPrefix(FieldUserID, v)) +} + +// UserIDHasSuffix applies the HasSuffix predicate on the "user_id" field. +func UserIDHasSuffix(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldHasSuffix(FieldUserID, v)) +} + +// UserIDEqualFold applies the EqualFold predicate on the "user_id" field. +func UserIDEqualFold(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldEqualFold(FieldUserID, v)) +} + +// UserIDContainsFold applies the ContainsFold predicate on the "user_id" field. +func UserIDContainsFold(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldContainsFold(FieldUserID, v)) +} + +// RoleIDEQ applies the EQ predicate on the "role_id" field. +func RoleIDEQ(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldEQ(FieldRoleID, v)) +} + +// RoleIDNEQ applies the NEQ predicate on the "role_id" field. +func RoleIDNEQ(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldNEQ(FieldRoleID, v)) +} + +// RoleIDIn applies the In predicate on the "role_id" field. +func RoleIDIn(vs ...string) predicate.UserRole { + return predicate.UserRole(sql.FieldIn(FieldRoleID, vs...)) +} + +// RoleIDNotIn applies the NotIn predicate on the "role_id" field. +func RoleIDNotIn(vs ...string) predicate.UserRole { + return predicate.UserRole(sql.FieldNotIn(FieldRoleID, vs...)) +} + +// RoleIDGT applies the GT predicate on the "role_id" field. +func RoleIDGT(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldGT(FieldRoleID, v)) +} + +// RoleIDGTE applies the GTE predicate on the "role_id" field. +func RoleIDGTE(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldGTE(FieldRoleID, v)) +} + +// RoleIDLT applies the LT predicate on the "role_id" field. +func RoleIDLT(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldLT(FieldRoleID, v)) +} + +// RoleIDLTE applies the LTE predicate on the "role_id" field. +func RoleIDLTE(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldLTE(FieldRoleID, v)) +} + +// RoleIDContains applies the Contains predicate on the "role_id" field. +func RoleIDContains(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldContains(FieldRoleID, v)) +} + +// RoleIDHasPrefix applies the HasPrefix predicate on the "role_id" field. +func RoleIDHasPrefix(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldHasPrefix(FieldRoleID, v)) +} + +// RoleIDHasSuffix applies the HasSuffix predicate on the "role_id" field. +func RoleIDHasSuffix(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldHasSuffix(FieldRoleID, v)) +} + +// RoleIDEqualFold applies the EqualFold predicate on the "role_id" field. +func RoleIDEqualFold(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldEqualFold(FieldRoleID, v)) +} + +// RoleIDContainsFold applies the ContainsFold predicate on the "role_id" field. +func RoleIDContainsFold(v string) predicate.UserRole { + return predicate.UserRole(sql.FieldContainsFold(FieldRoleID, v)) +} + +// HasUser applies the HasEdge predicate on the "user" edge. +func HasUser() predicate.UserRole { + return predicate.UserRole(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, 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.UserRole { + return predicate.UserRole(func(s *sql.Selector) { + step := newUserStep() + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// HasRole applies the HasEdge predicate on the "role" edge. +func HasRole() predicate.UserRole { + return predicate.UserRole(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, RoleTable, RoleColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasRoleWith applies the HasEdge predicate on the "role" edge with a given conditions (other predicates). +func HasRoleWith(preds ...predicate.Role) predicate.UserRole { + return predicate.UserRole(func(s *sql.Selector) { + step := newRoleStep() + 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.UserRole) predicate.UserRole { + return predicate.UserRole(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.UserRole) predicate.UserRole { + return predicate.UserRole(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.UserRole) predicate.UserRole { + return predicate.UserRole(sql.NotPredicates(p)) +} diff --git a/internal/ent/userrole_create.go b/internal/ent/userrole_create.go new file mode 100644 index 0000000..5670f74 --- /dev/null +++ b/internal/ent/userrole_create.go @@ -0,0 +1,240 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserRoleCreate is the builder for creating a UserRole entity. +type UserRoleCreate struct { + config + mutation *UserRoleMutation + hooks []Hook +} + +// SetUserID sets the "user_id" field. +func (_c *UserRoleCreate) SetUserID(v string) *UserRoleCreate { + _c.mutation.SetUserID(v) + return _c +} + +// SetRoleID sets the "role_id" field. +func (_c *UserRoleCreate) SetRoleID(v string) *UserRoleCreate { + _c.mutation.SetRoleID(v) + return _c +} + +// SetUser sets the "user" edge to the User entity. +func (_c *UserRoleCreate) SetUser(v *User) *UserRoleCreate { + return _c.SetUserID(v.ID) +} + +// SetRole sets the "role" edge to the Role entity. +func (_c *UserRoleCreate) SetRole(v *Role) *UserRoleCreate { + return _c.SetRoleID(v.ID) +} + +// Mutation returns the UserRoleMutation object of the builder. +func (_c *UserRoleCreate) Mutation() *UserRoleMutation { + return _c.mutation +} + +// Save creates the UserRole in the database. +func (_c *UserRoleCreate) Save(ctx context.Context) (*UserRole, error) { + return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (_c *UserRoleCreate) SaveX(ctx context.Context) *UserRole { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *UserRoleCreate) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *UserRoleCreate) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_c *UserRoleCreate) check() error { + if _, ok := _c.mutation.UserID(); !ok { + return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "UserRole.user_id"`)} + } + if _, ok := _c.mutation.RoleID(); !ok { + return &ValidationError{Name: "role_id", err: errors.New(`ent: missing required field "UserRole.role_id"`)} + } + if len(_c.mutation.UserIDs()) == 0 { + return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "UserRole.user"`)} + } + if len(_c.mutation.RoleIDs()) == 0 { + return &ValidationError{Name: "role", err: errors.New(`ent: missing required edge "UserRole.role"`)} + } + return nil +} + +func (_c *UserRoleCreate) sqlSave(ctx context.Context) (*UserRole, 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 + } + id := _spec.ID.Value.(int64) + _node.ID = int(id) + _c.mutation.id = &_node.ID + _c.mutation.done = true + return _node, nil +} + +func (_c *UserRoleCreate) createSpec() (*UserRole, *sqlgraph.CreateSpec) { + var ( + _node = &UserRole{config: _c.config} + _spec = sqlgraph.NewCreateSpec(userrole.Table, sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt)) + ) + if nodes := _c.mutation.UserIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.UserTable, + Columns: []string{userrole.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.UserID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + if nodes := _c.mutation.RoleIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.RoleTable, + Columns: []string{userrole.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.RoleID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + return _node, _spec +} + +// UserRoleCreateBulk is the builder for creating many UserRole entities in bulk. +type UserRoleCreateBulk struct { + config + err error + builders []*UserRoleCreate +} + +// Save creates the UserRole entities in the database. +func (_c *UserRoleCreateBulk) Save(ctx context.Context) ([]*UserRole, error) { + if _c.err != nil { + return nil, _c.err + } + specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) + nodes := make([]*UserRole, len(_c.builders)) + mutators := make([]Mutator, len(_c.builders)) + for i := range _c.builders { + func(i int, root context.Context) { + builder := _c.builders[i] + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*UserRoleMutation) + 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 + if specs[i].ID.Value != nil { + id := specs[i].ID.Value.(int64) + nodes[i].ID = int(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 *UserRoleCreateBulk) SaveX(ctx context.Context) []*UserRole { + v, err := _c.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (_c *UserRoleCreateBulk) Exec(ctx context.Context) error { + _, err := _c.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_c *UserRoleCreateBulk) ExecX(ctx context.Context) { + if err := _c.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/userrole_delete.go b/internal/ent/userrole_delete.go new file mode 100644 index 0000000..f2828af --- /dev/null +++ b/internal/ent/userrole_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" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserRoleDelete is the builder for deleting a UserRole entity. +type UserRoleDelete struct { + config + hooks []Hook + mutation *UserRoleMutation +} + +// Where appends a list predicates to the UserRoleDelete builder. +func (_d *UserRoleDelete) Where(ps ...predicate.UserRole) *UserRoleDelete { + _d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (_d *UserRoleDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *UserRoleDelete) ExecX(ctx context.Context) int { + n, err := _d.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (_d *UserRoleDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(userrole.Table, sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt)) + if ps := _d.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + _d.mutation.done = true + return affected, err +} + +// UserRoleDeleteOne is the builder for deleting a single UserRole entity. +type UserRoleDeleteOne struct { + _d *UserRoleDelete +} + +// Where appends a list predicates to the UserRoleDelete builder. +func (_d *UserRoleDeleteOne) Where(ps ...predicate.UserRole) *UserRoleDeleteOne { + _d._d.mutation.Where(ps...) + return _d +} + +// Exec executes the deletion query. +func (_d *UserRoleDeleteOne) Exec(ctx context.Context) error { + n, err := _d._d.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{userrole.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (_d *UserRoleDeleteOne) ExecX(ctx context.Context) { + if err := _d.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/ent/userrole_query.go b/internal/ent/userrole_query.go new file mode 100644 index 0000000..0327d3a --- /dev/null +++ b/internal/ent/userrole_query.go @@ -0,0 +1,686 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + "math" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserRoleQuery is the builder for querying UserRole entities. +type UserRoleQuery struct { + config + ctx *QueryContext + order []userrole.OrderOption + inters []Interceptor + predicates []predicate.UserRole + withUser *UserQuery + withRole *RoleQuery + withFKs bool + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the UserRoleQuery builder. +func (_q *UserRoleQuery) Where(ps ...predicate.UserRole) *UserRoleQuery { + _q.predicates = append(_q.predicates, ps...) + return _q +} + +// Limit the number of records to be returned by this query. +func (_q *UserRoleQuery) Limit(limit int) *UserRoleQuery { + _q.ctx.Limit = &limit + return _q +} + +// Offset to start from. +func (_q *UserRoleQuery) Offset(offset int) *UserRoleQuery { + _q.ctx.Offset = &offset + return _q +} + +// 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 (_q *UserRoleQuery) Unique(unique bool) *UserRoleQuery { + _q.ctx.Unique = &unique + return _q +} + +// Order specifies how the records should be ordered. +func (_q *UserRoleQuery) Order(o ...userrole.OrderOption) *UserRoleQuery { + _q.order = append(_q.order, o...) + return _q +} + +// QueryUser chains the current query on the "user" edge. +func (_q *UserRoleQuery) QueryUser() *UserQuery { + query := (&UserClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(userrole.Table, userrole.FieldID, selector), + sqlgraph.To(user.Table, user.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, userrole.UserTable, userrole.UserColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// QueryRole chains the current query on the "role" edge. +func (_q *UserRoleQuery) QueryRole() *RoleQuery { + query := (&RoleClient{config: _q.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + selector := _q.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(userrole.Table, userrole.FieldID, selector), + sqlgraph.To(role.Table, role.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, userrole.RoleTable, userrole.RoleColumn), + ) + fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// First returns the first UserRole entity from the query. +// Returns a *NotFoundError when no UserRole was found. +func (_q *UserRoleQuery) First(ctx context.Context) (*UserRole, error) { + nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{userrole.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (_q *UserRoleQuery) FirstX(ctx context.Context) *UserRole { + node, err := _q.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first UserRole ID from the query. +// Returns a *NotFoundError when no UserRole ID was found. +func (_q *UserRoleQuery) FirstID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{userrole.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (_q *UserRoleQuery) FirstIDX(ctx context.Context) int { + id, err := _q.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single UserRole entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one UserRole entity is found. +// Returns a *NotFoundError when no UserRole entities are found. +func (_q *UserRoleQuery) Only(ctx context.Context) (*UserRole, error) { + nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{userrole.Label} + default: + return nil, &NotSingularError{userrole.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (_q *UserRoleQuery) OnlyX(ctx context.Context) *UserRole { + node, err := _q.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only UserRole ID in the query. +// Returns a *NotSingularError when more than one UserRole ID is found. +// Returns a *NotFoundError when no entities are found. +func (_q *UserRoleQuery) OnlyID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{userrole.Label} + default: + err = &NotSingularError{userrole.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (_q *UserRoleQuery) OnlyIDX(ctx context.Context) int { + id, err := _q.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of UserRoles. +func (_q *UserRoleQuery) All(ctx context.Context) ([]*UserRole, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll) + if err := _q.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*UserRole, *UserRoleQuery]() + return withInterceptors[[]*UserRole](ctx, _q, qr, _q.inters) +} + +// AllX is like All, but panics if an error occurs. +func (_q *UserRoleQuery) AllX(ctx context.Context) []*UserRole { + nodes, err := _q.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of UserRole IDs. +func (_q *UserRoleQuery) IDs(ctx context.Context) (ids []int, err error) { + if _q.ctx.Unique == nil && _q.path != nil { + _q.Unique(true) + } + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs) + if err = _q.Select(userrole.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (_q *UserRoleQuery) IDsX(ctx context.Context) []int { + ids, err := _q.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (_q *UserRoleQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount) + if err := _q.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, _q, querierCount[*UserRoleQuery](), _q.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (_q *UserRoleQuery) CountX(ctx context.Context) int { + count, err := _q.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (_q *UserRoleQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist) + switch _, err := _q.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 (_q *UserRoleQuery) ExistX(ctx context.Context) bool { + exist, err := _q.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the UserRoleQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (_q *UserRoleQuery) Clone() *UserRoleQuery { + if _q == nil { + return nil + } + return &UserRoleQuery{ + config: _q.config, + ctx: _q.ctx.Clone(), + order: append([]userrole.OrderOption{}, _q.order...), + inters: append([]Interceptor{}, _q.inters...), + predicates: append([]predicate.UserRole{}, _q.predicates...), + withUser: _q.withUser.Clone(), + withRole: _q.withRole.Clone(), + // clone intermediate query. + sql: _q.sql.Clone(), + path: _q.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 (_q *UserRoleQuery) WithUser(opts ...func(*UserQuery)) *UserRoleQuery { + query := (&UserClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withUser = query + return _q +} + +// WithRole tells the query-builder to eager-load the nodes that are connected to +// the "role" edge. The optional arguments are used to configure the query builder of the edge. +func (_q *UserRoleQuery) WithRole(opts ...func(*RoleQuery)) *UserRoleQuery { + query := (&RoleClient{config: _q.config}).Query() + for _, opt := range opts { + opt(query) + } + _q.withRole = query + return _q +} + +// 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 { +// UserID string `json:"user_id,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.UserRole.Query(). +// GroupBy(userrole.FieldUserID). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (_q *UserRoleQuery) GroupBy(field string, fields ...string) *UserRoleGroupBy { + _q.ctx.Fields = append([]string{field}, fields...) + grbuild := &UserRoleGroupBy{build: _q} + grbuild.flds = &_q.ctx.Fields + grbuild.label = userrole.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 { +// UserID string `json:"user_id,omitempty"` +// } +// +// client.UserRole.Query(). +// Select(userrole.FieldUserID). +// Scan(ctx, &v) +func (_q *UserRoleQuery) Select(fields ...string) *UserRoleSelect { + _q.ctx.Fields = append(_q.ctx.Fields, fields...) + sbuild := &UserRoleSelect{UserRoleQuery: _q} + sbuild.label = userrole.Label + sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a UserRoleSelect configured with the given aggregations. +func (_q *UserRoleQuery) Aggregate(fns ...AggregateFunc) *UserRoleSelect { + return _q.Select().Aggregate(fns...) +} + +func (_q *UserRoleQuery) prepareQuery(ctx context.Context) error { + for _, inter := range _q.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, _q); err != nil { + return err + } + } + } + for _, f := range _q.ctx.Fields { + if !userrole.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if _q.path != nil { + prev, err := _q.path(ctx) + if err != nil { + return err + } + _q.sql = prev + } + return nil +} + +func (_q *UserRoleQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*UserRole, error) { + var ( + nodes = []*UserRole{} + withFKs = _q.withFKs + _spec = _q.querySpec() + loadedTypes = [2]bool{ + _q.withUser != nil, + _q.withRole != nil, + } + ) + if withFKs { + _spec.Node.Columns = append(_spec.Node.Columns, userrole.ForeignKeys...) + } + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*UserRole).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &UserRole{config: _q.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, _q.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := _q.withUser; query != nil { + if err := _q.loadUser(ctx, query, nodes, nil, + func(n *UserRole, e *User) { n.Edges.User = e }); err != nil { + return nil, err + } + } + if query := _q.withRole; query != nil { + if err := _q.loadRole(ctx, query, nodes, nil, + func(n *UserRole, e *Role) { n.Edges.Role = e }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (_q *UserRoleQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*UserRole, init func(*UserRole), assign func(*UserRole, *User)) error { + ids := make([]string, 0, len(nodes)) + nodeids := make(map[string][]*UserRole) + 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 (_q *UserRoleQuery) loadRole(ctx context.Context, query *RoleQuery, nodes []*UserRole, init func(*UserRole), assign func(*UserRole, *Role)) error { + ids := make([]string, 0, len(nodes)) + nodeids := make(map[string][]*UserRole) + for i := range nodes { + fk := nodes[i].RoleID + if _, ok := nodeids[fk]; !ok { + ids = append(ids, fk) + } + nodeids[fk] = append(nodeids[fk], nodes[i]) + } + if len(ids) == 0 { + return nil + } + query.Where(role.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 "role_id" returned %v`, n.ID) + } + for i := range nodes { + assign(nodes[i], n) + } + } + return nil +} + +func (_q *UserRoleQuery) sqlCount(ctx context.Context) (int, error) { + _spec := _q.querySpec() + _spec.Node.Columns = _q.ctx.Fields + if len(_q.ctx.Fields) > 0 { + _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique + } + return sqlgraph.CountNodes(ctx, _q.driver, _spec) +} + +func (_q *UserRoleQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(userrole.Table, userrole.Columns, sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt)) + _spec.From = _q.sql + if unique := _q.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if _q.path != nil { + _spec.Unique = true + } + if fields := _q.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, userrole.FieldID) + for i := range fields { + if fields[i] != userrole.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + if _q.withUser != nil { + _spec.Node.AddColumnOnce(userrole.FieldUserID) + } + if _q.withRole != nil { + _spec.Node.AddColumnOnce(userrole.FieldRoleID) + } + } + if ps := _q.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := _q.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := _q.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := _q.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (_q *UserRoleQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(_q.driver.Dialect()) + t1 := builder.Table(userrole.Table) + columns := _q.ctx.Fields + if len(columns) == 0 { + columns = userrole.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if _q.sql != nil { + selector = _q.sql + selector.Select(selector.Columns(columns...)...) + } + if _q.ctx.Unique != nil && *_q.ctx.Unique { + selector.Distinct() + } + for _, p := range _q.predicates { + p(selector) + } + for _, p := range _q.order { + p(selector) + } + if offset := _q.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 := _q.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// UserRoleGroupBy is the group-by builder for UserRole entities. +type UserRoleGroupBy struct { + selector + build *UserRoleQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (_g *UserRoleGroupBy) Aggregate(fns ...AggregateFunc) *UserRoleGroupBy { + _g.fns = append(_g.fns, fns...) + return _g +} + +// Scan applies the selector query and scans the result into the given value. +func (_g *UserRoleGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*UserRoleQuery, *UserRoleGroupBy](ctx, _g.build, _g, _g.build.inters, v) +} + +func (_g *UserRoleGroupBy) sqlScan(ctx context.Context, root *UserRoleQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*_g.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// UserRoleSelect is the builder for selecting fields of UserRole entities. +type UserRoleSelect struct { + *UserRoleQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (_s *UserRoleSelect) Aggregate(fns ...AggregateFunc) *UserRoleSelect { + _s.fns = append(_s.fns, fns...) + return _s +} + +// Scan applies the selector query and scans the result into the given value. +func (_s *UserRoleSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*UserRoleQuery, *UserRoleSelect](ctx, _s.UserRoleQuery, _s, _s.inters, v) +} + +func (_s *UserRoleSelect) sqlScan(ctx context.Context, root *UserRoleQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/ent/userrole_update.go b/internal/ent/userrole_update.go new file mode 100644 index 0000000..ed4bf8f --- /dev/null +++ b/internal/ent/userrole_update.go @@ -0,0 +1,421 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "git.dcentral.systems/toolz/goplt/internal/ent/predicate" + "git.dcentral.systems/toolz/goplt/internal/ent/role" + "git.dcentral.systems/toolz/goplt/internal/ent/user" + "git.dcentral.systems/toolz/goplt/internal/ent/userrole" +) + +// UserRoleUpdate is the builder for updating UserRole entities. +type UserRoleUpdate struct { + config + hooks []Hook + mutation *UserRoleMutation +} + +// Where appends a list predicates to the UserRoleUpdate builder. +func (_u *UserRoleUpdate) Where(ps ...predicate.UserRole) *UserRoleUpdate { + _u.mutation.Where(ps...) + return _u +} + +// SetUserID sets the "user_id" field. +func (_u *UserRoleUpdate) SetUserID(v string) *UserRoleUpdate { + _u.mutation.SetUserID(v) + return _u +} + +// SetNillableUserID sets the "user_id" field if the given value is not nil. +func (_u *UserRoleUpdate) SetNillableUserID(v *string) *UserRoleUpdate { + if v != nil { + _u.SetUserID(*v) + } + return _u +} + +// SetRoleID sets the "role_id" field. +func (_u *UserRoleUpdate) SetRoleID(v string) *UserRoleUpdate { + _u.mutation.SetRoleID(v) + return _u +} + +// SetNillableRoleID sets the "role_id" field if the given value is not nil. +func (_u *UserRoleUpdate) SetNillableRoleID(v *string) *UserRoleUpdate { + if v != nil { + _u.SetRoleID(*v) + } + return _u +} + +// SetUser sets the "user" edge to the User entity. +func (_u *UserRoleUpdate) SetUser(v *User) *UserRoleUpdate { + return _u.SetUserID(v.ID) +} + +// SetRole sets the "role" edge to the Role entity. +func (_u *UserRoleUpdate) SetRole(v *Role) *UserRoleUpdate { + return _u.SetRoleID(v.ID) +} + +// Mutation returns the UserRoleMutation object of the builder. +func (_u *UserRoleUpdate) Mutation() *UserRoleMutation { + return _u.mutation +} + +// ClearUser clears the "user" edge to the User entity. +func (_u *UserRoleUpdate) ClearUser() *UserRoleUpdate { + _u.mutation.ClearUser() + return _u +} + +// ClearRole clears the "role" edge to the Role entity. +func (_u *UserRoleUpdate) ClearRole() *UserRoleUpdate { + _u.mutation.ClearRole() + return _u +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (_u *UserRoleUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *UserRoleUpdate) SaveX(ctx context.Context) int { + affected, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (_u *UserRoleUpdate) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *UserRoleUpdate) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *UserRoleUpdate) check() error { + if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "UserRole.user"`) + } + if _u.mutation.RoleCleared() && len(_u.mutation.RoleIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "UserRole.role"`) + } + return nil +} + +func (_u *UserRoleUpdate) sqlSave(ctx context.Context) (_node int, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(userrole.Table, userrole.Columns, sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt)) + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if _u.mutation.UserCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.UserTable, + Columns: []string{userrole.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.UserIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.UserTable, + Columns: []string{userrole.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _u.mutation.RoleCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.RoleTable, + Columns: []string{userrole.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RoleIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.RoleTable, + Columns: []string{userrole.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{userrole.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + _u.mutation.done = true + return _node, nil +} + +// UserRoleUpdateOne is the builder for updating a single UserRole entity. +type UserRoleUpdateOne struct { + config + fields []string + hooks []Hook + mutation *UserRoleMutation +} + +// SetUserID sets the "user_id" field. +func (_u *UserRoleUpdateOne) SetUserID(v string) *UserRoleUpdateOne { + _u.mutation.SetUserID(v) + return _u +} + +// SetNillableUserID sets the "user_id" field if the given value is not nil. +func (_u *UserRoleUpdateOne) SetNillableUserID(v *string) *UserRoleUpdateOne { + if v != nil { + _u.SetUserID(*v) + } + return _u +} + +// SetRoleID sets the "role_id" field. +func (_u *UserRoleUpdateOne) SetRoleID(v string) *UserRoleUpdateOne { + _u.mutation.SetRoleID(v) + return _u +} + +// SetNillableRoleID sets the "role_id" field if the given value is not nil. +func (_u *UserRoleUpdateOne) SetNillableRoleID(v *string) *UserRoleUpdateOne { + if v != nil { + _u.SetRoleID(*v) + } + return _u +} + +// SetUser sets the "user" edge to the User entity. +func (_u *UserRoleUpdateOne) SetUser(v *User) *UserRoleUpdateOne { + return _u.SetUserID(v.ID) +} + +// SetRole sets the "role" edge to the Role entity. +func (_u *UserRoleUpdateOne) SetRole(v *Role) *UserRoleUpdateOne { + return _u.SetRoleID(v.ID) +} + +// Mutation returns the UserRoleMutation object of the builder. +func (_u *UserRoleUpdateOne) Mutation() *UserRoleMutation { + return _u.mutation +} + +// ClearUser clears the "user" edge to the User entity. +func (_u *UserRoleUpdateOne) ClearUser() *UserRoleUpdateOne { + _u.mutation.ClearUser() + return _u +} + +// ClearRole clears the "role" edge to the Role entity. +func (_u *UserRoleUpdateOne) ClearRole() *UserRoleUpdateOne { + _u.mutation.ClearRole() + return _u +} + +// Where appends a list predicates to the UserRoleUpdate builder. +func (_u *UserRoleUpdateOne) Where(ps ...predicate.UserRole) *UserRoleUpdateOne { + _u.mutation.Where(ps...) + return _u +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (_u *UserRoleUpdateOne) Select(field string, fields ...string) *UserRoleUpdateOne { + _u.fields = append([]string{field}, fields...) + return _u +} + +// Save executes the query and returns the updated UserRole entity. +func (_u *UserRoleUpdateOne) Save(ctx context.Context) (*UserRole, error) { + return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (_u *UserRoleUpdateOne) SaveX(ctx context.Context) *UserRole { + node, err := _u.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (_u *UserRoleUpdateOne) Exec(ctx context.Context) error { + _, err := _u.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (_u *UserRoleUpdateOne) ExecX(ctx context.Context) { + if err := _u.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (_u *UserRoleUpdateOne) check() error { + if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "UserRole.user"`) + } + if _u.mutation.RoleCleared() && len(_u.mutation.RoleIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "UserRole.role"`) + } + return nil +} + +func (_u *UserRoleUpdateOne) sqlSave(ctx context.Context) (_node *UserRole, err error) { + if err := _u.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(userrole.Table, userrole.Columns, sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt)) + id, ok := _u.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserRole.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := _u.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, userrole.FieldID) + for _, f := range fields { + if !userrole.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != userrole.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := _u.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if _u.mutation.UserCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.UserTable, + Columns: []string{userrole.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.UserIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.UserTable, + Columns: []string{userrole.UserColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if _u.mutation.RoleCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.RoleTable, + Columns: []string{userrole.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := _u.mutation.RoleIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: userrole.RoleTable, + Columns: []string{userrole.RoleColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &UserRole{config: _u.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{userrole.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + _u.mutation.done = true + return _node, nil +} diff --git a/internal/errorbus/channel_bus.go b/internal/errorbus/channel_bus.go new file mode 100644 index 0000000..5d1f32e --- /dev/null +++ b/internal/errorbus/channel_bus.go @@ -0,0 +1,165 @@ +package errorbus + +import ( + "context" + "runtime" + "sync" + + "git.dcentral.systems/toolz/goplt/pkg/errorbus" + "git.dcentral.systems/toolz/goplt/pkg/logger" +) + +// ChannelBus implements a channel-based error bus. +type ChannelBus struct { + errors chan errorWithContext + logger logger.Logger + done chan struct{} + wg sync.WaitGroup + once sync.Once +} + +type errorWithContext struct { + err error + ctx context.Context + stack []byte +} + +// NewChannelBus creates a new channel-based error bus. +func NewChannelBus(log logger.Logger, bufferSize int) *ChannelBus { + if bufferSize <= 0 { + bufferSize = 100 + } + + bus := &ChannelBus{ + errors: make(chan errorWithContext, bufferSize), + logger: log, + done: make(chan struct{}), + } + + // Start background consumer + bus.wg.Add(1) + go bus.consume() + + return bus +} + +// Publish publishes an error to the error bus. +func (b *ChannelBus) Publish(ctx context.Context, err error) { + if err == nil { + return + } + + // Capture stack trace + stack := make([]byte, 4096) + n := runtime.Stack(stack, false) + stack = stack[:n] + + select { + case b.errors <- errorWithContext{ + err: err, + ctx: ctx, + stack: stack, + }: + // Successfully queued + default: + // Channel is full, log directly to avoid blocking + b.logger.Error("Error bus channel full, logging directly", + logger.String("error", err.Error()), + ) + } +} + +// consume consumes errors from the channel and logs them. +func (b *ChannelBus) consume() { + defer b.wg.Done() + + for { + select { + case errCtx := <-b.errors: + b.handleError(errCtx) + case <-b.done: + return + } + } +} + +// handleError handles a single error by logging it with context. +func (b *ChannelBus) handleError(errCtx errorWithContext) { + fields := []logger.Field{ + logger.String("error", errCtx.err.Error()), + } + + // Extract request ID from context + if requestID := extractRequestID(errCtx.ctx); requestID != "" { + fields = append(fields, logger.String("request_id", requestID)) + } + + // Extract user ID from context + if userID := extractUserID(errCtx.ctx); userID != "" { + fields = append(fields, logger.String("user_id", userID)) + } + + // Add stack trace for debugging + if len(errCtx.stack) > 0 { + fields = append(fields, logger.String("stack", string(errCtx.stack))) + } + + b.logger.Error("Error captured by error bus", fields...) + + // TODO: In Epic 6, add Sentry integration here + // if b.sentryClient != nil { + // b.sentryClient.CaptureException(errCtx.err, ...) + // } +} + +// extractRequestID extracts request ID from context. +func extractRequestID(ctx context.Context) string { + if ctx == nil { + return "" + } + // Try common context key patterns + if val := ctx.Value("request_id"); val != nil { + if str, ok := val.(string); ok { + return str + } + } + if val := ctx.Value("RequestID"); val != nil { + if str, ok := val.(string); ok { + return str + } + } + return "" +} + +// extractUserID extracts user ID from context. +func extractUserID(ctx context.Context) string { + if ctx == nil { + return "" + } + // Try common context key patterns + if val := ctx.Value("user_id"); val != nil { + if str, ok := val.(string); ok { + return str + } + } + if val := ctx.Value("UserID"); val != nil { + if str, ok := val.(string); ok { + return str + } + } + return "" +} + +// Close closes the error bus and waits for all errors to be processed. +func (b *ChannelBus) Close() error { + b.once.Do(func() { + close(b.done) + }) + b.wg.Wait() + close(b.errors) + return nil +} + +// Ensure ChannelBus implements ErrorPublisher +var _ errorbus.ErrorPublisher = (*ChannelBus)(nil) + diff --git a/internal/health/database.go b/internal/health/database.go new file mode 100644 index 0000000..e9bf674 --- /dev/null +++ b/internal/health/database.go @@ -0,0 +1,26 @@ +package health + +import ( + "context" + + "git.dcentral.systems/toolz/goplt/internal/infra/database" + "git.dcentral.systems/toolz/goplt/pkg/health" +) + +// DatabaseChecker implements health checks for the database. +type DatabaseChecker struct { + client *database.Client +} + +// NewDatabaseChecker creates a new database health checker. +func NewDatabaseChecker(client *database.Client) health.HealthChecker { + return &DatabaseChecker{ + client: client, + } +} + +// Check performs a database health check. +func (d *DatabaseChecker) Check(ctx context.Context) error { + return d.client.Ping(ctx) +} + diff --git a/internal/health/registry.go b/internal/health/registry.go new file mode 100644 index 0000000..8ffdb24 --- /dev/null +++ b/internal/health/registry.go @@ -0,0 +1,74 @@ +package health + +import ( + "context" + "sync" + + "git.dcentral.systems/toolz/goplt/pkg/health" +) + +// Registry manages health checkers. +type Registry struct { + checkers map[string]health.HealthChecker + mu sync.RWMutex +} + +// NewRegistry creates a new health check registry. +func NewRegistry() *Registry { + return &Registry{ + checkers: make(map[string]health.HealthChecker), + } +} + +// Register registers a health checker with the given name. +func (r *Registry) Register(name string, checker health.HealthChecker) { + r.mu.Lock() + defer r.mu.Unlock() + r.checkers[name] = checker +} + +// Check performs health checks for all registered checkers. +func (r *Registry) Check(ctx context.Context) health.HealthStatus { + r.mu.RLock() + defer r.mu.RUnlock() + + components := make([]health.ComponentStatus, 0, len(r.checkers)) + overallStatus := health.StatusHealthy + + for name, checker := range r.checkers { + err := checker.Check(ctx) + status := health.StatusHealthy + errorMsg := "" + + if err != nil { + status = health.StatusUnhealthy + errorMsg = err.Error() + overallStatus = health.StatusUnhealthy + } + + components = append(components, health.ComponentStatus{ + Name: name, + Status: status, + Error: errorMsg, + }) + } + + return health.HealthStatus{ + Status: overallStatus, + Components: components, + } +} + +// LivenessCheck performs a basic liveness check (no dependencies). +func (r *Registry) LivenessCheck(ctx context.Context) health.HealthStatus { + // Liveness is always healthy if the service is running + return health.HealthStatus{ + Status: health.StatusHealthy, + } +} + +// ReadinessCheck performs a readiness check (includes dependency checks). +func (r *Registry) ReadinessCheck(ctx context.Context) health.HealthStatus { + return r.Check(ctx) +} + diff --git a/internal/infra/database/client.go b/internal/infra/database/client.go new file mode 100644 index 0000000..fa68855 --- /dev/null +++ b/internal/infra/database/client.go @@ -0,0 +1,87 @@ +package database + +import ( + "context" + "database/sql" + "fmt" + "time" + + "entgo.io/ent/dialect" + entsql "entgo.io/ent/dialect/sql" + "git.dcentral.systems/toolz/goplt/internal/ent" + _ "github.com/lib/pq" // PostgreSQL driver +) + +// Client wraps the Ent client with additional functionality. +type Client struct { + *ent.Client + db *sql.DB +} + +// Config holds database configuration. +type Config struct { + DSN string + MaxConnections int + MaxIdleConns int + ConnMaxLifetime time.Duration + ConnMaxIdleTime time.Duration +} + +// NewClient creates a new Ent client with connection pooling. +func NewClient(cfg Config) (*Client, error) { + // Open database connection + db, err := sql.Open("postgres", cfg.DSN) + if err != nil { + return nil, fmt.Errorf("failed to open database connection: %w", err) + } + + // Configure connection pool + db.SetMaxOpenConns(cfg.MaxConnections) + db.SetMaxIdleConns(cfg.MaxIdleConns) + db.SetConnMaxLifetime(cfg.ConnMaxLifetime) + db.SetConnMaxIdleTime(cfg.ConnMaxIdleTime) + + // Test connection + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := db.PingContext(ctx); err != nil { + db.Close() + return nil, fmt.Errorf("failed to ping database: %w", err) + } + + // Create Ent driver + drv := entsql.OpenDB(dialect.Postgres, db) + + // Create Ent client + entClient := ent.NewClient(ent.Driver(drv)) + + return &Client{ + Client: entClient, + db: db, + }, nil +} + +// Close closes the database connection. +func (c *Client) Close() error { + if err := c.Client.Close(); err != nil { + return err + } + return c.db.Close() +} + +// Migrate runs database migrations. +func (c *Client) Migrate(ctx context.Context) error { + return c.Client.Schema.Create(ctx) +} + +// Ping checks database connectivity. +func (c *Client) Ping(ctx context.Context) error { + return c.db.PingContext(ctx) +} + +// DB returns the underlying *sql.DB for advanced operations. +func (c *Client) DB() *sql.DB { + return c.db +} + diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go new file mode 100644 index 0000000..4f20fe9 --- /dev/null +++ b/internal/metrics/metrics.go @@ -0,0 +1,97 @@ +package metrics + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// Metrics holds all Prometheus metrics. +type Metrics struct { + httpRequestDuration *prometheus.HistogramVec + httpRequestTotal *prometheus.CounterVec + httpErrorsTotal *prometheus.CounterVec + registry *prometheus.Registry +} + +// NewMetrics creates a new metrics registry with all metrics. +func NewMetrics() *Metrics { + registry := prometheus.NewRegistry() + + m := &Metrics{ + registry: registry, + httpRequestDuration: prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Name: "http_request_duration_seconds", + Help: "HTTP request duration in seconds", + Buckets: prometheus.DefBuckets, + }, + []string{"method", "path", "status"}, + ), + httpRequestTotal: prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "http_requests_total", + Help: "Total number of HTTP requests", + }, + []string{"method", "path", "status"}, + ), + httpErrorsTotal: prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "http_errors_total", + Help: "Total number of HTTP errors", + }, + []string{"method", "path", "status"}, + ), + } + + // Register all metrics + registry.MustRegister(m.httpRequestDuration) + registry.MustRegister(m.httpRequestTotal) + registry.MustRegister(m.httpErrorsTotal) + + return m +} + +// HTTPMiddleware returns a Gin middleware for collecting HTTP metrics. +func (m *Metrics) HTTPMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + + // Process request + c.Next() + + // Calculate duration + duration := time.Since(start).Seconds() + + // Get request details + method := c.Request.Method + path := c.FullPath() + if path == "" { + path = c.Request.URL.Path + } + status := c.Writer.Status() + + // Record metrics + m.httpRequestDuration.WithLabelValues(method, path, http.StatusText(status)).Observe(duration) + m.httpRequestTotal.WithLabelValues(method, path, http.StatusText(status)).Inc() + + // Record errors (4xx and 5xx) + if status >= 400 { + m.httpErrorsTotal.WithLabelValues(method, path, http.StatusText(status)).Inc() + } + } +} + +// Handler returns an HTTP handler for the /metrics endpoint. +func (m *Metrics) Handler() http.Handler { + return promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{}) +} + +// Registry returns the Prometheus registry. +func (m *Metrics) Registry() *prometheus.Registry { + return m.registry +} + diff --git a/internal/server/middleware.go b/internal/server/middleware.go new file mode 100644 index 0000000..0aecdc8 --- /dev/null +++ b/internal/server/middleware.go @@ -0,0 +1,141 @@ +package server + +import ( + "context" + "net/http" + "runtime" + "time" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "git.dcentral.systems/toolz/goplt/pkg/errorbus" + "git.dcentral.systems/toolz/goplt/pkg/logger" +) + +const ( + requestIDKey = "request_id" + userIDKey = "user_id" +) + +// RequestIDMiddleware generates a unique request ID for each request. +func RequestIDMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + requestID := c.GetHeader("X-Request-ID") + if requestID == "" { + requestID = uuid.New().String() + } + + c.Set(requestIDKey, requestID) + c.Header("X-Request-ID", requestID) + c.Next() + } +} + +// LoggingMiddleware logs all HTTP requests with structured logging. +func LoggingMiddleware(log logger.Logger) gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + path := c.Request.URL.Path + method := c.Request.Method + + // Process request + c.Next() + + // Calculate duration + duration := time.Since(start) + + // Get request ID from context + requestID, _ := c.Get(requestIDKey) + requestIDStr := "" + if id, ok := requestID.(string); ok { + requestIDStr = id + } + + // Log request + log.Info("HTTP request", + logger.String("method", method), + logger.String("path", path), + logger.Int("status", c.Writer.Status()), + logger.Any("duration_ms", duration.Milliseconds()), + logger.String("request_id", requestIDStr), + logger.String("ip", c.ClientIP()), + ) + } +} + +// PanicRecoveryMiddleware recovers from panics and publishes them to the error bus. +func PanicRecoveryMiddleware(errorBus errorbus.ErrorPublisher) gin.HandlerFunc { + return func(c *gin.Context) { + defer func() { + if err := recover(); err != nil { + // Capture stack trace + stack := make([]byte, 4096) + n := runtime.Stack(stack, false) + stack = stack[:n] + + // Get request ID from context + requestID, _ := c.Get(requestIDKey) + ctx := context.WithValue(context.Background(), "request_id", requestID) + + // Create error + var panicErr error + if e, ok := err.(error); ok { + panicErr = e + } else { + panicErr = &panicError{value: err, stack: stack} + } + + // Publish to error bus + errorBus.Publish(ctx, panicErr) + + // Return error response + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Internal server error", + }) + + c.Abort() + } + }() + + c.Next() + } +} + +// panicError wraps a panic value as an error. +type panicError struct { + value interface{} + stack []byte +} + +func (e *panicError) Error() string { + return "panic recovered" +} + +// CORSMiddleware provides CORS support. +func CORSMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") + c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE, PATCH") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(http.StatusNoContent) + return + } + + c.Next() + } +} + +// TimeoutMiddleware sets a request timeout. +func TimeoutMiddleware(timeout time.Duration) gin.HandlerFunc { + return func(c *gin.Context) { + ctx, cancel := context.WithTimeout(c.Request.Context(), timeout) + defer cancel() + + c.Request = c.Request.WithContext(ctx) + c.Next() + } +} + diff --git a/internal/server/server.go b/internal/server/server.go new file mode 100644 index 0000000..9df7f2a --- /dev/null +++ b/internal/server/server.go @@ -0,0 +1,131 @@ +package server + +import ( + "context" + "fmt" + "net/http" + "time" + + "github.com/gin-gonic/gin" + "git.dcentral.systems/toolz/goplt/internal/health" + "git.dcentral.systems/toolz/goplt/internal/metrics" + "git.dcentral.systems/toolz/goplt/pkg/config" + "git.dcentral.systems/toolz/goplt/pkg/errorbus" + "git.dcentral.systems/toolz/goplt/pkg/logger" +) + +// Server wraps the HTTP server and Gin router. +type Server struct { + httpServer *http.Server + router *gin.Engine +} + +// NewServer creates a new HTTP server with all middleware and routes. +func NewServer( + cfg config.ConfigProvider, + log logger.Logger, + healthRegistry *health.Registry, + metricsRegistry *metrics.Metrics, + errorBus errorbus.ErrorPublisher, +) (*Server, error) { + // Set Gin mode + env := cfg.GetString("environment") + if env == "production" { + gin.SetMode(gin.ReleaseMode) + } + + router := gin.New() + + // Add middleware (order matters!) + router.Use(RequestIDMiddleware()) + router.Use(LoggingMiddleware(log)) + router.Use(PanicRecoveryMiddleware(errorBus)) + router.Use(metricsRegistry.HTTPMiddleware()) + router.Use(CORSMiddleware()) + + // Request timeout middleware (optional, can be configured per route if needed) + // router.Use(TimeoutMiddleware(timeout)) + + // Register core routes + registerRoutes(router, healthRegistry, metricsRegistry) + + // Get server configuration + port := cfg.GetInt("server.port") + if port == 0 { + port = 8080 + } + host := cfg.GetString("server.host") + if host == "" { + host = "0.0.0.0" + } + + readTimeout := cfg.GetDuration("server.read_timeout") + if readTimeout == 0 { + readTimeout = 30 * time.Second + } + + writeTimeout := cfg.GetDuration("server.write_timeout") + if writeTimeout == 0 { + writeTimeout = 30 * time.Second + } + + addr := fmt.Sprintf("%s:%d", host, port) + + httpServer := &http.Server{ + Addr: addr, + Handler: router, + ReadTimeout: readTimeout, + WriteTimeout: writeTimeout, + IdleTimeout: 120 * time.Second, + } + + return &Server{ + httpServer: httpServer, + router: router, + }, nil +} + +// registerRoutes registers all core routes. +func registerRoutes( + router *gin.Engine, + healthRegistry *health.Registry, + metricsRegistry *metrics.Metrics, +) { + // Health endpoints + router.GET("/healthz", func(c *gin.Context) { + status := healthRegistry.LivenessCheck(c.Request.Context()) + if status.Status == "healthy" { + c.JSON(http.StatusOK, status) + } else { + c.JSON(http.StatusServiceUnavailable, status) + } + }) + + router.GET("/ready", func(c *gin.Context) { + status := healthRegistry.ReadinessCheck(c.Request.Context()) + if status.Status == "healthy" { + c.JSON(http.StatusOK, status) + } else { + c.JSON(http.StatusServiceUnavailable, status) + } + }) + + // Metrics endpoint + router.GET("/metrics", gin.WrapH(metricsRegistry.Handler())) +} + +// Start starts the HTTP server. +func (s *Server) Start() error { + return s.httpServer.ListenAndServe() +} + +// Shutdown gracefully shuts down the HTTP server. +func (s *Server) Shutdown(ctx context.Context) error { + return s.httpServer.Shutdown(ctx) +} + +// Router returns the Gin router (for adding additional routes). +func (s *Server) Router() *gin.Engine { + return s.router +} + diff --git a/pkg/errorbus/errorbus.go b/pkg/errorbus/errorbus.go new file mode 100644 index 0000000..378bbf7 --- /dev/null +++ b/pkg/errorbus/errorbus.go @@ -0,0 +1,21 @@ +package errorbus + +import ( + "context" +) + +// ErrorPublisher defines the interface for publishing errors to the error bus. +type ErrorPublisher interface { + // Publish publishes an error to the error bus. + // The error will be logged and optionally reported to external services. + Publish(ctx context.Context, err error) +} + +// ErrorContext provides additional context for errors. +type ErrorContext struct { + RequestID string + UserID string + Component string + Metadata map[string]interface{} +} + diff --git a/pkg/health/health.go b/pkg/health/health.go new file mode 100644 index 0000000..cc5847b --- /dev/null +++ b/pkg/health/health.go @@ -0,0 +1,34 @@ +package health + +import "context" + +// HealthChecker defines the interface for health checks. +type HealthChecker interface { + // Check performs a health check and returns an error if unhealthy. + // Returns nil if the component is healthy. + Check(ctx context.Context) error +} + +// Status represents the health status of a component. +type Status string + +const ( + // StatusHealthy indicates the component is healthy. + StatusHealthy Status = "healthy" + // StatusUnhealthy indicates the component is unhealthy. + StatusUnhealthy Status = "unhealthy" +) + +// ComponentStatus represents the health status of a single component. +type ComponentStatus struct { + Name string `json:"name"` + Status Status `json:"status"` + Error string `json:"error,omitempty"` +} + +// HealthStatus represents the overall health status. +type HealthStatus struct { + Status Status `json:"status"` + Components []ComponentStatus `json:"components,omitempty"` +} +