mirror of
https://github.com/cloudreve/cloudreve.git
synced 2026-04-18 12:09:40 +00:00
feat: implement batch delete share service (#3356)
* feat: implement batch delete service * refactor: implement `GetByHashIDs` to make sure only query database once * refactor: use decode instead of `Get` before `Delete` - fix incorrect `DeleteBatch` method (for admin) calling - implement DeleteBatchByUserID
This commit is contained in:
@@ -49,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 {
|
||||
@@ -195,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)
|
||||
}
|
||||
|
||||
@@ -72,6 +72,16 @@ func DeleteShare(c *gin.Context) {
|
||||
c.JSON(200, serializer.Response{})
|
||||
}
|
||||
|
||||
func BatchDeleteShare(c *gin.Context) {
|
||||
service := ParametersFromContext[*share.BatchDeleteShareService](c, share.BatchDeleteParamCtx{})
|
||||
err := service.Delete(c)
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.Err(c, err))
|
||||
}
|
||||
|
||||
c.JSON(200, serializer.Response{})
|
||||
}
|
||||
|
||||
func ShareRedirect(c *gin.Context) {
|
||||
service := ParametersFromContext[*share.ShortLinkRedirectService](c, share.ShortLinkRedirectParamCtx{})
|
||||
c.Redirect(http.StatusFound, service.RedirectTo(c))
|
||||
|
||||
@@ -38,7 +38,6 @@ func InitRouter(dep dependency.Dep) *gin.Engine {
|
||||
|
||||
l.Info("Current running mode: Slave.")
|
||||
return initSlaveRouter(dep)
|
||||
|
||||
}
|
||||
|
||||
func newGinEngine(dep dependency.Dep) *gin.Engine {
|
||||
@@ -842,6 +841,12 @@ func initMasterRouter(dep dependency.Dep) *gin.Engine {
|
||||
middleware.HashID(hashid.ShareID),
|
||||
controllers.DeleteShare,
|
||||
)
|
||||
share.DELETE("",
|
||||
middleware.LoginRequired(),
|
||||
middleware.RequiredScopes(types.ScopeSharesWrite),
|
||||
controllers.FromJSON[sharesvc.BatchDeleteShareService](sharesvc.BatchDeleteParamCtx{}),
|
||||
controllers.BatchDeleteShare,
|
||||
)
|
||||
//// 获取README文本文件内容
|
||||
//share.GET("readme/:id",
|
||||
// middleware.CheckShareUnlocked(),
|
||||
|
||||
@@ -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"
|
||||
@@ -27,8 +29,36 @@ type (
|
||||
ShowReadMe bool `json:"show_readme"`
|
||||
}
|
||||
ShareCreateParamCtx struct{}
|
||||
|
||||
BatchDeleteShareService struct {
|
||||
ShareIDs []string `json:"ids" binding:"required"`
|
||||
}
|
||||
BatchDeleteParamCtx struct{}
|
||||
)
|
||||
|
||||
func (service *BatchDeleteShareService) Delete(c *gin.Context) error {
|
||||
dep := dependency.FromContext(c)
|
||||
uid := inventory.UserIDFromContext(c)
|
||||
shareClient := dep.ShareClient()
|
||||
|
||||
var ids []int
|
||||
|
||||
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.DeleteBatchByUserID(c, uid, ids); err != nil {
|
||||
return serializer.NewError(serializer.CodeDBError, "Failed to delete shares", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Upsert 创建或更新分享
|
||||
func (service *ShareCreateService) Upsert(c *gin.Context, existed int) (string, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
|
||||
Reference in New Issue
Block a user