Skip to content

Commit

Permalink
feat: impl CreateFeedActionSet UpdateFeedActionSet `ListFeedActio…
Browse files Browse the repository at this point in the history
…nSets`
  • Loading branch information
MuZhou233 committed Jun 28, 2024
1 parent 3da81e0 commit f6f0de8
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 1 deletion.
57 changes: 57 additions & 0 deletions app/sephirah/internal/biz/bizyesod/feed_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package bizyesod

import (
"context"

"github.com/tuihub/librarian/app/sephirah/internal/biz/bizutils"
"github.com/tuihub/librarian/app/sephirah/internal/model/modelyesod"
"github.com/tuihub/librarian/internal/lib/libauth"
"github.com/tuihub/librarian/internal/model"
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"

"github.com/go-kratos/kratos/v2/errors"
)

func (y *Yesod) CreateFeedActionSet(
ctx context.Context,
set *modelyesod.FeedActionSet,
) (model.InternalID, *errors.Error) {
claims := libauth.FromContextAssertUserType(ctx)
if claims == nil {
return 0, bizutils.NoPermissionError()
}
id, err := y.searcher.NewID(ctx)
if err != nil {
return 0, pb.ErrorErrorReasonUnspecified("%s", err.Error())
}
set.ID = id
err = y.repo.CreateFeedActionSet(ctx, claims.UserID, set)
if err != nil {
return 0, pb.ErrorErrorReasonUnspecified("%s", err.Error())
}
return set.ID, nil
}

func (y *Yesod) UpdateFeedActionSet(ctx context.Context, set *modelyesod.FeedActionSet) *errors.Error {
claims := libauth.FromContextAssertUserType(ctx)
if claims == nil {
return bizutils.NoPermissionError()
}
err := y.repo.UpdateFeedActionSet(ctx, claims.UserID, set)
if err != nil {
return pb.ErrorErrorReasonUnspecified("%s", err.Error())
}
return nil
}

func (y *Yesod) ListFeedActionSets(ctx context.Context, paging model.Paging) ([]*modelyesod.FeedActionSet, int, *errors.Error) {
claims := libauth.FromContextAssertUserType(ctx)
if claims == nil {
return nil, 0, bizutils.NoPermissionError()
}
sets, total, err := y.repo.ListFeedActionSets(ctx, claims.UserID, paging)
if err != nil {
return nil, 0, pb.ErrorErrorReasonUnspecified("%s", err.Error())
}
return sets, total, nil
}
3 changes: 3 additions & 0 deletions app/sephirah/internal/biz/bizyesod/yesod.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type YesodRepo interface {
ListFeedItemsInCollection(context.Context, model.InternalID, model.Paging, []model.InternalID, []string,
[]string, []string, *model.TimeRange) ([]*modelyesod.FeedItemDigest, int, error)
GetFeedOwner(context.Context, model.InternalID) (*modeltiphereth.User, error)
CreateFeedActionSet(context.Context, model.InternalID, *modelyesod.FeedActionSet) error
UpdateFeedActionSet(context.Context, model.InternalID, *modelyesod.FeedActionSet) error
ListFeedActionSets(context.Context, model.InternalID, model.Paging) ([]*modelyesod.FeedActionSet, int, error)
}

