Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions internal/biz/bizgebura/app_category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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"
"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
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.ListAppCategories(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
}

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
}
95 changes: 95 additions & 0 deletions internal/data/gebura.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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"
"github.com/tuihub/librarian/internal/data/internal/ent/user"
Expand Down Expand Up @@ -521,3 +523,96 @@ 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)
}
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) ListAppCategories(ctx context.Context, userID model.InternalID) ([]*modelgebura.AppCategory, error) {
acs, err := g.data.db.AppCategory.Query().
WithAppAppCategory().
Where(appcategory.UserIDEQ(userID)).
All(ctx)
if err != nil {
return nil, err
}
res := make([]*modelgebura.AppCategory, len(acs))
for i := range acs {
res[i] = converter.ToBizAppCategoryExtend(acs[i])
}
return res, nil
}
func (g *GeburaRepo) UpdateAppCategory(
ctx context.Context,
userID model.InternalID,
ac *modelgebura.AppCategory) error {
return g.data.WithTx(ctx, func(tx *ent.Tx) error {
// get old
old, err := tx.AppCategory.Query().
Where(
appcategory.IDEQ(ac.ID),
appcategory.UserIDEQ(userID),
).
Only(ctx)
if err != nil {
return err
}
// 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(
appcategory.IDEQ(ac.ID),
appcategory.UserIDEQ(userID),
).
SetName(ac.Name).
SetVersionNumber(old.VersionNumber + 1).
SetVersionDate(time.Now()).
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 {
_, err := tx.AppAppCategory.Delete().Where(
appappcategory.HasAppCategoryWith(
appcategory.IDEQ(id),
appcategory.UserIDEQ(userID),
),
).Exec(ctx)
if err != nil {
return err
}
_, err = tx.AppCategory.Delete().Where(
appcategory.IDEQ(id),
appcategory.UserIDEQ(userID),
).Exec(ctx)
return err
})
}
16 changes: 16 additions & 0 deletions internal/data/internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"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"
)
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions internal/data/internal/converter/ent_to_biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/data/internal/converter/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion internal/data/internal/ent/app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions internal/data/internal/ent/app/app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading