refactor: use decode instead of `Get` before `Delete`

- fix incorrect `DeleteBatch` method (for admin) calling
- implement DeleteBatchByUserID
pull/3356/head
scholar7r 4 days ago
parent a2552bbf90
commit 533b4c3d5d

@ -41,8 +41,6 @@ type (
GetByIDUser(ctx context.Context, id, uid int) (*ent.Share, error)
// GetByHashID returns the share with given hash id.
GetByHashID(ctx context.Context, idRaw string) (*ent.Share, error)
// GetByHashIDs returns the shares with given hash ids
GetByHashIDs(ctx context.Context, idsRaw []string) ([]*ent.Share, error)
// Upsert creates or update a new share record.
Upsert(ctx context.Context, params *CreateShareParams) (*ent.Share, error)
// Viewed increase the view count of the share.
@ -51,12 +49,14 @@ type (
Downloaded(ctx context.Context, share *ent.Share) error
// Delete deletes the share.
Delete(ctx context.Context, shareId int) error
// DeleteBatch deletes the shares with the given ids.
DeleteBatch(ctx context.Context, shareIds []int) error
// DeleteBatchByUserID deletes the shares with the given ids and user id
DeleteBatchByUserID(ctx context.Context, uid int, shareIds []int) error
// List returns a list of shares with the given args.
List(ctx context.Context, args *ListShareArgs) (*ListShareResult, error)
// CountByTimeRange counts the number of shares created in the given time range.
CountByTimeRange(ctx context.Context, start, end *time.Time) (int, error)
// DeleteBatch deletes the shares with the given ids.
DeleteBatch(ctx context.Context, shareIds []int) error
}
CreateShareParams struct {
@ -163,21 +163,6 @@ func (c *shareClient) GetByHashID(ctx context.Context, idRaw string) (*ent.Share
return c.GetByID(ctx, id)
}
func (c *shareClient) GetByHashIDs(ctx context.Context, idsRaw []string) ([]*ent.Share, error) {
var ids []int
for _, v := range idsRaw {
id, err := c.hasher.Decode(v, hashid.ShareID)
if err != nil {
return nil, fmt.Errorf("failed to decode hash id %q: %w", v, err)
}
ids = append(ids, id)
}
return c.GetByIDs(ctx, ids)
}
func (c *shareClient) GetByID(ctx context.Context, id int) (*ent.Share, error) {
s, err := withShareEagerLoading(ctx, c.client.Share.Query().Where(share.ID(id))).First(ctx)
if err != nil {
@ -212,6 +197,11 @@ func (c *shareClient) DeleteBatch(ctx context.Context, shareIds []int) error {
return err
}
func (c *shareClient) DeleteBatchByUserID(ctx context.Context, uid int, shareIds []int) error {
_, err := c.client.Share.Delete().Where(share.IDIn(shareIds...)).Where(share.HasUserWith(user.ID(uid))).Exec(ctx)
return err
}
func (c *shareClient) Delete(ctx context.Context, shareId int) error {
return c.client.Share.DeleteOneID(shareId).Exec(ctx)
}

@ -2,6 +2,7 @@ package share
import (
"context"
"fmt"
"time"
"github.com/cloudreve/Cloudreve/v4/application/dependency"
@ -10,6 +11,7 @@ import (
"github.com/cloudreve/Cloudreve/v4/inventory/types"
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/manager"
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
"github.com/cloudreve/Cloudreve/v4/pkg/serializer"
"github.com/cloudreve/Cloudreve/v4/service/explorer"
"github.com/gin-gonic/gin"
@ -34,21 +36,23 @@ type (
BatchDeleteParamCtx struct{}
)
func (s *BatchDeleteShareService) Delete(c *gin.Context) error {
func (service *BatchDeleteShareService) Delete(c *gin.Context) error {
dep := dependency.FromContext(c)
uid := inventory.UserIDFromContext(c)
shareClient := dep.ShareClient()
shares, err := shareClient.GetByHashIDs(c, s.ShareIDs)
if err != nil {
return err
}
var ids []int
for _, v := range shares {
ids = append(ids, v.ID)
for _, v := range service.ShareIDs {
id, err := dep.HashIDEncoder().Decode(v, hashid.ShareID)
if err != nil {
return fmt.Errorf("failed to decode hash id %q: %w", v, err)
}
ids = append(ids, id)
}
if err := shareClient.DeleteBatch(c, ids); err != nil {
if err := shareClient.DeleteBatchByUserID(c, uid, ids); err != nil {
return serializer.NewError(serializer.CodeDBError, "Failed to delete shares", err)
}

Loading…
Cancel
Save