Skip to content

Commit

Permalink
update: resources data sort
Browse files Browse the repository at this point in the history
  • Loading branch information
kainonly committed Apr 26, 2023
1 parent 436c68a commit c396b12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 9 additions & 4 deletions resources/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,14 @@ func (x *Controller) BulkDelete(ctx context.Context, c *app.RequestContext) {
}

type SortDto struct {
Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
Data []primitive.ObjectID `json:"data,required" vd:"len($)>0;msg:'the submission data must be an array of ObjectId'"`
Txn string `json:"txn"`
Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
Data SortDtoData `json:"data,required"`
Txn string `json:"txn"`
}

type SortDtoData struct {
Key string `json:"key,required"`
Values []primitive.ObjectID `json:"values,required" vd:"len($)>0;msg:'the submission data must be an array of ObjectId'"`
}

// Sort
Expand All @@ -540,7 +545,7 @@ func (x *Controller) Sort(ctx context.Context, c *app.RequestContext) {
return
}

_, err := x.Service.Sort(ctx, dto.Collection, dto.Data)
_, err := x.Service.Sort(ctx, dto.Collection, dto.Data.Key, dto.Data.Values)
if err != nil {
c.Error(err)
return
Expand Down
7 changes: 4 additions & 3 deletions resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ func (x *Service) BulkDelete(ctx context.Context, name string, filter M) (r inte
return
}

func (x *Service) Sort(ctx context.Context, name string, ids []primitive.ObjectID) (r interface{}, err error) {
func (x *Service) Sort(ctx context.Context, name string, key string, ids []primitive.ObjectID) (r interface{}, err error) {
var wms []mongo.WriteModel
for i, id := range ids {
update := M{
"$set": M{
"sort": i,
key: i,
"update_time": time.Now(),
},
}
Expand Down Expand Up @@ -303,7 +303,8 @@ func (x *Service) Invoke(ctx context.Context, dto PendingDto) (_ interface{}, _
case "bulk_delete":
return x.BulkDelete(ctx, dto.Name, dto.Data.(M))
case "sort":
return x.Sort(ctx, dto.Name, dto.Data.([]primitive.ObjectID))
data := dto.Data.(SortDtoData)
return x.Sort(ctx, dto.Name, data.Key, data.Values)
}
return
}
Expand Down

0 comments on commit c396b12

Please sign in to comment.