From f4b753b9749bad96839ba22e765f3ab5256e131d Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Sun, 6 Apr 2025 00:09:59 +0800 Subject: [PATCH 1/7] feat(gebura): add AppCategory part 1 --- internal/biz/bizgebura/app_category.go | 25 + internal/data/gebura.go | 11 + internal/data/internal/ent/app.go | 34 +- internal/data/internal/ent/app/app.go | 64 + internal/data/internal/ent/app/where.go | 46 + internal/data/internal/ent/app_create.go | 64 + internal/data/internal/ent/app_query.go | 211 ++- internal/data/internal/ent/app_update.go | 326 ++++ internal/data/internal/ent/appappcategory.go | 161 ++ .../ent/appappcategory/appappcategory.go | 102 ++ .../data/internal/ent/appappcategory/where.go | 188 +++ .../internal/ent/appappcategory_create.go | 552 +++++++ .../internal/ent/appappcategory_delete.go | 88 ++ .../data/internal/ent/appappcategory_query.go | 682 +++++++++ .../internal/ent/appappcategory_update.go | 422 ++++++ internal/data/internal/ent/appcategory.go | 104 +- .../internal/ent/appcategory/appcategory.go | 101 +- .../data/internal/ent/appcategory/where.go | 282 ++-- .../data/internal/ent/appcategory_create.go | 304 ++-- .../data/internal/ent/appcategory_delete.go | 2 +- .../data/internal/ent/appcategory_query.go | 225 ++- .../data/internal/ent/appcategory_update.go | 478 ++++-- internal/data/internal/ent/client.go | 281 +++- internal/data/internal/ent/ent.go | 2 + internal/data/internal/ent/hook/hook.go | 12 + internal/data/internal/ent/migrate/schema.go | 50 +- internal/data/internal/ent/mutation.go | 1312 +++++++++++++---- .../data/internal/ent/predicate/predicate.go | 3 + internal/data/internal/ent/runtime.go | 4 +- internal/data/internal/ent/runtime/runtime.go | 4 +- internal/data/internal/ent/schema/app.go | 3 + .../internal/ent/schema/app_app_category.go | 40 + .../data/internal/ent/schema/app_category.go | 19 +- internal/data/internal/ent/tx.go | 3 + internal/model/modelgebura/modelgebura.go | 8 + .../service/sephirah/converter/biz_to_pb.go | 2 + .../service/sephirah/converter/generated.go | 32 +- .../service/sephirah/converter/pb_to_biz.go | 2 + internal/service/sephirah/gebura.go | 29 + internal/service/sephirah/tiphereth.go | 2 +- 40 files changed, 5572 insertions(+), 708 deletions(-) create mode 100644 internal/biz/bizgebura/app_category.go create mode 100644 internal/data/internal/ent/appappcategory.go create mode 100644 internal/data/internal/ent/appappcategory/appappcategory.go create mode 100644 internal/data/internal/ent/appappcategory/where.go create mode 100644 internal/data/internal/ent/appappcategory_create.go create mode 100644 internal/data/internal/ent/appappcategory_delete.go create mode 100644 internal/data/internal/ent/appappcategory_query.go create mode 100644 internal/data/internal/ent/appappcategory_update.go create mode 100644 internal/data/internal/ent/schema/app_app_category.go diff --git a/internal/biz/bizgebura/app_category.go b/internal/biz/bizgebura/app_category.go new file mode 100644 index 00000000..86a43223 --- /dev/null +++ b/internal/biz/bizgebura/app_category.go @@ -0,0 +1,25 @@ +package bizgebura + +import ( + "context" + "github.com/tuihub/librarian/internal/biz/bizutils" + "github.com/tuihub/librarian/internal/lib/libauth" + "github.com/tuihub/librarian/internal/model/modelgebura" + pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1" +) + +func (g *Gebura) CreateAppCategory(ctx context.Context, ac *modelgebura.AppCategory) (*modelgebura.AppCategory, error) { + claims := libauth.FromContextAssertUserType(ctx) + if claims == nil { + return nil, bizutils.NoPermissionError() + } + id, err := g.id.New() + if err != nil { + return nil, pb.ErrorErrorReasonUnspecified("%s", err) + } + ac.ID = id + if err = g.repo.CreateAppCategory(ctx, claims.UserID, ac); err != nil { + return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + return ac, nil +} diff --git a/internal/data/gebura.go b/internal/data/gebura.go index c3a43530..ec8c5c66 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -521,3 +521,14 @@ func (g *GeburaRepo) ListAppRunTimes( func (g *GeburaRepo) DeleteAppRunTime(ctx context.Context, userID model.InternalID, id model.InternalID) error { return g.data.db.AppRunTime.DeleteOneID(id).Exec(ctx) } + +func (g *GeburaRepo) CreateAppCategory(ctx context.Context, userID model.InternalID, ac *modelgebura.AppCategory) error { + q := g.data.db.AppCategory.Create(). + SetID(ac.ID). + SetUserID(userID). + SetVersionNumber(ac.VersionNumber). + SetVersionDate(ac.VersionDate). + SetName(ac.Name). + AddAppIDs(ac.AppIDs...) + return q.Exec(ctx) +} diff --git a/internal/data/internal/ent/app.go b/internal/data/internal/ent/app.go index 4dbe8c2e..fabfc511 100644 --- a/internal/data/internal/ent/app.go +++ b/internal/data/internal/ent/app.go @@ -85,9 +85,13 @@ type AppEdges struct { Device *Device `json:"device,omitempty"` // AppRunTime holds the value of the app_run_time edge. AppRunTime []*AppRunTime `json:"app_run_time,omitempty"` + // AppCategory holds the value of the app_category edge. + AppCategory []*AppCategory `json:"app_category,omitempty"` + // AppAppCategory holds the value of the app_app_category edge. + AppAppCategory []*AppAppCategory `json:"app_app_category,omitempty"` // loadedTypes holds the information for reporting if a // type was loaded (or requested) in eager-loading or not. - loadedTypes [3]bool + loadedTypes [5]bool } // UserOrErr returns the User value or an error if the edge @@ -121,6 +125,24 @@ func (e AppEdges) AppRunTimeOrErr() ([]*AppRunTime, error) { return nil, &NotLoadedError{edge: "app_run_time"} } +// AppCategoryOrErr returns the AppCategory value or an error if the edge +// was not loaded in eager-loading. +func (e AppEdges) AppCategoryOrErr() ([]*AppCategory, error) { + if e.loadedTypes[3] { + return e.AppCategory, nil + } + return nil, &NotLoadedError{edge: "app_category"} +} + +// AppAppCategoryOrErr returns the AppAppCategory value or an error if the edge +// was not loaded in eager-loading. +func (e AppEdges) AppAppCategoryOrErr() ([]*AppAppCategory, error) { + if e.loadedTypes[4] { + return e.AppAppCategory, nil + } + return nil, &NotLoadedError{edge: "app_app_category"} +} + // scanValues returns the types for scanning values from sql.Rows. func (*App) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) @@ -341,6 +363,16 @@ func (a *App) QueryAppRunTime() *AppRunTimeQuery { return NewAppClient(a.config).QueryAppRunTime(a) } +// QueryAppCategory queries the "app_category" edge of the App entity. +func (a *App) QueryAppCategory() *AppCategoryQuery { + return NewAppClient(a.config).QueryAppCategory(a) +} + +// QueryAppAppCategory queries the "app_app_category" edge of the App entity. +func (a *App) QueryAppAppCategory() *AppAppCategoryQuery { + return NewAppClient(a.config).QueryAppAppCategory(a) +} + // Update returns a builder for updating this App. // Note that you need to call App.Unwrap() before calling this method if this App // was returned from a transaction, and the transaction was committed or rolled back. diff --git a/internal/data/internal/ent/app/app.go b/internal/data/internal/ent/app/app.go index 0d7ec773..96dc91f0 100644 --- a/internal/data/internal/ent/app/app.go +++ b/internal/data/internal/ent/app/app.go @@ -71,6 +71,10 @@ const ( EdgeDevice = "device" // EdgeAppRunTime holds the string denoting the app_run_time edge name in mutations. EdgeAppRunTime = "app_run_time" + // EdgeAppCategory holds the string denoting the app_category edge name in mutations. + EdgeAppCategory = "app_category" + // EdgeAppAppCategory holds the string denoting the app_app_category edge name in mutations. + EdgeAppAppCategory = "app_app_category" // Table holds the table name of the app in the database. Table = "apps" // UserTable is the table that holds the user relation/edge. @@ -94,6 +98,18 @@ const ( AppRunTimeInverseTable = "app_run_times" // AppRunTimeColumn is the table column denoting the app_run_time relation/edge. AppRunTimeColumn = "app_id" + // AppCategoryTable is the table that holds the app_category relation/edge. The primary key declared below. + AppCategoryTable = "app_app_categories" + // AppCategoryInverseTable is the table name for the AppCategory entity. + // It exists in this package in order to avoid circular dependency with the "appcategory" package. + AppCategoryInverseTable = "app_categories" + // AppAppCategoryTable is the table that holds the app_app_category relation/edge. + AppAppCategoryTable = "app_app_categories" + // AppAppCategoryInverseTable is the table name for the AppAppCategory entity. + // It exists in this package in order to avoid circular dependency with the "appappcategory" package. + AppAppCategoryInverseTable = "app_app_categories" + // AppAppCategoryColumn is the table column denoting the app_app_category relation/edge. + AppAppCategoryColumn = "app_id" ) // Columns holds all SQL columns for app fields. @@ -126,6 +142,12 @@ var Columns = []string{ FieldCreatedAt, } +var ( + // AppCategoryPrimaryKey and AppCategoryColumn2 are the table columns denoting the + // primary key for the app_category relation (M2M). + AppCategoryPrimaryKey = []string{"app_category_id", "app_id"} +) + // ValidColumn reports if the column name is valid (part of the table columns). func ValidColumn(column string) bool { for i := range Columns { @@ -313,6 +335,34 @@ func ByAppRunTime(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { sqlgraph.OrderByNeighborTerms(s, newAppRunTimeStep(), append([]sql.OrderTerm{term}, terms...)...) } } + +// ByAppCategoryCount orders the results by app_category count. +func ByAppCategoryCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newAppCategoryStep(), opts...) + } +} + +// ByAppCategory orders the results by app_category terms. +func ByAppCategory(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newAppCategoryStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} + +// ByAppAppCategoryCount orders the results by app_app_category count. +func ByAppAppCategoryCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newAppAppCategoryStep(), opts...) + } +} + +// ByAppAppCategory orders the results by app_app_category terms. +func ByAppAppCategory(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newAppAppCategoryStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} func newUserStep() *sqlgraph.Step { return sqlgraph.NewStep( sqlgraph.From(Table, FieldID), @@ -334,3 +384,17 @@ func newAppRunTimeStep() *sqlgraph.Step { sqlgraph.Edge(sqlgraph.O2M, false, AppRunTimeTable, AppRunTimeColumn), ) } +func newAppCategoryStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(AppCategoryInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2M, true, AppCategoryTable, AppCategoryPrimaryKey...), + ) +} +func newAppAppCategoryStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(AppAppCategoryInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, AppAppCategoryTable, AppAppCategoryColumn), + ) +} diff --git a/internal/data/internal/ent/app/where.go b/internal/data/internal/ent/app/where.go index 7afef07a..b2c9ff12 100644 --- a/internal/data/internal/ent/app/where.go +++ b/internal/data/internal/ent/app/where.go @@ -1397,6 +1397,52 @@ func HasAppRunTimeWith(preds ...predicate.AppRunTime) predicate.App { }) } +// HasAppCategory applies the HasEdge predicate on the "app_category" edge. +func HasAppCategory() predicate.App { + return predicate.App(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2M, true, AppCategoryTable, AppCategoryPrimaryKey...), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasAppCategoryWith applies the HasEdge predicate on the "app_category" edge with a given conditions (other predicates). +func HasAppCategoryWith(preds ...predicate.AppCategory) predicate.App { + return predicate.App(func(s *sql.Selector) { + step := newAppCategoryStep() + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// HasAppAppCategory applies the HasEdge predicate on the "app_app_category" edge. +func HasAppAppCategory() predicate.App { + return predicate.App(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, AppAppCategoryTable, AppAppCategoryColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasAppAppCategoryWith applies the HasEdge predicate on the "app_app_category" edge with a given conditions (other predicates). +func HasAppAppCategoryWith(preds ...predicate.AppAppCategory) predicate.App { + return predicate.App(func(s *sql.Selector) { + step := newAppAppCategoryStep() + 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.App) predicate.App { return predicate.App(sql.AndPredicates(predicates...)) diff --git a/internal/data/internal/ent/app_create.go b/internal/data/internal/ent/app_create.go index 28d8d89f..7e16cc18 100644 --- a/internal/data/internal/ent/app_create.go +++ b/internal/data/internal/ent/app_create.go @@ -12,6 +12,8 @@ import ( "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" "github.com/tuihub/librarian/internal/data/internal/ent/device" "github.com/tuihub/librarian/internal/data/internal/ent/user" @@ -309,6 +311,36 @@ func (ac *AppCreate) AddAppRunTime(a ...*AppRunTime) *AppCreate { return ac.AddAppRunTimeIDs(ids...) } +// AddAppCategoryIDs adds the "app_category" edge to the AppCategory entity by IDs. +func (ac *AppCreate) AddAppCategoryIDs(ids ...model.InternalID) *AppCreate { + ac.mutation.AddAppCategoryIDs(ids...) + return ac +} + +// AddAppCategory adds the "app_category" edges to the AppCategory entity. +func (ac *AppCreate) AddAppCategory(a ...*AppCategory) *AppCreate { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return ac.AddAppCategoryIDs(ids...) +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by IDs. +func (ac *AppCreate) AddAppAppCategoryIDs(ids ...int) *AppCreate { + ac.mutation.AddAppAppCategoryIDs(ids...) + return ac +} + +// AddAppAppCategory adds the "app_app_category" edges to the AppAppCategory entity. +func (ac *AppCreate) AddAppAppCategory(a ...*AppAppCategory) *AppCreate { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return ac.AddAppAppCategoryIDs(ids...) +} + // Mutation returns the AppMutation object of the builder. func (ac *AppCreate) Mutation() *AppMutation { return ac.mutation @@ -587,6 +619,38 @@ func (ac *AppCreate) createSpec() (*App, *sqlgraph.CreateSpec) { } _spec.Edges = append(_spec.Edges, edge) } + if nodes := ac.mutation.AppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } + if nodes := ac.mutation.AppAppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } return _node, _spec } diff --git a/internal/data/internal/ent/app_query.go b/internal/data/internal/ent/app_query.go index 9d0cd4c1..d782def3 100644 --- a/internal/data/internal/ent/app_query.go +++ b/internal/data/internal/ent/app_query.go @@ -13,6 +13,8 @@ import ( "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" "github.com/tuihub/librarian/internal/data/internal/ent/device" "github.com/tuihub/librarian/internal/data/internal/ent/predicate" @@ -23,13 +25,15 @@ import ( // AppQuery is the builder for querying App entities. type AppQuery struct { config - ctx *QueryContext - order []app.OrderOption - inters []Interceptor - predicates []predicate.App - withUser *UserQuery - withDevice *DeviceQuery - withAppRunTime *AppRunTimeQuery + ctx *QueryContext + order []app.OrderOption + inters []Interceptor + predicates []predicate.App + withUser *UserQuery + withDevice *DeviceQuery + withAppRunTime *AppRunTimeQuery + withAppCategory *AppCategoryQuery + withAppAppCategory *AppAppCategoryQuery // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) @@ -132,6 +136,50 @@ func (aq *AppQuery) QueryAppRunTime() *AppRunTimeQuery { return query } +// QueryAppCategory chains the current query on the "app_category" edge. +func (aq *AppQuery) QueryAppCategory() *AppCategoryQuery { + query := (&AppCategoryClient{config: aq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := aq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := aq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(app.Table, app.FieldID, selector), + sqlgraph.To(appcategory.Table, appcategory.FieldID), + sqlgraph.Edge(sqlgraph.M2M, true, app.AppCategoryTable, app.AppCategoryPrimaryKey...), + ) + fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// QueryAppAppCategory chains the current query on the "app_app_category" edge. +func (aq *AppQuery) QueryAppAppCategory() *AppAppCategoryQuery { + query := (&AppAppCategoryClient{config: aq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := aq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := aq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(app.Table, app.FieldID, selector), + sqlgraph.To(appappcategory.Table, appappcategory.FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, app.AppAppCategoryTable, app.AppAppCategoryColumn), + ) + fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) + return fromU, nil + } + return query +} + // First returns the first App entity from the query. // Returns a *NotFoundError when no App was found. func (aq *AppQuery) First(ctx context.Context) (*App, error) { @@ -319,14 +367,16 @@ func (aq *AppQuery) Clone() *AppQuery { return nil } return &AppQuery{ - config: aq.config, - ctx: aq.ctx.Clone(), - order: append([]app.OrderOption{}, aq.order...), - inters: append([]Interceptor{}, aq.inters...), - predicates: append([]predicate.App{}, aq.predicates...), - withUser: aq.withUser.Clone(), - withDevice: aq.withDevice.Clone(), - withAppRunTime: aq.withAppRunTime.Clone(), + config: aq.config, + ctx: aq.ctx.Clone(), + order: append([]app.OrderOption{}, aq.order...), + inters: append([]Interceptor{}, aq.inters...), + predicates: append([]predicate.App{}, aq.predicates...), + withUser: aq.withUser.Clone(), + withDevice: aq.withDevice.Clone(), + withAppRunTime: aq.withAppRunTime.Clone(), + withAppCategory: aq.withAppCategory.Clone(), + withAppAppCategory: aq.withAppAppCategory.Clone(), // clone intermediate query. sql: aq.sql.Clone(), path: aq.path, @@ -366,6 +416,28 @@ func (aq *AppQuery) WithAppRunTime(opts ...func(*AppRunTimeQuery)) *AppQuery { return aq } +// WithAppCategory tells the query-builder to eager-load the nodes that are connected to +// the "app_category" edge. The optional arguments are used to configure the query builder of the edge. +func (aq *AppQuery) WithAppCategory(opts ...func(*AppCategoryQuery)) *AppQuery { + query := (&AppCategoryClient{config: aq.config}).Query() + for _, opt := range opts { + opt(query) + } + aq.withAppCategory = query + return aq +} + +// WithAppAppCategory tells the query-builder to eager-load the nodes that are connected to +// the "app_app_category" edge. The optional arguments are used to configure the query builder of the edge. +func (aq *AppQuery) WithAppAppCategory(opts ...func(*AppAppCategoryQuery)) *AppQuery { + query := (&AppAppCategoryClient{config: aq.config}).Query() + for _, opt := range opts { + opt(query) + } + aq.withAppAppCategory = query + return aq +} + // 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. // @@ -444,10 +516,12 @@ func (aq *AppQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*App, err var ( nodes = []*App{} _spec = aq.querySpec() - loadedTypes = [3]bool{ + loadedTypes = [5]bool{ aq.withUser != nil, aq.withDevice != nil, aq.withAppRunTime != nil, + aq.withAppCategory != nil, + aq.withAppAppCategory != nil, } ) _spec.ScanValues = func(columns []string) ([]any, error) { @@ -487,6 +561,20 @@ func (aq *AppQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*App, err return nil, err } } + if query := aq.withAppCategory; query != nil { + if err := aq.loadAppCategory(ctx, query, nodes, + func(n *App) { n.Edges.AppCategory = []*AppCategory{} }, + func(n *App, e *AppCategory) { n.Edges.AppCategory = append(n.Edges.AppCategory, e) }); err != nil { + return nil, err + } + } + if query := aq.withAppAppCategory; query != nil { + if err := aq.loadAppAppCategory(ctx, query, nodes, + func(n *App) { n.Edges.AppAppCategory = []*AppAppCategory{} }, + func(n *App, e *AppAppCategory) { n.Edges.AppAppCategory = append(n.Edges.AppAppCategory, e) }); err != nil { + return nil, err + } + } return nodes, nil } @@ -578,6 +666,97 @@ func (aq *AppQuery) loadAppRunTime(ctx context.Context, query *AppRunTimeQuery, } return nil } +func (aq *AppQuery) loadAppCategory(ctx context.Context, query *AppCategoryQuery, nodes []*App, init func(*App), assign func(*App, *AppCategory)) error { + edgeIDs := make([]driver.Value, len(nodes)) + byID := make(map[model.InternalID]*App) + nids := make(map[model.InternalID]map[*App]struct{}) + for i, node := range nodes { + edgeIDs[i] = node.ID + byID[node.ID] = node + if init != nil { + init(node) + } + } + query.Where(func(s *sql.Selector) { + joinT := sql.Table(app.AppCategoryTable) + s.Join(joinT).On(s.C(appcategory.FieldID), joinT.C(app.AppCategoryPrimaryKey[0])) + s.Where(sql.InValues(joinT.C(app.AppCategoryPrimaryKey[1]), edgeIDs...)) + columns := s.SelectedColumns() + s.Select(joinT.C(app.AppCategoryPrimaryKey[1])) + s.AppendSelect(columns...) + s.SetDistinct(false) + }) + if err := query.prepareQuery(ctx); err != nil { + return err + } + qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) { + return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) { + assign := spec.Assign + values := spec.ScanValues + spec.ScanValues = func(columns []string) ([]any, error) { + values, err := values(columns[1:]) + if err != nil { + return nil, err + } + return append([]any{new(sql.NullInt64)}, values...), nil + } + spec.Assign = func(columns []string, values []any) error { + outValue := model.InternalID(values[0].(*sql.NullInt64).Int64) + inValue := model.InternalID(values[1].(*sql.NullInt64).Int64) + if nids[inValue] == nil { + nids[inValue] = map[*App]struct{}{byID[outValue]: {}} + return assign(columns[1:], values[1:]) + } + nids[inValue][byID[outValue]] = struct{}{} + return nil + } + }) + }) + neighbors, err := withInterceptors[[]*AppCategory](ctx, query, qr, query.inters) + if err != nil { + return err + } + for _, n := range neighbors { + nodes, ok := nids[n.ID] + if !ok { + return fmt.Errorf(`unexpected "app_category" node returned %v`, n.ID) + } + for kn := range nodes { + assign(kn, n) + } + } + return nil +} +func (aq *AppQuery) loadAppAppCategory(ctx context.Context, query *AppAppCategoryQuery, nodes []*App, init func(*App), assign func(*App, *AppAppCategory)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[model.InternalID]*App) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + if len(query.ctx.Fields) > 0 { + query.ctx.AppendFieldOnce(appappcategory.FieldAppID) + } + query.Where(predicate.AppAppCategory(func(s *sql.Selector) { + s.Where(sql.InValues(s.C(app.AppAppCategoryColumn), fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.AppID + node, ok := nodeids[fk] + if !ok { + return fmt.Errorf(`unexpected referenced foreign-key "app_id" returned %v for node %v`, fk, n.ID) + } + assign(node, n) + } + return nil +} func (aq *AppQuery) sqlCount(ctx context.Context) (int, error) { _spec := aq.querySpec() diff --git a/internal/data/internal/ent/app_update.go b/internal/data/internal/ent/app_update.go index 605faaf3..e8c2ec05 100644 --- a/internal/data/internal/ent/app_update.go +++ b/internal/data/internal/ent/app_update.go @@ -13,6 +13,8 @@ import ( "entgo.io/ent/dialect/sql/sqljson" "entgo.io/ent/schema/field" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" "github.com/tuihub/librarian/internal/data/internal/ent/predicate" "github.com/tuihub/librarian/internal/data/internal/ent/user" @@ -436,6 +438,36 @@ func (au *AppUpdate) AddAppRunTime(a ...*AppRunTime) *AppUpdate { return au.AddAppRunTimeIDs(ids...) } +// AddAppCategoryIDs adds the "app_category" edge to the AppCategory entity by IDs. +func (au *AppUpdate) AddAppCategoryIDs(ids ...model.InternalID) *AppUpdate { + au.mutation.AddAppCategoryIDs(ids...) + return au +} + +// AddAppCategory adds the "app_category" edges to the AppCategory entity. +func (au *AppUpdate) AddAppCategory(a ...*AppCategory) *AppUpdate { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return au.AddAppCategoryIDs(ids...) +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by IDs. +func (au *AppUpdate) AddAppAppCategoryIDs(ids ...int) *AppUpdate { + au.mutation.AddAppAppCategoryIDs(ids...) + return au +} + +// AddAppAppCategory adds the "app_app_category" edges to the AppAppCategory entity. +func (au *AppUpdate) AddAppAppCategory(a ...*AppAppCategory) *AppUpdate { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return au.AddAppAppCategoryIDs(ids...) +} + // Mutation returns the AppMutation object of the builder. func (au *AppUpdate) Mutation() *AppMutation { return au.mutation @@ -468,6 +500,48 @@ func (au *AppUpdate) RemoveAppRunTime(a ...*AppRunTime) *AppUpdate { return au.RemoveAppRunTimeIDs(ids...) } +// ClearAppCategory clears all "app_category" edges to the AppCategory entity. +func (au *AppUpdate) ClearAppCategory() *AppUpdate { + au.mutation.ClearAppCategory() + return au +} + +// RemoveAppCategoryIDs removes the "app_category" edge to AppCategory entities by IDs. +func (au *AppUpdate) RemoveAppCategoryIDs(ids ...model.InternalID) *AppUpdate { + au.mutation.RemoveAppCategoryIDs(ids...) + return au +} + +// RemoveAppCategory removes "app_category" edges to AppCategory entities. +func (au *AppUpdate) RemoveAppCategory(a ...*AppCategory) *AppUpdate { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return au.RemoveAppCategoryIDs(ids...) +} + +// ClearAppAppCategory clears all "app_app_category" edges to the AppAppCategory entity. +func (au *AppUpdate) ClearAppAppCategory() *AppUpdate { + au.mutation.ClearAppAppCategory() + return au +} + +// RemoveAppAppCategoryIDs removes the "app_app_category" edge to AppAppCategory entities by IDs. +func (au *AppUpdate) RemoveAppAppCategoryIDs(ids ...int) *AppUpdate { + au.mutation.RemoveAppAppCategoryIDs(ids...) + return au +} + +// RemoveAppAppCategory removes "app_app_category" edges to AppAppCategory entities. +func (au *AppUpdate) RemoveAppAppCategory(a ...*AppAppCategory) *AppUpdate { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return au.RemoveAppAppCategoryIDs(ids...) +} + // Save executes the query and returns the number of nodes affected by the update operation. func (au *AppUpdate) Save(ctx context.Context) (int, error) { au.defaults() @@ -724,6 +798,96 @@ func (au *AppUpdate) sqlSave(ctx context.Context) (n int, err error) { } _spec.Edges.Add = append(_spec.Edges.Add, edge) } + if au.mutation.AppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := au.mutation.RemovedAppCategoryIDs(); len(nodes) > 0 && !au.mutation.AppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := au.mutation.AppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if au.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := au.mutation.RemovedAppAppCategoryIDs(); len(nodes) > 0 && !au.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.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 := au.mutation.AppAppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } if n, err = sqlgraph.UpdateNodes(ctx, au.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{app.Label} @@ -1148,6 +1312,36 @@ func (auo *AppUpdateOne) AddAppRunTime(a ...*AppRunTime) *AppUpdateOne { return auo.AddAppRunTimeIDs(ids...) } +// AddAppCategoryIDs adds the "app_category" edge to the AppCategory entity by IDs. +func (auo *AppUpdateOne) AddAppCategoryIDs(ids ...model.InternalID) *AppUpdateOne { + auo.mutation.AddAppCategoryIDs(ids...) + return auo +} + +// AddAppCategory adds the "app_category" edges to the AppCategory entity. +func (auo *AppUpdateOne) AddAppCategory(a ...*AppCategory) *AppUpdateOne { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return auo.AddAppCategoryIDs(ids...) +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by IDs. +func (auo *AppUpdateOne) AddAppAppCategoryIDs(ids ...int) *AppUpdateOne { + auo.mutation.AddAppAppCategoryIDs(ids...) + return auo +} + +// AddAppAppCategory adds the "app_app_category" edges to the AppAppCategory entity. +func (auo *AppUpdateOne) AddAppAppCategory(a ...*AppAppCategory) *AppUpdateOne { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return auo.AddAppAppCategoryIDs(ids...) +} + // Mutation returns the AppMutation object of the builder. func (auo *AppUpdateOne) Mutation() *AppMutation { return auo.mutation @@ -1180,6 +1374,48 @@ func (auo *AppUpdateOne) RemoveAppRunTime(a ...*AppRunTime) *AppUpdateOne { return auo.RemoveAppRunTimeIDs(ids...) } +// ClearAppCategory clears all "app_category" edges to the AppCategory entity. +func (auo *AppUpdateOne) ClearAppCategory() *AppUpdateOne { + auo.mutation.ClearAppCategory() + return auo +} + +// RemoveAppCategoryIDs removes the "app_category" edge to AppCategory entities by IDs. +func (auo *AppUpdateOne) RemoveAppCategoryIDs(ids ...model.InternalID) *AppUpdateOne { + auo.mutation.RemoveAppCategoryIDs(ids...) + return auo +} + +// RemoveAppCategory removes "app_category" edges to AppCategory entities. +func (auo *AppUpdateOne) RemoveAppCategory(a ...*AppCategory) *AppUpdateOne { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return auo.RemoveAppCategoryIDs(ids...) +} + +// ClearAppAppCategory clears all "app_app_category" edges to the AppAppCategory entity. +func (auo *AppUpdateOne) ClearAppAppCategory() *AppUpdateOne { + auo.mutation.ClearAppAppCategory() + return auo +} + +// RemoveAppAppCategoryIDs removes the "app_app_category" edge to AppAppCategory entities by IDs. +func (auo *AppUpdateOne) RemoveAppAppCategoryIDs(ids ...int) *AppUpdateOne { + auo.mutation.RemoveAppAppCategoryIDs(ids...) + return auo +} + +// RemoveAppAppCategory removes "app_app_category" edges to AppAppCategory entities. +func (auo *AppUpdateOne) RemoveAppAppCategory(a ...*AppAppCategory) *AppUpdateOne { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return auo.RemoveAppAppCategoryIDs(ids...) +} + // Where appends a list predicates to the AppUpdate builder. func (auo *AppUpdateOne) Where(ps ...predicate.App) *AppUpdateOne { auo.mutation.Where(ps...) @@ -1466,6 +1702,96 @@ func (auo *AppUpdateOne) sqlSave(ctx context.Context) (_node *App, err error) { } _spec.Edges.Add = append(_spec.Edges.Add, edge) } + if auo.mutation.AppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := auo.mutation.RemovedAppCategoryIDs(); len(nodes) > 0 && !auo.mutation.AppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := auo.mutation.AppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: true, + Table: app.AppCategoryTable, + Columns: app.AppCategoryPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if auo.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := auo.mutation.RemovedAppAppCategoryIDs(); len(nodes) > 0 && !auo.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.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 := auo.mutation.AppAppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: app.AppAppCategoryTable, + Columns: []string{app.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } _node = &App{config: auo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues diff --git a/internal/data/internal/ent/appappcategory.go b/internal/data/internal/ent/appappcategory.go new file mode 100644 index 00000000..bcff4291 --- /dev/null +++ b/internal/data/internal/ent/appappcategory.go @@ -0,0 +1,161 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" + "github.com/tuihub/librarian/internal/model" +) + +// AppAppCategory is the model entity for the AppAppCategory schema. +type AppAppCategory struct { + config `json:"-"` + // ID of the ent. + ID int `json:"id,omitempty"` + // AppCategoryID holds the value of the "app_category_id" field. + AppCategoryID model.InternalID `json:"app_category_id,omitempty"` + // AppID holds the value of the "app_id" field. + AppID model.InternalID `json:"app_id,omitempty"` + // Edges holds the relations/edges for other nodes in the graph. + // The values are being populated by the AppAppCategoryQuery when eager-loading is set. + Edges AppAppCategoryEdges `json:"edges"` + selectValues sql.SelectValues +} + +// AppAppCategoryEdges holds the relations/edges for other nodes in the graph. +type AppAppCategoryEdges struct { + // AppCategory holds the value of the app_category edge. + AppCategory *AppCategory `json:"app_category,omitempty"` + // App holds the value of the app edge. + App *App `json:"app,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [2]bool +} + +// AppCategoryOrErr returns the AppCategory value or an error if the edge +// was not loaded in eager-loading, or loaded but was not found. +func (e AppAppCategoryEdges) AppCategoryOrErr() (*AppCategory, error) { + if e.AppCategory != nil { + return e.AppCategory, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: appcategory.Label} + } + return nil, &NotLoadedError{edge: "app_category"} +} + +// AppOrErr returns the App value or an error if the edge +// was not loaded in eager-loading, or loaded but was not found. +func (e AppAppCategoryEdges) AppOrErr() (*App, error) { + if e.App != nil { + return e.App, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: app.Label} + } + return nil, &NotLoadedError{edge: "app"} +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*AppAppCategory) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case appappcategory.FieldID, appappcategory.FieldAppCategoryID, appappcategory.FieldAppID: + values[i] = new(sql.NullInt64) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the AppAppCategory fields. +func (aac *AppAppCategory) 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 appappcategory.FieldID: + value, ok := values[i].(*sql.NullInt64) + if !ok { + return fmt.Errorf("unexpected type %T for field id", value) + } + aac.ID = int(value.Int64) + case appappcategory.FieldAppCategoryID: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field app_category_id", values[i]) + } else if value.Valid { + aac.AppCategoryID = model.InternalID(value.Int64) + } + case appappcategory.FieldAppID: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field app_id", values[i]) + } else if value.Valid { + aac.AppID = model.InternalID(value.Int64) + } + default: + aac.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the AppAppCategory. +// This includes values selected through modifiers, order, etc. +func (aac *AppAppCategory) Value(name string) (ent.Value, error) { + return aac.selectValues.Get(name) +} + +// QueryAppCategory queries the "app_category" edge of the AppAppCategory entity. +func (aac *AppAppCategory) QueryAppCategory() *AppCategoryQuery { + return NewAppAppCategoryClient(aac.config).QueryAppCategory(aac) +} + +// QueryApp queries the "app" edge of the AppAppCategory entity. +func (aac *AppAppCategory) QueryApp() *AppQuery { + return NewAppAppCategoryClient(aac.config).QueryApp(aac) +} + +// Update returns a builder for updating this AppAppCategory. +// Note that you need to call AppAppCategory.Unwrap() before calling this method if this AppAppCategory +// was returned from a transaction, and the transaction was committed or rolled back. +func (aac *AppAppCategory) Update() *AppAppCategoryUpdateOne { + return NewAppAppCategoryClient(aac.config).UpdateOne(aac) +} + +// Unwrap unwraps the AppAppCategory 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 (aac *AppAppCategory) Unwrap() *AppAppCategory { + _tx, ok := aac.config.driver.(*txDriver) + if !ok { + panic("ent: AppAppCategory is not a transactional entity") + } + aac.config.driver = _tx.drv + return aac +} + +// String implements the fmt.Stringer. +func (aac *AppAppCategory) String() string { + var builder strings.Builder + builder.WriteString("AppAppCategory(") + builder.WriteString(fmt.Sprintf("id=%v, ", aac.ID)) + builder.WriteString("app_category_id=") + builder.WriteString(fmt.Sprintf("%v", aac.AppCategoryID)) + builder.WriteString(", ") + builder.WriteString("app_id=") + builder.WriteString(fmt.Sprintf("%v", aac.AppID)) + builder.WriteByte(')') + return builder.String() +} + +// AppAppCategories is a parsable slice of AppAppCategory. +type AppAppCategories []*AppAppCategory diff --git a/internal/data/internal/ent/appappcategory/appappcategory.go b/internal/data/internal/ent/appappcategory/appappcategory.go new file mode 100644 index 00000000..7feca9f5 --- /dev/null +++ b/internal/data/internal/ent/appappcategory/appappcategory.go @@ -0,0 +1,102 @@ +// Code generated by ent, DO NOT EDIT. + +package appappcategory + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" +) + +const ( + // Label holds the string label denoting the appappcategory type in the database. + Label = "app_app_category" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldAppCategoryID holds the string denoting the app_category_id field in the database. + FieldAppCategoryID = "app_category_id" + // FieldAppID holds the string denoting the app_id field in the database. + FieldAppID = "app_id" + // EdgeAppCategory holds the string denoting the app_category edge name in mutations. + EdgeAppCategory = "app_category" + // EdgeApp holds the string denoting the app edge name in mutations. + EdgeApp = "app" + // Table holds the table name of the appappcategory in the database. + Table = "app_app_categories" + // AppCategoryTable is the table that holds the app_category relation/edge. + AppCategoryTable = "app_app_categories" + // AppCategoryInverseTable is the table name for the AppCategory entity. + // It exists in this package in order to avoid circular dependency with the "appcategory" package. + AppCategoryInverseTable = "app_categories" + // AppCategoryColumn is the table column denoting the app_category relation/edge. + AppCategoryColumn = "app_category_id" + // AppTable is the table that holds the app relation/edge. + AppTable = "app_app_categories" + // AppInverseTable is the table name for the App entity. + // It exists in this package in order to avoid circular dependency with the "app" package. + AppInverseTable = "apps" + // AppColumn is the table column denoting the app relation/edge. + AppColumn = "app_id" +) + +// Columns holds all SQL columns for appappcategory fields. +var Columns = []string{ + FieldID, + FieldAppCategoryID, + FieldAppID, +} + +// 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 +} + +// OrderOption defines the ordering options for the AppAppCategory 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() +} + +// ByAppCategoryID orders the results by the app_category_id field. +func ByAppCategoryID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldAppCategoryID, opts...).ToFunc() +} + +// ByAppID orders the results by the app_id field. +func ByAppID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldAppID, opts...).ToFunc() +} + +// ByAppCategoryField orders the results by app_category field. +func ByAppCategoryField(field string, opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newAppCategoryStep(), sql.OrderByField(field, opts...)) + } +} + +// ByAppField orders the results by app field. +func ByAppField(field string, opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newAppStep(), sql.OrderByField(field, opts...)) + } +} +func newAppCategoryStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(AppCategoryInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, AppCategoryTable, AppCategoryColumn), + ) +} +func newAppStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(AppInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, AppTable, AppColumn), + ) +} diff --git a/internal/data/internal/ent/appappcategory/where.go b/internal/data/internal/ent/appappcategory/where.go new file mode 100644 index 00000000..ffec1916 --- /dev/null +++ b/internal/data/internal/ent/appappcategory/where.go @@ -0,0 +1,188 @@ +// Code generated by ent, DO NOT EDIT. + +package appappcategory + +import ( + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "github.com/tuihub/librarian/internal/data/internal/ent/predicate" + "github.com/tuihub/librarian/internal/model" +) + +// ID filters vertices based on their ID field. +func ID(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id int) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.FieldLTE(FieldID, id)) +} + +// AppCategoryID applies equality check predicate on the "app_category_id" field. It's identical to AppCategoryIDEQ. +func AppCategoryID(v model.InternalID) predicate.AppAppCategory { + vc := int64(v) + return predicate.AppAppCategory(sql.FieldEQ(FieldAppCategoryID, vc)) +} + +// AppID applies equality check predicate on the "app_id" field. It's identical to AppIDEQ. +func AppID(v model.InternalID) predicate.AppAppCategory { + vc := int64(v) + return predicate.AppAppCategory(sql.FieldEQ(FieldAppID, vc)) +} + +// AppCategoryIDEQ applies the EQ predicate on the "app_category_id" field. +func AppCategoryIDEQ(v model.InternalID) predicate.AppAppCategory { + vc := int64(v) + return predicate.AppAppCategory(sql.FieldEQ(FieldAppCategoryID, vc)) +} + +// AppCategoryIDNEQ applies the NEQ predicate on the "app_category_id" field. +func AppCategoryIDNEQ(v model.InternalID) predicate.AppAppCategory { + vc := int64(v) + return predicate.AppAppCategory(sql.FieldNEQ(FieldAppCategoryID, vc)) +} + +// AppCategoryIDIn applies the In predicate on the "app_category_id" field. +func AppCategoryIDIn(vs ...model.InternalID) predicate.AppAppCategory { + v := make([]any, len(vs)) + for i := range v { + v[i] = int64(vs[i]) + } + return predicate.AppAppCategory(sql.FieldIn(FieldAppCategoryID, v...)) +} + +// AppCategoryIDNotIn applies the NotIn predicate on the "app_category_id" field. +func AppCategoryIDNotIn(vs ...model.InternalID) predicate.AppAppCategory { + v := make([]any, len(vs)) + for i := range v { + v[i] = int64(vs[i]) + } + return predicate.AppAppCategory(sql.FieldNotIn(FieldAppCategoryID, v...)) +} + +// AppIDEQ applies the EQ predicate on the "app_id" field. +func AppIDEQ(v model.InternalID) predicate.AppAppCategory { + vc := int64(v) + return predicate.AppAppCategory(sql.FieldEQ(FieldAppID, vc)) +} + +// AppIDNEQ applies the NEQ predicate on the "app_id" field. +func AppIDNEQ(v model.InternalID) predicate.AppAppCategory { + vc := int64(v) + return predicate.AppAppCategory(sql.FieldNEQ(FieldAppID, vc)) +} + +// AppIDIn applies the In predicate on the "app_id" field. +func AppIDIn(vs ...model.InternalID) predicate.AppAppCategory { + v := make([]any, len(vs)) + for i := range v { + v[i] = int64(vs[i]) + } + return predicate.AppAppCategory(sql.FieldIn(FieldAppID, v...)) +} + +// AppIDNotIn applies the NotIn predicate on the "app_id" field. +func AppIDNotIn(vs ...model.InternalID) predicate.AppAppCategory { + v := make([]any, len(vs)) + for i := range v { + v[i] = int64(vs[i]) + } + return predicate.AppAppCategory(sql.FieldNotIn(FieldAppID, v...)) +} + +// HasAppCategory applies the HasEdge predicate on the "app_category" edge. +func HasAppCategory() predicate.AppAppCategory { + return predicate.AppAppCategory(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, AppCategoryTable, AppCategoryColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasAppCategoryWith applies the HasEdge predicate on the "app_category" edge with a given conditions (other predicates). +func HasAppCategoryWith(preds ...predicate.AppCategory) predicate.AppAppCategory { + return predicate.AppAppCategory(func(s *sql.Selector) { + step := newAppCategoryStep() + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// HasApp applies the HasEdge predicate on the "app" edge. +func HasApp() predicate.AppAppCategory { + return predicate.AppAppCategory(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, AppTable, AppColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasAppWith applies the HasEdge predicate on the "app" edge with a given conditions (other predicates). +func HasAppWith(preds ...predicate.App) predicate.AppAppCategory { + return predicate.AppAppCategory(func(s *sql.Selector) { + step := newAppStep() + 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.AppAppCategory) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.AppAppCategory) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.AppAppCategory) predicate.AppAppCategory { + return predicate.AppAppCategory(sql.NotPredicates(p)) +} diff --git a/internal/data/internal/ent/appappcategory_create.go b/internal/data/internal/ent/appappcategory_create.go new file mode 100644 index 00000000..f744157e --- /dev/null +++ b/internal/data/internal/ent/appappcategory_create.go @@ -0,0 +1,552 @@ +// 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" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" + "github.com/tuihub/librarian/internal/model" +) + +// AppAppCategoryCreate is the builder for creating a AppAppCategory entity. +type AppAppCategoryCreate struct { + config + mutation *AppAppCategoryMutation + hooks []Hook + conflict []sql.ConflictOption +} + +// SetAppCategoryID sets the "app_category_id" field. +func (aacc *AppAppCategoryCreate) SetAppCategoryID(mi model.InternalID) *AppAppCategoryCreate { + aacc.mutation.SetAppCategoryID(mi) + return aacc +} + +// SetAppID sets the "app_id" field. +func (aacc *AppAppCategoryCreate) SetAppID(mi model.InternalID) *AppAppCategoryCreate { + aacc.mutation.SetAppID(mi) + return aacc +} + +// SetAppCategory sets the "app_category" edge to the AppCategory entity. +func (aacc *AppAppCategoryCreate) SetAppCategory(a *AppCategory) *AppAppCategoryCreate { + return aacc.SetAppCategoryID(a.ID) +} + +// SetApp sets the "app" edge to the App entity. +func (aacc *AppAppCategoryCreate) SetApp(a *App) *AppAppCategoryCreate { + return aacc.SetAppID(a.ID) +} + +// Mutation returns the AppAppCategoryMutation object of the builder. +func (aacc *AppAppCategoryCreate) Mutation() *AppAppCategoryMutation { + return aacc.mutation +} + +// Save creates the AppAppCategory in the database. +func (aacc *AppAppCategoryCreate) Save(ctx context.Context) (*AppAppCategory, error) { + return withHooks(ctx, aacc.sqlSave, aacc.mutation, aacc.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (aacc *AppAppCategoryCreate) SaveX(ctx context.Context) *AppAppCategory { + v, err := aacc.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (aacc *AppAppCategoryCreate) Exec(ctx context.Context) error { + _, err := aacc.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (aacc *AppAppCategoryCreate) ExecX(ctx context.Context) { + if err := aacc.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (aacc *AppAppCategoryCreate) check() error { + if _, ok := aacc.mutation.AppCategoryID(); !ok { + return &ValidationError{Name: "app_category_id", err: errors.New(`ent: missing required field "AppAppCategory.app_category_id"`)} + } + if _, ok := aacc.mutation.AppID(); !ok { + return &ValidationError{Name: "app_id", err: errors.New(`ent: missing required field "AppAppCategory.app_id"`)} + } + if len(aacc.mutation.AppCategoryIDs()) == 0 { + return &ValidationError{Name: "app_category", err: errors.New(`ent: missing required edge "AppAppCategory.app_category"`)} + } + if len(aacc.mutation.AppIDs()) == 0 { + return &ValidationError{Name: "app", err: errors.New(`ent: missing required edge "AppAppCategory.app"`)} + } + return nil +} + +func (aacc *AppAppCategoryCreate) sqlSave(ctx context.Context) (*AppAppCategory, error) { + if err := aacc.check(); err != nil { + return nil, err + } + _node, _spec := aacc.createSpec() + if err := sqlgraph.CreateNode(ctx, aacc.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) + aacc.mutation.id = &_node.ID + aacc.mutation.done = true + return _node, nil +} + +func (aacc *AppAppCategoryCreate) createSpec() (*AppAppCategory, *sqlgraph.CreateSpec) { + var ( + _node = &AppAppCategory{config: aacc.config} + _spec = sqlgraph.NewCreateSpec(appappcategory.Table, sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt)) + ) + _spec.OnConflict = aacc.conflict + if nodes := aacc.mutation.AppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppCategoryTable, + Columns: []string{appappcategory.AppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.AppCategoryID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + if nodes := aacc.mutation.AppIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppTable, + Columns: []string{appappcategory.AppColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _node.AppID = nodes[0] + _spec.Edges = append(_spec.Edges, edge) + } + return _node, _spec +} + +// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause +// of the `INSERT` statement. For example: +// +// client.AppAppCategory.Create(). +// SetAppCategoryID(v). +// OnConflict( +// // Update the row with the new values +// // the was proposed for insertion. +// sql.ResolveWithNewValues(), +// ). +// // Override some of the fields with custom +// // update values. +// Update(func(u *ent.AppAppCategoryUpsert) { +// SetAppCategoryID(v+v). +// }). +// Exec(ctx) +func (aacc *AppAppCategoryCreate) OnConflict(opts ...sql.ConflictOption) *AppAppCategoryUpsertOne { + aacc.conflict = opts + return &AppAppCategoryUpsertOne{ + create: aacc, + } +} + +// OnConflictColumns calls `OnConflict` and configures the columns +// as conflict target. Using this option is equivalent to using: +// +// client.AppAppCategory.Create(). +// OnConflict(sql.ConflictColumns(columns...)). +// Exec(ctx) +func (aacc *AppAppCategoryCreate) OnConflictColumns(columns ...string) *AppAppCategoryUpsertOne { + aacc.conflict = append(aacc.conflict, sql.ConflictColumns(columns...)) + return &AppAppCategoryUpsertOne{ + create: aacc, + } +} + +type ( + // AppAppCategoryUpsertOne is the builder for "upsert"-ing + // one AppAppCategory node. + AppAppCategoryUpsertOne struct { + create *AppAppCategoryCreate + } + + // AppAppCategoryUpsert is the "OnConflict" setter. + AppAppCategoryUpsert struct { + *sql.UpdateSet + } +) + +// SetAppCategoryID sets the "app_category_id" field. +func (u *AppAppCategoryUpsert) SetAppCategoryID(v model.InternalID) *AppAppCategoryUpsert { + u.Set(appappcategory.FieldAppCategoryID, v) + return u +} + +// UpdateAppCategoryID sets the "app_category_id" field to the value that was provided on create. +func (u *AppAppCategoryUpsert) UpdateAppCategoryID() *AppAppCategoryUpsert { + u.SetExcluded(appappcategory.FieldAppCategoryID) + return u +} + +// SetAppID sets the "app_id" field. +func (u *AppAppCategoryUpsert) SetAppID(v model.InternalID) *AppAppCategoryUpsert { + u.Set(appappcategory.FieldAppID, v) + return u +} + +// UpdateAppID sets the "app_id" field to the value that was provided on create. +func (u *AppAppCategoryUpsert) UpdateAppID() *AppAppCategoryUpsert { + u.SetExcluded(appappcategory.FieldAppID) + return u +} + +// UpdateNewValues updates the mutable fields using the new values that were set on create. +// Using this option is equivalent to using: +// +// client.AppAppCategory.Create(). +// OnConflict( +// sql.ResolveWithNewValues(), +// ). +// Exec(ctx) +func (u *AppAppCategoryUpsertOne) UpdateNewValues() *AppAppCategoryUpsertOne { + u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) + return u +} + +// Ignore sets each column to itself in case of conflict. +// Using this option is equivalent to using: +// +// client.AppAppCategory.Create(). +// OnConflict(sql.ResolveWithIgnore()). +// Exec(ctx) +func (u *AppAppCategoryUpsertOne) Ignore() *AppAppCategoryUpsertOne { + u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) + return u +} + +// DoNothing configures the conflict_action to `DO NOTHING`. +// Supported only by SQLite and PostgreSQL. +func (u *AppAppCategoryUpsertOne) DoNothing() *AppAppCategoryUpsertOne { + u.create.conflict = append(u.create.conflict, sql.DoNothing()) + return u +} + +// Update allows overriding fields `UPDATE` values. See the AppAppCategoryCreate.OnConflict +// documentation for more info. +func (u *AppAppCategoryUpsertOne) Update(set func(*AppAppCategoryUpsert)) *AppAppCategoryUpsertOne { + u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { + set(&AppAppCategoryUpsert{UpdateSet: update}) + })) + return u +} + +// SetAppCategoryID sets the "app_category_id" field. +func (u *AppAppCategoryUpsertOne) SetAppCategoryID(v model.InternalID) *AppAppCategoryUpsertOne { + return u.Update(func(s *AppAppCategoryUpsert) { + s.SetAppCategoryID(v) + }) +} + +// UpdateAppCategoryID sets the "app_category_id" field to the value that was provided on create. +func (u *AppAppCategoryUpsertOne) UpdateAppCategoryID() *AppAppCategoryUpsertOne { + return u.Update(func(s *AppAppCategoryUpsert) { + s.UpdateAppCategoryID() + }) +} + +// SetAppID sets the "app_id" field. +func (u *AppAppCategoryUpsertOne) SetAppID(v model.InternalID) *AppAppCategoryUpsertOne { + return u.Update(func(s *AppAppCategoryUpsert) { + s.SetAppID(v) + }) +} + +// UpdateAppID sets the "app_id" field to the value that was provided on create. +func (u *AppAppCategoryUpsertOne) UpdateAppID() *AppAppCategoryUpsertOne { + return u.Update(func(s *AppAppCategoryUpsert) { + s.UpdateAppID() + }) +} + +// Exec executes the query. +func (u *AppAppCategoryUpsertOne) Exec(ctx context.Context) error { + if len(u.create.conflict) == 0 { + return errors.New("ent: missing options for AppAppCategoryCreate.OnConflict") + } + return u.create.Exec(ctx) +} + +// ExecX is like Exec, but panics if an error occurs. +func (u *AppAppCategoryUpsertOne) ExecX(ctx context.Context) { + if err := u.create.Exec(ctx); err != nil { + panic(err) + } +} + +// Exec executes the UPSERT query and returns the inserted/updated ID. +func (u *AppAppCategoryUpsertOne) ID(ctx context.Context) (id int, err error) { + node, err := u.create.Save(ctx) + if err != nil { + return id, err + } + return node.ID, nil +} + +// IDX is like ID, but panics if an error occurs. +func (u *AppAppCategoryUpsertOne) IDX(ctx context.Context) int { + id, err := u.ID(ctx) + if err != nil { + panic(err) + } + return id +} + +// AppAppCategoryCreateBulk is the builder for creating many AppAppCategory entities in bulk. +type AppAppCategoryCreateBulk struct { + config + err error + builders []*AppAppCategoryCreate + conflict []sql.ConflictOption +} + +// Save creates the AppAppCategory entities in the database. +func (aaccb *AppAppCategoryCreateBulk) Save(ctx context.Context) ([]*AppAppCategory, error) { + if aaccb.err != nil { + return nil, aaccb.err + } + specs := make([]*sqlgraph.CreateSpec, len(aaccb.builders)) + nodes := make([]*AppAppCategory, len(aaccb.builders)) + mutators := make([]Mutator, len(aaccb.builders)) + for i := range aaccb.builders { + func(i int, root context.Context) { + builder := aaccb.builders[i] + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*AppAppCategoryMutation) + 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, aaccb.builders[i+1].mutation) + } else { + spec := &sqlgraph.BatchCreateSpec{Nodes: specs} + spec.OnConflict = aaccb.conflict + // Invoke the actual operation on the latest mutation in the chain. + if err = sqlgraph.BatchCreate(ctx, aaccb.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, aaccb.builders[0].mutation); err != nil { + return nil, err + } + } + return nodes, nil +} + +// SaveX is like Save, but panics if an error occurs. +func (aaccb *AppAppCategoryCreateBulk) SaveX(ctx context.Context) []*AppAppCategory { + v, err := aaccb.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (aaccb *AppAppCategoryCreateBulk) Exec(ctx context.Context) error { + _, err := aaccb.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (aaccb *AppAppCategoryCreateBulk) ExecX(ctx context.Context) { + if err := aaccb.Exec(ctx); err != nil { + panic(err) + } +} + +// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause +// of the `INSERT` statement. For example: +// +// client.AppAppCategory.CreateBulk(builders...). +// OnConflict( +// // Update the row with the new values +// // the was proposed for insertion. +// sql.ResolveWithNewValues(), +// ). +// // Override some of the fields with custom +// // update values. +// Update(func(u *ent.AppAppCategoryUpsert) { +// SetAppCategoryID(v+v). +// }). +// Exec(ctx) +func (aaccb *AppAppCategoryCreateBulk) OnConflict(opts ...sql.ConflictOption) *AppAppCategoryUpsertBulk { + aaccb.conflict = opts + return &AppAppCategoryUpsertBulk{ + create: aaccb, + } +} + +// OnConflictColumns calls `OnConflict` and configures the columns +// as conflict target. Using this option is equivalent to using: +// +// client.AppAppCategory.Create(). +// OnConflict(sql.ConflictColumns(columns...)). +// Exec(ctx) +func (aaccb *AppAppCategoryCreateBulk) OnConflictColumns(columns ...string) *AppAppCategoryUpsertBulk { + aaccb.conflict = append(aaccb.conflict, sql.ConflictColumns(columns...)) + return &AppAppCategoryUpsertBulk{ + create: aaccb, + } +} + +// AppAppCategoryUpsertBulk is the builder for "upsert"-ing +// a bulk of AppAppCategory nodes. +type AppAppCategoryUpsertBulk struct { + create *AppAppCategoryCreateBulk +} + +// UpdateNewValues updates the mutable fields using the new values that +// were set on create. Using this option is equivalent to using: +// +// client.AppAppCategory.Create(). +// OnConflict( +// sql.ResolveWithNewValues(), +// ). +// Exec(ctx) +func (u *AppAppCategoryUpsertBulk) UpdateNewValues() *AppAppCategoryUpsertBulk { + u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) + return u +} + +// Ignore sets each column to itself in case of conflict. +// Using this option is equivalent to using: +// +// client.AppAppCategory.Create(). +// OnConflict(sql.ResolveWithIgnore()). +// Exec(ctx) +func (u *AppAppCategoryUpsertBulk) Ignore() *AppAppCategoryUpsertBulk { + u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) + return u +} + +// DoNothing configures the conflict_action to `DO NOTHING`. +// Supported only by SQLite and PostgreSQL. +func (u *AppAppCategoryUpsertBulk) DoNothing() *AppAppCategoryUpsertBulk { + u.create.conflict = append(u.create.conflict, sql.DoNothing()) + return u +} + +// Update allows overriding fields `UPDATE` values. See the AppAppCategoryCreateBulk.OnConflict +// documentation for more info. +func (u *AppAppCategoryUpsertBulk) Update(set func(*AppAppCategoryUpsert)) *AppAppCategoryUpsertBulk { + u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { + set(&AppAppCategoryUpsert{UpdateSet: update}) + })) + return u +} + +// SetAppCategoryID sets the "app_category_id" field. +func (u *AppAppCategoryUpsertBulk) SetAppCategoryID(v model.InternalID) *AppAppCategoryUpsertBulk { + return u.Update(func(s *AppAppCategoryUpsert) { + s.SetAppCategoryID(v) + }) +} + +// UpdateAppCategoryID sets the "app_category_id" field to the value that was provided on create. +func (u *AppAppCategoryUpsertBulk) UpdateAppCategoryID() *AppAppCategoryUpsertBulk { + return u.Update(func(s *AppAppCategoryUpsert) { + s.UpdateAppCategoryID() + }) +} + +// SetAppID sets the "app_id" field. +func (u *AppAppCategoryUpsertBulk) SetAppID(v model.InternalID) *AppAppCategoryUpsertBulk { + return u.Update(func(s *AppAppCategoryUpsert) { + s.SetAppID(v) + }) +} + +// UpdateAppID sets the "app_id" field to the value that was provided on create. +func (u *AppAppCategoryUpsertBulk) UpdateAppID() *AppAppCategoryUpsertBulk { + return u.Update(func(s *AppAppCategoryUpsert) { + s.UpdateAppID() + }) +} + +// Exec executes the query. +func (u *AppAppCategoryUpsertBulk) Exec(ctx context.Context) error { + if u.create.err != nil { + return u.create.err + } + for i, b := range u.create.builders { + if len(b.conflict) != 0 { + return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the AppAppCategoryCreateBulk instead", i) + } + } + if len(u.create.conflict) == 0 { + return errors.New("ent: missing options for AppAppCategoryCreateBulk.OnConflict") + } + return u.create.Exec(ctx) +} + +// ExecX is like Exec, but panics if an error occurs. +func (u *AppAppCategoryUpsertBulk) ExecX(ctx context.Context) { + if err := u.create.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/data/internal/ent/appappcategory_delete.go b/internal/data/internal/ent/appappcategory_delete.go new file mode 100644 index 00000000..ea9e83f5 --- /dev/null +++ b/internal/data/internal/ent/appappcategory_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" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/predicate" +) + +// AppAppCategoryDelete is the builder for deleting a AppAppCategory entity. +type AppAppCategoryDelete struct { + config + hooks []Hook + mutation *AppAppCategoryMutation +} + +// Where appends a list predicates to the AppAppCategoryDelete builder. +func (aacd *AppAppCategoryDelete) Where(ps ...predicate.AppAppCategory) *AppAppCategoryDelete { + aacd.mutation.Where(ps...) + return aacd +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (aacd *AppAppCategoryDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, aacd.sqlExec, aacd.mutation, aacd.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (aacd *AppAppCategoryDelete) ExecX(ctx context.Context) int { + n, err := aacd.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (aacd *AppAppCategoryDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(appappcategory.Table, sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt)) + if ps := aacd.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, aacd.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + aacd.mutation.done = true + return affected, err +} + +// AppAppCategoryDeleteOne is the builder for deleting a single AppAppCategory entity. +type AppAppCategoryDeleteOne struct { + aacd *AppAppCategoryDelete +} + +// Where appends a list predicates to the AppAppCategoryDelete builder. +func (aacdo *AppAppCategoryDeleteOne) Where(ps ...predicate.AppAppCategory) *AppAppCategoryDeleteOne { + aacdo.aacd.mutation.Where(ps...) + return aacdo +} + +// Exec executes the deletion query. +func (aacdo *AppAppCategoryDeleteOne) Exec(ctx context.Context) error { + n, err := aacdo.aacd.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{appappcategory.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (aacdo *AppAppCategoryDeleteOne) ExecX(ctx context.Context) { + if err := aacdo.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/data/internal/ent/appappcategory_query.go b/internal/data/internal/ent/appappcategory_query.go new file mode 100644 index 00000000..484e3298 --- /dev/null +++ b/internal/data/internal/ent/appappcategory_query.go @@ -0,0 +1,682 @@ +// 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" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/predicate" + "github.com/tuihub/librarian/internal/model" +) + +// AppAppCategoryQuery is the builder for querying AppAppCategory entities. +type AppAppCategoryQuery struct { + config + ctx *QueryContext + order []appappcategory.OrderOption + inters []Interceptor + predicates []predicate.AppAppCategory + withAppCategory *AppCategoryQuery + withApp *AppQuery + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the AppAppCategoryQuery builder. +func (aacq *AppAppCategoryQuery) Where(ps ...predicate.AppAppCategory) *AppAppCategoryQuery { + aacq.predicates = append(aacq.predicates, ps...) + return aacq +} + +// Limit the number of records to be returned by this query. +func (aacq *AppAppCategoryQuery) Limit(limit int) *AppAppCategoryQuery { + aacq.ctx.Limit = &limit + return aacq +} + +// Offset to start from. +func (aacq *AppAppCategoryQuery) Offset(offset int) *AppAppCategoryQuery { + aacq.ctx.Offset = &offset + return aacq +} + +// 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 (aacq *AppAppCategoryQuery) Unique(unique bool) *AppAppCategoryQuery { + aacq.ctx.Unique = &unique + return aacq +} + +// Order specifies how the records should be ordered. +func (aacq *AppAppCategoryQuery) Order(o ...appappcategory.OrderOption) *AppAppCategoryQuery { + aacq.order = append(aacq.order, o...) + return aacq +} + +// QueryAppCategory chains the current query on the "app_category" edge. +func (aacq *AppAppCategoryQuery) QueryAppCategory() *AppCategoryQuery { + query := (&AppCategoryClient{config: aacq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := aacq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := aacq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(appappcategory.Table, appappcategory.FieldID, selector), + sqlgraph.To(appcategory.Table, appcategory.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, appappcategory.AppCategoryTable, appappcategory.AppCategoryColumn), + ) + fromU = sqlgraph.SetNeighbors(aacq.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// QueryApp chains the current query on the "app" edge. +func (aacq *AppAppCategoryQuery) QueryApp() *AppQuery { + query := (&AppClient{config: aacq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := aacq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := aacq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(appappcategory.Table, appappcategory.FieldID, selector), + sqlgraph.To(app.Table, app.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, appappcategory.AppTable, appappcategory.AppColumn), + ) + fromU = sqlgraph.SetNeighbors(aacq.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// First returns the first AppAppCategory entity from the query. +// Returns a *NotFoundError when no AppAppCategory was found. +func (aacq *AppAppCategoryQuery) First(ctx context.Context) (*AppAppCategory, error) { + nodes, err := aacq.Limit(1).All(setContextOp(ctx, aacq.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{appappcategory.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) FirstX(ctx context.Context) *AppAppCategory { + node, err := aacq.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first AppAppCategory ID from the query. +// Returns a *NotFoundError when no AppAppCategory ID was found. +func (aacq *AppAppCategoryQuery) FirstID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = aacq.Limit(1).IDs(setContextOp(ctx, aacq.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{appappcategory.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) FirstIDX(ctx context.Context) int { + id, err := aacq.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single AppAppCategory entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one AppAppCategory entity is found. +// Returns a *NotFoundError when no AppAppCategory entities are found. +func (aacq *AppAppCategoryQuery) Only(ctx context.Context) (*AppAppCategory, error) { + nodes, err := aacq.Limit(2).All(setContextOp(ctx, aacq.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{appappcategory.Label} + default: + return nil, &NotSingularError{appappcategory.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) OnlyX(ctx context.Context) *AppAppCategory { + node, err := aacq.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only AppAppCategory ID in the query. +// Returns a *NotSingularError when more than one AppAppCategory ID is found. +// Returns a *NotFoundError when no entities are found. +func (aacq *AppAppCategoryQuery) OnlyID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = aacq.Limit(2).IDs(setContextOp(ctx, aacq.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{appappcategory.Label} + default: + err = &NotSingularError{appappcategory.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) OnlyIDX(ctx context.Context) int { + id, err := aacq.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of AppAppCategories. +func (aacq *AppAppCategoryQuery) All(ctx context.Context) ([]*AppAppCategory, error) { + ctx = setContextOp(ctx, aacq.ctx, ent.OpQueryAll) + if err := aacq.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*AppAppCategory, *AppAppCategoryQuery]() + return withInterceptors[[]*AppAppCategory](ctx, aacq, qr, aacq.inters) +} + +// AllX is like All, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) AllX(ctx context.Context) []*AppAppCategory { + nodes, err := aacq.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of AppAppCategory IDs. +func (aacq *AppAppCategoryQuery) IDs(ctx context.Context) (ids []int, err error) { + if aacq.ctx.Unique == nil && aacq.path != nil { + aacq.Unique(true) + } + ctx = setContextOp(ctx, aacq.ctx, ent.OpQueryIDs) + if err = aacq.Select(appappcategory.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) IDsX(ctx context.Context) []int { + ids, err := aacq.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (aacq *AppAppCategoryQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, aacq.ctx, ent.OpQueryCount) + if err := aacq.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, aacq, querierCount[*AppAppCategoryQuery](), aacq.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (aacq *AppAppCategoryQuery) CountX(ctx context.Context) int { + count, err := aacq.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (aacq *AppAppCategoryQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, aacq.ctx, ent.OpQueryExist) + switch _, err := aacq.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 (aacq *AppAppCategoryQuery) ExistX(ctx context.Context) bool { + exist, err := aacq.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the AppAppCategoryQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (aacq *AppAppCategoryQuery) Clone() *AppAppCategoryQuery { + if aacq == nil { + return nil + } + return &AppAppCategoryQuery{ + config: aacq.config, + ctx: aacq.ctx.Clone(), + order: append([]appappcategory.OrderOption{}, aacq.order...), + inters: append([]Interceptor{}, aacq.inters...), + predicates: append([]predicate.AppAppCategory{}, aacq.predicates...), + withAppCategory: aacq.withAppCategory.Clone(), + withApp: aacq.withApp.Clone(), + // clone intermediate query. + sql: aacq.sql.Clone(), + path: aacq.path, + } +} + +// WithAppCategory tells the query-builder to eager-load the nodes that are connected to +// the "app_category" edge. The optional arguments are used to configure the query builder of the edge. +func (aacq *AppAppCategoryQuery) WithAppCategory(opts ...func(*AppCategoryQuery)) *AppAppCategoryQuery { + query := (&AppCategoryClient{config: aacq.config}).Query() + for _, opt := range opts { + opt(query) + } + aacq.withAppCategory = query + return aacq +} + +// WithApp tells the query-builder to eager-load the nodes that are connected to +// the "app" edge. The optional arguments are used to configure the query builder of the edge. +func (aacq *AppAppCategoryQuery) WithApp(opts ...func(*AppQuery)) *AppAppCategoryQuery { + query := (&AppClient{config: aacq.config}).Query() + for _, opt := range opts { + opt(query) + } + aacq.withApp = query + return aacq +} + +// 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 { +// AppCategoryID model.InternalID `json:"app_category_id,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.AppAppCategory.Query(). +// GroupBy(appappcategory.FieldAppCategoryID). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (aacq *AppAppCategoryQuery) GroupBy(field string, fields ...string) *AppAppCategoryGroupBy { + aacq.ctx.Fields = append([]string{field}, fields...) + grbuild := &AppAppCategoryGroupBy{build: aacq} + grbuild.flds = &aacq.ctx.Fields + grbuild.label = appappcategory.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 { +// AppCategoryID model.InternalID `json:"app_category_id,omitempty"` +// } +// +// client.AppAppCategory.Query(). +// Select(appappcategory.FieldAppCategoryID). +// Scan(ctx, &v) +func (aacq *AppAppCategoryQuery) Select(fields ...string) *AppAppCategorySelect { + aacq.ctx.Fields = append(aacq.ctx.Fields, fields...) + sbuild := &AppAppCategorySelect{AppAppCategoryQuery: aacq} + sbuild.label = appappcategory.Label + sbuild.flds, sbuild.scan = &aacq.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a AppAppCategorySelect configured with the given aggregations. +func (aacq *AppAppCategoryQuery) Aggregate(fns ...AggregateFunc) *AppAppCategorySelect { + return aacq.Select().Aggregate(fns...) +} + +func (aacq *AppAppCategoryQuery) prepareQuery(ctx context.Context) error { + for _, inter := range aacq.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, aacq); err != nil { + return err + } + } + } + for _, f := range aacq.ctx.Fields { + if !appappcategory.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if aacq.path != nil { + prev, err := aacq.path(ctx) + if err != nil { + return err + } + aacq.sql = prev + } + return nil +} + +func (aacq *AppAppCategoryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*AppAppCategory, error) { + var ( + nodes = []*AppAppCategory{} + _spec = aacq.querySpec() + loadedTypes = [2]bool{ + aacq.withAppCategory != nil, + aacq.withApp != nil, + } + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*AppAppCategory).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &AppAppCategory{config: aacq.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, aacq.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + if query := aacq.withAppCategory; query != nil { + if err := aacq.loadAppCategory(ctx, query, nodes, nil, + func(n *AppAppCategory, e *AppCategory) { n.Edges.AppCategory = e }); err != nil { + return nil, err + } + } + if query := aacq.withApp; query != nil { + if err := aacq.loadApp(ctx, query, nodes, nil, + func(n *AppAppCategory, e *App) { n.Edges.App = e }); err != nil { + return nil, err + } + } + return nodes, nil +} + +func (aacq *AppAppCategoryQuery) loadAppCategory(ctx context.Context, query *AppCategoryQuery, nodes []*AppAppCategory, init func(*AppAppCategory), assign func(*AppAppCategory, *AppCategory)) error { + ids := make([]model.InternalID, 0, len(nodes)) + nodeids := make(map[model.InternalID][]*AppAppCategory) + for i := range nodes { + fk := nodes[i].AppCategoryID + if _, ok := nodeids[fk]; !ok { + ids = append(ids, fk) + } + nodeids[fk] = append(nodeids[fk], nodes[i]) + } + if len(ids) == 0 { + return nil + } + query.Where(appcategory.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 "app_category_id" returned %v`, n.ID) + } + for i := range nodes { + assign(nodes[i], n) + } + } + return nil +} +func (aacq *AppAppCategoryQuery) loadApp(ctx context.Context, query *AppQuery, nodes []*AppAppCategory, init func(*AppAppCategory), assign func(*AppAppCategory, *App)) error { + ids := make([]model.InternalID, 0, len(nodes)) + nodeids := make(map[model.InternalID][]*AppAppCategory) + for i := range nodes { + fk := nodes[i].AppID + if _, ok := nodeids[fk]; !ok { + ids = append(ids, fk) + } + nodeids[fk] = append(nodeids[fk], nodes[i]) + } + if len(ids) == 0 { + return nil + } + query.Where(app.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 "app_id" returned %v`, n.ID) + } + for i := range nodes { + assign(nodes[i], n) + } + } + return nil +} + +func (aacq *AppAppCategoryQuery) sqlCount(ctx context.Context) (int, error) { + _spec := aacq.querySpec() + _spec.Node.Columns = aacq.ctx.Fields + if len(aacq.ctx.Fields) > 0 { + _spec.Unique = aacq.ctx.Unique != nil && *aacq.ctx.Unique + } + return sqlgraph.CountNodes(ctx, aacq.driver, _spec) +} + +func (aacq *AppAppCategoryQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(appappcategory.Table, appappcategory.Columns, sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt)) + _spec.From = aacq.sql + if unique := aacq.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if aacq.path != nil { + _spec.Unique = true + } + if fields := aacq.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, appappcategory.FieldID) + for i := range fields { + if fields[i] != appappcategory.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + if aacq.withAppCategory != nil { + _spec.Node.AddColumnOnce(appappcategory.FieldAppCategoryID) + } + if aacq.withApp != nil { + _spec.Node.AddColumnOnce(appappcategory.FieldAppID) + } + } + if ps := aacq.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := aacq.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := aacq.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := aacq.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (aacq *AppAppCategoryQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(aacq.driver.Dialect()) + t1 := builder.Table(appappcategory.Table) + columns := aacq.ctx.Fields + if len(columns) == 0 { + columns = appappcategory.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if aacq.sql != nil { + selector = aacq.sql + selector.Select(selector.Columns(columns...)...) + } + if aacq.ctx.Unique != nil && *aacq.ctx.Unique { + selector.Distinct() + } + for _, p := range aacq.predicates { + p(selector) + } + for _, p := range aacq.order { + p(selector) + } + if offset := aacq.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 := aacq.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// AppAppCategoryGroupBy is the group-by builder for AppAppCategory entities. +type AppAppCategoryGroupBy struct { + selector + build *AppAppCategoryQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (aacgb *AppAppCategoryGroupBy) Aggregate(fns ...AggregateFunc) *AppAppCategoryGroupBy { + aacgb.fns = append(aacgb.fns, fns...) + return aacgb +} + +// Scan applies the selector query and scans the result into the given value. +func (aacgb *AppAppCategoryGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, aacgb.build.ctx, ent.OpQueryGroupBy) + if err := aacgb.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*AppAppCategoryQuery, *AppAppCategoryGroupBy](ctx, aacgb.build, aacgb, aacgb.build.inters, v) +} + +func (aacgb *AppAppCategoryGroupBy) sqlScan(ctx context.Context, root *AppAppCategoryQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(aacgb.fns)) + for _, fn := range aacgb.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*aacgb.flds)+len(aacgb.fns)) + for _, f := range *aacgb.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*aacgb.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := aacgb.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// AppAppCategorySelect is the builder for selecting fields of AppAppCategory entities. +type AppAppCategorySelect struct { + *AppAppCategoryQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (aacs *AppAppCategorySelect) Aggregate(fns ...AggregateFunc) *AppAppCategorySelect { + aacs.fns = append(aacs.fns, fns...) + return aacs +} + +// Scan applies the selector query and scans the result into the given value. +func (aacs *AppAppCategorySelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, aacs.ctx, ent.OpQuerySelect) + if err := aacs.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*AppAppCategoryQuery, *AppAppCategorySelect](ctx, aacs.AppAppCategoryQuery, aacs, aacs.inters, v) +} + +func (aacs *AppAppCategorySelect) sqlScan(ctx context.Context, root *AppAppCategoryQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(aacs.fns)) + for _, fn := range aacs.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*aacs.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 := aacs.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/data/internal/ent/appappcategory_update.go b/internal/data/internal/ent/appappcategory_update.go new file mode 100644 index 00000000..6295a8f5 --- /dev/null +++ b/internal/data/internal/ent/appappcategory_update.go @@ -0,0 +1,422 @@ +// 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" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" + "github.com/tuihub/librarian/internal/data/internal/ent/predicate" + "github.com/tuihub/librarian/internal/model" +) + +// AppAppCategoryUpdate is the builder for updating AppAppCategory entities. +type AppAppCategoryUpdate struct { + config + hooks []Hook + mutation *AppAppCategoryMutation +} + +// Where appends a list predicates to the AppAppCategoryUpdate builder. +func (aacu *AppAppCategoryUpdate) Where(ps ...predicate.AppAppCategory) *AppAppCategoryUpdate { + aacu.mutation.Where(ps...) + return aacu +} + +// SetAppCategoryID sets the "app_category_id" field. +func (aacu *AppAppCategoryUpdate) SetAppCategoryID(mi model.InternalID) *AppAppCategoryUpdate { + aacu.mutation.SetAppCategoryID(mi) + return aacu +} + +// SetNillableAppCategoryID sets the "app_category_id" field if the given value is not nil. +func (aacu *AppAppCategoryUpdate) SetNillableAppCategoryID(mi *model.InternalID) *AppAppCategoryUpdate { + if mi != nil { + aacu.SetAppCategoryID(*mi) + } + return aacu +} + +// SetAppID sets the "app_id" field. +func (aacu *AppAppCategoryUpdate) SetAppID(mi model.InternalID) *AppAppCategoryUpdate { + aacu.mutation.SetAppID(mi) + return aacu +} + +// SetNillableAppID sets the "app_id" field if the given value is not nil. +func (aacu *AppAppCategoryUpdate) SetNillableAppID(mi *model.InternalID) *AppAppCategoryUpdate { + if mi != nil { + aacu.SetAppID(*mi) + } + return aacu +} + +// SetAppCategory sets the "app_category" edge to the AppCategory entity. +func (aacu *AppAppCategoryUpdate) SetAppCategory(a *AppCategory) *AppAppCategoryUpdate { + return aacu.SetAppCategoryID(a.ID) +} + +// SetApp sets the "app" edge to the App entity. +func (aacu *AppAppCategoryUpdate) SetApp(a *App) *AppAppCategoryUpdate { + return aacu.SetAppID(a.ID) +} + +// Mutation returns the AppAppCategoryMutation object of the builder. +func (aacu *AppAppCategoryUpdate) Mutation() *AppAppCategoryMutation { + return aacu.mutation +} + +// ClearAppCategory clears the "app_category" edge to the AppCategory entity. +func (aacu *AppAppCategoryUpdate) ClearAppCategory() *AppAppCategoryUpdate { + aacu.mutation.ClearAppCategory() + return aacu +} + +// ClearApp clears the "app" edge to the App entity. +func (aacu *AppAppCategoryUpdate) ClearApp() *AppAppCategoryUpdate { + aacu.mutation.ClearApp() + return aacu +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (aacu *AppAppCategoryUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, aacu.sqlSave, aacu.mutation, aacu.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (aacu *AppAppCategoryUpdate) SaveX(ctx context.Context) int { + affected, err := aacu.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (aacu *AppAppCategoryUpdate) Exec(ctx context.Context) error { + _, err := aacu.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (aacu *AppAppCategoryUpdate) ExecX(ctx context.Context) { + if err := aacu.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (aacu *AppAppCategoryUpdate) check() error { + if aacu.mutation.AppCategoryCleared() && len(aacu.mutation.AppCategoryIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "AppAppCategory.app_category"`) + } + if aacu.mutation.AppCleared() && len(aacu.mutation.AppIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "AppAppCategory.app"`) + } + return nil +} + +func (aacu *AppAppCategoryUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := aacu.check(); err != nil { + return n, err + } + _spec := sqlgraph.NewUpdateSpec(appappcategory.Table, appappcategory.Columns, sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt)) + if ps := aacu.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if aacu.mutation.AppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppCategoryTable, + Columns: []string{appappcategory.AppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := aacu.mutation.AppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppCategoryTable, + Columns: []string{appappcategory.AppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if aacu.mutation.AppCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppTable, + Columns: []string{appappcategory.AppColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := aacu.mutation.AppIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppTable, + Columns: []string{appappcategory.AppColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if n, err = sqlgraph.UpdateNodes(ctx, aacu.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{appappcategory.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + aacu.mutation.done = true + return n, nil +} + +// AppAppCategoryUpdateOne is the builder for updating a single AppAppCategory entity. +type AppAppCategoryUpdateOne struct { + config + fields []string + hooks []Hook + mutation *AppAppCategoryMutation +} + +// SetAppCategoryID sets the "app_category_id" field. +func (aacuo *AppAppCategoryUpdateOne) SetAppCategoryID(mi model.InternalID) *AppAppCategoryUpdateOne { + aacuo.mutation.SetAppCategoryID(mi) + return aacuo +} + +// SetNillableAppCategoryID sets the "app_category_id" field if the given value is not nil. +func (aacuo *AppAppCategoryUpdateOne) SetNillableAppCategoryID(mi *model.InternalID) *AppAppCategoryUpdateOne { + if mi != nil { + aacuo.SetAppCategoryID(*mi) + } + return aacuo +} + +// SetAppID sets the "app_id" field. +func (aacuo *AppAppCategoryUpdateOne) SetAppID(mi model.InternalID) *AppAppCategoryUpdateOne { + aacuo.mutation.SetAppID(mi) + return aacuo +} + +// SetNillableAppID sets the "app_id" field if the given value is not nil. +func (aacuo *AppAppCategoryUpdateOne) SetNillableAppID(mi *model.InternalID) *AppAppCategoryUpdateOne { + if mi != nil { + aacuo.SetAppID(*mi) + } + return aacuo +} + +// SetAppCategory sets the "app_category" edge to the AppCategory entity. +func (aacuo *AppAppCategoryUpdateOne) SetAppCategory(a *AppCategory) *AppAppCategoryUpdateOne { + return aacuo.SetAppCategoryID(a.ID) +} + +// SetApp sets the "app" edge to the App entity. +func (aacuo *AppAppCategoryUpdateOne) SetApp(a *App) *AppAppCategoryUpdateOne { + return aacuo.SetAppID(a.ID) +} + +// Mutation returns the AppAppCategoryMutation object of the builder. +func (aacuo *AppAppCategoryUpdateOne) Mutation() *AppAppCategoryMutation { + return aacuo.mutation +} + +// ClearAppCategory clears the "app_category" edge to the AppCategory entity. +func (aacuo *AppAppCategoryUpdateOne) ClearAppCategory() *AppAppCategoryUpdateOne { + aacuo.mutation.ClearAppCategory() + return aacuo +} + +// ClearApp clears the "app" edge to the App entity. +func (aacuo *AppAppCategoryUpdateOne) ClearApp() *AppAppCategoryUpdateOne { + aacuo.mutation.ClearApp() + return aacuo +} + +// Where appends a list predicates to the AppAppCategoryUpdate builder. +func (aacuo *AppAppCategoryUpdateOne) Where(ps ...predicate.AppAppCategory) *AppAppCategoryUpdateOne { + aacuo.mutation.Where(ps...) + return aacuo +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (aacuo *AppAppCategoryUpdateOne) Select(field string, fields ...string) *AppAppCategoryUpdateOne { + aacuo.fields = append([]string{field}, fields...) + return aacuo +} + +// Save executes the query and returns the updated AppAppCategory entity. +func (aacuo *AppAppCategoryUpdateOne) Save(ctx context.Context) (*AppAppCategory, error) { + return withHooks(ctx, aacuo.sqlSave, aacuo.mutation, aacuo.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (aacuo *AppAppCategoryUpdateOne) SaveX(ctx context.Context) *AppAppCategory { + node, err := aacuo.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (aacuo *AppAppCategoryUpdateOne) Exec(ctx context.Context) error { + _, err := aacuo.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (aacuo *AppAppCategoryUpdateOne) ExecX(ctx context.Context) { + if err := aacuo.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (aacuo *AppAppCategoryUpdateOne) check() error { + if aacuo.mutation.AppCategoryCleared() && len(aacuo.mutation.AppCategoryIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "AppAppCategory.app_category"`) + } + if aacuo.mutation.AppCleared() && len(aacuo.mutation.AppIDs()) > 0 { + return errors.New(`ent: clearing a required unique edge "AppAppCategory.app"`) + } + return nil +} + +func (aacuo *AppAppCategoryUpdateOne) sqlSave(ctx context.Context) (_node *AppAppCategory, err error) { + if err := aacuo.check(); err != nil { + return _node, err + } + _spec := sqlgraph.NewUpdateSpec(appappcategory.Table, appappcategory.Columns, sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt)) + id, ok := aacuo.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AppAppCategory.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := aacuo.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, appappcategory.FieldID) + for _, f := range fields { + if !appappcategory.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != appappcategory.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := aacuo.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if aacuo.mutation.AppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppCategoryTable, + Columns: []string{appappcategory.AppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := aacuo.mutation.AppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppCategoryTable, + Columns: []string{appappcategory.AppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if aacuo.mutation.AppCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppTable, + Columns: []string{appappcategory.AppColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := aacuo.mutation.AppIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2O, + Inverse: false, + Table: appappcategory.AppTable, + Columns: []string{appappcategory.AppColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + _node = &AppAppCategory{config: aacuo.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, aacuo.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{appappcategory.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + aacuo.mutation.done = true + return _node, nil +} diff --git a/internal/data/internal/ent/appcategory.go b/internal/data/internal/ent/appcategory.go index bcdc0ac4..8a2a46de 100644 --- a/internal/data/internal/ent/appcategory.go +++ b/internal/data/internal/ent/appcategory.go @@ -17,30 +17,64 @@ import ( type AppCategory struct { config `json:"-"` // ID of the ent. - ID int `json:"id,omitempty"` + ID model.InternalID `json:"id,omitempty"` // UserID holds the value of the "user_id" field. UserID model.InternalID `json:"user_id,omitempty"` - // AppID holds the value of the "app_id" field. - AppID model.InternalID `json:"app_id,omitempty"` - // StartTime holds the value of the "start_time" field. - StartTime time.Time `json:"start_time,omitempty"` - // RunDuration holds the value of the "run_duration" field. - RunDuration time.Duration `json:"run_duration,omitempty"` + // VersionNumber holds the value of the "version_number" field. + VersionNumber uint64 `json:"version_number,omitempty"` + // VersionDate holds the value of the "version_date" field. + VersionDate time.Time `json:"version_date,omitempty"` + // Name holds the value of the "name" field. + Name string `json:"name,omitempty"` // UpdatedAt holds the value of the "updated_at" field. UpdatedAt time.Time `json:"updated_at,omitempty"` // CreatedAt holds the value of the "created_at" field. - CreatedAt time.Time `json:"created_at,omitempty"` + 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 AppCategoryQuery when eager-loading is set. + Edges AppCategoryEdges `json:"edges"` selectValues sql.SelectValues } +// AppCategoryEdges holds the relations/edges for other nodes in the graph. +type AppCategoryEdges struct { + // App holds the value of the app edge. + App []*App `json:"app,omitempty"` + // AppAppCategory holds the value of the app_app_category edge. + AppAppCategory []*AppAppCategory `json:"app_app_category,omitempty"` + // loadedTypes holds the information for reporting if a + // type was loaded (or requested) in eager-loading or not. + loadedTypes [2]bool +} + +// AppOrErr returns the App value or an error if the edge +// was not loaded in eager-loading. +func (e AppCategoryEdges) AppOrErr() ([]*App, error) { + if e.loadedTypes[0] { + return e.App, nil + } + return nil, &NotLoadedError{edge: "app"} +} + +// AppAppCategoryOrErr returns the AppAppCategory value or an error if the edge +// was not loaded in eager-loading. +func (e AppCategoryEdges) AppAppCategoryOrErr() ([]*AppAppCategory, error) { + if e.loadedTypes[1] { + return e.AppAppCategory, nil + } + return nil, &NotLoadedError{edge: "app_app_category"} +} + // scanValues returns the types for scanning values from sql.Rows. func (*AppCategory) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case appcategory.FieldID, appcategory.FieldUserID, appcategory.FieldAppID, appcategory.FieldRunDuration: + case appcategory.FieldID, appcategory.FieldUserID, appcategory.FieldVersionNumber: values[i] = new(sql.NullInt64) - case appcategory.FieldStartTime, appcategory.FieldUpdatedAt, appcategory.FieldCreatedAt: + case appcategory.FieldName: + values[i] = new(sql.NullString) + case appcategory.FieldVersionDate, appcategory.FieldUpdatedAt, appcategory.FieldCreatedAt: values[i] = new(sql.NullTime) default: values[i] = new(sql.UnknownType) @@ -58,34 +92,34 @@ func (ac *AppCategory) assignValues(columns []string, values []any) error { for i := range columns { switch columns[i] { case appcategory.FieldID: - value, ok := values[i].(*sql.NullInt64) - if !ok { - return fmt.Errorf("unexpected type %T for field id", value) + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value.Valid { + ac.ID = model.InternalID(value.Int64) } - ac.ID = int(value.Int64) case appcategory.FieldUserID: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field user_id", values[i]) } else if value.Valid { ac.UserID = model.InternalID(value.Int64) } - case appcategory.FieldAppID: + case appcategory.FieldVersionNumber: if value, ok := values[i].(*sql.NullInt64); !ok { - return fmt.Errorf("unexpected type %T for field app_id", values[i]) + return fmt.Errorf("unexpected type %T for field version_number", values[i]) } else if value.Valid { - ac.AppID = model.InternalID(value.Int64) + ac.VersionNumber = uint64(value.Int64) } - case appcategory.FieldStartTime: + case appcategory.FieldVersionDate: if value, ok := values[i].(*sql.NullTime); !ok { - return fmt.Errorf("unexpected type %T for field start_time", values[i]) + return fmt.Errorf("unexpected type %T for field version_date", values[i]) } else if value.Valid { - ac.StartTime = value.Time + ac.VersionDate = value.Time } - case appcategory.FieldRunDuration: - if value, ok := values[i].(*sql.NullInt64); !ok { - return fmt.Errorf("unexpected type %T for field run_duration", values[i]) + case appcategory.FieldName: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field name", values[i]) } else if value.Valid { - ac.RunDuration = time.Duration(value.Int64) + ac.Name = value.String } case appcategory.FieldUpdatedAt: if value, ok := values[i].(*sql.NullTime); !ok { @@ -112,6 +146,16 @@ func (ac *AppCategory) Value(name string) (ent.Value, error) { return ac.selectValues.Get(name) } +// QueryApp queries the "app" edge of the AppCategory entity. +func (ac *AppCategory) QueryApp() *AppQuery { + return NewAppCategoryClient(ac.config).QueryApp(ac) +} + +// QueryAppAppCategory queries the "app_app_category" edge of the AppCategory entity. +func (ac *AppCategory) QueryAppAppCategory() *AppAppCategoryQuery { + return NewAppCategoryClient(ac.config).QueryAppAppCategory(ac) +} + // Update returns a builder for updating this AppCategory. // Note that you need to call AppCategory.Unwrap() before calling this method if this AppCategory // was returned from a transaction, and the transaction was committed or rolled back. @@ -138,14 +182,14 @@ func (ac *AppCategory) String() string { builder.WriteString("user_id=") builder.WriteString(fmt.Sprintf("%v", ac.UserID)) builder.WriteString(", ") - builder.WriteString("app_id=") - builder.WriteString(fmt.Sprintf("%v", ac.AppID)) + builder.WriteString("version_number=") + builder.WriteString(fmt.Sprintf("%v", ac.VersionNumber)) builder.WriteString(", ") - builder.WriteString("start_time=") - builder.WriteString(ac.StartTime.Format(time.ANSIC)) + builder.WriteString("version_date=") + builder.WriteString(ac.VersionDate.Format(time.ANSIC)) builder.WriteString(", ") - builder.WriteString("run_duration=") - builder.WriteString(fmt.Sprintf("%v", ac.RunDuration)) + builder.WriteString("name=") + builder.WriteString(ac.Name) builder.WriteString(", ") builder.WriteString("updated_at=") builder.WriteString(ac.UpdatedAt.Format(time.ANSIC)) diff --git a/internal/data/internal/ent/appcategory/appcategory.go b/internal/data/internal/ent/appcategory/appcategory.go index 18a09b4f..55ba5171 100644 --- a/internal/data/internal/ent/appcategory/appcategory.go +++ b/internal/data/internal/ent/appcategory/appcategory.go @@ -6,6 +6,7 @@ import ( "time" "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" ) const ( @@ -15,31 +16,53 @@ const ( FieldID = "id" // FieldUserID holds the string denoting the user_id field in the database. FieldUserID = "user_id" - // FieldAppID holds the string denoting the app_id field in the database. - FieldAppID = "app_id" - // FieldStartTime holds the string denoting the start_time field in the database. - FieldStartTime = "start_time" - // FieldRunDuration holds the string denoting the run_duration field in the database. - FieldRunDuration = "run_duration" + // FieldVersionNumber holds the string denoting the version_number field in the database. + FieldVersionNumber = "version_number" + // FieldVersionDate holds the string denoting the version_date field in the database. + FieldVersionDate = "version_date" + // FieldName holds the string denoting the name field in the database. + FieldName = "name" // FieldUpdatedAt holds the string denoting the updated_at field in the database. FieldUpdatedAt = "updated_at" // FieldCreatedAt holds the string denoting the created_at field in the database. FieldCreatedAt = "created_at" + // EdgeApp holds the string denoting the app edge name in mutations. + EdgeApp = "app" + // EdgeAppAppCategory holds the string denoting the app_app_category edge name in mutations. + EdgeAppAppCategory = "app_app_category" // Table holds the table name of the appcategory in the database. Table = "app_categories" + // AppTable is the table that holds the app relation/edge. The primary key declared below. + AppTable = "app_app_categories" + // AppInverseTable is the table name for the App entity. + // It exists in this package in order to avoid circular dependency with the "app" package. + AppInverseTable = "apps" + // AppAppCategoryTable is the table that holds the app_app_category relation/edge. + AppAppCategoryTable = "app_app_categories" + // AppAppCategoryInverseTable is the table name for the AppAppCategory entity. + // It exists in this package in order to avoid circular dependency with the "appappcategory" package. + AppAppCategoryInverseTable = "app_app_categories" + // AppAppCategoryColumn is the table column denoting the app_app_category relation/edge. + AppAppCategoryColumn = "app_category_id" ) // Columns holds all SQL columns for appcategory fields. var Columns = []string{ FieldID, FieldUserID, - FieldAppID, - FieldStartTime, - FieldRunDuration, + FieldVersionNumber, + FieldVersionDate, + FieldName, FieldUpdatedAt, FieldCreatedAt, } +var ( + // AppPrimaryKey and AppColumn2 are the table columns denoting the + // primary key for the app relation (M2M). + AppPrimaryKey = []string{"app_category_id", "app_id"} +) + // ValidColumn reports if the column name is valid (part of the table columns). func ValidColumn(column string) bool { for i := range Columns { @@ -72,19 +95,19 @@ func ByUserID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldUserID, opts...).ToFunc() } -// ByAppID orders the results by the app_id field. -func ByAppID(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldAppID, opts...).ToFunc() +// ByVersionNumber orders the results by the version_number field. +func ByVersionNumber(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldVersionNumber, opts...).ToFunc() } -// ByStartTime orders the results by the start_time field. -func ByStartTime(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldStartTime, opts...).ToFunc() +// ByVersionDate orders the results by the version_date field. +func ByVersionDate(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldVersionDate, opts...).ToFunc() } -// ByRunDuration orders the results by the run_duration field. -func ByRunDuration(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldRunDuration, opts...).ToFunc() +// ByName orders the results by the name field. +func ByName(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldName, opts...).ToFunc() } // ByUpdatedAt orders the results by the updated_at field. @@ -96,3 +119,45 @@ func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption { func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() } + +// ByAppCount orders the results by app count. +func ByAppCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newAppStep(), opts...) + } +} + +// ByApp orders the results by app terms. +func ByApp(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newAppStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} + +// ByAppAppCategoryCount orders the results by app_app_category count. +func ByAppAppCategoryCount(opts ...sql.OrderTermOption) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborsCount(s, newAppAppCategoryStep(), opts...) + } +} + +// ByAppAppCategory orders the results by app_app_category terms. +func ByAppAppCategory(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { + return func(s *sql.Selector) { + sqlgraph.OrderByNeighborTerms(s, newAppAppCategoryStep(), append([]sql.OrderTerm{term}, terms...)...) + } +} +func newAppStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(AppInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.M2M, false, AppTable, AppPrimaryKey...), + ) +} +func newAppAppCategoryStep() *sqlgraph.Step { + return sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.To(AppAppCategoryInverseTable, FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, AppAppCategoryTable, AppAppCategoryColumn), + ) +} diff --git a/internal/data/internal/ent/appcategory/where.go b/internal/data/internal/ent/appcategory/where.go index 53622d79..c4dc9fbd 100644 --- a/internal/data/internal/ent/appcategory/where.go +++ b/internal/data/internal/ent/appcategory/where.go @@ -6,52 +6,53 @@ import ( "time" "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" "github.com/tuihub/librarian/internal/data/internal/ent/predicate" "github.com/tuihub/librarian/internal/model" ) // ID filters vertices based on their ID field. -func ID(id int) predicate.AppCategory { +func ID(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldEQ(FieldID, id)) } // IDEQ applies the EQ predicate on the ID field. -func IDEQ(id int) predicate.AppCategory { +func IDEQ(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldEQ(FieldID, id)) } // IDNEQ applies the NEQ predicate on the ID field. -func IDNEQ(id int) predicate.AppCategory { +func IDNEQ(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldNEQ(FieldID, id)) } // IDIn applies the In predicate on the ID field. -func IDIn(ids ...int) predicate.AppCategory { +func IDIn(ids ...model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldIn(FieldID, ids...)) } // IDNotIn applies the NotIn predicate on the ID field. -func IDNotIn(ids ...int) predicate.AppCategory { +func IDNotIn(ids ...model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldNotIn(FieldID, ids...)) } // IDGT applies the GT predicate on the ID field. -func IDGT(id int) predicate.AppCategory { +func IDGT(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldGT(FieldID, id)) } // IDGTE applies the GTE predicate on the ID field. -func IDGTE(id int) predicate.AppCategory { +func IDGTE(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldGTE(FieldID, id)) } // IDLT applies the LT predicate on the ID field. -func IDLT(id int) predicate.AppCategory { +func IDLT(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldLT(FieldID, id)) } // IDLTE applies the LTE predicate on the ID field. -func IDLTE(id int) predicate.AppCategory { +func IDLTE(id model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldLTE(FieldID, id)) } @@ -61,21 +62,19 @@ func UserID(v model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldEQ(FieldUserID, vc)) } -// AppID applies equality check predicate on the "app_id" field. It's identical to AppIDEQ. -func AppID(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldEQ(FieldAppID, vc)) +// VersionNumber applies equality check predicate on the "version_number" field. It's identical to VersionNumberEQ. +func VersionNumber(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEQ(FieldVersionNumber, v)) } -// StartTime applies equality check predicate on the "start_time" field. It's identical to StartTimeEQ. -func StartTime(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldEQ(FieldStartTime, v)) +// VersionDate applies equality check predicate on the "version_date" field. It's identical to VersionDateEQ. +func VersionDate(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEQ(FieldVersionDate, v)) } -// RunDuration applies equality check predicate on the "run_duration" field. It's identical to RunDurationEQ. -func RunDuration(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldEQ(FieldRunDuration, vc)) +// Name applies equality check predicate on the "name" field. It's identical to NameEQ. +func Name(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEQ(FieldName, v)) } // UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ. @@ -142,152 +141,149 @@ func UserIDLTE(v model.InternalID) predicate.AppCategory { return predicate.AppCategory(sql.FieldLTE(FieldUserID, vc)) } -// AppIDEQ applies the EQ predicate on the "app_id" field. -func AppIDEQ(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldEQ(FieldAppID, vc)) +// VersionNumberEQ applies the EQ predicate on the "version_number" field. +func VersionNumberEQ(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEQ(FieldVersionNumber, v)) } -// AppIDNEQ applies the NEQ predicate on the "app_id" field. -func AppIDNEQ(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldNEQ(FieldAppID, vc)) +// VersionNumberNEQ applies the NEQ predicate on the "version_number" field. +func VersionNumberNEQ(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldNEQ(FieldVersionNumber, v)) } -// AppIDIn applies the In predicate on the "app_id" field. -func AppIDIn(vs ...model.InternalID) predicate.AppCategory { - v := make([]any, len(vs)) - for i := range v { - v[i] = int64(vs[i]) - } - return predicate.AppCategory(sql.FieldIn(FieldAppID, v...)) +// VersionNumberIn applies the In predicate on the "version_number" field. +func VersionNumberIn(vs ...uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldIn(FieldVersionNumber, vs...)) } -// AppIDNotIn applies the NotIn predicate on the "app_id" field. -func AppIDNotIn(vs ...model.InternalID) predicate.AppCategory { - v := make([]any, len(vs)) - for i := range v { - v[i] = int64(vs[i]) - } - return predicate.AppCategory(sql.FieldNotIn(FieldAppID, v...)) +// VersionNumberNotIn applies the NotIn predicate on the "version_number" field. +func VersionNumberNotIn(vs ...uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldNotIn(FieldVersionNumber, vs...)) } -// AppIDGT applies the GT predicate on the "app_id" field. -func AppIDGT(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldGT(FieldAppID, vc)) +// VersionNumberGT applies the GT predicate on the "version_number" field. +func VersionNumberGT(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldGT(FieldVersionNumber, v)) } -// AppIDGTE applies the GTE predicate on the "app_id" field. -func AppIDGTE(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldGTE(FieldAppID, vc)) +// VersionNumberGTE applies the GTE predicate on the "version_number" field. +func VersionNumberGTE(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldGTE(FieldVersionNumber, v)) } -// AppIDLT applies the LT predicate on the "app_id" field. -func AppIDLT(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldLT(FieldAppID, vc)) +// VersionNumberLT applies the LT predicate on the "version_number" field. +func VersionNumberLT(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldLT(FieldVersionNumber, v)) } -// AppIDLTE applies the LTE predicate on the "app_id" field. -func AppIDLTE(v model.InternalID) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldLTE(FieldAppID, vc)) +// VersionNumberLTE applies the LTE predicate on the "version_number" field. +func VersionNumberLTE(v uint64) predicate.AppCategory { + return predicate.AppCategory(sql.FieldLTE(FieldVersionNumber, v)) } -// StartTimeEQ applies the EQ predicate on the "start_time" field. -func StartTimeEQ(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldEQ(FieldStartTime, v)) +// VersionDateEQ applies the EQ predicate on the "version_date" field. +func VersionDateEQ(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEQ(FieldVersionDate, v)) } -// StartTimeNEQ applies the NEQ predicate on the "start_time" field. -func StartTimeNEQ(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldNEQ(FieldStartTime, v)) +// VersionDateNEQ applies the NEQ predicate on the "version_date" field. +func VersionDateNEQ(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldNEQ(FieldVersionDate, v)) } -// StartTimeIn applies the In predicate on the "start_time" field. -func StartTimeIn(vs ...time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldIn(FieldStartTime, vs...)) +// VersionDateIn applies the In predicate on the "version_date" field. +func VersionDateIn(vs ...time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldIn(FieldVersionDate, vs...)) } -// StartTimeNotIn applies the NotIn predicate on the "start_time" field. -func StartTimeNotIn(vs ...time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldNotIn(FieldStartTime, vs...)) +// VersionDateNotIn applies the NotIn predicate on the "version_date" field. +func VersionDateNotIn(vs ...time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldNotIn(FieldVersionDate, vs...)) } -// StartTimeGT applies the GT predicate on the "start_time" field. -func StartTimeGT(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldGT(FieldStartTime, v)) +// VersionDateGT applies the GT predicate on the "version_date" field. +func VersionDateGT(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldGT(FieldVersionDate, v)) } -// StartTimeGTE applies the GTE predicate on the "start_time" field. -func StartTimeGTE(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldGTE(FieldStartTime, v)) +// VersionDateGTE applies the GTE predicate on the "version_date" field. +func VersionDateGTE(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldGTE(FieldVersionDate, v)) } -// StartTimeLT applies the LT predicate on the "start_time" field. -func StartTimeLT(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldLT(FieldStartTime, v)) +// VersionDateLT applies the LT predicate on the "version_date" field. +func VersionDateLT(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldLT(FieldVersionDate, v)) } -// StartTimeLTE applies the LTE predicate on the "start_time" field. -func StartTimeLTE(v time.Time) predicate.AppCategory { - return predicate.AppCategory(sql.FieldLTE(FieldStartTime, v)) +// VersionDateLTE applies the LTE predicate on the "version_date" field. +func VersionDateLTE(v time.Time) predicate.AppCategory { + return predicate.AppCategory(sql.FieldLTE(FieldVersionDate, v)) } -// RunDurationEQ applies the EQ predicate on the "run_duration" field. -func RunDurationEQ(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldEQ(FieldRunDuration, vc)) +// NameEQ applies the EQ predicate on the "name" field. +func NameEQ(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEQ(FieldName, v)) } -// RunDurationNEQ applies the NEQ predicate on the "run_duration" field. -func RunDurationNEQ(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldNEQ(FieldRunDuration, vc)) +// NameNEQ applies the NEQ predicate on the "name" field. +func NameNEQ(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldNEQ(FieldName, v)) } -// RunDurationIn applies the In predicate on the "run_duration" field. -func RunDurationIn(vs ...time.Duration) predicate.AppCategory { - v := make([]any, len(vs)) - for i := range v { - v[i] = int64(vs[i]) - } - return predicate.AppCategory(sql.FieldIn(FieldRunDuration, v...)) +// NameIn applies the In predicate on the "name" field. +func NameIn(vs ...string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldIn(FieldName, vs...)) } -// RunDurationNotIn applies the NotIn predicate on the "run_duration" field. -func RunDurationNotIn(vs ...time.Duration) predicate.AppCategory { - v := make([]any, len(vs)) - for i := range v { - v[i] = int64(vs[i]) - } - return predicate.AppCategory(sql.FieldNotIn(FieldRunDuration, v...)) +// NameNotIn applies the NotIn predicate on the "name" field. +func NameNotIn(vs ...string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldNotIn(FieldName, vs...)) } -// RunDurationGT applies the GT predicate on the "run_duration" field. -func RunDurationGT(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldGT(FieldRunDuration, vc)) +// NameGT applies the GT predicate on the "name" field. +func NameGT(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldGT(FieldName, v)) } -// RunDurationGTE applies the GTE predicate on the "run_duration" field. -func RunDurationGTE(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldGTE(FieldRunDuration, vc)) +// NameGTE applies the GTE predicate on the "name" field. +func NameGTE(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldGTE(FieldName, v)) } -// RunDurationLT applies the LT predicate on the "run_duration" field. -func RunDurationLT(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldLT(FieldRunDuration, vc)) +// NameLT applies the LT predicate on the "name" field. +func NameLT(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldLT(FieldName, v)) } -// RunDurationLTE applies the LTE predicate on the "run_duration" field. -func RunDurationLTE(v time.Duration) predicate.AppCategory { - vc := int64(v) - return predicate.AppCategory(sql.FieldLTE(FieldRunDuration, vc)) +// NameLTE applies the LTE predicate on the "name" field. +func NameLTE(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldLTE(FieldName, v)) +} + +// NameContains applies the Contains predicate on the "name" field. +func NameContains(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldContains(FieldName, v)) +} + +// NameHasPrefix applies the HasPrefix predicate on the "name" field. +func NameHasPrefix(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldHasPrefix(FieldName, v)) +} + +// NameHasSuffix applies the HasSuffix predicate on the "name" field. +func NameHasSuffix(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldHasSuffix(FieldName, v)) +} + +// NameEqualFold applies the EqualFold predicate on the "name" field. +func NameEqualFold(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldEqualFold(FieldName, v)) +} + +// NameContainsFold applies the ContainsFold predicate on the "name" field. +func NameContainsFold(v string) predicate.AppCategory { + return predicate.AppCategory(sql.FieldContainsFold(FieldName, v)) } // UpdatedAtEQ applies the EQ predicate on the "updated_at" field. @@ -370,6 +366,52 @@ func CreatedAtLTE(v time.Time) predicate.AppCategory { return predicate.AppCategory(sql.FieldLTE(FieldCreatedAt, v)) } +// HasApp applies the HasEdge predicate on the "app" edge. +func HasApp() predicate.AppCategory { + return predicate.AppCategory(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.M2M, false, AppTable, AppPrimaryKey...), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasAppWith applies the HasEdge predicate on the "app" edge with a given conditions (other predicates). +func HasAppWith(preds ...predicate.App) predicate.AppCategory { + return predicate.AppCategory(func(s *sql.Selector) { + step := newAppStep() + sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) { + for _, p := range preds { + p(s) + } + }) + }) +} + +// HasAppAppCategory applies the HasEdge predicate on the "app_app_category" edge. +func HasAppAppCategory() predicate.AppCategory { + return predicate.AppCategory(func(s *sql.Selector) { + step := sqlgraph.NewStep( + sqlgraph.From(Table, FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, AppAppCategoryTable, AppAppCategoryColumn), + ) + sqlgraph.HasNeighbors(s, step) + }) +} + +// HasAppAppCategoryWith applies the HasEdge predicate on the "app_app_category" edge with a given conditions (other predicates). +func HasAppAppCategoryWith(preds ...predicate.AppAppCategory) predicate.AppCategory { + return predicate.AppCategory(func(s *sql.Selector) { + step := newAppAppCategoryStep() + 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.AppCategory) predicate.AppCategory { return predicate.AppCategory(sql.AndPredicates(predicates...)) diff --git a/internal/data/internal/ent/appcategory_create.go b/internal/data/internal/ent/appcategory_create.go index 4e835de0..056daa6f 100644 --- a/internal/data/internal/ent/appcategory_create.go +++ b/internal/data/internal/ent/appcategory_create.go @@ -11,6 +11,8 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/model" ) @@ -29,21 +31,21 @@ func (acc *AppCategoryCreate) SetUserID(mi model.InternalID) *AppCategoryCreate return acc } -// SetAppID sets the "app_id" field. -func (acc *AppCategoryCreate) SetAppID(mi model.InternalID) *AppCategoryCreate { - acc.mutation.SetAppID(mi) +// SetVersionNumber sets the "version_number" field. +func (acc *AppCategoryCreate) SetVersionNumber(u uint64) *AppCategoryCreate { + acc.mutation.SetVersionNumber(u) return acc } -// SetStartTime sets the "start_time" field. -func (acc *AppCategoryCreate) SetStartTime(t time.Time) *AppCategoryCreate { - acc.mutation.SetStartTime(t) +// SetVersionDate sets the "version_date" field. +func (acc *AppCategoryCreate) SetVersionDate(t time.Time) *AppCategoryCreate { + acc.mutation.SetVersionDate(t) return acc } -// SetRunDuration sets the "run_duration" field. -func (acc *AppCategoryCreate) SetRunDuration(t time.Duration) *AppCategoryCreate { - acc.mutation.SetRunDuration(t) +// SetName sets the "name" field. +func (acc *AppCategoryCreate) SetName(s string) *AppCategoryCreate { + acc.mutation.SetName(s) return acc } @@ -75,6 +77,42 @@ func (acc *AppCategoryCreate) SetNillableCreatedAt(t *time.Time) *AppCategoryCre return acc } +// SetID sets the "id" field. +func (acc *AppCategoryCreate) SetID(mi model.InternalID) *AppCategoryCreate { + acc.mutation.SetID(mi) + return acc +} + +// AddAppIDs adds the "app" edge to the App entity by IDs. +func (acc *AppCategoryCreate) AddAppIDs(ids ...model.InternalID) *AppCategoryCreate { + acc.mutation.AddAppIDs(ids...) + return acc +} + +// AddApp adds the "app" edges to the App entity. +func (acc *AppCategoryCreate) AddApp(a ...*App) *AppCategoryCreate { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acc.AddAppIDs(ids...) +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by IDs. +func (acc *AppCategoryCreate) AddAppAppCategoryIDs(ids ...int) *AppCategoryCreate { + acc.mutation.AddAppAppCategoryIDs(ids...) + return acc +} + +// AddAppAppCategory adds the "app_app_category" edges to the AppAppCategory entity. +func (acc *AppCategoryCreate) AddAppAppCategory(a ...*AppAppCategory) *AppCategoryCreate { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acc.AddAppAppCategoryIDs(ids...) +} + // Mutation returns the AppCategoryMutation object of the builder. func (acc *AppCategoryCreate) Mutation() *AppCategoryMutation { return acc.mutation @@ -125,14 +163,14 @@ func (acc *AppCategoryCreate) check() error { if _, ok := acc.mutation.UserID(); !ok { return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "AppCategory.user_id"`)} } - if _, ok := acc.mutation.AppID(); !ok { - return &ValidationError{Name: "app_id", err: errors.New(`ent: missing required field "AppCategory.app_id"`)} + if _, ok := acc.mutation.VersionNumber(); !ok { + return &ValidationError{Name: "version_number", err: errors.New(`ent: missing required field "AppCategory.version_number"`)} } - if _, ok := acc.mutation.StartTime(); !ok { - return &ValidationError{Name: "start_time", err: errors.New(`ent: missing required field "AppCategory.start_time"`)} + if _, ok := acc.mutation.VersionDate(); !ok { + return &ValidationError{Name: "version_date", err: errors.New(`ent: missing required field "AppCategory.version_date"`)} } - if _, ok := acc.mutation.RunDuration(); !ok { - return &ValidationError{Name: "run_duration", err: errors.New(`ent: missing required field "AppCategory.run_duration"`)} + if _, ok := acc.mutation.Name(); !ok { + return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "AppCategory.name"`)} } if _, ok := acc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "AppCategory.updated_at"`)} @@ -154,8 +192,10 @@ func (acc *AppCategoryCreate) sqlSave(ctx context.Context) (*AppCategory, error) } return nil, err } - id := _spec.ID.Value.(int64) - _node.ID = int(id) + if _spec.ID.Value != _node.ID { + id := _spec.ID.Value.(int64) + _node.ID = model.InternalID(id) + } acc.mutation.id = &_node.ID acc.mutation.done = true return _node, nil @@ -164,24 +204,28 @@ func (acc *AppCategoryCreate) sqlSave(ctx context.Context) (*AppCategory, error) func (acc *AppCategoryCreate) createSpec() (*AppCategory, *sqlgraph.CreateSpec) { var ( _node = &AppCategory{config: acc.config} - _spec = sqlgraph.NewCreateSpec(appcategory.Table, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt)) + _spec = sqlgraph.NewCreateSpec(appcategory.Table, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64)) ) _spec.OnConflict = acc.conflict + if id, ok := acc.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = id + } if value, ok := acc.mutation.UserID(); ok { _spec.SetField(appcategory.FieldUserID, field.TypeInt64, value) _node.UserID = value } - if value, ok := acc.mutation.AppID(); ok { - _spec.SetField(appcategory.FieldAppID, field.TypeInt64, value) - _node.AppID = value + if value, ok := acc.mutation.VersionNumber(); ok { + _spec.SetField(appcategory.FieldVersionNumber, field.TypeUint64, value) + _node.VersionNumber = value } - if value, ok := acc.mutation.StartTime(); ok { - _spec.SetField(appcategory.FieldStartTime, field.TypeTime, value) - _node.StartTime = value + if value, ok := acc.mutation.VersionDate(); ok { + _spec.SetField(appcategory.FieldVersionDate, field.TypeTime, value) + _node.VersionDate = value } - if value, ok := acc.mutation.RunDuration(); ok { - _spec.SetField(appcategory.FieldRunDuration, field.TypeInt64, value) - _node.RunDuration = value + if value, ok := acc.mutation.Name(); ok { + _spec.SetField(appcategory.FieldName, field.TypeString, value) + _node.Name = value } if value, ok := acc.mutation.UpdatedAt(); ok { _spec.SetField(appcategory.FieldUpdatedAt, field.TypeTime, value) @@ -191,6 +235,38 @@ func (acc *AppCategoryCreate) createSpec() (*AppCategory, *sqlgraph.CreateSpec) _spec.SetField(appcategory.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } + if nodes := acc.mutation.AppIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } + if nodes := acc.mutation.AppAppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges = append(_spec.Edges, edge) + } return _node, _spec } @@ -261,51 +337,45 @@ func (u *AppCategoryUpsert) AddUserID(v model.InternalID) *AppCategoryUpsert { return u } -// SetAppID sets the "app_id" field. -func (u *AppCategoryUpsert) SetAppID(v model.InternalID) *AppCategoryUpsert { - u.Set(appcategory.FieldAppID, v) - return u -} - -// UpdateAppID sets the "app_id" field to the value that was provided on create. -func (u *AppCategoryUpsert) UpdateAppID() *AppCategoryUpsert { - u.SetExcluded(appcategory.FieldAppID) +// SetVersionNumber sets the "version_number" field. +func (u *AppCategoryUpsert) SetVersionNumber(v uint64) *AppCategoryUpsert { + u.Set(appcategory.FieldVersionNumber, v) return u } -// AddAppID adds v to the "app_id" field. -func (u *AppCategoryUpsert) AddAppID(v model.InternalID) *AppCategoryUpsert { - u.Add(appcategory.FieldAppID, v) +// UpdateVersionNumber sets the "version_number" field to the value that was provided on create. +func (u *AppCategoryUpsert) UpdateVersionNumber() *AppCategoryUpsert { + u.SetExcluded(appcategory.FieldVersionNumber) return u } -// SetStartTime sets the "start_time" field. -func (u *AppCategoryUpsert) SetStartTime(v time.Time) *AppCategoryUpsert { - u.Set(appcategory.FieldStartTime, v) +// AddVersionNumber adds v to the "version_number" field. +func (u *AppCategoryUpsert) AddVersionNumber(v uint64) *AppCategoryUpsert { + u.Add(appcategory.FieldVersionNumber, v) return u } -// UpdateStartTime sets the "start_time" field to the value that was provided on create. -func (u *AppCategoryUpsert) UpdateStartTime() *AppCategoryUpsert { - u.SetExcluded(appcategory.FieldStartTime) +// SetVersionDate sets the "version_date" field. +func (u *AppCategoryUpsert) SetVersionDate(v time.Time) *AppCategoryUpsert { + u.Set(appcategory.FieldVersionDate, v) return u } -// SetRunDuration sets the "run_duration" field. -func (u *AppCategoryUpsert) SetRunDuration(v time.Duration) *AppCategoryUpsert { - u.Set(appcategory.FieldRunDuration, v) +// UpdateVersionDate sets the "version_date" field to the value that was provided on create. +func (u *AppCategoryUpsert) UpdateVersionDate() *AppCategoryUpsert { + u.SetExcluded(appcategory.FieldVersionDate) return u } -// UpdateRunDuration sets the "run_duration" field to the value that was provided on create. -func (u *AppCategoryUpsert) UpdateRunDuration() *AppCategoryUpsert { - u.SetExcluded(appcategory.FieldRunDuration) +// SetName sets the "name" field. +func (u *AppCategoryUpsert) SetName(v string) *AppCategoryUpsert { + u.Set(appcategory.FieldName, v) return u } -// AddRunDuration adds v to the "run_duration" field. -func (u *AppCategoryUpsert) AddRunDuration(v time.Duration) *AppCategoryUpsert { - u.Add(appcategory.FieldRunDuration, v) +// UpdateName sets the "name" field to the value that was provided on create. +func (u *AppCategoryUpsert) UpdateName() *AppCategoryUpsert { + u.SetExcluded(appcategory.FieldName) return u } @@ -333,16 +403,24 @@ func (u *AppCategoryUpsert) UpdateCreatedAt() *AppCategoryUpsert { return u } -// UpdateNewValues updates the mutable fields using the new values that were set on create. +// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field. // Using this option is equivalent to using: // // client.AppCategory.Create(). // OnConflict( // sql.ResolveWithNewValues(), +// sql.ResolveWith(func(u *sql.UpdateSet) { +// u.SetIgnore(appcategory.FieldID) +// }), // ). // Exec(ctx) func (u *AppCategoryUpsertOne) UpdateNewValues() *AppCategoryUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) + u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { + if _, exists := u.create.mutation.ID(); exists { + s.SetIgnore(appcategory.FieldID) + } + })) return u } @@ -394,59 +472,52 @@ func (u *AppCategoryUpsertOne) UpdateUserID() *AppCategoryUpsertOne { }) } -// SetAppID sets the "app_id" field. -func (u *AppCategoryUpsertOne) SetAppID(v model.InternalID) *AppCategoryUpsertOne { +// SetVersionNumber sets the "version_number" field. +func (u *AppCategoryUpsertOne) SetVersionNumber(v uint64) *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.SetAppID(v) + s.SetVersionNumber(v) }) } -// AddAppID adds v to the "app_id" field. -func (u *AppCategoryUpsertOne) AddAppID(v model.InternalID) *AppCategoryUpsertOne { +// AddVersionNumber adds v to the "version_number" field. +func (u *AppCategoryUpsertOne) AddVersionNumber(v uint64) *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.AddAppID(v) + s.AddVersionNumber(v) }) } -// UpdateAppID sets the "app_id" field to the value that was provided on create. -func (u *AppCategoryUpsertOne) UpdateAppID() *AppCategoryUpsertOne { +// UpdateVersionNumber sets the "version_number" field to the value that was provided on create. +func (u *AppCategoryUpsertOne) UpdateVersionNumber() *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.UpdateAppID() + s.UpdateVersionNumber() }) } -// SetStartTime sets the "start_time" field. -func (u *AppCategoryUpsertOne) SetStartTime(v time.Time) *AppCategoryUpsertOne { +// SetVersionDate sets the "version_date" field. +func (u *AppCategoryUpsertOne) SetVersionDate(v time.Time) *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.SetStartTime(v) + s.SetVersionDate(v) }) } -// UpdateStartTime sets the "start_time" field to the value that was provided on create. -func (u *AppCategoryUpsertOne) UpdateStartTime() *AppCategoryUpsertOne { +// UpdateVersionDate sets the "version_date" field to the value that was provided on create. +func (u *AppCategoryUpsertOne) UpdateVersionDate() *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.UpdateStartTime() + s.UpdateVersionDate() }) } -// SetRunDuration sets the "run_duration" field. -func (u *AppCategoryUpsertOne) SetRunDuration(v time.Duration) *AppCategoryUpsertOne { +// SetName sets the "name" field. +func (u *AppCategoryUpsertOne) SetName(v string) *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.SetRunDuration(v) + s.SetName(v) }) } -// AddRunDuration adds v to the "run_duration" field. -func (u *AppCategoryUpsertOne) AddRunDuration(v time.Duration) *AppCategoryUpsertOne { +// UpdateName sets the "name" field to the value that was provided on create. +func (u *AppCategoryUpsertOne) UpdateName() *AppCategoryUpsertOne { return u.Update(func(s *AppCategoryUpsert) { - s.AddRunDuration(v) - }) -} - -// UpdateRunDuration sets the "run_duration" field to the value that was provided on create. -func (u *AppCategoryUpsertOne) UpdateRunDuration() *AppCategoryUpsertOne { - return u.Update(func(s *AppCategoryUpsert) { - s.UpdateRunDuration() + s.UpdateName() }) } @@ -494,7 +565,7 @@ func (u *AppCategoryUpsertOne) ExecX(ctx context.Context) { } // Exec executes the UPSERT query and returns the inserted/updated ID. -func (u *AppCategoryUpsertOne) ID(ctx context.Context) (id int, err error) { +func (u *AppCategoryUpsertOne) ID(ctx context.Context) (id model.InternalID, err error) { node, err := u.create.Save(ctx) if err != nil { return id, err @@ -503,7 +574,7 @@ func (u *AppCategoryUpsertOne) ID(ctx context.Context) (id int, err error) { } // IDX is like ID, but panics if an error occurs. -func (u *AppCategoryUpsertOne) IDX(ctx context.Context) int { +func (u *AppCategoryUpsertOne) IDX(ctx context.Context) model.InternalID { id, err := u.ID(ctx) if err != nil { panic(err) @@ -558,9 +629,9 @@ func (accb *AppCategoryCreateBulk) Save(ctx context.Context) ([]*AppCategory, er return nil, err } mutation.id = &nodes[i].ID - if specs[i].ID.Value != nil { + if specs[i].ID.Value != nil && nodes[i].ID == 0 { id := specs[i].ID.Value.(int64) - nodes[i].ID = int(id) + nodes[i].ID = model.InternalID(id) } mutation.done = true return nodes[i], nil @@ -648,10 +719,20 @@ type AppCategoryUpsertBulk struct { // client.AppCategory.Create(). // OnConflict( // sql.ResolveWithNewValues(), +// sql.ResolveWith(func(u *sql.UpdateSet) { +// u.SetIgnore(appcategory.FieldID) +// }), // ). // Exec(ctx) func (u *AppCategoryUpsertBulk) UpdateNewValues() *AppCategoryUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) + u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { + for _, b := range u.create.builders { + if _, exists := b.mutation.ID(); exists { + s.SetIgnore(appcategory.FieldID) + } + } + })) return u } @@ -703,59 +784,52 @@ func (u *AppCategoryUpsertBulk) UpdateUserID() *AppCategoryUpsertBulk { }) } -// SetAppID sets the "app_id" field. -func (u *AppCategoryUpsertBulk) SetAppID(v model.InternalID) *AppCategoryUpsertBulk { - return u.Update(func(s *AppCategoryUpsert) { - s.SetAppID(v) - }) -} - -// AddAppID adds v to the "app_id" field. -func (u *AppCategoryUpsertBulk) AddAppID(v model.InternalID) *AppCategoryUpsertBulk { +// SetVersionNumber sets the "version_number" field. +func (u *AppCategoryUpsertBulk) SetVersionNumber(v uint64) *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.AddAppID(v) + s.SetVersionNumber(v) }) } -// UpdateAppID sets the "app_id" field to the value that was provided on create. -func (u *AppCategoryUpsertBulk) UpdateAppID() *AppCategoryUpsertBulk { +// AddVersionNumber adds v to the "version_number" field. +func (u *AppCategoryUpsertBulk) AddVersionNumber(v uint64) *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.UpdateAppID() + s.AddVersionNumber(v) }) } -// SetStartTime sets the "start_time" field. -func (u *AppCategoryUpsertBulk) SetStartTime(v time.Time) *AppCategoryUpsertBulk { +// UpdateVersionNumber sets the "version_number" field to the value that was provided on create. +func (u *AppCategoryUpsertBulk) UpdateVersionNumber() *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.SetStartTime(v) + s.UpdateVersionNumber() }) } -// UpdateStartTime sets the "start_time" field to the value that was provided on create. -func (u *AppCategoryUpsertBulk) UpdateStartTime() *AppCategoryUpsertBulk { +// SetVersionDate sets the "version_date" field. +func (u *AppCategoryUpsertBulk) SetVersionDate(v time.Time) *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.UpdateStartTime() + s.SetVersionDate(v) }) } -// SetRunDuration sets the "run_duration" field. -func (u *AppCategoryUpsertBulk) SetRunDuration(v time.Duration) *AppCategoryUpsertBulk { +// UpdateVersionDate sets the "version_date" field to the value that was provided on create. +func (u *AppCategoryUpsertBulk) UpdateVersionDate() *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.SetRunDuration(v) + s.UpdateVersionDate() }) } -// AddRunDuration adds v to the "run_duration" field. -func (u *AppCategoryUpsertBulk) AddRunDuration(v time.Duration) *AppCategoryUpsertBulk { +// SetName sets the "name" field. +func (u *AppCategoryUpsertBulk) SetName(v string) *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.AddRunDuration(v) + s.SetName(v) }) } -// UpdateRunDuration sets the "run_duration" field to the value that was provided on create. -func (u *AppCategoryUpsertBulk) UpdateRunDuration() *AppCategoryUpsertBulk { +// UpdateName sets the "name" field to the value that was provided on create. +func (u *AppCategoryUpsertBulk) UpdateName() *AppCategoryUpsertBulk { return u.Update(func(s *AppCategoryUpsert) { - s.UpdateRunDuration() + s.UpdateName() }) } diff --git a/internal/data/internal/ent/appcategory_delete.go b/internal/data/internal/ent/appcategory_delete.go index 11b813c5..5735288e 100644 --- a/internal/data/internal/ent/appcategory_delete.go +++ b/internal/data/internal/ent/appcategory_delete.go @@ -40,7 +40,7 @@ func (acd *AppCategoryDelete) ExecX(ctx context.Context) int { } func (acd *AppCategoryDelete) sqlExec(ctx context.Context) (int, error) { - _spec := sqlgraph.NewDeleteSpec(appcategory.Table, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt)) + _spec := sqlgraph.NewDeleteSpec(appcategory.Table, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64)) if ps := acd.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { diff --git a/internal/data/internal/ent/appcategory_query.go b/internal/data/internal/ent/appcategory_query.go index 84c9f8aa..8bfcb57d 100644 --- a/internal/data/internal/ent/appcategory_query.go +++ b/internal/data/internal/ent/appcategory_query.go @@ -4,6 +4,7 @@ package ent import ( "context" + "database/sql/driver" "fmt" "math" @@ -11,17 +12,22 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/predicate" + "github.com/tuihub/librarian/internal/model" ) // AppCategoryQuery is the builder for querying AppCategory entities. type AppCategoryQuery struct { config - ctx *QueryContext - order []appcategory.OrderOption - inters []Interceptor - predicates []predicate.AppCategory + ctx *QueryContext + order []appcategory.OrderOption + inters []Interceptor + predicates []predicate.AppCategory + withApp *AppQuery + withAppAppCategory *AppAppCategoryQuery // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) @@ -58,6 +64,50 @@ func (acq *AppCategoryQuery) Order(o ...appcategory.OrderOption) *AppCategoryQue return acq } +// QueryApp chains the current query on the "app" edge. +func (acq *AppCategoryQuery) QueryApp() *AppQuery { + query := (&AppClient{config: acq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := acq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := acq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(appcategory.Table, appcategory.FieldID, selector), + sqlgraph.To(app.Table, app.FieldID), + sqlgraph.Edge(sqlgraph.M2M, false, appcategory.AppTable, appcategory.AppPrimaryKey...), + ) + fromU = sqlgraph.SetNeighbors(acq.driver.Dialect(), step) + return fromU, nil + } + return query +} + +// QueryAppAppCategory chains the current query on the "app_app_category" edge. +func (acq *AppCategoryQuery) QueryAppAppCategory() *AppAppCategoryQuery { + query := (&AppAppCategoryClient{config: acq.config}).Query() + query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { + if err := acq.prepareQuery(ctx); err != nil { + return nil, err + } + selector := acq.sqlQuery(ctx) + if err := selector.Err(); err != nil { + return nil, err + } + step := sqlgraph.NewStep( + sqlgraph.From(appcategory.Table, appcategory.FieldID, selector), + sqlgraph.To(appappcategory.Table, appappcategory.FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, appcategory.AppAppCategoryTable, appcategory.AppAppCategoryColumn), + ) + fromU = sqlgraph.SetNeighbors(acq.driver.Dialect(), step) + return fromU, nil + } + return query +} + // First returns the first AppCategory entity from the query. // Returns a *NotFoundError when no AppCategory was found. func (acq *AppCategoryQuery) First(ctx context.Context) (*AppCategory, error) { @@ -82,8 +132,8 @@ func (acq *AppCategoryQuery) FirstX(ctx context.Context) *AppCategory { // FirstID returns the first AppCategory ID from the query. // Returns a *NotFoundError when no AppCategory ID was found. -func (acq *AppCategoryQuery) FirstID(ctx context.Context) (id int, err error) { - var ids []int +func (acq *AppCategoryQuery) FirstID(ctx context.Context) (id model.InternalID, err error) { + var ids []model.InternalID if ids, err = acq.Limit(1).IDs(setContextOp(ctx, acq.ctx, ent.OpQueryFirstID)); err != nil { return } @@ -95,7 +145,7 @@ func (acq *AppCategoryQuery) FirstID(ctx context.Context) (id int, err error) { } // FirstIDX is like FirstID, but panics if an error occurs. -func (acq *AppCategoryQuery) FirstIDX(ctx context.Context) int { +func (acq *AppCategoryQuery) FirstIDX(ctx context.Context) model.InternalID { id, err := acq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) @@ -133,8 +183,8 @@ func (acq *AppCategoryQuery) OnlyX(ctx context.Context) *AppCategory { // OnlyID is like Only, but returns the only AppCategory ID in the query. // Returns a *NotSingularError when more than one AppCategory ID is found. // Returns a *NotFoundError when no entities are found. -func (acq *AppCategoryQuery) OnlyID(ctx context.Context) (id int, err error) { - var ids []int +func (acq *AppCategoryQuery) OnlyID(ctx context.Context) (id model.InternalID, err error) { + var ids []model.InternalID if ids, err = acq.Limit(2).IDs(setContextOp(ctx, acq.ctx, ent.OpQueryOnlyID)); err != nil { return } @@ -150,7 +200,7 @@ func (acq *AppCategoryQuery) OnlyID(ctx context.Context) (id int, err error) { } // OnlyIDX is like OnlyID, but panics if an error occurs. -func (acq *AppCategoryQuery) OnlyIDX(ctx context.Context) int { +func (acq *AppCategoryQuery) OnlyIDX(ctx context.Context) model.InternalID { id, err := acq.OnlyID(ctx) if err != nil { panic(err) @@ -178,7 +228,7 @@ func (acq *AppCategoryQuery) AllX(ctx context.Context) []*AppCategory { } // IDs executes the query and returns a list of AppCategory IDs. -func (acq *AppCategoryQuery) IDs(ctx context.Context) (ids []int, err error) { +func (acq *AppCategoryQuery) IDs(ctx context.Context) (ids []model.InternalID, err error) { if acq.ctx.Unique == nil && acq.path != nil { acq.Unique(true) } @@ -190,7 +240,7 @@ func (acq *AppCategoryQuery) IDs(ctx context.Context) (ids []int, err error) { } // IDsX is like IDs, but panics if an error occurs. -func (acq *AppCategoryQuery) IDsX(ctx context.Context) []int { +func (acq *AppCategoryQuery) IDsX(ctx context.Context) []model.InternalID { ids, err := acq.IDs(ctx) if err != nil { panic(err) @@ -245,17 +295,41 @@ func (acq *AppCategoryQuery) Clone() *AppCategoryQuery { return nil } return &AppCategoryQuery{ - config: acq.config, - ctx: acq.ctx.Clone(), - order: append([]appcategory.OrderOption{}, acq.order...), - inters: append([]Interceptor{}, acq.inters...), - predicates: append([]predicate.AppCategory{}, acq.predicates...), + config: acq.config, + ctx: acq.ctx.Clone(), + order: append([]appcategory.OrderOption{}, acq.order...), + inters: append([]Interceptor{}, acq.inters...), + predicates: append([]predicate.AppCategory{}, acq.predicates...), + withApp: acq.withApp.Clone(), + withAppAppCategory: acq.withAppAppCategory.Clone(), // clone intermediate query. sql: acq.sql.Clone(), path: acq.path, } } +// WithApp tells the query-builder to eager-load the nodes that are connected to +// the "app" edge. The optional arguments are used to configure the query builder of the edge. +func (acq *AppCategoryQuery) WithApp(opts ...func(*AppQuery)) *AppCategoryQuery { + query := (&AppClient{config: acq.config}).Query() + for _, opt := range opts { + opt(query) + } + acq.withApp = query + return acq +} + +// WithAppAppCategory tells the query-builder to eager-load the nodes that are connected to +// the "app_app_category" edge. The optional arguments are used to configure the query builder of the edge. +func (acq *AppCategoryQuery) WithAppAppCategory(opts ...func(*AppAppCategoryQuery)) *AppCategoryQuery { + query := (&AppAppCategoryClient{config: acq.config}).Query() + for _, opt := range opts { + opt(query) + } + acq.withAppAppCategory = query + return acq +} + // 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. // @@ -332,8 +406,12 @@ func (acq *AppCategoryQuery) prepareQuery(ctx context.Context) error { func (acq *AppCategoryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*AppCategory, error) { var ( - nodes = []*AppCategory{} - _spec = acq.querySpec() + nodes = []*AppCategory{} + _spec = acq.querySpec() + loadedTypes = [2]bool{ + acq.withApp != nil, + acq.withAppAppCategory != nil, + } ) _spec.ScanValues = func(columns []string) ([]any, error) { return (*AppCategory).scanValues(nil, columns) @@ -341,6 +419,7 @@ func (acq *AppCategoryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([] _spec.Assign = func(columns []string, values []any) error { node := &AppCategory{config: acq.config} nodes = append(nodes, node) + node.Edges.loadedTypes = loadedTypes return node.assignValues(columns, values) } for i := range hooks { @@ -352,9 +431,115 @@ func (acq *AppCategoryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([] if len(nodes) == 0 { return nodes, nil } + if query := acq.withApp; query != nil { + if err := acq.loadApp(ctx, query, nodes, + func(n *AppCategory) { n.Edges.App = []*App{} }, + func(n *AppCategory, e *App) { n.Edges.App = append(n.Edges.App, e) }); err != nil { + return nil, err + } + } + if query := acq.withAppAppCategory; query != nil { + if err := acq.loadAppAppCategory(ctx, query, nodes, + func(n *AppCategory) { n.Edges.AppAppCategory = []*AppAppCategory{} }, + func(n *AppCategory, e *AppAppCategory) { n.Edges.AppAppCategory = append(n.Edges.AppAppCategory, e) }); err != nil { + return nil, err + } + } return nodes, nil } +func (acq *AppCategoryQuery) loadApp(ctx context.Context, query *AppQuery, nodes []*AppCategory, init func(*AppCategory), assign func(*AppCategory, *App)) error { + edgeIDs := make([]driver.Value, len(nodes)) + byID := make(map[model.InternalID]*AppCategory) + nids := make(map[model.InternalID]map[*AppCategory]struct{}) + for i, node := range nodes { + edgeIDs[i] = node.ID + byID[node.ID] = node + if init != nil { + init(node) + } + } + query.Where(func(s *sql.Selector) { + joinT := sql.Table(appcategory.AppTable) + s.Join(joinT).On(s.C(app.FieldID), joinT.C(appcategory.AppPrimaryKey[1])) + s.Where(sql.InValues(joinT.C(appcategory.AppPrimaryKey[0]), edgeIDs...)) + columns := s.SelectedColumns() + s.Select(joinT.C(appcategory.AppPrimaryKey[0])) + s.AppendSelect(columns...) + s.SetDistinct(false) + }) + if err := query.prepareQuery(ctx); err != nil { + return err + } + qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) { + return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) { + assign := spec.Assign + values := spec.ScanValues + spec.ScanValues = func(columns []string) ([]any, error) { + values, err := values(columns[1:]) + if err != nil { + return nil, err + } + return append([]any{new(sql.NullInt64)}, values...), nil + } + spec.Assign = func(columns []string, values []any) error { + outValue := model.InternalID(values[0].(*sql.NullInt64).Int64) + inValue := model.InternalID(values[1].(*sql.NullInt64).Int64) + if nids[inValue] == nil { + nids[inValue] = map[*AppCategory]struct{}{byID[outValue]: {}} + return assign(columns[1:], values[1:]) + } + nids[inValue][byID[outValue]] = struct{}{} + return nil + } + }) + }) + neighbors, err := withInterceptors[[]*App](ctx, query, qr, query.inters) + if err != nil { + return err + } + for _, n := range neighbors { + nodes, ok := nids[n.ID] + if !ok { + return fmt.Errorf(`unexpected "app" node returned %v`, n.ID) + } + for kn := range nodes { + assign(kn, n) + } + } + return nil +} +func (acq *AppCategoryQuery) loadAppAppCategory(ctx context.Context, query *AppAppCategoryQuery, nodes []*AppCategory, init func(*AppCategory), assign func(*AppCategory, *AppAppCategory)) error { + fks := make([]driver.Value, 0, len(nodes)) + nodeids := make(map[model.InternalID]*AppCategory) + for i := range nodes { + fks = append(fks, nodes[i].ID) + nodeids[nodes[i].ID] = nodes[i] + if init != nil { + init(nodes[i]) + } + } + if len(query.ctx.Fields) > 0 { + query.ctx.AppendFieldOnce(appappcategory.FieldAppCategoryID) + } + query.Where(predicate.AppAppCategory(func(s *sql.Selector) { + s.Where(sql.InValues(s.C(appcategory.AppAppCategoryColumn), fks...)) + })) + neighbors, err := query.All(ctx) + if err != nil { + return err + } + for _, n := range neighbors { + fk := n.AppCategoryID + node, ok := nodeids[fk] + if !ok { + return fmt.Errorf(`unexpected referenced foreign-key "app_category_id" returned %v for node %v`, fk, n.ID) + } + assign(node, n) + } + return nil +} + func (acq *AppCategoryQuery) sqlCount(ctx context.Context) (int, error) { _spec := acq.querySpec() _spec.Node.Columns = acq.ctx.Fields @@ -365,7 +550,7 @@ func (acq *AppCategoryQuery) sqlCount(ctx context.Context) (int, error) { } func (acq *AppCategoryQuery) querySpec() *sqlgraph.QuerySpec { - _spec := sqlgraph.NewQuerySpec(appcategory.Table, appcategory.Columns, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt)) + _spec := sqlgraph.NewQuerySpec(appcategory.Table, appcategory.Columns, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64)) _spec.From = acq.sql if unique := acq.ctx.Unique; unique != nil { _spec.Unique = *unique diff --git a/internal/data/internal/ent/appcategory_update.go b/internal/data/internal/ent/appcategory_update.go index ca50fc86..e02319e4 100644 --- a/internal/data/internal/ent/appcategory_update.go +++ b/internal/data/internal/ent/appcategory_update.go @@ -11,6 +11,8 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" + "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/predicate" "github.com/tuihub/librarian/internal/model" @@ -50,62 +52,55 @@ func (acu *AppCategoryUpdate) AddUserID(mi model.InternalID) *AppCategoryUpdate return acu } -// SetAppID sets the "app_id" field. -func (acu *AppCategoryUpdate) SetAppID(mi model.InternalID) *AppCategoryUpdate { - acu.mutation.ResetAppID() - acu.mutation.SetAppID(mi) +// SetVersionNumber sets the "version_number" field. +func (acu *AppCategoryUpdate) SetVersionNumber(u uint64) *AppCategoryUpdate { + acu.mutation.ResetVersionNumber() + acu.mutation.SetVersionNumber(u) return acu } -// SetNillableAppID sets the "app_id" field if the given value is not nil. -func (acu *AppCategoryUpdate) SetNillableAppID(mi *model.InternalID) *AppCategoryUpdate { - if mi != nil { - acu.SetAppID(*mi) +// SetNillableVersionNumber sets the "version_number" field if the given value is not nil. +func (acu *AppCategoryUpdate) SetNillableVersionNumber(u *uint64) *AppCategoryUpdate { + if u != nil { + acu.SetVersionNumber(*u) } return acu } -// AddAppID adds mi to the "app_id" field. -func (acu *AppCategoryUpdate) AddAppID(mi model.InternalID) *AppCategoryUpdate { - acu.mutation.AddAppID(mi) +// AddVersionNumber adds u to the "version_number" field. +func (acu *AppCategoryUpdate) AddVersionNumber(u int64) *AppCategoryUpdate { + acu.mutation.AddVersionNumber(u) return acu } -// SetStartTime sets the "start_time" field. -func (acu *AppCategoryUpdate) SetStartTime(t time.Time) *AppCategoryUpdate { - acu.mutation.SetStartTime(t) +// SetVersionDate sets the "version_date" field. +func (acu *AppCategoryUpdate) SetVersionDate(t time.Time) *AppCategoryUpdate { + acu.mutation.SetVersionDate(t) return acu } -// SetNillableStartTime sets the "start_time" field if the given value is not nil. -func (acu *AppCategoryUpdate) SetNillableStartTime(t *time.Time) *AppCategoryUpdate { +// SetNillableVersionDate sets the "version_date" field if the given value is not nil. +func (acu *AppCategoryUpdate) SetNillableVersionDate(t *time.Time) *AppCategoryUpdate { if t != nil { - acu.SetStartTime(*t) + acu.SetVersionDate(*t) } return acu } -// SetRunDuration sets the "run_duration" field. -func (acu *AppCategoryUpdate) SetRunDuration(t time.Duration) *AppCategoryUpdate { - acu.mutation.ResetRunDuration() - acu.mutation.SetRunDuration(t) +// SetName sets the "name" field. +func (acu *AppCategoryUpdate) SetName(s string) *AppCategoryUpdate { + acu.mutation.SetName(s) return acu } -// SetNillableRunDuration sets the "run_duration" field if the given value is not nil. -func (acu *AppCategoryUpdate) SetNillableRunDuration(t *time.Duration) *AppCategoryUpdate { - if t != nil { - acu.SetRunDuration(*t) +// SetNillableName sets the "name" field if the given value is not nil. +func (acu *AppCategoryUpdate) SetNillableName(s *string) *AppCategoryUpdate { + if s != nil { + acu.SetName(*s) } return acu } -// AddRunDuration adds t to the "run_duration" field. -func (acu *AppCategoryUpdate) AddRunDuration(t time.Duration) *AppCategoryUpdate { - acu.mutation.AddRunDuration(t) - return acu -} - // SetUpdatedAt sets the "updated_at" field. func (acu *AppCategoryUpdate) SetUpdatedAt(t time.Time) *AppCategoryUpdate { acu.mutation.SetUpdatedAt(t) @@ -126,11 +121,83 @@ func (acu *AppCategoryUpdate) SetNillableCreatedAt(t *time.Time) *AppCategoryUpd return acu } +// AddAppIDs adds the "app" edge to the App entity by IDs. +func (acu *AppCategoryUpdate) AddAppIDs(ids ...model.InternalID) *AppCategoryUpdate { + acu.mutation.AddAppIDs(ids...) + return acu +} + +// AddApp adds the "app" edges to the App entity. +func (acu *AppCategoryUpdate) AddApp(a ...*App) *AppCategoryUpdate { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acu.AddAppIDs(ids...) +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by IDs. +func (acu *AppCategoryUpdate) AddAppAppCategoryIDs(ids ...int) *AppCategoryUpdate { + acu.mutation.AddAppAppCategoryIDs(ids...) + return acu +} + +// AddAppAppCategory adds the "app_app_category" edges to the AppAppCategory entity. +func (acu *AppCategoryUpdate) AddAppAppCategory(a ...*AppAppCategory) *AppCategoryUpdate { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acu.AddAppAppCategoryIDs(ids...) +} + // Mutation returns the AppCategoryMutation object of the builder. func (acu *AppCategoryUpdate) Mutation() *AppCategoryMutation { return acu.mutation } +// ClearApp clears all "app" edges to the App entity. +func (acu *AppCategoryUpdate) ClearApp() *AppCategoryUpdate { + acu.mutation.ClearApp() + return acu +} + +// RemoveAppIDs removes the "app" edge to App entities by IDs. +func (acu *AppCategoryUpdate) RemoveAppIDs(ids ...model.InternalID) *AppCategoryUpdate { + acu.mutation.RemoveAppIDs(ids...) + return acu +} + +// RemoveApp removes "app" edges to App entities. +func (acu *AppCategoryUpdate) RemoveApp(a ...*App) *AppCategoryUpdate { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acu.RemoveAppIDs(ids...) +} + +// ClearAppAppCategory clears all "app_app_category" edges to the AppAppCategory entity. +func (acu *AppCategoryUpdate) ClearAppAppCategory() *AppCategoryUpdate { + acu.mutation.ClearAppAppCategory() + return acu +} + +// RemoveAppAppCategoryIDs removes the "app_app_category" edge to AppAppCategory entities by IDs. +func (acu *AppCategoryUpdate) RemoveAppAppCategoryIDs(ids ...int) *AppCategoryUpdate { + acu.mutation.RemoveAppAppCategoryIDs(ids...) + return acu +} + +// RemoveAppAppCategory removes "app_app_category" edges to AppAppCategory entities. +func (acu *AppCategoryUpdate) RemoveAppAppCategory(a ...*AppAppCategory) *AppCategoryUpdate { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acu.RemoveAppAppCategoryIDs(ids...) +} + // Save executes the query and returns the number of nodes affected by the update operation. func (acu *AppCategoryUpdate) Save(ctx context.Context) (int, error) { acu.defaults() @@ -168,7 +235,7 @@ func (acu *AppCategoryUpdate) defaults() { } func (acu *AppCategoryUpdate) sqlSave(ctx context.Context) (n int, err error) { - _spec := sqlgraph.NewUpdateSpec(appcategory.Table, appcategory.Columns, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt)) + _spec := sqlgraph.NewUpdateSpec(appcategory.Table, appcategory.Columns, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64)) if ps := acu.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { @@ -182,20 +249,17 @@ func (acu *AppCategoryUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := acu.mutation.AddedUserID(); ok { _spec.AddField(appcategory.FieldUserID, field.TypeInt64, value) } - if value, ok := acu.mutation.AppID(); ok { - _spec.SetField(appcategory.FieldAppID, field.TypeInt64, value) - } - if value, ok := acu.mutation.AddedAppID(); ok { - _spec.AddField(appcategory.FieldAppID, field.TypeInt64, value) + if value, ok := acu.mutation.VersionNumber(); ok { + _spec.SetField(appcategory.FieldVersionNumber, field.TypeUint64, value) } - if value, ok := acu.mutation.StartTime(); ok { - _spec.SetField(appcategory.FieldStartTime, field.TypeTime, value) + if value, ok := acu.mutation.AddedVersionNumber(); ok { + _spec.AddField(appcategory.FieldVersionNumber, field.TypeUint64, value) } - if value, ok := acu.mutation.RunDuration(); ok { - _spec.SetField(appcategory.FieldRunDuration, field.TypeInt64, value) + if value, ok := acu.mutation.VersionDate(); ok { + _spec.SetField(appcategory.FieldVersionDate, field.TypeTime, value) } - if value, ok := acu.mutation.AddedRunDuration(); ok { - _spec.AddField(appcategory.FieldRunDuration, field.TypeInt64, value) + if value, ok := acu.mutation.Name(); ok { + _spec.SetField(appcategory.FieldName, field.TypeString, value) } if value, ok := acu.mutation.UpdatedAt(); ok { _spec.SetField(appcategory.FieldUpdatedAt, field.TypeTime, value) @@ -203,6 +267,96 @@ func (acu *AppCategoryUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := acu.mutation.CreatedAt(); ok { _spec.SetField(appcategory.FieldCreatedAt, field.TypeTime, value) } + if acu.mutation.AppCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := acu.mutation.RemovedAppIDs(); len(nodes) > 0 && !acu.mutation.AppCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := acu.mutation.AppIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if acu.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := acu.mutation.RemovedAppAppCategoryIDs(); len(nodes) > 0 && !acu.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.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 := acu.mutation.AppAppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } if n, err = sqlgraph.UpdateNodes(ctx, acu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{appcategory.Label} @@ -244,62 +398,55 @@ func (acuo *AppCategoryUpdateOne) AddUserID(mi model.InternalID) *AppCategoryUpd return acuo } -// SetAppID sets the "app_id" field. -func (acuo *AppCategoryUpdateOne) SetAppID(mi model.InternalID) *AppCategoryUpdateOne { - acuo.mutation.ResetAppID() - acuo.mutation.SetAppID(mi) +// SetVersionNumber sets the "version_number" field. +func (acuo *AppCategoryUpdateOne) SetVersionNumber(u uint64) *AppCategoryUpdateOne { + acuo.mutation.ResetVersionNumber() + acuo.mutation.SetVersionNumber(u) return acuo } -// SetNillableAppID sets the "app_id" field if the given value is not nil. -func (acuo *AppCategoryUpdateOne) SetNillableAppID(mi *model.InternalID) *AppCategoryUpdateOne { - if mi != nil { - acuo.SetAppID(*mi) +// SetNillableVersionNumber sets the "version_number" field if the given value is not nil. +func (acuo *AppCategoryUpdateOne) SetNillableVersionNumber(u *uint64) *AppCategoryUpdateOne { + if u != nil { + acuo.SetVersionNumber(*u) } return acuo } -// AddAppID adds mi to the "app_id" field. -func (acuo *AppCategoryUpdateOne) AddAppID(mi model.InternalID) *AppCategoryUpdateOne { - acuo.mutation.AddAppID(mi) +// AddVersionNumber adds u to the "version_number" field. +func (acuo *AppCategoryUpdateOne) AddVersionNumber(u int64) *AppCategoryUpdateOne { + acuo.mutation.AddVersionNumber(u) return acuo } -// SetStartTime sets the "start_time" field. -func (acuo *AppCategoryUpdateOne) SetStartTime(t time.Time) *AppCategoryUpdateOne { - acuo.mutation.SetStartTime(t) +// SetVersionDate sets the "version_date" field. +func (acuo *AppCategoryUpdateOne) SetVersionDate(t time.Time) *AppCategoryUpdateOne { + acuo.mutation.SetVersionDate(t) return acuo } -// SetNillableStartTime sets the "start_time" field if the given value is not nil. -func (acuo *AppCategoryUpdateOne) SetNillableStartTime(t *time.Time) *AppCategoryUpdateOne { +// SetNillableVersionDate sets the "version_date" field if the given value is not nil. +func (acuo *AppCategoryUpdateOne) SetNillableVersionDate(t *time.Time) *AppCategoryUpdateOne { if t != nil { - acuo.SetStartTime(*t) + acuo.SetVersionDate(*t) } return acuo } -// SetRunDuration sets the "run_duration" field. -func (acuo *AppCategoryUpdateOne) SetRunDuration(t time.Duration) *AppCategoryUpdateOne { - acuo.mutation.ResetRunDuration() - acuo.mutation.SetRunDuration(t) +// SetName sets the "name" field. +func (acuo *AppCategoryUpdateOne) SetName(s string) *AppCategoryUpdateOne { + acuo.mutation.SetName(s) return acuo } -// SetNillableRunDuration sets the "run_duration" field if the given value is not nil. -func (acuo *AppCategoryUpdateOne) SetNillableRunDuration(t *time.Duration) *AppCategoryUpdateOne { - if t != nil { - acuo.SetRunDuration(*t) +// SetNillableName sets the "name" field if the given value is not nil. +func (acuo *AppCategoryUpdateOne) SetNillableName(s *string) *AppCategoryUpdateOne { + if s != nil { + acuo.SetName(*s) } return acuo } -// AddRunDuration adds t to the "run_duration" field. -func (acuo *AppCategoryUpdateOne) AddRunDuration(t time.Duration) *AppCategoryUpdateOne { - acuo.mutation.AddRunDuration(t) - return acuo -} - // SetUpdatedAt sets the "updated_at" field. func (acuo *AppCategoryUpdateOne) SetUpdatedAt(t time.Time) *AppCategoryUpdateOne { acuo.mutation.SetUpdatedAt(t) @@ -320,11 +467,83 @@ func (acuo *AppCategoryUpdateOne) SetNillableCreatedAt(t *time.Time) *AppCategor return acuo } +// AddAppIDs adds the "app" edge to the App entity by IDs. +func (acuo *AppCategoryUpdateOne) AddAppIDs(ids ...model.InternalID) *AppCategoryUpdateOne { + acuo.mutation.AddAppIDs(ids...) + return acuo +} + +// AddApp adds the "app" edges to the App entity. +func (acuo *AppCategoryUpdateOne) AddApp(a ...*App) *AppCategoryUpdateOne { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acuo.AddAppIDs(ids...) +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by IDs. +func (acuo *AppCategoryUpdateOne) AddAppAppCategoryIDs(ids ...int) *AppCategoryUpdateOne { + acuo.mutation.AddAppAppCategoryIDs(ids...) + return acuo +} + +// AddAppAppCategory adds the "app_app_category" edges to the AppAppCategory entity. +func (acuo *AppCategoryUpdateOne) AddAppAppCategory(a ...*AppAppCategory) *AppCategoryUpdateOne { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acuo.AddAppAppCategoryIDs(ids...) +} + // Mutation returns the AppCategoryMutation object of the builder. func (acuo *AppCategoryUpdateOne) Mutation() *AppCategoryMutation { return acuo.mutation } +// ClearApp clears all "app" edges to the App entity. +func (acuo *AppCategoryUpdateOne) ClearApp() *AppCategoryUpdateOne { + acuo.mutation.ClearApp() + return acuo +} + +// RemoveAppIDs removes the "app" edge to App entities by IDs. +func (acuo *AppCategoryUpdateOne) RemoveAppIDs(ids ...model.InternalID) *AppCategoryUpdateOne { + acuo.mutation.RemoveAppIDs(ids...) + return acuo +} + +// RemoveApp removes "app" edges to App entities. +func (acuo *AppCategoryUpdateOne) RemoveApp(a ...*App) *AppCategoryUpdateOne { + ids := make([]model.InternalID, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acuo.RemoveAppIDs(ids...) +} + +// ClearAppAppCategory clears all "app_app_category" edges to the AppAppCategory entity. +func (acuo *AppCategoryUpdateOne) ClearAppAppCategory() *AppCategoryUpdateOne { + acuo.mutation.ClearAppAppCategory() + return acuo +} + +// RemoveAppAppCategoryIDs removes the "app_app_category" edge to AppAppCategory entities by IDs. +func (acuo *AppCategoryUpdateOne) RemoveAppAppCategoryIDs(ids ...int) *AppCategoryUpdateOne { + acuo.mutation.RemoveAppAppCategoryIDs(ids...) + return acuo +} + +// RemoveAppAppCategory removes "app_app_category" edges to AppAppCategory entities. +func (acuo *AppCategoryUpdateOne) RemoveAppAppCategory(a ...*AppAppCategory) *AppCategoryUpdateOne { + ids := make([]int, len(a)) + for i := range a { + ids[i] = a[i].ID + } + return acuo.RemoveAppAppCategoryIDs(ids...) +} + // Where appends a list predicates to the AppCategoryUpdate builder. func (acuo *AppCategoryUpdateOne) Where(ps ...predicate.AppCategory) *AppCategoryUpdateOne { acuo.mutation.Where(ps...) @@ -375,7 +594,7 @@ func (acuo *AppCategoryUpdateOne) defaults() { } func (acuo *AppCategoryUpdateOne) sqlSave(ctx context.Context) (_node *AppCategory, err error) { - _spec := sqlgraph.NewUpdateSpec(appcategory.Table, appcategory.Columns, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt)) + _spec := sqlgraph.NewUpdateSpec(appcategory.Table, appcategory.Columns, sqlgraph.NewFieldSpec(appcategory.FieldID, field.TypeInt64)) id, ok := acuo.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AppCategory.id" for update`)} @@ -406,20 +625,17 @@ func (acuo *AppCategoryUpdateOne) sqlSave(ctx context.Context) (_node *AppCatego if value, ok := acuo.mutation.AddedUserID(); ok { _spec.AddField(appcategory.FieldUserID, field.TypeInt64, value) } - if value, ok := acuo.mutation.AppID(); ok { - _spec.SetField(appcategory.FieldAppID, field.TypeInt64, value) - } - if value, ok := acuo.mutation.AddedAppID(); ok { - _spec.AddField(appcategory.FieldAppID, field.TypeInt64, value) + if value, ok := acuo.mutation.VersionNumber(); ok { + _spec.SetField(appcategory.FieldVersionNumber, field.TypeUint64, value) } - if value, ok := acuo.mutation.StartTime(); ok { - _spec.SetField(appcategory.FieldStartTime, field.TypeTime, value) + if value, ok := acuo.mutation.AddedVersionNumber(); ok { + _spec.AddField(appcategory.FieldVersionNumber, field.TypeUint64, value) } - if value, ok := acuo.mutation.RunDuration(); ok { - _spec.SetField(appcategory.FieldRunDuration, field.TypeInt64, value) + if value, ok := acuo.mutation.VersionDate(); ok { + _spec.SetField(appcategory.FieldVersionDate, field.TypeTime, value) } - if value, ok := acuo.mutation.AddedRunDuration(); ok { - _spec.AddField(appcategory.FieldRunDuration, field.TypeInt64, value) + if value, ok := acuo.mutation.Name(); ok { + _spec.SetField(appcategory.FieldName, field.TypeString, value) } if value, ok := acuo.mutation.UpdatedAt(); ok { _spec.SetField(appcategory.FieldUpdatedAt, field.TypeTime, value) @@ -427,6 +643,96 @@ func (acuo *AppCategoryUpdateOne) sqlSave(ctx context.Context) (_node *AppCatego if value, ok := acuo.mutation.CreatedAt(); ok { _spec.SetField(appcategory.FieldCreatedAt, field.TypeTime, value) } + if acuo.mutation.AppCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := acuo.mutation.RemovedAppIDs(); len(nodes) > 0 && !acuo.mutation.AppCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := acuo.mutation.AppIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.M2M, + Inverse: false, + Table: appcategory.AppTable, + Columns: appcategory.AppPrimaryKey, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(app.FieldID, field.TypeInt64), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } + if acuo.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + _spec.Edges.Clear = append(_spec.Edges.Clear, edge) + } + if nodes := acuo.mutation.RemovedAppAppCategoryIDs(); len(nodes) > 0 && !acuo.mutation.AppAppCategoryCleared() { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.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 := acuo.mutation.AppAppCategoryIDs(); len(nodes) > 0 { + edge := &sqlgraph.EdgeSpec{ + Rel: sqlgraph.O2M, + Inverse: true, + Table: appcategory.AppAppCategoryTable, + Columns: []string{appcategory.AppAppCategoryColumn}, + Bidi: false, + Target: &sqlgraph.EdgeTarget{ + IDSpec: sqlgraph.NewFieldSpec(appappcategory.FieldID, field.TypeInt), + }, + } + for _, k := range nodes { + edge.Target.Nodes = append(edge.Target.Nodes, k) + } + _spec.Edges.Add = append(_spec.Edges.Add, edge) + } _node = &AppCategory{config: acuo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues diff --git a/internal/data/internal/ent/client.go b/internal/data/internal/ent/client.go index c99a553b..d2657bca 100644 --- a/internal/data/internal/ent/client.go +++ b/internal/data/internal/ent/client.go @@ -18,6 +18,7 @@ import ( "entgo.io/ent/dialect/sql/sqlgraph" "github.com/tuihub/librarian/internal/data/internal/ent/account" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appinfo" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" @@ -54,6 +55,8 @@ type Client struct { Account *AccountClient // App is the client for interacting with the App builders. App *AppClient + // AppAppCategory is the client for interacting with the AppAppCategory builders. + AppAppCategory *AppAppCategoryClient // AppCategory is the client for interacting with the AppCategory builders. AppCategory *AppCategoryClient // AppInfo is the client for interacting with the AppInfo builders. @@ -117,6 +120,7 @@ func (c *Client) init() { c.Schema = migrate.NewSchema(c.driver) c.Account = NewAccountClient(c.config) c.App = NewAppClient(c.config) + c.AppAppCategory = NewAppAppCategoryClient(c.config) c.AppCategory = NewAppCategoryClient(c.config) c.AppInfo = NewAppInfoClient(c.config) c.AppRunTime = NewAppRunTimeClient(c.config) @@ -236,6 +240,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { config: cfg, Account: NewAccountClient(cfg), App: NewAppClient(cfg), + AppAppCategory: NewAppAppCategoryClient(cfg), AppCategory: NewAppCategoryClient(cfg), AppInfo: NewAppInfoClient(cfg), AppRunTime: NewAppRunTimeClient(cfg), @@ -282,6 +287,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) config: cfg, Account: NewAccountClient(cfg), App: NewAppClient(cfg), + AppAppCategory: NewAppAppCategoryClient(cfg), AppCategory: NewAppCategoryClient(cfg), AppInfo: NewAppInfoClient(cfg), AppRunTime: NewAppRunTimeClient(cfg), @@ -336,11 +342,11 @@ func (c *Client) Close() error { // 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.Account, c.App, c.AppCategory, c.AppInfo, c.AppRunTime, c.Device, c.Feed, - c.FeedActionSet, c.FeedConfig, c.FeedConfigAction, c.FeedItem, - c.FeedItemCollection, c.File, c.Image, c.NotifyFlow, c.NotifyFlowSource, - c.NotifyFlowTarget, c.NotifySource, c.NotifyTarget, c.PorterContext, - c.PorterInstance, c.Session, c.StoreApp, c.StoreAppBinary, + c.Account, c.App, c.AppAppCategory, c.AppCategory, c.AppInfo, c.AppRunTime, + c.Device, c.Feed, c.FeedActionSet, c.FeedConfig, c.FeedConfigAction, + c.FeedItem, c.FeedItemCollection, c.File, c.Image, c.NotifyFlow, + c.NotifyFlowSource, c.NotifyFlowTarget, c.NotifySource, c.NotifyTarget, + c.PorterContext, c.PorterInstance, c.Session, c.StoreApp, c.StoreAppBinary, c.SystemNotification, c.Tag, c.User, } { n.Use(hooks...) @@ -351,11 +357,11 @@ func (c *Client) Use(hooks ...Hook) { // 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.Account, c.App, c.AppCategory, c.AppInfo, c.AppRunTime, c.Device, c.Feed, - c.FeedActionSet, c.FeedConfig, c.FeedConfigAction, c.FeedItem, - c.FeedItemCollection, c.File, c.Image, c.NotifyFlow, c.NotifyFlowSource, - c.NotifyFlowTarget, c.NotifySource, c.NotifyTarget, c.PorterContext, - c.PorterInstance, c.Session, c.StoreApp, c.StoreAppBinary, + c.Account, c.App, c.AppAppCategory, c.AppCategory, c.AppInfo, c.AppRunTime, + c.Device, c.Feed, c.FeedActionSet, c.FeedConfig, c.FeedConfigAction, + c.FeedItem, c.FeedItemCollection, c.File, c.Image, c.NotifyFlow, + c.NotifyFlowSource, c.NotifyFlowTarget, c.NotifySource, c.NotifyTarget, + c.PorterContext, c.PorterInstance, c.Session, c.StoreApp, c.StoreAppBinary, c.SystemNotification, c.Tag, c.User, } { n.Intercept(interceptors...) @@ -369,6 +375,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { return c.Account.mutate(ctx, m) case *AppMutation: return c.App.mutate(ctx, m) + case *AppAppCategoryMutation: + return c.AppAppCategory.mutate(ctx, m) case *AppCategoryMutation: return c.AppCategory.mutate(ctx, m) case *AppInfoMutation: @@ -729,6 +737,38 @@ func (c *AppClient) QueryAppRunTime(a *App) *AppRunTimeQuery { return query } +// QueryAppCategory queries the app_category edge of a App. +func (c *AppClient) QueryAppCategory(a *App) *AppCategoryQuery { + query := (&AppCategoryClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := a.ID + step := sqlgraph.NewStep( + sqlgraph.From(app.Table, app.FieldID, id), + sqlgraph.To(appcategory.Table, appcategory.FieldID), + sqlgraph.Edge(sqlgraph.M2M, true, app.AppCategoryTable, app.AppCategoryPrimaryKey...), + ) + fromV = sqlgraph.Neighbors(a.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// QueryAppAppCategory queries the app_app_category edge of a App. +func (c *AppClient) QueryAppAppCategory(a *App) *AppAppCategoryQuery { + query := (&AppAppCategoryClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := a.ID + step := sqlgraph.NewStep( + sqlgraph.From(app.Table, app.FieldID, id), + sqlgraph.To(appappcategory.Table, appappcategory.FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, app.AppAppCategoryTable, app.AppAppCategoryColumn), + ) + fromV = sqlgraph.Neighbors(a.driver.Dialect(), step) + return fromV, nil + } + return query +} + // Hooks returns the client hooks. func (c *AppClient) Hooks() []Hook { return c.hooks.App @@ -754,6 +794,171 @@ func (c *AppClient) mutate(ctx context.Context, m *AppMutation) (Value, error) { } } +// AppAppCategoryClient is a client for the AppAppCategory schema. +type AppAppCategoryClient struct { + config +} + +// NewAppAppCategoryClient returns a client for the AppAppCategory from the given config. +func NewAppAppCategoryClient(c config) *AppAppCategoryClient { + return &AppAppCategoryClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `appappcategory.Hooks(f(g(h())))`. +func (c *AppAppCategoryClient) Use(hooks ...Hook) { + c.hooks.AppAppCategory = append(c.hooks.AppAppCategory, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `appappcategory.Intercept(f(g(h())))`. +func (c *AppAppCategoryClient) Intercept(interceptors ...Interceptor) { + c.inters.AppAppCategory = append(c.inters.AppAppCategory, interceptors...) +} + +// Create returns a builder for creating a AppAppCategory entity. +func (c *AppAppCategoryClient) Create() *AppAppCategoryCreate { + mutation := newAppAppCategoryMutation(c.config, OpCreate) + return &AppAppCategoryCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of AppAppCategory entities. +func (c *AppAppCategoryClient) CreateBulk(builders ...*AppAppCategoryCreate) *AppAppCategoryCreateBulk { + return &AppAppCategoryCreateBulk{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 *AppAppCategoryClient) MapCreateBulk(slice any, setFunc func(*AppAppCategoryCreate, int)) *AppAppCategoryCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &AppAppCategoryCreateBulk{err: fmt.Errorf("calling to AppAppCategoryClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*AppAppCategoryCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &AppAppCategoryCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for AppAppCategory. +func (c *AppAppCategoryClient) Update() *AppAppCategoryUpdate { + mutation := newAppAppCategoryMutation(c.config, OpUpdate) + return &AppAppCategoryUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *AppAppCategoryClient) UpdateOne(aac *AppAppCategory) *AppAppCategoryUpdateOne { + mutation := newAppAppCategoryMutation(c.config, OpUpdateOne, withAppAppCategory(aac)) + return &AppAppCategoryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *AppAppCategoryClient) UpdateOneID(id int) *AppAppCategoryUpdateOne { + mutation := newAppAppCategoryMutation(c.config, OpUpdateOne, withAppAppCategoryID(id)) + return &AppAppCategoryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for AppAppCategory. +func (c *AppAppCategoryClient) Delete() *AppAppCategoryDelete { + mutation := newAppAppCategoryMutation(c.config, OpDelete) + return &AppAppCategoryDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *AppAppCategoryClient) DeleteOne(aac *AppAppCategory) *AppAppCategoryDeleteOne { + return c.DeleteOneID(aac.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *AppAppCategoryClient) DeleteOneID(id int) *AppAppCategoryDeleteOne { + builder := c.Delete().Where(appappcategory.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &AppAppCategoryDeleteOne{builder} +} + +// Query returns a query builder for AppAppCategory. +func (c *AppAppCategoryClient) Query() *AppAppCategoryQuery { + return &AppAppCategoryQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeAppAppCategory}, + inters: c.Interceptors(), + } +} + +// Get returns a AppAppCategory entity by its id. +func (c *AppAppCategoryClient) Get(ctx context.Context, id int) (*AppAppCategory, error) { + return c.Query().Where(appappcategory.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *AppAppCategoryClient) GetX(ctx context.Context, id int) *AppAppCategory { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryAppCategory queries the app_category edge of a AppAppCategory. +func (c *AppAppCategoryClient) QueryAppCategory(aac *AppAppCategory) *AppCategoryQuery { + query := (&AppCategoryClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := aac.ID + step := sqlgraph.NewStep( + sqlgraph.From(appappcategory.Table, appappcategory.FieldID, id), + sqlgraph.To(appcategory.Table, appcategory.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, appappcategory.AppCategoryTable, appappcategory.AppCategoryColumn), + ) + fromV = sqlgraph.Neighbors(aac.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// QueryApp queries the app edge of a AppAppCategory. +func (c *AppAppCategoryClient) QueryApp(aac *AppAppCategory) *AppQuery { + query := (&AppClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := aac.ID + step := sqlgraph.NewStep( + sqlgraph.From(appappcategory.Table, appappcategory.FieldID, id), + sqlgraph.To(app.Table, app.FieldID), + sqlgraph.Edge(sqlgraph.M2O, false, appappcategory.AppTable, appappcategory.AppColumn), + ) + fromV = sqlgraph.Neighbors(aac.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *AppAppCategoryClient) Hooks() []Hook { + return c.hooks.AppAppCategory +} + +// Interceptors returns the client interceptors. +func (c *AppAppCategoryClient) Interceptors() []Interceptor { + return c.inters.AppAppCategory +} + +func (c *AppAppCategoryClient) mutate(ctx context.Context, m *AppAppCategoryMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&AppAppCategoryCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&AppAppCategoryUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&AppAppCategoryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&AppAppCategoryDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown AppAppCategory mutation op: %q", m.Op()) + } +} + // AppCategoryClient is a client for the AppCategory schema. type AppCategoryClient struct { config @@ -815,7 +1020,7 @@ func (c *AppCategoryClient) UpdateOne(ac *AppCategory) *AppCategoryUpdateOne { } // UpdateOneID returns an update builder for the given id. -func (c *AppCategoryClient) UpdateOneID(id int) *AppCategoryUpdateOne { +func (c *AppCategoryClient) UpdateOneID(id model.InternalID) *AppCategoryUpdateOne { mutation := newAppCategoryMutation(c.config, OpUpdateOne, withAppCategoryID(id)) return &AppCategoryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } @@ -832,7 +1037,7 @@ func (c *AppCategoryClient) DeleteOne(ac *AppCategory) *AppCategoryDeleteOne { } // DeleteOneID returns a builder for deleting the given entity by its id. -func (c *AppCategoryClient) DeleteOneID(id int) *AppCategoryDeleteOne { +func (c *AppCategoryClient) DeleteOneID(id model.InternalID) *AppCategoryDeleteOne { builder := c.Delete().Where(appcategory.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne @@ -849,12 +1054,12 @@ func (c *AppCategoryClient) Query() *AppCategoryQuery { } // Get returns a AppCategory entity by its id. -func (c *AppCategoryClient) Get(ctx context.Context, id int) (*AppCategory, error) { +func (c *AppCategoryClient) Get(ctx context.Context, id model.InternalID) (*AppCategory, error) { return c.Query().Where(appcategory.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. -func (c *AppCategoryClient) GetX(ctx context.Context, id int) *AppCategory { +func (c *AppCategoryClient) GetX(ctx context.Context, id model.InternalID) *AppCategory { obj, err := c.Get(ctx, id) if err != nil { panic(err) @@ -862,6 +1067,38 @@ func (c *AppCategoryClient) GetX(ctx context.Context, id int) *AppCategory { return obj } +// QueryApp queries the app edge of a AppCategory. +func (c *AppCategoryClient) QueryApp(ac *AppCategory) *AppQuery { + query := (&AppClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := ac.ID + step := sqlgraph.NewStep( + sqlgraph.From(appcategory.Table, appcategory.FieldID, id), + sqlgraph.To(app.Table, app.FieldID), + sqlgraph.Edge(sqlgraph.M2M, false, appcategory.AppTable, appcategory.AppPrimaryKey...), + ) + fromV = sqlgraph.Neighbors(ac.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// QueryAppAppCategory queries the app_app_category edge of a AppCategory. +func (c *AppCategoryClient) QueryAppAppCategory(ac *AppCategory) *AppAppCategoryQuery { + query := (&AppAppCategoryClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := ac.ID + step := sqlgraph.NewStep( + sqlgraph.From(appcategory.Table, appcategory.FieldID, id), + sqlgraph.To(appappcategory.Table, appappcategory.FieldID), + sqlgraph.Edge(sqlgraph.O2M, true, appcategory.AppAppCategoryTable, appcategory.AppAppCategoryColumn), + ) + fromV = sqlgraph.Neighbors(ac.driver.Dialect(), step) + return fromV, nil + } + return query +} + // Hooks returns the client hooks. func (c *AppCategoryClient) Hooks() []Hook { return c.hooks.AppCategory @@ -5026,17 +5263,17 @@ func (c *UserClient) mutate(ctx context.Context, m *UserMutation) (Value, error) // hooks and interceptors per client, for fast access. type ( hooks struct { - Account, App, AppCategory, AppInfo, AppRunTime, Device, Feed, FeedActionSet, - FeedConfig, FeedConfigAction, FeedItem, FeedItemCollection, File, Image, - NotifyFlow, NotifyFlowSource, NotifyFlowTarget, NotifySource, NotifyTarget, - PorterContext, PorterInstance, Session, StoreApp, StoreAppBinary, + Account, App, AppAppCategory, AppCategory, AppInfo, AppRunTime, Device, Feed, + FeedActionSet, FeedConfig, FeedConfigAction, FeedItem, FeedItemCollection, + File, Image, NotifyFlow, NotifyFlowSource, NotifyFlowTarget, NotifySource, + NotifyTarget, PorterContext, PorterInstance, Session, StoreApp, StoreAppBinary, SystemNotification, Tag, User []ent.Hook } inters struct { - Account, App, AppCategory, AppInfo, AppRunTime, Device, Feed, FeedActionSet, - FeedConfig, FeedConfigAction, FeedItem, FeedItemCollection, File, Image, - NotifyFlow, NotifyFlowSource, NotifyFlowTarget, NotifySource, NotifyTarget, - PorterContext, PorterInstance, Session, StoreApp, StoreAppBinary, + Account, App, AppAppCategory, AppCategory, AppInfo, AppRunTime, Device, Feed, + FeedActionSet, FeedConfig, FeedConfigAction, FeedItem, FeedItemCollection, + File, Image, NotifyFlow, NotifyFlowSource, NotifyFlowTarget, NotifySource, + NotifyTarget, PorterContext, PorterInstance, Session, StoreApp, StoreAppBinary, SystemNotification, Tag, User []ent.Interceptor } ) diff --git a/internal/data/internal/ent/ent.go b/internal/data/internal/ent/ent.go index e9326ebd..e6fd38df 100644 --- a/internal/data/internal/ent/ent.go +++ b/internal/data/internal/ent/ent.go @@ -14,6 +14,7 @@ import ( "entgo.io/ent/dialect/sql/sqlgraph" "github.com/tuihub/librarian/internal/data/internal/ent/account" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appinfo" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" @@ -101,6 +102,7 @@ func checkColumn(table, column string) error { columnCheck = sql.NewColumnCheck(map[string]func(string) bool{ account.Table: account.ValidColumn, app.Table: app.ValidColumn, + appappcategory.Table: appappcategory.ValidColumn, appcategory.Table: appcategory.ValidColumn, appinfo.Table: appinfo.ValidColumn, appruntime.Table: appruntime.ValidColumn, diff --git a/internal/data/internal/ent/hook/hook.go b/internal/data/internal/ent/hook/hook.go index b0bd6719..4f933b73 100644 --- a/internal/data/internal/ent/hook/hook.go +++ b/internal/data/internal/ent/hook/hook.go @@ -33,6 +33,18 @@ func (f AppFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AppMutation", m) } +// The AppAppCategoryFunc type is an adapter to allow the use of ordinary +// function as AppAppCategory mutator. +type AppAppCategoryFunc func(context.Context, *ent.AppAppCategoryMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f AppAppCategoryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.AppAppCategoryMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AppAppCategoryMutation", m) +} + // The AppCategoryFunc type is an adapter to allow the use of ordinary // function as AppCategory mutator. type AppCategoryFunc func(context.Context, *ent.AppCategoryMutation) (ent.Value, error) diff --git a/internal/data/internal/ent/migrate/schema.go b/internal/data/internal/ent/migrate/schema.go index b26c7ff2..e04c9756 100644 --- a/internal/data/internal/ent/migrate/schema.go +++ b/internal/data/internal/ent/migrate/schema.go @@ -97,13 +97,46 @@ var ( }, }, } + // AppAppCategoriesColumns holds the columns for the "app_app_categories" table. + AppAppCategoriesColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "app_category_id", Type: field.TypeInt64}, + {Name: "app_id", Type: field.TypeInt64}, + } + // AppAppCategoriesTable holds the schema information for the "app_app_categories" table. + AppAppCategoriesTable = &schema.Table{ + Name: "app_app_categories", + Columns: AppAppCategoriesColumns, + PrimaryKey: []*schema.Column{AppAppCategoriesColumns[0]}, + ForeignKeys: []*schema.ForeignKey{ + { + Symbol: "app_app_categories_app_categories_app_category", + Columns: []*schema.Column{AppAppCategoriesColumns[1]}, + RefColumns: []*schema.Column{AppCategoriesColumns[0]}, + OnDelete: schema.NoAction, + }, + { + Symbol: "app_app_categories_apps_app", + Columns: []*schema.Column{AppAppCategoriesColumns[2]}, + RefColumns: []*schema.Column{AppsColumns[0]}, + OnDelete: schema.NoAction, + }, + }, + Indexes: []*schema.Index{ + { + Name: "appappcategory_app_category_id_app_id", + Unique: true, + Columns: []*schema.Column{AppAppCategoriesColumns[1], AppAppCategoriesColumns[2]}, + }, + }, + } // AppCategoriesColumns holds the columns for the "app_categories" table. AppCategoriesColumns = []*schema.Column{ - {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "id", Type: field.TypeInt64}, {Name: "user_id", Type: field.TypeInt64}, - {Name: "app_id", Type: field.TypeInt64}, - {Name: "start_time", Type: field.TypeTime}, - {Name: "run_duration", Type: field.TypeInt64}, + {Name: "version_number", Type: field.TypeUint64}, + {Name: "version_date", Type: field.TypeTime}, + {Name: "name", Type: field.TypeString}, {Name: "updated_at", Type: field.TypeTime}, {Name: "created_at", Type: field.TypeTime}, } @@ -114,9 +147,9 @@ var ( PrimaryKey: []*schema.Column{AppCategoriesColumns[0]}, Indexes: []*schema.Index{ { - Name: "appcategory_user_id_app_id_start_time_run_duration", - Unique: true, - Columns: []*schema.Column{AppCategoriesColumns[1], AppCategoriesColumns[2], AppCategoriesColumns[3], AppCategoriesColumns[4]}, + Name: "appcategory_user_id", + Unique: false, + Columns: []*schema.Column{AppCategoriesColumns[1]}, }, }, } @@ -878,6 +911,7 @@ var ( Tables = []*schema.Table{ AccountsTable, AppsTable, + AppAppCategoriesTable, AppCategoriesTable, AppInfosTable, AppRunTimesTable, @@ -911,6 +945,8 @@ func init() { AccountsTable.ForeignKeys[0].RefTable = UsersTable AppsTable.ForeignKeys[0].RefTable = DevicesTable AppsTable.ForeignKeys[1].RefTable = UsersTable + AppAppCategoriesTable.ForeignKeys[0].RefTable = AppCategoriesTable + AppAppCategoriesTable.ForeignKeys[1].RefTable = AppsTable AppRunTimesTable.ForeignKeys[0].RefTable = AppsTable FeedsTable.ForeignKeys[0].RefTable = FeedConfigsTable FeedActionSetsTable.ForeignKeys[0].RefTable = UsersTable diff --git a/internal/data/internal/ent/mutation.go b/internal/data/internal/ent/mutation.go index a077813e..8cf5a1e0 100644 --- a/internal/data/internal/ent/mutation.go +++ b/internal/data/internal/ent/mutation.go @@ -13,6 +13,7 @@ import ( "entgo.io/ent/dialect/sql" "github.com/tuihub/librarian/internal/data/internal/ent/account" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appinfo" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" @@ -55,6 +56,7 @@ const ( // Node types. TypeAccount = "Account" TypeApp = "App" + TypeAppAppCategory = "AppAppCategory" TypeAppCategory = "AppCategory" TypeAppInfo = "AppInfo" TypeAppRunTime = "AppRunTime" @@ -915,6 +917,12 @@ type AppMutation struct { app_run_time map[model.InternalID]struct{} removedapp_run_time map[model.InternalID]struct{} clearedapp_run_time bool + app_category map[model.InternalID]struct{} + removedapp_category map[model.InternalID]struct{} + clearedapp_category bool + app_app_category map[int]struct{} + removedapp_app_category map[int]struct{} + clearedapp_app_category bool done bool oldValue func(context.Context) (*App, error) predicates []predicate.App @@ -2306,6 +2314,114 @@ func (m *AppMutation) ResetAppRunTime() { m.removedapp_run_time = nil } +// AddAppCategoryIDs adds the "app_category" edge to the AppCategory entity by ids. +func (m *AppMutation) AddAppCategoryIDs(ids ...model.InternalID) { + if m.app_category == nil { + m.app_category = make(map[model.InternalID]struct{}) + } + for i := range ids { + m.app_category[ids[i]] = struct{}{} + } +} + +// ClearAppCategory clears the "app_category" edge to the AppCategory entity. +func (m *AppMutation) ClearAppCategory() { + m.clearedapp_category = true +} + +// AppCategoryCleared reports if the "app_category" edge to the AppCategory entity was cleared. +func (m *AppMutation) AppCategoryCleared() bool { + return m.clearedapp_category +} + +// RemoveAppCategoryIDs removes the "app_category" edge to the AppCategory entity by IDs. +func (m *AppMutation) RemoveAppCategoryIDs(ids ...model.InternalID) { + if m.removedapp_category == nil { + m.removedapp_category = make(map[model.InternalID]struct{}) + } + for i := range ids { + delete(m.app_category, ids[i]) + m.removedapp_category[ids[i]] = struct{}{} + } +} + +// RemovedAppCategory returns the removed IDs of the "app_category" edge to the AppCategory entity. +func (m *AppMutation) RemovedAppCategoryIDs() (ids []model.InternalID) { + for id := range m.removedapp_category { + ids = append(ids, id) + } + return +} + +// AppCategoryIDs returns the "app_category" edge IDs in the mutation. +func (m *AppMutation) AppCategoryIDs() (ids []model.InternalID) { + for id := range m.app_category { + ids = append(ids, id) + } + return +} + +// ResetAppCategory resets all changes to the "app_category" edge. +func (m *AppMutation) ResetAppCategory() { + m.app_category = nil + m.clearedapp_category = false + m.removedapp_category = nil +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by ids. +func (m *AppMutation) AddAppAppCategoryIDs(ids ...int) { + if m.app_app_category == nil { + m.app_app_category = make(map[int]struct{}) + } + for i := range ids { + m.app_app_category[ids[i]] = struct{}{} + } +} + +// ClearAppAppCategory clears the "app_app_category" edge to the AppAppCategory entity. +func (m *AppMutation) ClearAppAppCategory() { + m.clearedapp_app_category = true +} + +// AppAppCategoryCleared reports if the "app_app_category" edge to the AppAppCategory entity was cleared. +func (m *AppMutation) AppAppCategoryCleared() bool { + return m.clearedapp_app_category +} + +// RemoveAppAppCategoryIDs removes the "app_app_category" edge to the AppAppCategory entity by IDs. +func (m *AppMutation) RemoveAppAppCategoryIDs(ids ...int) { + if m.removedapp_app_category == nil { + m.removedapp_app_category = make(map[int]struct{}) + } + for i := range ids { + delete(m.app_app_category, ids[i]) + m.removedapp_app_category[ids[i]] = struct{}{} + } +} + +// RemovedAppAppCategory returns the removed IDs of the "app_app_category" edge to the AppAppCategory entity. +func (m *AppMutation) RemovedAppAppCategoryIDs() (ids []int) { + for id := range m.removedapp_app_category { + ids = append(ids, id) + } + return +} + +// AppAppCategoryIDs returns the "app_app_category" edge IDs in the mutation. +func (m *AppMutation) AppAppCategoryIDs() (ids []int) { + for id := range m.app_app_category { + ids = append(ids, id) + } + return +} + +// ResetAppAppCategory resets all changes to the "app_app_category" edge. +func (m *AppMutation) ResetAppAppCategory() { + m.app_app_category = nil + m.clearedapp_app_category = false + m.removedapp_app_category = nil +} + // Where appends a list predicates to the AppMutation builder. func (m *AppMutation) Where(ps ...predicate.App) { m.predicates = append(m.predicates, ps...) @@ -2973,7 +3089,7 @@ func (m *AppMutation) ResetField(name string) error { // AddedEdges returns all edge names that were set/added in this mutation. func (m *AppMutation) AddedEdges() []string { - edges := make([]string, 0, 3) + edges := make([]string, 0, 5) if m.user != nil { edges = append(edges, app.EdgeUser) } @@ -2983,6 +3099,12 @@ func (m *AppMutation) AddedEdges() []string { if m.app_run_time != nil { edges = append(edges, app.EdgeAppRunTime) } + if m.app_category != nil { + edges = append(edges, app.EdgeAppCategory) + } + if m.app_app_category != nil { + edges = append(edges, app.EdgeAppAppCategory) + } return edges } @@ -3004,16 +3126,34 @@ func (m *AppMutation) AddedIDs(name string) []ent.Value { ids = append(ids, id) } return ids + case app.EdgeAppCategory: + ids := make([]ent.Value, 0, len(m.app_category)) + for id := range m.app_category { + ids = append(ids, id) + } + return ids + case app.EdgeAppAppCategory: + ids := make([]ent.Value, 0, len(m.app_app_category)) + for id := range m.app_app_category { + ids = append(ids, id) + } + return ids } return nil } // RemovedEdges returns all edge names that were removed in this mutation. func (m *AppMutation) RemovedEdges() []string { - edges := make([]string, 0, 3) + edges := make([]string, 0, 5) if m.removedapp_run_time != nil { edges = append(edges, app.EdgeAppRunTime) } + if m.removedapp_category != nil { + edges = append(edges, app.EdgeAppCategory) + } + if m.removedapp_app_category != nil { + edges = append(edges, app.EdgeAppAppCategory) + } return edges } @@ -3027,13 +3167,25 @@ func (m *AppMutation) RemovedIDs(name string) []ent.Value { ids = append(ids, id) } return ids + case app.EdgeAppCategory: + ids := make([]ent.Value, 0, len(m.removedapp_category)) + for id := range m.removedapp_category { + ids = append(ids, id) + } + return ids + case app.EdgeAppAppCategory: + ids := make([]ent.Value, 0, len(m.removedapp_app_category)) + for id := range m.removedapp_app_category { + ids = append(ids, id) + } + return ids } return nil } // ClearedEdges returns all edge names that were cleared in this mutation. func (m *AppMutation) ClearedEdges() []string { - edges := make([]string, 0, 3) + edges := make([]string, 0, 5) if m.cleareduser { edges = append(edges, app.EdgeUser) } @@ -3043,6 +3195,12 @@ func (m *AppMutation) ClearedEdges() []string { if m.clearedapp_run_time { edges = append(edges, app.EdgeAppRunTime) } + if m.clearedapp_category { + edges = append(edges, app.EdgeAppCategory) + } + if m.clearedapp_app_category { + edges = append(edges, app.EdgeAppAppCategory) + } return edges } @@ -3056,6 +3214,10 @@ func (m *AppMutation) EdgeCleared(name string) bool { return m.cleareddevice case app.EdgeAppRunTime: return m.clearedapp_run_time + case app.EdgeAppCategory: + return m.clearedapp_category + case app.EdgeAppAppCategory: + return m.clearedapp_app_category } return false } @@ -3087,42 +3249,43 @@ func (m *AppMutation) ResetEdge(name string) error { case app.EdgeAppRunTime: m.ResetAppRunTime() return nil + case app.EdgeAppCategory: + m.ResetAppCategory() + return nil + case app.EdgeAppAppCategory: + m.ResetAppAppCategory() + return nil } return fmt.Errorf("unknown App edge %s", name) } -// AppCategoryMutation represents an operation that mutates the AppCategory nodes in the graph. -type AppCategoryMutation struct { +// AppAppCategoryMutation represents an operation that mutates the AppAppCategory nodes in the graph. +type AppAppCategoryMutation struct { config - op Op - typ string - id *int - user_id *model.InternalID - adduser_id *model.InternalID - app_id *model.InternalID - addapp_id *model.InternalID - start_time *time.Time - run_duration *time.Duration - addrun_duration *time.Duration - updated_at *time.Time - created_at *time.Time - clearedFields map[string]struct{} - done bool - oldValue func(context.Context) (*AppCategory, error) - predicates []predicate.AppCategory + op Op + typ string + id *int + clearedFields map[string]struct{} + app_category *model.InternalID + clearedapp_category bool + app *model.InternalID + clearedapp bool + done bool + oldValue func(context.Context) (*AppAppCategory, error) + predicates []predicate.AppAppCategory } -var _ ent.Mutation = (*AppCategoryMutation)(nil) +var _ ent.Mutation = (*AppAppCategoryMutation)(nil) -// appcategoryOption allows management of the mutation configuration using functional options. -type appcategoryOption func(*AppCategoryMutation) +// appappcategoryOption allows management of the mutation configuration using functional options. +type appappcategoryOption func(*AppAppCategoryMutation) -// newAppCategoryMutation creates new mutation for the AppCategory entity. -func newAppCategoryMutation(c config, op Op, opts ...appcategoryOption) *AppCategoryMutation { - m := &AppCategoryMutation{ +// newAppAppCategoryMutation creates new mutation for the AppAppCategory entity. +func newAppAppCategoryMutation(c config, op Op, opts ...appappcategoryOption) *AppAppCategoryMutation { + m := &AppAppCategoryMutation{ config: c, op: op, - typ: TypeAppCategory, + typ: TypeAppAppCategory, clearedFields: make(map[string]struct{}), } for _, opt := range opts { @@ -3131,20 +3294,20 @@ func newAppCategoryMutation(c config, op Op, opts ...appcategoryOption) *AppCate return m } -// withAppCategoryID sets the ID field of the mutation. -func withAppCategoryID(id int) appcategoryOption { - return func(m *AppCategoryMutation) { +// withAppAppCategoryID sets the ID field of the mutation. +func withAppAppCategoryID(id int) appappcategoryOption { + return func(m *AppAppCategoryMutation) { var ( err error once sync.Once - value *AppCategory + value *AppAppCategory ) - m.oldValue = func(ctx context.Context) (*AppCategory, error) { + m.oldValue = func(ctx context.Context) (*AppAppCategory, error) { once.Do(func() { if m.done { err = errors.New("querying old values post mutation is not allowed") } else { - value, err = m.Client().AppCategory.Get(ctx, id) + value, err = m.Client().AppAppCategory.Get(ctx, id) } }) return value, err @@ -3153,10 +3316,10 @@ func withAppCategoryID(id int) appcategoryOption { } } -// withAppCategory sets the old AppCategory of the mutation. -func withAppCategory(node *AppCategory) appcategoryOption { - return func(m *AppCategoryMutation) { - m.oldValue = func(context.Context) (*AppCategory, error) { +// withAppAppCategory sets the old AppAppCategory of the mutation. +func withAppAppCategory(node *AppAppCategory) appappcategoryOption { + return func(m *AppAppCategoryMutation) { + m.oldValue = func(context.Context) (*AppAppCategory, error) { return node, nil } m.id = &node.ID @@ -3165,7 +3328,7 @@ func withAppCategory(node *AppCategory) appcategoryOption { // 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 AppCategoryMutation) Client() *Client { +func (m AppAppCategoryMutation) Client() *Client { client := &Client{config: m.config} client.init() return client @@ -3173,7 +3336,7 @@ func (m AppCategoryMutation) Client() *Client { // Tx returns an `ent.Tx` for mutations that were executed in transactions; // it returns an error otherwise. -func (m AppCategoryMutation) Tx() (*Tx, error) { +func (m AppAppCategoryMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { return nil, errors.New("ent: mutation is not running in a transaction") } @@ -3184,7 +3347,7 @@ func (m AppCategoryMutation) Tx() (*Tx, error) { // 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 *AppCategoryMutation) ID() (id int, exists bool) { +func (m *AppAppCategoryMutation) ID() (id int, exists bool) { if m.id == nil { return } @@ -3195,7 +3358,7 @@ func (m *AppCategoryMutation) ID() (id int, exists bool) { // 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 *AppCategoryMutation) IDs(ctx context.Context) ([]int, error) { +func (m *AppAppCategoryMutation) IDs(ctx context.Context) ([]int, error) { switch { case m.op.Is(OpUpdateOne | OpDeleteOne): id, exists := m.ID() @@ -3204,87 +3367,66 @@ func (m *AppCategoryMutation) IDs(ctx context.Context) ([]int, error) { } fallthrough case m.op.Is(OpUpdate | OpDelete): - return m.Client().AppCategory.Query().Where(m.predicates...).IDs(ctx) + return m.Client().AppAppCategory.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 *AppCategoryMutation) SetUserID(mi model.InternalID) { - m.user_id = &mi - m.adduser_id = nil +// SetAppCategoryID sets the "app_category_id" field. +func (m *AppAppCategoryMutation) SetAppCategoryID(mi model.InternalID) { + m.app_category = &mi } -// UserID returns the value of the "user_id" field in the mutation. -func (m *AppCategoryMutation) UserID() (r model.InternalID, exists bool) { - v := m.user_id +// AppCategoryID returns the value of the "app_category_id" field in the mutation. +func (m *AppAppCategoryMutation) AppCategoryID() (r model.InternalID, exists bool) { + v := m.app_category if v == nil { return } return *v, true } -// OldUserID returns the old "user_id" field's value of the AppCategory entity. -// If the AppCategory object wasn't provided to the builder, the object is fetched from the database. +// OldAppCategoryID returns the old "app_category_id" field's value of the AppAppCategory entity. +// If the AppAppCategory 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 *AppCategoryMutation) OldUserID(ctx context.Context) (v model.InternalID, err error) { +func (m *AppAppCategoryMutation) OldAppCategoryID(ctx context.Context) (v model.InternalID, err error) { if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldUserID is only allowed on UpdateOne operations") + return v, errors.New("OldAppCategoryID 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") + return v, errors.New("OldAppCategoryID 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 -} - -// AddUserID adds mi to the "user_id" field. -func (m *AppCategoryMutation) AddUserID(mi model.InternalID) { - if m.adduser_id != nil { - *m.adduser_id += mi - } else { - m.adduser_id = &mi - } -} - -// AddedUserID returns the value that was added to the "user_id" field in this mutation. -func (m *AppCategoryMutation) AddedUserID() (r model.InternalID, exists bool) { - v := m.adduser_id - if v == nil { - return + return v, fmt.Errorf("querying old value for OldAppCategoryID: %w", err) } - return *v, true + return oldValue.AppCategoryID, nil } -// ResetUserID resets all changes to the "user_id" field. -func (m *AppCategoryMutation) ResetUserID() { - m.user_id = nil - m.adduser_id = nil +// ResetAppCategoryID resets all changes to the "app_category_id" field. +func (m *AppAppCategoryMutation) ResetAppCategoryID() { + m.app_category = nil } // SetAppID sets the "app_id" field. -func (m *AppCategoryMutation) SetAppID(mi model.InternalID) { - m.app_id = &mi - m.addapp_id = nil +func (m *AppAppCategoryMutation) SetAppID(mi model.InternalID) { + m.app = &mi } // AppID returns the value of the "app_id" field in the mutation. -func (m *AppCategoryMutation) AppID() (r model.InternalID, exists bool) { - v := m.app_id +func (m *AppAppCategoryMutation) AppID() (r model.InternalID, exists bool) { + v := m.app if v == nil { return } return *v, true } -// OldAppID returns the old "app_id" field's value of the AppCategory entity. -// If the AppCategory object wasn't provided to the builder, the object is fetched from the database. +// OldAppID returns the old "app_id" field's value of the AppAppCategory entity. +// If the AppAppCategory 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 *AppCategoryMutation) OldAppID(ctx context.Context) (v model.InternalID, err error) { +func (m *AppAppCategoryMutation) OldAppID(ctx context.Context) (v model.InternalID, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldAppID is only allowed on UpdateOne operations") } @@ -3298,156 +3440,656 @@ func (m *AppCategoryMutation) OldAppID(ctx context.Context) (v model.InternalID, return oldValue.AppID, nil } -// AddAppID adds mi to the "app_id" field. -func (m *AppCategoryMutation) AddAppID(mi model.InternalID) { - if m.addapp_id != nil { - *m.addapp_id += mi - } else { - m.addapp_id = &mi - } -} - -// AddedAppID returns the value that was added to the "app_id" field in this mutation. -func (m *AppCategoryMutation) AddedAppID() (r model.InternalID, exists bool) { - v := m.addapp_id - if v == nil { - return - } - return *v, true +// ResetAppID resets all changes to the "app_id" field. +func (m *AppAppCategoryMutation) ResetAppID() { + m.app = nil } -// ResetAppID resets all changes to the "app_id" field. -func (m *AppCategoryMutation) ResetAppID() { - m.app_id = nil - m.addapp_id = nil +// ClearAppCategory clears the "app_category" edge to the AppCategory entity. +func (m *AppAppCategoryMutation) ClearAppCategory() { + m.clearedapp_category = true + m.clearedFields[appappcategory.FieldAppCategoryID] = struct{}{} } -// SetStartTime sets the "start_time" field. -func (m *AppCategoryMutation) SetStartTime(t time.Time) { - m.start_time = &t +// AppCategoryCleared reports if the "app_category" edge to the AppCategory entity was cleared. +func (m *AppAppCategoryMutation) AppCategoryCleared() bool { + return m.clearedapp_category } -// StartTime returns the value of the "start_time" field in the mutation. -func (m *AppCategoryMutation) StartTime() (r time.Time, exists bool) { - v := m.start_time - if v == nil { - return +// AppCategoryIDs returns the "app_category" edge IDs in the mutation. +// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use +// AppCategoryID instead. It exists only for internal usage by the builders. +func (m *AppAppCategoryMutation) AppCategoryIDs() (ids []model.InternalID) { + if id := m.app_category; id != nil { + ids = append(ids, *id) } - return *v, true + return } -// OldStartTime returns the old "start_time" field's value of the AppCategory entity. -// If the AppCategory 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 *AppCategoryMutation) OldStartTime(ctx context.Context) (v time.Time, err error) { - if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldStartTime is only allowed on UpdateOne operations") - } - if m.id == nil || m.oldValue == nil { - return v, errors.New("OldStartTime requires an ID field in the mutation") - } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldStartTime: %w", err) - } - return oldValue.StartTime, nil +// ResetAppCategory resets all changes to the "app_category" edge. +func (m *AppAppCategoryMutation) ResetAppCategory() { + m.app_category = nil + m.clearedapp_category = false } -// ResetStartTime resets all changes to the "start_time" field. -func (m *AppCategoryMutation) ResetStartTime() { - m.start_time = nil +// ClearApp clears the "app" edge to the App entity. +func (m *AppAppCategoryMutation) ClearApp() { + m.clearedapp = true + m.clearedFields[appappcategory.FieldAppID] = struct{}{} } -// SetRunDuration sets the "run_duration" field. -func (m *AppCategoryMutation) SetRunDuration(t time.Duration) { - m.run_duration = &t - m.addrun_duration = nil +// AppCleared reports if the "app" edge to the App entity was cleared. +func (m *AppAppCategoryMutation) AppCleared() bool { + return m.clearedapp } -// RunDuration returns the value of the "run_duration" field in the mutation. -func (m *AppCategoryMutation) RunDuration() (r time.Duration, exists bool) { - v := m.run_duration - if v == nil { - return +// AppIDs returns the "app" edge IDs in the mutation. +// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use +// AppID instead. It exists only for internal usage by the builders. +func (m *AppAppCategoryMutation) AppIDs() (ids []model.InternalID) { + if id := m.app; id != nil { + ids = append(ids, *id) } - return *v, true + return } -// OldRunDuration returns the old "run_duration" field's value of the AppCategory entity. -// If the AppCategory 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 *AppCategoryMutation) OldRunDuration(ctx context.Context) (v time.Duration, err error) { - if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldRunDuration is only allowed on UpdateOne operations") - } - if m.id == nil || m.oldValue == nil { - return v, errors.New("OldRunDuration requires an ID field in the mutation") - } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldRunDuration: %w", err) - } - return oldValue.RunDuration, nil +// ResetApp resets all changes to the "app" edge. +func (m *AppAppCategoryMutation) ResetApp() { + m.app = nil + m.clearedapp = false } -// AddRunDuration adds t to the "run_duration" field. -func (m *AppCategoryMutation) AddRunDuration(t time.Duration) { - if m.addrun_duration != nil { - *m.addrun_duration += t - } else { - m.addrun_duration = &t - } +// Where appends a list predicates to the AppAppCategoryMutation builder. +func (m *AppAppCategoryMutation) Where(ps ...predicate.AppAppCategory) { + m.predicates = append(m.predicates, ps...) } -// AddedRunDuration returns the value that was added to the "run_duration" field in this mutation. -func (m *AppCategoryMutation) AddedRunDuration() (r time.Duration, exists bool) { - v := m.addrun_duration - if v == nil { - return +// WhereP appends storage-level predicates to the AppAppCategoryMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *AppAppCategoryMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.AppAppCategory, len(ps)) + for i := range ps { + p[i] = ps[i] } - return *v, true + m.Where(p...) } -// ResetRunDuration resets all changes to the "run_duration" field. -func (m *AppCategoryMutation) ResetRunDuration() { - m.run_duration = nil - m.addrun_duration = nil +// Op returns the operation name. +func (m *AppAppCategoryMutation) Op() Op { + return m.op } -// SetUpdatedAt sets the "updated_at" field. -func (m *AppCategoryMutation) SetUpdatedAt(t time.Time) { - m.updated_at = &t +// SetOp allows setting the mutation operation. +func (m *AppAppCategoryMutation) SetOp(op Op) { + m.op = op } -// UpdatedAt returns the value of the "updated_at" field in the mutation. -func (m *AppCategoryMutation) UpdatedAt() (r time.Time, exists bool) { - v := m.updated_at - if v == nil { - return - } - return *v, true +// Type returns the node type of this mutation (AppAppCategory). +func (m *AppAppCategoryMutation) Type() string { + return m.typ } -// OldUpdatedAt returns the old "updated_at" field's value of the AppCategory entity. -// If the AppCategory 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 *AppCategoryMutation) 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") +// 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 *AppAppCategoryMutation) Fields() []string { + fields := make([]string, 0, 2) + if m.app_category != nil { + fields = append(fields, appappcategory.FieldAppCategoryID) } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err) + if m.app != nil { + fields = append(fields, appappcategory.FieldAppID) } - return oldValue.UpdatedAt, nil + return fields } -// ResetUpdatedAt resets all changes to the "updated_at" field. -func (m *AppCategoryMutation) ResetUpdatedAt() { - m.updated_at = nil +// 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 *AppAppCategoryMutation) Field(name string) (ent.Value, bool) { + switch name { + case appappcategory.FieldAppCategoryID: + return m.AppCategoryID() + case appappcategory.FieldAppID: + return m.AppID() + } + 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 *AppAppCategoryMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case appappcategory.FieldAppCategoryID: + return m.OldAppCategoryID(ctx) + case appappcategory.FieldAppID: + return m.OldAppID(ctx) + } + return nil, fmt.Errorf("unknown AppAppCategory 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 *AppAppCategoryMutation) SetField(name string, value ent.Value) error { + switch name { + case appappcategory.FieldAppCategoryID: + v, ok := value.(model.InternalID) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetAppCategoryID(v) + return nil + case appappcategory.FieldAppID: + v, ok := value.(model.InternalID) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetAppID(v) + return nil + } + return fmt.Errorf("unknown AppAppCategory field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *AppAppCategoryMutation) AddedFields() []string { + var fields []string + return fields +} + +// 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 *AppAppCategoryMutation) AddedField(name string) (ent.Value, bool) { + switch name { + } + 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 *AppAppCategoryMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown AppAppCategory numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *AppAppCategoryMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *AppAppCategoryMutation) 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 *AppAppCategoryMutation) ClearField(name string) error { + return fmt.Errorf("unknown AppAppCategory 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 *AppAppCategoryMutation) ResetField(name string) error { + switch name { + case appappcategory.FieldAppCategoryID: + m.ResetAppCategoryID() + return nil + case appappcategory.FieldAppID: + m.ResetAppID() + return nil + } + return fmt.Errorf("unknown AppAppCategory field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *AppAppCategoryMutation) AddedEdges() []string { + edges := make([]string, 0, 2) + if m.app_category != nil { + edges = append(edges, appappcategory.EdgeAppCategory) + } + if m.app != nil { + edges = append(edges, appappcategory.EdgeApp) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *AppAppCategoryMutation) AddedIDs(name string) []ent.Value { + switch name { + case appappcategory.EdgeAppCategory: + if id := m.app_category; id != nil { + return []ent.Value{*id} + } + case appappcategory.EdgeApp: + if id := m.app; id != nil { + return []ent.Value{*id} + } + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *AppAppCategoryMutation) 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 *AppAppCategoryMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *AppAppCategoryMutation) ClearedEdges() []string { + edges := make([]string, 0, 2) + if m.clearedapp_category { + edges = append(edges, appappcategory.EdgeAppCategory) + } + if m.clearedapp { + edges = append(edges, appappcategory.EdgeApp) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *AppAppCategoryMutation) EdgeCleared(name string) bool { + switch name { + case appappcategory.EdgeAppCategory: + return m.clearedapp_category + case appappcategory.EdgeApp: + return m.clearedapp + } + 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 *AppAppCategoryMutation) ClearEdge(name string) error { + switch name { + case appappcategory.EdgeAppCategory: + m.ClearAppCategory() + return nil + case appappcategory.EdgeApp: + m.ClearApp() + return nil + } + return fmt.Errorf("unknown AppAppCategory 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 *AppAppCategoryMutation) ResetEdge(name string) error { + switch name { + case appappcategory.EdgeAppCategory: + m.ResetAppCategory() + return nil + case appappcategory.EdgeApp: + m.ResetApp() + return nil + } + return fmt.Errorf("unknown AppAppCategory edge %s", name) +} + +// AppCategoryMutation represents an operation that mutates the AppCategory nodes in the graph. +type AppCategoryMutation struct { + config + op Op + typ string + id *model.InternalID + user_id *model.InternalID + adduser_id *model.InternalID + version_number *uint64 + addversion_number *int64 + version_date *time.Time + name *string + updated_at *time.Time + created_at *time.Time + clearedFields map[string]struct{} + app map[model.InternalID]struct{} + removedapp map[model.InternalID]struct{} + clearedapp bool + app_app_category map[int]struct{} + removedapp_app_category map[int]struct{} + clearedapp_app_category bool + done bool + oldValue func(context.Context) (*AppCategory, error) + predicates []predicate.AppCategory +} + +var _ ent.Mutation = (*AppCategoryMutation)(nil) + +// appcategoryOption allows management of the mutation configuration using functional options. +type appcategoryOption func(*AppCategoryMutation) + +// newAppCategoryMutation creates new mutation for the AppCategory entity. +func newAppCategoryMutation(c config, op Op, opts ...appcategoryOption) *AppCategoryMutation { + m := &AppCategoryMutation{ + config: c, + op: op, + typ: TypeAppCategory, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withAppCategoryID sets the ID field of the mutation. +func withAppCategoryID(id model.InternalID) appcategoryOption { + return func(m *AppCategoryMutation) { + var ( + err error + once sync.Once + value *AppCategory + ) + m.oldValue = func(ctx context.Context) (*AppCategory, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().AppCategory.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withAppCategory sets the old AppCategory of the mutation. +func withAppCategory(node *AppCategory) appcategoryOption { + return func(m *AppCategoryMutation) { + m.oldValue = func(context.Context) (*AppCategory, 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 AppCategoryMutation) 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 AppCategoryMutation) 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 AppCategory entities. +func (m *AppCategoryMutation) SetID(id model.InternalID) { + 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 *AppCategoryMutation) ID() (id model.InternalID, 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 *AppCategoryMutation) IDs(ctx context.Context) ([]model.InternalID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []model.InternalID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().AppCategory.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 *AppCategoryMutation) SetUserID(mi model.InternalID) { + m.user_id = &mi + m.adduser_id = nil +} + +// UserID returns the value of the "user_id" field in the mutation. +func (m *AppCategoryMutation) UserID() (r model.InternalID, exists bool) { + v := m.user_id + if v == nil { + return + } + return *v, true +} + +// OldUserID returns the old "user_id" field's value of the AppCategory entity. +// If the AppCategory 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 *AppCategoryMutation) OldUserID(ctx context.Context) (v model.InternalID, 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 +} + +// AddUserID adds mi to the "user_id" field. +func (m *AppCategoryMutation) AddUserID(mi model.InternalID) { + if m.adduser_id != nil { + *m.adduser_id += mi + } else { + m.adduser_id = &mi + } +} + +// AddedUserID returns the value that was added to the "user_id" field in this mutation. +func (m *AppCategoryMutation) AddedUserID() (r model.InternalID, exists bool) { + v := m.adduser_id + if v == nil { + return + } + return *v, true +} + +// ResetUserID resets all changes to the "user_id" field. +func (m *AppCategoryMutation) ResetUserID() { + m.user_id = nil + m.adduser_id = nil +} + +// SetVersionNumber sets the "version_number" field. +func (m *AppCategoryMutation) SetVersionNumber(u uint64) { + m.version_number = &u + m.addversion_number = nil +} + +// VersionNumber returns the value of the "version_number" field in the mutation. +func (m *AppCategoryMutation) VersionNumber() (r uint64, exists bool) { + v := m.version_number + if v == nil { + return + } + return *v, true +} + +// OldVersionNumber returns the old "version_number" field's value of the AppCategory entity. +// If the AppCategory 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 *AppCategoryMutation) OldVersionNumber(ctx context.Context) (v uint64, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldVersionNumber is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldVersionNumber requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldVersionNumber: %w", err) + } + return oldValue.VersionNumber, nil +} + +// AddVersionNumber adds u to the "version_number" field. +func (m *AppCategoryMutation) AddVersionNumber(u int64) { + if m.addversion_number != nil { + *m.addversion_number += u + } else { + m.addversion_number = &u + } +} + +// AddedVersionNumber returns the value that was added to the "version_number" field in this mutation. +func (m *AppCategoryMutation) AddedVersionNumber() (r int64, exists bool) { + v := m.addversion_number + if v == nil { + return + } + return *v, true +} + +// ResetVersionNumber resets all changes to the "version_number" field. +func (m *AppCategoryMutation) ResetVersionNumber() { + m.version_number = nil + m.addversion_number = nil +} + +// SetVersionDate sets the "version_date" field. +func (m *AppCategoryMutation) SetVersionDate(t time.Time) { + m.version_date = &t +} + +// VersionDate returns the value of the "version_date" field in the mutation. +func (m *AppCategoryMutation) VersionDate() (r time.Time, exists bool) { + v := m.version_date + if v == nil { + return + } + return *v, true +} + +// OldVersionDate returns the old "version_date" field's value of the AppCategory entity. +// If the AppCategory 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 *AppCategoryMutation) OldVersionDate(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldVersionDate is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldVersionDate requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldVersionDate: %w", err) + } + return oldValue.VersionDate, nil +} + +// ResetVersionDate resets all changes to the "version_date" field. +func (m *AppCategoryMutation) ResetVersionDate() { + m.version_date = nil +} + +// SetName sets the "name" field. +func (m *AppCategoryMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *AppCategoryMutation) 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 AppCategory entity. +// If the AppCategory 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 *AppCategoryMutation) 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 *AppCategoryMutation) ResetName() { + m.name = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *AppCategoryMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *AppCategoryMutation) 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 AppCategory entity. +// If the AppCategory 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 *AppCategoryMutation) 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 *AppCategoryMutation) ResetUpdatedAt() { + m.updated_at = nil } // SetCreatedAt sets the "created_at" field. @@ -3486,6 +4128,114 @@ func (m *AppCategoryMutation) ResetCreatedAt() { m.created_at = nil } +// AddAppIDs adds the "app" edge to the App entity by ids. +func (m *AppCategoryMutation) AddAppIDs(ids ...model.InternalID) { + if m.app == nil { + m.app = make(map[model.InternalID]struct{}) + } + for i := range ids { + m.app[ids[i]] = struct{}{} + } +} + +// ClearApp clears the "app" edge to the App entity. +func (m *AppCategoryMutation) ClearApp() { + m.clearedapp = true +} + +// AppCleared reports if the "app" edge to the App entity was cleared. +func (m *AppCategoryMutation) AppCleared() bool { + return m.clearedapp +} + +// RemoveAppIDs removes the "app" edge to the App entity by IDs. +func (m *AppCategoryMutation) RemoveAppIDs(ids ...model.InternalID) { + if m.removedapp == nil { + m.removedapp = make(map[model.InternalID]struct{}) + } + for i := range ids { + delete(m.app, ids[i]) + m.removedapp[ids[i]] = struct{}{} + } +} + +// RemovedApp returns the removed IDs of the "app" edge to the App entity. +func (m *AppCategoryMutation) RemovedAppIDs() (ids []model.InternalID) { + for id := range m.removedapp { + ids = append(ids, id) + } + return +} + +// AppIDs returns the "app" edge IDs in the mutation. +func (m *AppCategoryMutation) AppIDs() (ids []model.InternalID) { + for id := range m.app { + ids = append(ids, id) + } + return +} + +// ResetApp resets all changes to the "app" edge. +func (m *AppCategoryMutation) ResetApp() { + m.app = nil + m.clearedapp = false + m.removedapp = nil +} + +// AddAppAppCategoryIDs adds the "app_app_category" edge to the AppAppCategory entity by ids. +func (m *AppCategoryMutation) AddAppAppCategoryIDs(ids ...int) { + if m.app_app_category == nil { + m.app_app_category = make(map[int]struct{}) + } + for i := range ids { + m.app_app_category[ids[i]] = struct{}{} + } +} + +// ClearAppAppCategory clears the "app_app_category" edge to the AppAppCategory entity. +func (m *AppCategoryMutation) ClearAppAppCategory() { + m.clearedapp_app_category = true +} + +// AppAppCategoryCleared reports if the "app_app_category" edge to the AppAppCategory entity was cleared. +func (m *AppCategoryMutation) AppAppCategoryCleared() bool { + return m.clearedapp_app_category +} + +// RemoveAppAppCategoryIDs removes the "app_app_category" edge to the AppAppCategory entity by IDs. +func (m *AppCategoryMutation) RemoveAppAppCategoryIDs(ids ...int) { + if m.removedapp_app_category == nil { + m.removedapp_app_category = make(map[int]struct{}) + } + for i := range ids { + delete(m.app_app_category, ids[i]) + m.removedapp_app_category[ids[i]] = struct{}{} + } +} + +// RemovedAppAppCategory returns the removed IDs of the "app_app_category" edge to the AppAppCategory entity. +func (m *AppCategoryMutation) RemovedAppAppCategoryIDs() (ids []int) { + for id := range m.removedapp_app_category { + ids = append(ids, id) + } + return +} + +// AppAppCategoryIDs returns the "app_app_category" edge IDs in the mutation. +func (m *AppCategoryMutation) AppAppCategoryIDs() (ids []int) { + for id := range m.app_app_category { + ids = append(ids, id) + } + return +} + +// ResetAppAppCategory resets all changes to the "app_app_category" edge. +func (m *AppCategoryMutation) ResetAppAppCategory() { + m.app_app_category = nil + m.clearedapp_app_category = false + m.removedapp_app_category = nil +} + // Where appends a list predicates to the AppCategoryMutation builder. func (m *AppCategoryMutation) Where(ps ...predicate.AppCategory) { m.predicates = append(m.predicates, ps...) @@ -3524,14 +4274,14 @@ func (m *AppCategoryMutation) Fields() []string { if m.user_id != nil { fields = append(fields, appcategory.FieldUserID) } - if m.app_id != nil { - fields = append(fields, appcategory.FieldAppID) + if m.version_number != nil { + fields = append(fields, appcategory.FieldVersionNumber) } - if m.start_time != nil { - fields = append(fields, appcategory.FieldStartTime) + if m.version_date != nil { + fields = append(fields, appcategory.FieldVersionDate) } - if m.run_duration != nil { - fields = append(fields, appcategory.FieldRunDuration) + if m.name != nil { + fields = append(fields, appcategory.FieldName) } if m.updated_at != nil { fields = append(fields, appcategory.FieldUpdatedAt) @@ -3549,12 +4299,12 @@ func (m *AppCategoryMutation) Field(name string) (ent.Value, bool) { switch name { case appcategory.FieldUserID: return m.UserID() - case appcategory.FieldAppID: - return m.AppID() - case appcategory.FieldStartTime: - return m.StartTime() - case appcategory.FieldRunDuration: - return m.RunDuration() + case appcategory.FieldVersionNumber: + return m.VersionNumber() + case appcategory.FieldVersionDate: + return m.VersionDate() + case appcategory.FieldName: + return m.Name() case appcategory.FieldUpdatedAt: return m.UpdatedAt() case appcategory.FieldCreatedAt: @@ -3570,12 +4320,12 @@ func (m *AppCategoryMutation) OldField(ctx context.Context, name string) (ent.Va switch name { case appcategory.FieldUserID: return m.OldUserID(ctx) - case appcategory.FieldAppID: - return m.OldAppID(ctx) - case appcategory.FieldStartTime: - return m.OldStartTime(ctx) - case appcategory.FieldRunDuration: - return m.OldRunDuration(ctx) + case appcategory.FieldVersionNumber: + return m.OldVersionNumber(ctx) + case appcategory.FieldVersionDate: + return m.OldVersionDate(ctx) + case appcategory.FieldName: + return m.OldName(ctx) case appcategory.FieldUpdatedAt: return m.OldUpdatedAt(ctx) case appcategory.FieldCreatedAt: @@ -3596,26 +4346,26 @@ func (m *AppCategoryMutation) SetField(name string, value ent.Value) error { } m.SetUserID(v) return nil - case appcategory.FieldAppID: - v, ok := value.(model.InternalID) + case appcategory.FieldVersionNumber: + v, ok := value.(uint64) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } - m.SetAppID(v) + m.SetVersionNumber(v) return nil - case appcategory.FieldStartTime: + case appcategory.FieldVersionDate: v, ok := value.(time.Time) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } - m.SetStartTime(v) + m.SetVersionDate(v) return nil - case appcategory.FieldRunDuration: - v, ok := value.(time.Duration) + case appcategory.FieldName: + v, ok := value.(string) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } - m.SetRunDuration(v) + m.SetName(v) return nil case appcategory.FieldUpdatedAt: v, ok := value.(time.Time) @@ -3642,11 +4392,8 @@ func (m *AppCategoryMutation) AddedFields() []string { if m.adduser_id != nil { fields = append(fields, appcategory.FieldUserID) } - if m.addapp_id != nil { - fields = append(fields, appcategory.FieldAppID) - } - if m.addrun_duration != nil { - fields = append(fields, appcategory.FieldRunDuration) + if m.addversion_number != nil { + fields = append(fields, appcategory.FieldVersionNumber) } return fields } @@ -3658,10 +4405,8 @@ func (m *AppCategoryMutation) AddedField(name string) (ent.Value, bool) { switch name { case appcategory.FieldUserID: return m.AddedUserID() - case appcategory.FieldAppID: - return m.AddedAppID() - case appcategory.FieldRunDuration: - return m.AddedRunDuration() + case appcategory.FieldVersionNumber: + return m.AddedVersionNumber() } return nil, false } @@ -3678,19 +4423,12 @@ func (m *AppCategoryMutation) AddField(name string, value ent.Value) error { } m.AddUserID(v) return nil - case appcategory.FieldAppID: - v, ok := value.(model.InternalID) - if !ok { - return fmt.Errorf("unexpected type %T for field %s", value, name) - } - m.AddAppID(v) - return nil - case appcategory.FieldRunDuration: - v, ok := value.(time.Duration) + case appcategory.FieldVersionNumber: + v, ok := value.(int64) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } - m.AddRunDuration(v) + m.AddVersionNumber(v) return nil } return fmt.Errorf("unknown AppCategory numeric field %s", name) @@ -3722,14 +4460,14 @@ func (m *AppCategoryMutation) ResetField(name string) error { case appcategory.FieldUserID: m.ResetUserID() return nil - case appcategory.FieldAppID: - m.ResetAppID() + case appcategory.FieldVersionNumber: + m.ResetVersionNumber() return nil - case appcategory.FieldStartTime: - m.ResetStartTime() + case appcategory.FieldVersionDate: + m.ResetVersionDate() return nil - case appcategory.FieldRunDuration: - m.ResetRunDuration() + case appcategory.FieldName: + m.ResetName() return nil case appcategory.FieldUpdatedAt: m.ResetUpdatedAt() @@ -3743,49 +4481,111 @@ func (m *AppCategoryMutation) ResetField(name string) error { // AddedEdges returns all edge names that were set/added in this mutation. func (m *AppCategoryMutation) AddedEdges() []string { - edges := make([]string, 0, 0) + edges := make([]string, 0, 2) + if m.app != nil { + edges = append(edges, appcategory.EdgeApp) + } + if m.app_app_category != nil { + edges = append(edges, appcategory.EdgeAppAppCategory) + } return edges } // AddedIDs returns all IDs (to other nodes) that were added for the given edge // name in this mutation. func (m *AppCategoryMutation) AddedIDs(name string) []ent.Value { + switch name { + case appcategory.EdgeApp: + ids := make([]ent.Value, 0, len(m.app)) + for id := range m.app { + ids = append(ids, id) + } + return ids + case appcategory.EdgeAppAppCategory: + ids := make([]ent.Value, 0, len(m.app_app_category)) + for id := range m.app_app_category { + ids = append(ids, id) + } + return ids + } return nil } // RemovedEdges returns all edge names that were removed in this mutation. func (m *AppCategoryMutation) RemovedEdges() []string { - edges := make([]string, 0, 0) + edges := make([]string, 0, 2) + if m.removedapp != nil { + edges = append(edges, appcategory.EdgeApp) + } + if m.removedapp_app_category != nil { + edges = append(edges, appcategory.EdgeAppAppCategory) + } return edges } // RemovedIDs returns all IDs (to other nodes) that were removed for the edge with // the given name in this mutation. func (m *AppCategoryMutation) RemovedIDs(name string) []ent.Value { + switch name { + case appcategory.EdgeApp: + ids := make([]ent.Value, 0, len(m.removedapp)) + for id := range m.removedapp { + ids = append(ids, id) + } + return ids + case appcategory.EdgeAppAppCategory: + ids := make([]ent.Value, 0, len(m.removedapp_app_category)) + for id := range m.removedapp_app_category { + ids = append(ids, id) + } + return ids + } return nil } // ClearedEdges returns all edge names that were cleared in this mutation. func (m *AppCategoryMutation) ClearedEdges() []string { - edges := make([]string, 0, 0) + edges := make([]string, 0, 2) + if m.clearedapp { + edges = append(edges, appcategory.EdgeApp) + } + if m.clearedapp_app_category { + edges = append(edges, appcategory.EdgeAppAppCategory) + } return edges } // EdgeCleared returns a boolean which indicates if the edge with the given name // was cleared in this mutation. func (m *AppCategoryMutation) EdgeCleared(name string) bool { + switch name { + case appcategory.EdgeApp: + return m.clearedapp + case appcategory.EdgeAppAppCategory: + return m.clearedapp_app_category + } 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 *AppCategoryMutation) ClearEdge(name string) error { + switch name { + } return fmt.Errorf("unknown AppCategory 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 *AppCategoryMutation) ResetEdge(name string) error { + switch name { + case appcategory.EdgeApp: + m.ResetApp() + return nil + case appcategory.EdgeAppAppCategory: + m.ResetAppAppCategory() + return nil + } return fmt.Errorf("unknown AppCategory edge %s", name) } diff --git a/internal/data/internal/ent/predicate/predicate.go b/internal/data/internal/ent/predicate/predicate.go index 3911ee7c..b03fedb6 100644 --- a/internal/data/internal/ent/predicate/predicate.go +++ b/internal/data/internal/ent/predicate/predicate.go @@ -12,6 +12,9 @@ type Account func(*sql.Selector) // App is the predicate function for app builders. type App func(*sql.Selector) +// AppAppCategory is the predicate function for appappcategory builders. +type AppAppCategory func(*sql.Selector) + // AppCategory is the predicate function for appcategory builders. type AppCategory func(*sql.Selector) diff --git a/internal/data/internal/ent/runtime.go b/internal/data/internal/ent/runtime.go index 999d3081..b26d0d81 100644 --- a/internal/data/internal/ent/runtime.go +++ b/internal/data/internal/ent/runtime.go @@ -66,13 +66,13 @@ func init() { appcategoryFields := schema.AppCategory{}.Fields() _ = appcategoryFields // appcategoryDescUpdatedAt is the schema descriptor for updated_at field. - appcategoryDescUpdatedAt := appcategoryFields[4].Descriptor() + appcategoryDescUpdatedAt := appcategoryFields[5].Descriptor() // appcategory.DefaultUpdatedAt holds the default value on creation for the updated_at field. appcategory.DefaultUpdatedAt = appcategoryDescUpdatedAt.Default.(func() time.Time) // appcategory.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. appcategory.UpdateDefaultUpdatedAt = appcategoryDescUpdatedAt.UpdateDefault.(func() time.Time) // appcategoryDescCreatedAt is the schema descriptor for created_at field. - appcategoryDescCreatedAt := appcategoryFields[5].Descriptor() + appcategoryDescCreatedAt := appcategoryFields[6].Descriptor() // appcategory.DefaultCreatedAt holds the default value on creation for the created_at field. appcategory.DefaultCreatedAt = appcategoryDescCreatedAt.Default.(func() time.Time) appinfoFields := schema.AppInfo{}.Fields() diff --git a/internal/data/internal/ent/runtime/runtime.go b/internal/data/internal/ent/runtime/runtime.go index 475d4c6f..dc439bf2 100644 --- a/internal/data/internal/ent/runtime/runtime.go +++ b/internal/data/internal/ent/runtime/runtime.go @@ -5,6 +5,6 @@ package runtime // The schema-stitching logic is generated in github.com/tuihub/librarian/internal/data/internal/ent/runtime.go const ( - Version = "v0.14.3" // Version of ent codegen. - Sum = "h1:wokAV/kIlH9TeklJWGGS7AYJdVckr0DloWjIcO9iIIQ=" // Sum of ent codegen. + Version = "v0.14.4" // Version of ent codegen. + Sum = "h1:/DhDraSLXIkBhyiVoJeSshr4ZYi7femzhj6/TckzZuI=" // Sum of ent codegen. ) diff --git a/internal/data/internal/ent/schema/app.go b/internal/data/internal/ent/schema/app.go index 351746f0..88d78829 100644 --- a/internal/data/internal/ent/schema/app.go +++ b/internal/data/internal/ent/schema/app.go @@ -70,5 +70,8 @@ func (App) Edges() []ent.Edge { Unique(). Immutable(), edge.To("app_run_time", AppRunTime.Type), + edge.From("app_category", AppCategory.Type). + Ref("app"). + Through("app_app_category", AppAppCategory.Type), } } diff --git a/internal/data/internal/ent/schema/app_app_category.go b/internal/data/internal/ent/schema/app_app_category.go new file mode 100644 index 00000000..b42eb2f2 --- /dev/null +++ b/internal/data/internal/ent/schema/app_app_category.go @@ -0,0 +1,40 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" + "github.com/tuihub/librarian/internal/model" +) + +type AppAppCategory struct { + ent.Schema +} + +func (AppAppCategory) Fields() []ent.Field { + return []ent.Field{ + field.Int64("app_category_id").GoType(model.InternalID(0)), + field.Int64("app_id").GoType(model.InternalID(0)), + } +} + +func (AppAppCategory) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("app_category_id", "app_id"). + Unique(), + } +} + +func (AppAppCategory) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("app_category", AppCategory.Type). + Unique(). + Required(). + Field("app_category_id"), + edge.To("app", App.Type). + Unique(). + Required(). + Field("app_id"), + } +} diff --git a/internal/data/internal/ent/schema/app_category.go b/internal/data/internal/ent/schema/app_category.go index a86796b0..22869f13 100644 --- a/internal/data/internal/ent/schema/app_category.go +++ b/internal/data/internal/ent/schema/app_category.go @@ -1,9 +1,9 @@ package schema import ( - "time" - + "entgo.io/ent/schema/edge" "github.com/tuihub/librarian/internal/model" + "time" "entgo.io/ent" "entgo.io/ent/schema/field" @@ -16,10 +16,11 @@ type AppCategory struct { func (AppCategory) Fields() []ent.Field { return []ent.Field{ + defaultPrimaryKey(), field.Int64("user_id").GoType(model.InternalID(0)), - field.Int64("app_id").GoType(model.InternalID(0)), - field.Time("start_time"), - field.Int64("run_duration").GoType(time.Duration(0)), + field.Uint64("version_number"), + field.Time("version_date"), + field.String("name"), field.Time("updated_at"). Default(time.Now).UpdateDefault(time.Now), field.Time("created_at"). @@ -29,11 +30,13 @@ func (AppCategory) Fields() []ent.Field { func (AppCategory) Indexes() []ent.Index { return []ent.Index{ - index.Fields("user_id", "app_id", "start_time", "run_duration"). - Unique(), + index.Fields("user_id"), } } func (AppCategory) Edges() []ent.Edge { - return []ent.Edge{} + return []ent.Edge{ + edge.To("app", App.Type). + Through("app_app_category", AppAppCategory.Type), + } } diff --git a/internal/data/internal/ent/tx.go b/internal/data/internal/ent/tx.go index 6ecbe8bb..5bcb4822 100644 --- a/internal/data/internal/ent/tx.go +++ b/internal/data/internal/ent/tx.go @@ -16,6 +16,8 @@ type Tx struct { Account *AccountClient // App is the client for interacting with the App builders. App *AppClient + // AppAppCategory is the client for interacting with the AppAppCategory builders. + AppAppCategory *AppAppCategoryClient // AppCategory is the client for interacting with the AppCategory builders. AppCategory *AppCategoryClient // AppInfo is the client for interacting with the AppInfo builders. @@ -199,6 +201,7 @@ func (tx *Tx) Client() *Client { func (tx *Tx) init() { tx.Account = NewAccountClient(tx.config) tx.App = NewAppClient(tx.config) + tx.AppAppCategory = NewAppAppCategoryClient(tx.config) tx.AppCategory = NewAppCategoryClient(tx.config) tx.AppInfo = NewAppInfoClient(tx.config) tx.AppRunTime = NewAppRunTimeClient(tx.config) diff --git a/internal/model/modelgebura/modelgebura.go b/internal/model/modelgebura/modelgebura.go index b529b90c..d65b866f 100644 --- a/internal/model/modelgebura/modelgebura.go +++ b/internal/model/modelgebura/modelgebura.go @@ -99,3 +99,11 @@ type AppBinaryChunk struct { PublicURL string Sha256 []byte } + +type AppCategory struct { + ID model.InternalID + VersionNumber uint64 + VersionDate time.Time + Name string + AppIDs []model.InternalID +} diff --git a/internal/service/sephirah/converter/biz_to_pb.go b/internal/service/sephirah/converter/biz_to_pb.go index 20bcb747..879532b8 100644 --- a/internal/service/sephirah/converter/biz_to_pb.go +++ b/internal/service/sephirah/converter/biz_to_pb.go @@ -119,6 +119,8 @@ type toPBConverter interface { //nolint:unused // used by generator ToPBAppRunTime(*modelgebura.AppRunTime) *sephirah.AppRunTime ToPBAppRunTimeList([]*modelgebura.AppRunTime) []*sephirah.AppRunTime + ToPBAppCategory(*modelgebura.AppCategory) *sephirah.AppCategory + // ToPBAppInst(*modelgebura.AppInst) *sephirah.AppInst // ToPBAppInstList([]*modelgebura.AppInst) []*sephirah.AppInst diff --git a/internal/service/sephirah/converter/generated.go b/internal/service/sephirah/converter/generated.go index f7f0b0f4..e63548ce 100644 --- a/internal/service/sephirah/converter/generated.go +++ b/internal/service/sephirah/converter/generated.go @@ -64,6 +64,19 @@ func ToBizApp(source *sephirah.App) *modelgebura.App { } return pModelgeburaApp } +func ToBizAppCategory(source *sephirah.AppCategory) *modelgebura.AppCategory { + var pModelgeburaAppCategory *modelgebura.AppCategory + if source != nil { + var modelgeburaAppCategory modelgebura.AppCategory + modelgeburaAppCategory.ID = ToBizInternalID((*source).Id) + modelgeburaAppCategory.VersionNumber = (*source).VersionNumber + modelgeburaAppCategory.VersionDate = ToBizTime((*source).VersionDate) + modelgeburaAppCategory.Name = (*source).Name + modelgeburaAppCategory.AppIDs = ToBizInternalIDList((*source).AppIds) + pModelgeburaAppCategory = &modelgeburaAppCategory + } + return pModelgeburaAppCategory +} func ToBizAppInfo(source *sephirah.AppInfo) *modelgebura.AppInfo { var pModelgeburaAppInfo *modelgebura.AppInfo if source != nil { @@ -860,6 +873,19 @@ func ToPBApp(source *modelgebura.App) *sephirah.App { } return pV1App } +func ToPBAppCategory(source *modelgebura.AppCategory) *sephirah.AppCategory { + var pV1AppCategory *sephirah.AppCategory + if source != nil { + var v1AppCategory sephirah.AppCategory + v1AppCategory.Id = ToPBInternalID((*source).ID) + v1AppCategory.VersionNumber = (*source).VersionNumber + v1AppCategory.VersionDate = ToPBTime((*source).VersionDate) + v1AppCategory.Name = (*source).Name + v1AppCategory.AppIds = ToPBInternalIDList((*source).AppIDs) + pV1AppCategory = &v1AppCategory + } + return pV1AppCategory +} func ToPBAppInfo(source *modelgebura.AppInfo) *sephirah.AppInfo { var pV1AppInfo *sephirah.AppInfo if source != nil { @@ -1451,7 +1477,7 @@ func ToPBPorterContextStatus(source modelsupervisor.PorterContextStatus) sephira } return v1PorterContextStatus } -func ToPBPorterGroup(source *modelsupervisor.PorterDigest) *sephirah.PorterDigest { +func ToPBPorterDigest(source *modelsupervisor.PorterDigest) *sephirah.PorterDigest { var pV1PorterDigest *sephirah.PorterDigest if source != nil { var v1PorterDigest sephirah.PorterDigest @@ -1470,12 +1496,12 @@ func ToPBPorterGroup(source *modelsupervisor.PorterDigest) *sephirah.PorterDiges } return pV1PorterDigest } -func ToPBPorterGroupList(source []*modelsupervisor.PorterDigest) []*sephirah.PorterDigest { +func ToPBPorterDigestList(source []*modelsupervisor.PorterDigest) []*sephirah.PorterDigest { var pV1PorterDigestList []*sephirah.PorterDigest if source != nil { pV1PorterDigestList = make([]*sephirah.PorterDigest, len(source)) for i := 0; i < len(source); i++ { - pV1PorterDigestList[i] = ToPBPorterGroup(source[i]) + pV1PorterDigestList[i] = ToPBPorterDigest(source[i]) } } return pV1PorterDigestList diff --git a/internal/service/sephirah/converter/pb_to_biz.go b/internal/service/sephirah/converter/pb_to_biz.go index e681b69a..8948b6db 100644 --- a/internal/service/sephirah/converter/pb_to_biz.go +++ b/internal/service/sephirah/converter/pb_to_biz.go @@ -101,6 +101,8 @@ type toBizConverter interface { //nolint:unused // used by generator ToBizAppRunTime(*sephirah.AppRunTime) *modelgebura.AppRunTime ToBizAppRunTimeList([]*sephirah.AppRunTime) []*modelgebura.AppRunTime + ToBizAppCategory(*sephirah.AppCategory) *modelgebura.AppCategory + // ToBizAppInst(*sephirah.AppInst) *modelgebura.AppInst // goverter:ignore DigestDescription diff --git a/internal/service/sephirah/gebura.go b/internal/service/sephirah/gebura.go index a573645b..6722645c 100644 --- a/internal/service/sephirah/gebura.go +++ b/internal/service/sephirah/gebura.go @@ -292,3 +292,32 @@ func (s *LibrarianSephirahService) ListAppSaveFiles( ) (*sephirah.ListAppSaveFilesResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method ListGameSaveFile not implemented") } + +func (s *LibrarianSephirahService) ListAppCategories( + ctx context.Context, + req *sephirah.ListAppCategoriesRequest, +) (*sephirah.ListAppCategoriesResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ListAppCategories not implemented") +} +func (s *LibrarianSephirahService) CreateAppCategory( + ctx context.Context, + req *sephirah.CreateAppCategoryRequest, +) (*sephirah.CreateAppCategoryResponse, error) { + ac, err := s.g.CreateAppCategory(ctx, converter.ToBizAppCategory(req.GetAppCategory())) + if err != nil { + return nil, err + } + return &sephirah.CreateAppCategoryResponse{Id: converter.ToPBInternalID(ac.ID)}, nil +} +func (s *LibrarianSephirahService) UpdateAppCategory( + ctx context.Context, + req *sephirah.UpdateAppCategoryRequest, +) (*sephirah.UpdateAppCategoryResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpdateAppCategory not implemented") +} +func (s *LibrarianSephirahService) DeleteAppCategory( + ctx context.Context, + req *sephirah.DeleteAppCategoryRequest, +) (*sephirah.DeleteAppCategoryResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method DeleteAppCategory not implemented") +} diff --git a/internal/service/sephirah/tiphereth.go b/internal/service/sephirah/tiphereth.go index 24932676..71e0ce66 100644 --- a/internal/service/sephirah/tiphereth.go +++ b/internal/service/sephirah/tiphereth.go @@ -304,6 +304,6 @@ func (s *LibrarianSephirahService) ListPorterDigests( } return &sephirah.ListPorterDigestsResponse{ Paging: &librarian.PagingResponse{TotalSize: total}, - PorterDigests: converter.ToPBPorterGroupList(groups), + PorterDigests: converter.ToPBPorterDigestList(groups), }, nil } From 3aeed920906ae32a72809b4211715b752a5a6f14 Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Sun, 6 Apr 2025 20:49:24 +0800 Subject: [PATCH 2/7] feat(gebura): add ListAppCategory --- internal/biz/bizgebura/app_category.go | 28 +++++++++++++++++++ internal/data/gebura.go | 25 +++++++++++++++++ internal/data/internal/converter/converter.go | 16 +++++++++++ .../data/internal/converter/ent_to_biz.go | 2 ++ internal/data/internal/converter/generated.go | 12 ++++++++ .../internal/ent/schema/app_app_category.go | 3 +- .../data/internal/ent/schema/app_category.go | 5 ++-- .../service/sephirah/converter/biz_to_pb.go | 1 + .../service/sephirah/converter/generated.go | 10 +++++++ internal/service/sephirah/gebura.go | 27 +++++++++++++----- 10 files changed, 119 insertions(+), 10 deletions(-) diff --git a/internal/biz/bizgebura/app_category.go b/internal/biz/bizgebura/app_category.go index 86a43223..7ba1ffa2 100644 --- a/internal/biz/bizgebura/app_category.go +++ b/internal/biz/bizgebura/app_category.go @@ -2,6 +2,8 @@ package bizgebura import ( "context" + "time" + "github.com/tuihub/librarian/internal/biz/bizutils" "github.com/tuihub/librarian/internal/lib/libauth" "github.com/tuihub/librarian/internal/model/modelgebura" @@ -18,8 +20,34 @@ func (g *Gebura) CreateAppCategory(ctx context.Context, ac *modelgebura.AppCateg return nil, pb.ErrorErrorReasonUnspecified("%s", err) } ac.ID = id + ac.VersionNumber = 1 + ac.VersionDate = time.Now() if err = g.repo.CreateAppCategory(ctx, claims.UserID, ac); err != nil { return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error()) } return ac, nil } + +func (g *Gebura) ListAppCategories(ctx context.Context) ([]*modelgebura.AppCategory, error) { + claims := libauth.FromContextAssertUserType(ctx) + if claims == nil { + return nil, bizutils.NoPermissionError() + } + acList, err := g.repo.ListAppCategory(ctx, claims.UserID) + if err != nil { + return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + return acList, nil +} + +//func (g *Gebura) UpdateAppCategory(ctx context.Context, ac *modelgebura.AppCategory) error { +// claims := libauth.FromContextAssertUserType(ctx) +// if claims == nil { +// return bizutils.NoPermissionError() +// } +// err := g.repo.UpdateAppCategory(ctx, claims.UserID, ac) +// if err != nil { +// return pb.ErrorErrorReasonUnspecified("%s", err.Error()) +// } +// return nil +//} diff --git a/internal/data/gebura.go b/internal/data/gebura.go index ec8c5c66..f6198f92 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -2,6 +2,8 @@ package data import ( "context" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" + pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1" "time" "github.com/tuihub/librarian/internal/data/internal/converter" @@ -532,3 +534,26 @@ func (g *GeburaRepo) CreateAppCategory(ctx context.Context, userID model.Interna AddAppIDs(ac.AppIDs...) return q.Exec(ctx) } +func (g *GeburaRepo) GetAppCategory(ctx context.Context, id model.InternalID) (*modelgebura.AppCategory, error) { + res, err := g.data.db.AppCategory.Query(). + Where(appcategory.IDEQ(id)). + Only(ctx) + if err != nil { + return nil, err + } + return converter.ToBizAppCategory(res), nil +} +func (g *GeburaRepo) ListAppCategory(ctx context.Context, userID model.InternalID) ([]*modelgebura.AppCategory, error) { + q := g.data.db.AppCategory.Query(). + WithAppAppCategory(). + Where(appcategory.UserIDEQ(userID)) + acs, err := q.All(ctx) + if err != nil { + return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + res := make([]*modelgebura.AppCategory, len(acs)) + for i := range acs { + res[i] = converter.ToBizAppCategoryExtend(acs[i]) + } + return res, nil +} diff --git a/internal/data/internal/converter/converter.go b/internal/data/internal/converter/converter.go index 20bd7f7b..58e8dc61 100644 --- a/internal/data/internal/converter/converter.go +++ b/internal/data/internal/converter/converter.go @@ -3,6 +3,8 @@ package converter import ( + "github.com/tuihub/librarian/internal/model" + "github.com/tuihub/librarian/internal/model/modelgebura" "strings" "github.com/tuihub/librarian/internal/data/internal/ent" @@ -80,3 +82,17 @@ func ToBizFeedItemDigest(a *ent.FeedItem) *modelyesod.FeedItemDigest { // TODO incomplete return digest } + +func ToBizAppCategoryExtend(ac *ent.AppCategory) *modelgebura.AppCategory { + res := ToBizAppCategory(ac) + if res == nil { + return res + } + if len(ac.Edges.AppAppCategory) > 0 { + res.AppIDs = make([]model.InternalID, 0, len(ac.Edges.AppAppCategory)) + for _, aac := range ac.Edges.AppAppCategory { + res.AppIDs = append(res.AppIDs, aac.AppID) + } + } + return res +} diff --git a/internal/data/internal/converter/ent_to_biz.go b/internal/data/internal/converter/ent_to_biz.go index 24a5088d..7113dee8 100644 --- a/internal/data/internal/converter/ent_to_biz.go +++ b/internal/data/internal/converter/ent_to_biz.go @@ -99,6 +99,8 @@ type toBizConverter interface { //nolint:unused // used by generator ToBizAppRunTime(*ent.AppRunTime) *modelgebura.AppRunTime ToBizAppRunTimeList([]*ent.AppRunTime) []*modelgebura.AppRunTime ToBizAppBinary(ent.StoreAppBinary) modelgebura.AppBinary + // goverter:ignore AppIDs + ToBizAppCategory(*ent.AppCategory) *modelgebura.AppCategory // goverter:map LatestPullAt LatestPullTime // goverter:ignore ActionSets diff --git a/internal/data/internal/converter/generated.go b/internal/data/internal/converter/generated.go index d482fd86..244c0d73 100644 --- a/internal/data/internal/converter/generated.go +++ b/internal/data/internal/converter/generated.go @@ -111,6 +111,18 @@ func ToBizAppBinary(source ent.StoreAppBinary) modelgebura.AppBinary { } return modelgeburaAppBinary } +func ToBizAppCategory(source *ent.AppCategory) *modelgebura.AppCategory { + var pModelgeburaAppCategory *modelgebura.AppCategory + if source != nil { + var modelgeburaAppCategory modelgebura.AppCategory + modelgeburaAppCategory.ID = modelInternalIDToModelInternalID((*source).ID) + modelgeburaAppCategory.VersionNumber = (*source).VersionNumber + modelgeburaAppCategory.VersionDate = TimeToTime((*source).VersionDate) + modelgeburaAppCategory.Name = (*source).Name + pModelgeburaAppCategory = &modelgeburaAppCategory + } + return pModelgeburaAppCategory +} func ToBizAppInfo(source *ent.AppInfo) *modelgebura.AppInfo { var pModelgeburaAppInfo *modelgebura.AppInfo if source != nil { diff --git a/internal/data/internal/ent/schema/app_app_category.go b/internal/data/internal/ent/schema/app_app_category.go index b42eb2f2..dd2bcb64 100644 --- a/internal/data/internal/ent/schema/app_app_category.go +++ b/internal/data/internal/ent/schema/app_app_category.go @@ -1,11 +1,12 @@ package schema import ( + "github.com/tuihub/librarian/internal/model" + "entgo.io/ent" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" - "github.com/tuihub/librarian/internal/model" ) type AppAppCategory struct { diff --git a/internal/data/internal/ent/schema/app_category.go b/internal/data/internal/ent/schema/app_category.go index 22869f13..cc3460b1 100644 --- a/internal/data/internal/ent/schema/app_category.go +++ b/internal/data/internal/ent/schema/app_category.go @@ -1,11 +1,12 @@ package schema import ( - "entgo.io/ent/schema/edge" - "github.com/tuihub/librarian/internal/model" "time" + "github.com/tuihub/librarian/internal/model" + "entgo.io/ent" + "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" ) diff --git a/internal/service/sephirah/converter/biz_to_pb.go b/internal/service/sephirah/converter/biz_to_pb.go index 879532b8..fc29fd21 100644 --- a/internal/service/sephirah/converter/biz_to_pb.go +++ b/internal/service/sephirah/converter/biz_to_pb.go @@ -120,6 +120,7 @@ type toPBConverter interface { //nolint:unused // used by generator ToPBAppRunTimeList([]*modelgebura.AppRunTime) []*sephirah.AppRunTime ToPBAppCategory(*modelgebura.AppCategory) *sephirah.AppCategory + ToPBAppCategoryList([]*modelgebura.AppCategory) []*sephirah.AppCategory // ToPBAppInst(*modelgebura.AppInst) *sephirah.AppInst // ToPBAppInstList([]*modelgebura.AppInst) []*sephirah.AppInst diff --git a/internal/service/sephirah/converter/generated.go b/internal/service/sephirah/converter/generated.go index e63548ce..0fd00443 100644 --- a/internal/service/sephirah/converter/generated.go +++ b/internal/service/sephirah/converter/generated.go @@ -886,6 +886,16 @@ func ToPBAppCategory(source *modelgebura.AppCategory) *sephirah.AppCategory { } return pV1AppCategory } +func ToPBAppCategoryList(source []*modelgebura.AppCategory) []*sephirah.AppCategory { + var pV1AppCategoryList []*sephirah.AppCategory + if source != nil { + pV1AppCategoryList = make([]*sephirah.AppCategory, len(source)) + for i := 0; i < len(source); i++ { + pV1AppCategoryList[i] = ToPBAppCategory(source[i]) + } + } + return pV1AppCategoryList +} func ToPBAppInfo(source *modelgebura.AppInfo) *sephirah.AppInfo { var pV1AppInfo *sephirah.AppInfo if source != nil { diff --git a/internal/service/sephirah/gebura.go b/internal/service/sephirah/gebura.go index 6722645c..4edbe3e0 100644 --- a/internal/service/sephirah/gebura.go +++ b/internal/service/sephirah/gebura.go @@ -297,7 +297,13 @@ func (s *LibrarianSephirahService) ListAppCategories( ctx context.Context, req *sephirah.ListAppCategoriesRequest, ) (*sephirah.ListAppCategoriesResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method ListAppCategories not implemented") + aps, err := s.g.ListAppCategories(ctx) + if err != nil { + return nil, err + } + return &sephirah.ListAppCategoriesResponse{ + AppCategories: converter.ToPBAppCategoryList(aps), + }, nil } func (s *LibrarianSephirahService) CreateAppCategory( ctx context.Context, @@ -309,12 +315,19 @@ func (s *LibrarianSephirahService) CreateAppCategory( } return &sephirah.CreateAppCategoryResponse{Id: converter.ToPBInternalID(ac.ID)}, nil } -func (s *LibrarianSephirahService) UpdateAppCategory( - ctx context.Context, - req *sephirah.UpdateAppCategoryRequest, -) (*sephirah.UpdateAppCategoryResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method UpdateAppCategory not implemented") -} + +// func (s *LibrarianSephirahService) UpdateAppCategory( +// +// ctx context.Context, +// req *sephirah.UpdateAppCategoryRequest, +// +// ) (*sephirah.UpdateAppCategoryResponse, error) { +// err := s.g.UpdateAppCategory(ctx, converter.ToBizAppCategory(req.GetAppCategory())) +// if err != nil { +// return nil, err +// } +// return &sephirah.UpdateAppCategoryResponse{}, nil +// } func (s *LibrarianSephirahService) DeleteAppCategory( ctx context.Context, req *sephirah.DeleteAppCategoryRequest, From db21db05ac3feaa845f7660bc49676402fa7a2bb Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Sun, 6 Apr 2025 21:56:20 +0800 Subject: [PATCH 3/7] feat(gebura): impl ListAppCategory --- internal/biz/bizgebura/app_category.go | 35 ++++++++++----- internal/biz/bizgebura/gebura.go | 1 - internal/data/gebura.go | 59 ++++++++++++++++++++++++++ internal/service/sephirah/gebura.go | 29 +++++++------ 4 files changed, 98 insertions(+), 26 deletions(-) diff --git a/internal/biz/bizgebura/app_category.go b/internal/biz/bizgebura/app_category.go index 7ba1ffa2..f63213eb 100644 --- a/internal/biz/bizgebura/app_category.go +++ b/internal/biz/bizgebura/app_category.go @@ -2,6 +2,7 @@ package bizgebura import ( "context" + "github.com/tuihub/librarian/internal/model" "time" "github.com/tuihub/librarian/internal/biz/bizutils" @@ -40,14 +41,26 @@ func (g *Gebura) ListAppCategories(ctx context.Context) ([]*modelgebura.AppCateg return acList, nil } -//func (g *Gebura) UpdateAppCategory(ctx context.Context, ac *modelgebura.AppCategory) error { -// claims := libauth.FromContextAssertUserType(ctx) -// if claims == nil { -// return bizutils.NoPermissionError() -// } -// err := g.repo.UpdateAppCategory(ctx, claims.UserID, ac) -// if err != nil { -// return pb.ErrorErrorReasonUnspecified("%s", err.Error()) -// } -// return nil -//} +func (g *Gebura) UpdateAppCategory(ctx context.Context, ac *modelgebura.AppCategory) error { + claims := libauth.FromContextAssertUserType(ctx) + if claims == nil { + return bizutils.NoPermissionError() + } + err := g.repo.UpdateAppCategory(ctx, claims.UserID, ac) + if err != nil { + return pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + return nil +} + +func (g *Gebura) DeleteAppCategory(ctx context.Context, id model.InternalID) error { + claims := libauth.FromContextAssertUserType(ctx) + if claims == nil { + return bizutils.NoPermissionError() + } + err := g.repo.DeleteAppCategory(ctx, claims.UserID, id) + if err != nil { + return pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + return nil +} diff --git a/internal/biz/bizgebura/gebura.go b/internal/biz/bizgebura/gebura.go index 2b245904..98f2075c 100644 --- a/internal/biz/bizgebura/gebura.go +++ b/internal/biz/bizgebura/gebura.go @@ -2,7 +2,6 @@ package bizgebura import ( "context" - "github.com/tuihub/librarian/internal/data" "github.com/tuihub/librarian/internal/lib/libauth" "github.com/tuihub/librarian/internal/lib/libcache" diff --git a/internal/data/gebura.go b/internal/data/gebura.go index f6198f92..5d74c4d9 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -557,3 +557,62 @@ func (g *GeburaRepo) ListAppCategory(ctx context.Context, userID model.InternalI } return res, nil } +func (g *GeburaRepo) UpdateAppCategory( + ctx context.Context, + userID model.InternalID, + ac *modelgebura.AppCategory) error { + q := g.data.db.AppCategory.Query(). + WithAppAppCategory(). + Where(appcategory.IDEQ(ac.ID)) + old, err := q.Only(ctx) + if err != nil { + return pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) + for _, aac := range old.Edges.AppAppCategory { + oldAppIDs = append(oldAppIDs, aac.AppID) + } + return g.data.WithTx(ctx, func(tx *ent.Tx) error { + q := tx.AppCategory.Update(). + Where( + appcategory.IDEQ(ac.ID), + appcategory.UserIDEQ(userID), + ). + SetName(ac.Name). + SetVersionNumber(old.VersionNumber + 1). + SetVersionDate(time.Now()). + RemoveAppIDs(oldAppIDs...). + AddAppIDs(ac.AppIDs...) + return q.Exec(ctx) + }) +} +func (g *GeburaRepo) DeleteAppCategory( + ctx context.Context, + userID model.InternalID, + id model.InternalID, +) error { + return g.data.WithTx(ctx, func(tx *ent.Tx) error { + q := g.data.db.AppCategory.Query(). + WithAppAppCategory(). + Where(appcategory.IDEQ(id)) + old, err := q.Only(ctx) + if err != nil { + return pb.ErrorErrorReasonUnspecified("%s", err.Error()) + } + oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) + for _, aac := range old.Edges.AppAppCategory { + oldAppIDs = append(oldAppIDs, aac.AppID) + } + err = tx.AppCategory.Update(). + Where( + appcategory.IDEQ(id), + appcategory.UserIDEQ(userID), + ). + RemoveAppIDs(oldAppIDs...). + Exec(ctx) + if err != nil { + return err + } + return tx.AppCategory.DeleteOneID(id).Exec(ctx) + }) +} diff --git a/internal/service/sephirah/gebura.go b/internal/service/sephirah/gebura.go index 4edbe3e0..0c1f7cad 100644 --- a/internal/service/sephirah/gebura.go +++ b/internal/service/sephirah/gebura.go @@ -315,22 +315,23 @@ func (s *LibrarianSephirahService) CreateAppCategory( } return &sephirah.CreateAppCategoryResponse{Id: converter.ToPBInternalID(ac.ID)}, nil } - -// func (s *LibrarianSephirahService) UpdateAppCategory( -// -// ctx context.Context, -// req *sephirah.UpdateAppCategoryRequest, -// -// ) (*sephirah.UpdateAppCategoryResponse, error) { -// err := s.g.UpdateAppCategory(ctx, converter.ToBizAppCategory(req.GetAppCategory())) -// if err != nil { -// return nil, err -// } -// return &sephirah.UpdateAppCategoryResponse{}, nil -// } +func (s *LibrarianSephirahService) UpdateAppCategory( + ctx context.Context, + req *sephirah.UpdateAppCategoryRequest, +) (*sephirah.UpdateAppCategoryResponse, error) { + err := s.g.UpdateAppCategory(ctx, converter.ToBizAppCategory(req.GetAppCategory())) + if err != nil { + return nil, err + } + return &sephirah.UpdateAppCategoryResponse{}, nil +} func (s *LibrarianSephirahService) DeleteAppCategory( ctx context.Context, req *sephirah.DeleteAppCategoryRequest, ) (*sephirah.DeleteAppCategoryResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method DeleteAppCategory not implemented") + err := s.g.DeleteAppCategory(ctx, converter.ToBizInternalID(req.GetId())) + if err != nil { + return nil, err + } + return &sephirah.DeleteAppCategoryResponse{}, nil } From e5958fbf8f4707ba17100f05138b87c3d53d4f39 Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Sun, 6 Apr 2025 22:05:18 +0800 Subject: [PATCH 4/7] chore(gebura): update style, fix typo --- internal/biz/bizgebura/app_category.go | 4 +- internal/biz/bizgebura/gebura.go | 1 + internal/data/gebura.go | 43 +++++++++---------- internal/data/internal/converter/converter.go | 4 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/internal/biz/bizgebura/app_category.go b/internal/biz/bizgebura/app_category.go index f63213eb..0b605c15 100644 --- a/internal/biz/bizgebura/app_category.go +++ b/internal/biz/bizgebura/app_category.go @@ -2,11 +2,11 @@ package bizgebura import ( "context" - "github.com/tuihub/librarian/internal/model" "time" "github.com/tuihub/librarian/internal/biz/bizutils" "github.com/tuihub/librarian/internal/lib/libauth" + "github.com/tuihub/librarian/internal/model" "github.com/tuihub/librarian/internal/model/modelgebura" pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1" ) @@ -34,7 +34,7 @@ func (g *Gebura) ListAppCategories(ctx context.Context) ([]*modelgebura.AppCateg if claims == nil { return nil, bizutils.NoPermissionError() } - acList, err := g.repo.ListAppCategory(ctx, claims.UserID) + acList, err := g.repo.ListAppCategories(ctx, claims.UserID) if err != nil { return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error()) } diff --git a/internal/biz/bizgebura/gebura.go b/internal/biz/bizgebura/gebura.go index 98f2075c..2b245904 100644 --- a/internal/biz/bizgebura/gebura.go +++ b/internal/biz/bizgebura/gebura.go @@ -2,6 +2,7 @@ package bizgebura import ( "context" + "github.com/tuihub/librarian/internal/data" "github.com/tuihub/librarian/internal/lib/libauth" "github.com/tuihub/librarian/internal/lib/libcache" diff --git a/internal/data/gebura.go b/internal/data/gebura.go index 5d74c4d9..0bc99360 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -2,13 +2,12 @@ package data import ( "context" - "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" - pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1" "time" "github.com/tuihub/librarian/internal/data/internal/converter" "github.com/tuihub/librarian/internal/data/internal/ent" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appinfo" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" "github.com/tuihub/librarian/internal/data/internal/ent/user" @@ -543,13 +542,13 @@ func (g *GeburaRepo) GetAppCategory(ctx context.Context, id model.InternalID) (* } return converter.ToBizAppCategory(res), nil } -func (g *GeburaRepo) ListAppCategory(ctx context.Context, userID model.InternalID) ([]*modelgebura.AppCategory, error) { - q := g.data.db.AppCategory.Query(). +func (g *GeburaRepo) ListAppCategories(ctx context.Context, userID model.InternalID) ([]*modelgebura.AppCategory, error) { + acs, err := g.data.db.AppCategory.Query(). WithAppAppCategory(). - Where(appcategory.UserIDEQ(userID)) - acs, err := q.All(ctx) + Where(appcategory.UserIDEQ(userID)). + All(ctx) if err != nil { - return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error()) + return nil, err } res := make([]*modelgebura.AppCategory, len(acs)) for i := range acs { @@ -561,12 +560,12 @@ func (g *GeburaRepo) UpdateAppCategory( ctx context.Context, userID model.InternalID, ac *modelgebura.AppCategory) error { - q := g.data.db.AppCategory.Query(). + old, err := g.data.db.AppCategory.Query(). WithAppAppCategory(). - Where(appcategory.IDEQ(ac.ID)) - old, err := q.Only(ctx) + Where(appcategory.IDEQ(ac.ID)). + Only(ctx) if err != nil { - return pb.ErrorErrorReasonUnspecified("%s", err.Error()) + return err } oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) for _, aac := range old.Edges.AppAppCategory { @@ -591,18 +590,18 @@ func (g *GeburaRepo) DeleteAppCategory( userID model.InternalID, id model.InternalID, ) error { + old, err := g.data.db.AppCategory.Query(). + WithAppAppCategory(). + Where(appcategory.IDEQ(id)). + Only(ctx) + if err != nil { + return err + } + oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) + for _, aac := range old.Edges.AppAppCategory { + oldAppIDs = append(oldAppIDs, aac.AppID) + } return g.data.WithTx(ctx, func(tx *ent.Tx) error { - q := g.data.db.AppCategory.Query(). - WithAppAppCategory(). - Where(appcategory.IDEQ(id)) - old, err := q.Only(ctx) - if err != nil { - return pb.ErrorErrorReasonUnspecified("%s", err.Error()) - } - oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) - for _, aac := range old.Edges.AppAppCategory { - oldAppIDs = append(oldAppIDs, aac.AppID) - } err = tx.AppCategory.Update(). Where( appcategory.IDEQ(id), diff --git a/internal/data/internal/converter/converter.go b/internal/data/internal/converter/converter.go index 58e8dc61..f3d0ec6a 100644 --- a/internal/data/internal/converter/converter.go +++ b/internal/data/internal/converter/converter.go @@ -3,11 +3,11 @@ package converter import ( - "github.com/tuihub/librarian/internal/model" - "github.com/tuihub/librarian/internal/model/modelgebura" "strings" "github.com/tuihub/librarian/internal/data/internal/ent" + "github.com/tuihub/librarian/internal/model" + "github.com/tuihub/librarian/internal/model/modelgebura" "github.com/tuihub/librarian/internal/model/modelnetzach" "github.com/tuihub/librarian/internal/model/modelyesod" ) From 1d3398f570ab075e27b0ad32f8c6d63722e57ac9 Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Mon, 7 Apr 2025 12:14:23 +0800 Subject: [PATCH 5/7] fix(gebura): move all ops into tx in repo with tx --- internal/data/gebura.go | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/internal/data/gebura.go b/internal/data/gebura.go index 0bc99360..918e9028 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -560,18 +560,18 @@ func (g *GeburaRepo) UpdateAppCategory( ctx context.Context, userID model.InternalID, ac *modelgebura.AppCategory) error { - old, err := g.data.db.AppCategory.Query(). - WithAppAppCategory(). - Where(appcategory.IDEQ(ac.ID)). - Only(ctx) - if err != nil { - return err - } - oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) - for _, aac := range old.Edges.AppAppCategory { - oldAppIDs = append(oldAppIDs, aac.AppID) - } return g.data.WithTx(ctx, func(tx *ent.Tx) error { + old, err := g.data.db.AppCategory.Query(). + WithAppAppCategory(). + Where(appcategory.IDEQ(ac.ID)). + Only(ctx) + if err != nil { + return err + } + oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) + for _, aac := range old.Edges.AppAppCategory { + oldAppIDs = append(oldAppIDs, aac.AppID) + } q := tx.AppCategory.Update(). Where( appcategory.IDEQ(ac.ID), @@ -590,18 +590,18 @@ func (g *GeburaRepo) DeleteAppCategory( userID model.InternalID, id model.InternalID, ) error { - old, err := g.data.db.AppCategory.Query(). - WithAppAppCategory(). - Where(appcategory.IDEQ(id)). - Only(ctx) - if err != nil { - return err - } - oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) - for _, aac := range old.Edges.AppAppCategory { - oldAppIDs = append(oldAppIDs, aac.AppID) - } return g.data.WithTx(ctx, func(tx *ent.Tx) error { + old, err := g.data.db.AppCategory.Query(). + WithAppAppCategory(). + Where(appcategory.IDEQ(id)). + Only(ctx) + if err != nil { + return err + } + oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) + for _, aac := range old.Edges.AppAppCategory { + oldAppIDs = append(oldAppIDs, aac.AppID) + } err = tx.AppCategory.Update(). Where( appcategory.IDEQ(id), From f5702d24dac9b5f186b3cfb41ab1f762ad74125f Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Mon, 7 Apr 2025 19:38:57 +0800 Subject: [PATCH 6/7] chore(gebura): improve db ops for appcategory --- internal/data/gebura.go | 47 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/internal/data/gebura.go b/internal/data/gebura.go index 918e9028..be32f52e 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -7,6 +7,7 @@ import ( "github.com/tuihub/librarian/internal/data/internal/converter" "github.com/tuihub/librarian/internal/data/internal/ent" "github.com/tuihub/librarian/internal/data/internal/ent/app" + "github.com/tuihub/librarian/internal/data/internal/ent/appappcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appcategory" "github.com/tuihub/librarian/internal/data/internal/ent/appinfo" "github.com/tuihub/librarian/internal/data/internal/ent/appruntime" @@ -561,16 +562,25 @@ func (g *GeburaRepo) UpdateAppCategory( userID model.InternalID, ac *modelgebura.AppCategory) error { return g.data.WithTx(ctx, func(tx *ent.Tx) error { + // get old old, err := g.data.db.AppCategory.Query(). - WithAppAppCategory(). - Where(appcategory.IDEQ(ac.ID)). + Where( + appcategory.IDEQ(ac.ID), + appcategory.UserIDEQ(userID), + ). Only(ctx) if err != nil { return err } - oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) - for _, aac := range old.Edges.AppAppCategory { - oldAppIDs = append(oldAppIDs, aac.AppID) + // remove existing + _, err = tx.AppAppCategory.Delete().Where( + appappcategory.HasAppCategoryWith( + appcategory.IDEQ(ac.ID), + appcategory.UserIDEQ(userID), + ), + ).Exec(ctx) + if err != nil { + return err } q := tx.AppCategory.Update(). Where( @@ -580,7 +590,6 @@ func (g *GeburaRepo) UpdateAppCategory( SetName(ac.Name). SetVersionNumber(old.VersionNumber + 1). SetVersionDate(time.Now()). - RemoveAppIDs(oldAppIDs...). AddAppIDs(ac.AppIDs...) return q.Exec(ctx) }) @@ -591,27 +600,19 @@ func (g *GeburaRepo) DeleteAppCategory( id model.InternalID, ) error { return g.data.WithTx(ctx, func(tx *ent.Tx) error { - old, err := g.data.db.AppCategory.Query(). - WithAppAppCategory(). - Where(appcategory.IDEQ(id)). - Only(ctx) - if err != nil { - return err - } - oldAppIDs := make([]model.InternalID, 0, len(old.Edges.AppAppCategory)) - for _, aac := range old.Edges.AppAppCategory { - oldAppIDs = append(oldAppIDs, aac.AppID) - } - err = tx.AppCategory.Update(). - Where( + _, err := tx.AppAppCategory.Delete().Where( + appappcategory.HasAppCategoryWith( appcategory.IDEQ(id), appcategory.UserIDEQ(userID), - ). - RemoveAppIDs(oldAppIDs...). - Exec(ctx) + ), + ).Exec(ctx) if err != nil { return err } - return tx.AppCategory.DeleteOneID(id).Exec(ctx) + _, err = tx.AppCategory.Delete().Where( + appcategory.IDEQ(id), + appcategory.UserIDEQ(userID), + ).Exec(ctx) + return err }) } From 77ca3dca062254f7672cc650d011d175c7c6d7d8 Mon Sep 17 00:00:00 2001 From: Tabing010102 Date: Mon, 7 Apr 2025 23:49:15 +0800 Subject: [PATCH 7/7] fix(gebura): use tx for UpdateAppCategory --- internal/data/gebura.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/data/gebura.go b/internal/data/gebura.go index be32f52e..0b03f49f 100644 --- a/internal/data/gebura.go +++ b/internal/data/gebura.go @@ -563,7 +563,7 @@ func (g *GeburaRepo) UpdateAppCategory( ac *modelgebura.AppCategory) error { return g.data.WithTx(ctx, func(tx *ent.Tx) error { // get old - old, err := g.data.db.AppCategory.Query(). + old, err := tx.AppCategory.Query(). Where( appcategory.IDEQ(ac.ID), appcategory.UserIDEQ(userID),