Skip to content

Commit

Permalink
feat: 任务列表分页
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 5, 2023
1 parent af495a7 commit 36a610a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
21 changes: 21 additions & 0 deletions module/dborm/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dborm

import (
"errors"
"regexp"
"strings"
)

var orderSafe = regexp.MustCompile(`^(\w+)( DESC)?$`)

func OrderSafe(data string) error {

for _, v := range strings.Split(data, ",") {
if !orderSafe.MatchString(v) {
return errors.New("unsafe order")
}
}

return nil

}
15 changes: 13 additions & 2 deletions module/model/taskline/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,30 @@ func Fetch(data *FetchParam) (*dborm.Taskline, error) {
type FetchAllParam struct {
UserId uint
WorkerId string
Page int
Order string
}

func FetchAll(data *FetchAllParam) ([]*dborm.Taskline, error) {

var items []*dborm.Taskline

offset := 0
if data.Page > 1 {
offset = (data.Page - 1) * 100
}

if err := dborm.OrderSafe(data.Order); err != nil {
return nil, err
}

result := dborm.Db.
Where(&dborm.Taskline{
UserId: data.UserId,
WorkerId: data.WorkerId,
}).
Order("id DESC").
Limit(50).
Order(data.Order).
Limit(100).Offset(offset).
Find(&items)

return items, result.Error
Expand Down

0 comments on commit 36a610a

Please sign in to comment.