type Yesod struct {
Expand Down
3 changes: 3 additions & 0 deletions app/sephirah/internal/data/internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func ToBizFeedConfig(a *ent.FeedConfig) *modelyesod.FeedConfig {
func ToBizFeedConfigList(a []*ent.FeedConfig) []*modelyesod.FeedConfig {
return toBiz.ToBizFeedConfigList(a)
}
func ToBizFeedActionSetList(a []*ent.FeedActionSet) []*modelyesod.FeedActionSet {
return toBiz.ToBizFeedActionSetList(a)
}
func ToBizFeed(a *ent.Feed) *modelfeed.Feed {
return toBiz.ToBizFeed(a)
}
Expand Down
4 changes: 4 additions & 0 deletions app/sephirah/internal/data/internal/converter/ent_to_biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ type toBizConverter interface { //nolint:unused // used by generator
// goverter:enum:map LatestPullStatusFailed FeedConfigPullStatusFailed
ToBizFeedConfigPullStatus(feedconfig.LatestPullStatus) modelyesod.FeedConfigPullStatus

// goverter:matchIgnoreCase
ToBizFeedActionSet(*ent.FeedActionSet) *modelyesod.FeedActionSet
ToBizFeedActionSetList([]*ent.FeedActionSet) []*modelyesod.FeedActionSet

// goverter:matchIgnoreCase
// goverter:ignore Items
// goverter:ignore FeedType
Expand Down
40 changes: 40 additions & 0 deletions app/sephirah/internal/data/internal/converter/generated.go

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

55 changes: 54 additions & 1 deletion app/sephirah/internal/data/yesod.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/converter"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/feed"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/feedactionset"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/feedconfig"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/feeditem"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/feeditemcollection"
Expand Down Expand Up @@ -43,7 +44,8 @@ func (y *yesodRepo) CreateFeedConfig(ctx context.Context, owner model.InternalID
SetPullInterval(c.PullInterval).
SetLatestPullStatus(converter.ToEntFeedConfigLatestPullStatus(c.LatestPullStatus)).
SetLatestPullMessage("").
SetHideItems(c.HideItems)
SetHideItems(c.HideItems).
AddFeedActionSetIDs(c.ActionSets...)
return q.Exec(ctx)
}

Expand Down Expand Up @@ -74,6 +76,9 @@ func (y *yesodRepo) UpdateFeedConfig(ctx context.Context, userID model.InternalI
if c.PullInterval > 0 {
q.SetPullInterval(c.PullInterval).SetNextPullBeginAt(time.Now())
}
if c.ActionSets != nil {
q.ClearFeedActionSet().AddFeedActionSetIDs(c.ActionSets...)
}
q.SetHideItems(c.HideItems)
return q.Exec(ctx)
}
Expand Down Expand Up @@ -522,3 +527,51 @@ func (y *yesodRepo) GetFeedOwner(ctx context.Context, id model.InternalID) (*mod
}
return converter.ToBizUser(only), nil
}

func (y *yesodRepo) CreateFeedActionSet(ctx context.Context, id model.InternalID, set *modelyesod.FeedActionSet) error {
return y.data.db.FeedActionSet.Create().
SetOwnerID(id).
SetID(set.ID).
SetName(set.Name).
SetDescription(set.Description).
SetActions(set.Actions).
Exec(ctx)
}

func (y *yesodRepo) UpdateFeedActionSet(ctx context.Context, id model.InternalID, set *modelyesod.FeedActionSet) error {
return y.data.db.FeedActionSet.UpdateOneID(set.ID).
Where(feedactionset.HasOwnerWith(user.IDEQ(id))).
SetName(set.Name).
SetDescription(set.Description).
SetActions(set.Actions).
Exec(ctx)
}

func (y *yesodRepo) ListFeedActionSets(ctx context.Context, id model.InternalID, paging model.Paging) ([]*modelyesod.FeedActionSet, int, error) {
var res []*modelyesod.FeedActionSet
var total int
err := y.data.WithTx(ctx, func(tx *ent.Tx) error {
u, err := tx.User.Get(ctx, id)
if err != nil {
return err
}
q := tx.User.QueryFeedActionSet(u)
total, err = q.Count(ctx)
if err != nil {
return err
}
sets, err := q.
Limit(paging.ToLimit()).
Offset(paging.ToOffset()).
All(ctx)
if err != nil {
return err
}
res = converter.ToBizFeedActionSetList(sets)
return nil
})
if err != nil {
return nil, 0, err
}
return res, total, nil
}
4 changes: 4 additions & 0 deletions app/sephirah/internal/model/converter/biz_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type toPBConverter interface { //nolint:unused // used by generator
ToPBFeedItemDigest(*modelyesod.FeedItemDigest) *pb.FeedItemDigest
ToPBFeedItemDigestList([]*modelyesod.FeedItemDigest) []*pb.FeedItemDigest

// goverter:matchIgnoreCase
ToPBFeedActionSet(*modelyesod.FeedActionSet) *pb.FeedActionSet
ToPBFeedActionSetList([]*modelyesod.FeedActionSet) []*pb.FeedActionSet

// goverter:matchIgnoreCase
ToPBFeedItemCollection(*modelyesod.FeedItemCollection) *pb.FeedItemCollection
ToPBFeedItemCollectionList([]*modelyesod.FeedItemCollection) []*pb.FeedItemCollection
Expand Down
6 changes: 6 additions & 0 deletions app/sephirah/internal/model/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func ToPBEnclosure(a *modelfeed.Enclosure) *librarian.FeedEnclosure {
func ToPBFeedConfig(a *modelyesod.FeedConfig) *pb.FeedConfig {
return toPB.ToPBFeedConfig(a)
}
func ToPBFeedActionSetList(a []*modelyesod.FeedActionSet) []*pb.FeedActionSet {
return toPB.ToPBFeedActionSetList(a)
}
func ToPBFeedWithConfig(a *modelyesod.FeedWithConfig) *pb.ListFeedConfigsResponse_FeedWithConfig {
return toPB.ToPBFeedWithConfig(a)
}
Expand Down Expand Up @@ -184,6 +187,9 @@ func ToBizFeedConfigStatusList(a []pb.FeedConfigStatus) []modelyesod.FeedConfigS
func ToBizFeedItemCollection(a *pb.FeedItemCollection) *modelyesod.FeedItemCollection {
return toBiz.ToBizFeedItemCollection(a)
}
func ToBizFeedActionSet(a *pb.FeedActionSet) *modelyesod.FeedActionSet {
return toBiz.ToBizFeedActionSet(a)
}
func ToBizTimeRange(a *librarian.TimeRange) *model.TimeRange {
return toBiz.ToBizTimeRange(a)
}
Expand Down
48 changes: 48 additions & 0 deletions app/sephirah/internal/model/converter/generated.go

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

3 changes: 3 additions & 0 deletions app/sephirah/internal/model/converter/pb_to_biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ type toBizConverter interface { //nolint:unused // used by generator
ToBizFeedConfigPullStatus(pb.FeedConfigPullStatus) modelyesod.FeedConfigPullStatus
ToBizFeedConfigStatusList([]pb.FeedConfigStatus) []modelyesod.FeedConfigStatus

// goverter:matchIgnoreCase
ToBizFeedActionSet(*pb.FeedActionSet) *modelyesod.FeedActionSet

// goverter:matchIgnoreCase
ToBizFeedItemCollection(*pb.FeedItemCollection) *modelyesod.FeedItemCollection

Expand Down
40 changes: 40 additions & 0 deletions app/sephirah/internal/service/yesod.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ func (s *LibrarianSephirahServiceService) ListFeedConfigs(
FeedsWithConfig: converter.ToPBFeedWithConfigList(feeds),
}, nil
}
func (s *LibrarianSephirahServiceService) CreateFeedActionSet(
ctx context.Context,
req *pb.CreateFeedActionSetRequest,
) (*pb.CreateFeedActionSetResponse, error) {
id, err := s.y.CreateFeedActionSet(ctx, converter.ToBizFeedActionSet(req.GetActionSet()))
if err != nil {
return nil, err
}
return &pb.CreateFeedActionSetResponse{
Id: converter.ToPBInternalID(id),
}, nil
}
func (s *LibrarianSephirahServiceService) UpdateFeedActionSet(
ctx context.Context,
req *pb.UpdateFeedActionSetRequest,
) (*pb.UpdateFeedActionSetResponse, error) {
err := s.y.UpdateFeedActionSet(ctx, converter.ToBizFeedActionSet(req.GetActionSet()))
if err != nil {
return nil, err
}
return &pb.UpdateFeedActionSetResponse{}, nil
}
func (s *LibrarianSephirahServiceService) ListFeedActionSets(
ctx context.Context,
req *pb.ListFeedActionSetsRequest,
) (*pb.ListFeedActionSetsResponse, error) {
if req.GetPaging() == nil {
return nil, pb.ErrorErrorReasonBadRequest("")
}
actions, total, err := s.y.ListFeedActionSets(ctx,
model.ToBizPaging(req.GetPaging()),
)
if err != nil {
return nil, err
}
return &pb.ListFeedActionSetsResponse{
Paging: &librarian.PagingResponse{TotalSize: int64(total)},
ActionSets: converter.ToPBFeedActionSetList(actions),
}, nil
}
func (s *LibrarianSephirahServiceService) ListFeedCategories(
ctx context.Context,
req *pb.ListFeedCategoriesRequest,
Expand Down

0 comments on commit f6f0de8

Please sign in to comment.