Skip to content

Commit 545ba62

Browse files
authored
feat(gebura): impl AppCategory (#122)
* feat(gebura): add AppCategory part 1 * feat(gebura): add ListAppCategory * feat(gebura): impl ListAppCategory * chore(gebura): update style, fix typo * fix(gebura): move all ops into tx in repo with tx * chore(gebura): improve db ops for appcategory * fix(gebura): use tx for UpdateAppCategory
1 parent a0ae9c3 commit 545ba62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5748
-702
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package bizgebura
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/tuihub/librarian/internal/biz/bizutils"
8+
"github.com/tuihub/librarian/internal/lib/libauth"
9+
"github.com/tuihub/librarian/internal/model"
10+
"github.com/tuihub/librarian/internal/model/modelgebura"
11+
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"
12+
)
13+
14+
func (g *Gebura) CreateAppCategory(ctx context.Context, ac *modelgebura.AppCategory) (*modelgebura.AppCategory, error) {
15+
claims := libauth.FromContextAssertUserType(ctx)
16+
if claims == nil {
17+
return nil, bizutils.NoPermissionError()
18+
}
19+
id, err := g.id.New()
20+
if err != nil {
21+
return nil, pb.ErrorErrorReasonUnspecified("%s", err)
22+
}
23+
ac.ID = id
24+
ac.VersionNumber = 1
25+
ac.VersionDate = time.Now()
26+
if err = g.repo.CreateAppCategory(ctx, claims.UserID, ac); err != nil {
27+
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
28+
}
29+
return ac, nil
30+
}
31+
32+
func (g *Gebura) ListAppCategories(ctx context.Context) ([]*modelgebura.AppCategory, error) {
33+
claims := libauth.FromContextAssertUserType(ctx)
34+
if claims == nil {
35+
return nil, bizutils.NoPermissionError()
36+
}
37+
acList, err := g.repo.ListAppCategories(ctx, claims.UserID)
38+
if err != nil {
39+
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
40+
}
41+
return acList, nil
42+
}
43+
44+
func (g *Gebura) UpdateAppCategory(ctx context.Context, ac *modelgebura.AppCategory) error {
45+
claims := libauth.FromContextAssertUserType(ctx)
46+
if claims == nil {
47+
return bizutils.NoPermissionError()
48+
}
49+
err := g.repo.UpdateAppCategory(ctx, claims.UserID, ac)
50+
if err != nil {
51+
return pb.ErrorErrorReasonUnspecified("%s", err.Error())
52+
}
53+
return nil
54+
}
55+
56+
func (g *Gebura) DeleteAppCategory(ctx context.Context, id model.InternalID) error {
57+
claims := libauth.FromContextAssertUserType(ctx)
58+
if claims == nil {
59+
return bizutils.NoPermissionError()
60+
}
61+
err := g.repo.DeleteAppCategory(ctx, claims.UserID, id)
62+
if err != nil {
63+
return pb.ErrorErrorReasonUnspecified("%s", err.Error())
64+
}
65+
return nil
66+
}

internal/data/gebura.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/tuihub/librarian/internal/data/internal/converter"
88
"github.com/tuihub/librarian/internal/data/internal/ent"
99
"github.com/tuihub/librarian/internal/data/internal/ent/app"
10+
"github.com/tuihub/librarian/internal/data/internal/ent/appappcategory"
11+
"github.com/tuihub/librarian/internal/data/internal/ent/appcategory"
1012
"github.com/tuihub/librarian/internal/data/internal/ent/appinfo"
1113
"github.com/tuihub/librarian/internal/data/internal/ent/appruntime"
1214
"github.com/tuihub/librarian/internal/data/internal/ent/user"
@@ -521,3 +523,96 @@ func (g *GeburaRepo) ListAppRunTimes(
521523
func (g *GeburaRepo) DeleteAppRunTime(ctx context.Context, userID model.InternalID, id model.InternalID) error {
522524
return g.data.db.AppRunTime.DeleteOneID(id).Exec(ctx)
523525
}
526+
527+
func (g *GeburaRepo) CreateAppCategory(ctx context.Context, userID model.InternalID, ac *modelgebura.AppCategory) error {
528+
q := g.data.db.AppCategory.Create().
529+
SetID(ac.ID).
530+
SetUserID(userID).
531+
SetVersionNumber(ac.VersionNumber).
532+
SetVersionDate(ac.VersionDate).
533+
SetName(ac.Name).
534+
AddAppIDs(ac.AppIDs...)
535+
return q.Exec(ctx)
536+
}
537+
func (g *GeburaRepo) GetAppCategory(ctx context.Context, id model.InternalID) (*modelgebura.AppCategory, error) {
538+
res, err := g.data.db.AppCategory.Query().
539+
Where(appcategory.IDEQ(id)).
540+
Only(ctx)
541+
if err != nil {
542+
return nil, err
543+
}
544+
return converter.ToBizAppCategory(res), nil
545+
}
546+
func (g *GeburaRepo) ListAppCategories(ctx context.Context, userID model.InternalID) ([]*modelgebura.AppCategory, error) {
547+
acs, err := g.data.db.AppCategory.Query().
548+
WithAppAppCategory().
549+
Where(appcategory.UserIDEQ(userID)).
550+
All(ctx)
551+
if err != nil {
552+
return nil, err
553+
}
554+
res := make([]*modelgebura.AppCategory, len(acs))
555+
for i := range acs {
556+
res[i] = converter.ToBizAppCategoryExtend(acs[i])
557+
}
558+
return res, nil
559+
}
560+
func (g *GeburaRepo) UpdateAppCategory(
561+
ctx context.Context,
562+
userID model.InternalID,
563+
ac *modelgebura.AppCategory) error {
564+
return g.data.WithTx(ctx, func(tx *ent.Tx) error {
565+
// get old
566+
old, err := tx.AppCategory.Query().
567+
Where(
568+
appcategory.IDEQ(ac.ID),
569+
appcategory.UserIDEQ(userID),
570+
).
571+
Only(ctx)
572+
if err != nil {
573+
return err
574+
}
575+
// remove existing
576+
_, err = tx.AppAppCategory.Delete().Where(
577+
appappcategory.HasAppCategoryWith(
578+
appcategory.IDEQ(ac.ID),
579+
appcategory.UserIDEQ(userID),
580+
),
581+
).Exec(ctx)
582+
if err != nil {
583+
return err
584+
}
585+
q := tx.AppCategory.Update().
586+
Where(
587+
appcategory.IDEQ(ac.ID),
588+
appcategory.UserIDEQ(userID),
589+
).
590+
SetName(ac.Name).
591+
SetVersionNumber(old.VersionNumber + 1).
592+
SetVersionDate(time.Now()).
593+
AddAppIDs(ac.AppIDs...)
594+
return q.Exec(ctx)
595+
})
596+
}
597+
func (g *GeburaRepo) DeleteAppCategory(
598+
ctx context.Context,
599+
userID model.InternalID,
600+
id model.InternalID,
601+
) error {
602+
return g.data.WithTx(ctx, func(tx *ent.Tx) error {
603+
_, err := tx.AppAppCategory.Delete().Where(
604+
appappcategory.HasAppCategoryWith(
605+
appcategory.IDEQ(id),
606+
appcategory.UserIDEQ(userID),
607+
),
608+
).Exec(ctx)
609+
if err != nil {
610+
return err
611+
}
612+
_, err = tx.AppCategory.Delete().Where(
613+
appcategory.IDEQ(id),
614+
appcategory.UserIDEQ(userID),
615+
).Exec(ctx)
616+
return err
617+
})
618+
}

internal/data/internal/converter/converter.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strings"
77

88
"github.com/tuihub/librarian/internal/data/internal/ent"
9+
"github.com/tuihub/librarian/internal/model"
10+
"github.com/tuihub/librarian/internal/model/modelgebura"
911
"github.com/tuihub/librarian/internal/model/modelnetzach"
1012
"github.com/tuihub/librarian/internal/model/modelyesod"
1113
)
@@ -80,3 +82,17 @@ func ToBizFeedItemDigest(a *ent.FeedItem) *modelyesod.FeedItemDigest {
8082
// TODO incomplete
8183
return digest
8284
}
85+
86+
func ToBizAppCategoryExtend(ac *ent.AppCategory) *modelgebura.AppCategory {
87+
res := ToBizAppCategory(ac)
88+
if res == nil {
89+
return res
90+
}
91+
if len(ac.Edges.AppAppCategory) > 0 {
92+
res.AppIDs = make([]model.InternalID, 0, len(ac.Edges.AppAppCategory))
93+
for _, aac := range ac.Edges.AppAppCategory {
94+
res.AppIDs = append(res.AppIDs, aac.AppID)
95+
}
96+
}
97+
return res
98+
}

internal/data/internal/converter/ent_to_biz.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ type toBizConverter interface { //nolint:unused // used by generator
9999
ToBizAppRunTime(*ent.AppRunTime) *modelgebura.AppRunTime
100100
ToBizAppRunTimeList([]*ent.AppRunTime) []*modelgebura.AppRunTime
101101
ToBizAppBinary(ent.StoreAppBinary) modelgebura.AppBinary
102+
// goverter:ignore AppIDs
103+
ToBizAppCategory(*ent.AppCategory) *modelgebura.AppCategory
102104

103105
// goverter:map LatestPullAt LatestPullTime
104106
// goverter:ignore ActionSets

internal/data/internal/converter/generated.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/data/internal/ent/app.go

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/data/internal/ent/app/app.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)