Skip to content

Commit

Permalink
新增paginatar方法
Browse files Browse the repository at this point in the history
  • Loading branch information
hulutech-web committed Jun 18, 2024
1 parent 81c5118 commit 9924119
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

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

9 changes: 9 additions & 0 deletions .idea/http_result.iml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ go get -u github.com/hulutech-web/http_result
```
##### 1.1发布资源
```go
go run . artisan vendor:publish --package=github.com:hulutech-web/http_result
go run . artisan vendor:publish --package=github.com/hulutech-web/http_result

```
##### 1.2 注册服务提供者:config/app.go
```go
import "github.com:hulutech-web/http_result"
import "github.com/hulutech-web/http_result"

func init() {
"providers": []foundation.ServiceProvider{
Expand Down
6 changes: 5 additions & 1 deletion contracts/http_result.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package contracts

import "github.com/goravel/framework/contracts/http"
import (
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/http"
)

type HttpResult interface {
Success(message string, data interface{}) http.Response
Error(code int, message string, data interface{}) http.Response
ValidError(message string, errors map[string]map[string]string) http.Response
SearchByParams(params map[string]string, excepts ...string) func(methods orm.Query) orm.Query
}
13 changes: 8 additions & 5 deletions http_result.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http_result

import (
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
)
Expand All @@ -14,10 +15,12 @@ type Instance interface {
Success(message string, data interface{}) http.Response
Error(code int, message string, data interface{}) http.Response
ValidError(message string, errors map[string]map[string]string) http.Response
SearchByParams(params map[string]string, excepts ...string) func(methods orm.Query) orm.Query
}

type HttpResult struct {
ctx http.Context
Query orm.Query
Context http.Context
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Expand All @@ -26,7 +29,7 @@ type HttpResult struct {

func NewResult(ctx http.Context) *HttpResult {
return &HttpResult{
ctx: ctx,
Context: ctx,
}
}

Expand All @@ -37,7 +40,7 @@ func (h *HttpResult) Success(message string, data interface{}) http.Response {
if message == "" {
message = facades.Config().GetString("http_result.Message")
}
return h.ctx.Response().Success().Json(http.Json{
return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": data,
})
Expand All @@ -55,7 +58,7 @@ func (h *HttpResult) Error(code int, message string, data interface{}) http.Resp
if code == 0 {
code = facades.Config().GetInt("http_result.Code")
}
h.ctx.Request().AbortWithStatusJson(code, http.Json{
h.Context.Request().AbortWithStatusJson(code, http.Json{
"message": message,
"data": data,
})
Expand All @@ -72,7 +75,7 @@ func (h *HttpResult) ValidError(message string, errors map[string]map[string]str
if message == "" {
message = facades.Config().GetString("http_result.Message", "验证失败")
}
h.ctx.Request().AbortWithStatusJson(h.Code, http.Json{
h.Context.Request().AbortWithStatusJson(h.Code, http.Json{
"message": message,
"errors": errors,
})
Expand Down
95 changes: 95 additions & 0 deletions paginator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package http_result

import (
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/spf13/cast"
"math"
"strconv"
)

type Meta struct {
TotalPage int `json:"total_page"`
CurrentPage int `json:"current_page"`
PerPage int `json:"per_page"`
Total int64 `json:"total"`
}

type Links struct {
First string `json:"first"`
Last string `json:"last"`
Prev string `json:"prev"`
Next string `json:"next"`
}

type PageResult struct {
Data any `json:"data"` // List of data
Total int64 `json:"total"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}

// SearchByParams
// ?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx
func (h *HttpResult) SearchByParams(params map[string]string, excepts ...string) *HttpResult {
for _, except := range excepts {
delete(params, except)
}
query := facades.Orm().Query()
h.Query = func(q orm.Query) orm.Query {
for key, value := range params {
if value == "" || key == "pageSize" || key == "total" || key == "currentPage" || key == "sort" || key == "order" {
continue
} else {
q = q.Where(key+" like ?", "%"+value+"%")
}
}
return q
}(query)
return h
}

func (r *HttpResult) ResultPagination(ctx http.Context, dest any) (http.Response, error) {
r.Context = ctx
request := ctx.Request()
pageSize := request.Query("pageSize", "10")
pageSizeInt := cast.ToInt(pageSize)
currentPage := request.Query("currentPage", "1")
currentPageInt := cast.ToInt(currentPage)
total := int64(0)
r.Query.Model(dest).Paginate(currentPageInt, pageSizeInt, dest, &total)

URL_PATH := ctx.Request().Origin().URL.Path
var proto = "http://"
if request.Origin().TLS != nil {
proto = "https://"
}
// Corrected links generation
links := Links{
First: proto + request.Origin().Host + "/" + URL_PATH + "?pageSize=" + pageSize + "&currentPage=1",
Last: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(int(total)/pageSizeInt),
Prev: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt-1),
Next: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt+1),
}

// Corrected total page calculation
totalPage := int(math.Ceil(float64(total) / float64(pageSizeInt)))

meta := Meta{
TotalPage: totalPage,
CurrentPage: currentPageInt,
PerPage: pageSizeInt,
Total: total,
}

pageResult := PageResult{
Data: dest,
Total: total,
Links: links,
Meta: meta,
}

// 返回构建好的分页结果
return r.Context.Response().Success().Json(pageResult), nil
}
25 changes: 25 additions & 0 deletions test_httprequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package http_result

import (
"github.com/goravel/framework/database/orm"
)

type User struct {
orm.Model
Name string `gorm:"column:name;type:varchar(255);not null;default:''" form:"name" json:"name"`
Mobile string `gorm:"column:mobile;type:varchar(255);not null;default:''" form:"mobile" json:"mobile"`
Password string `gorm:"column:password;type:varchar(255);not null;default:''" form:"password" json:"password"`
Area string `gorm:"column:area;type:varchar(255);not null;default:''" form:"area" json:"area"`
Contact string `gorm:"column:contact;type:varchar(255);not null;default:''" form:"contact" json:"contact"`
ContactMobile string `gorm:"column:contact_mobile;type:varchar(255);not null;default:''" form:"contact_mobile" json:"contact_mobile"`
Address string `gorm:"column:address;type:varchar(255);not null;default:''" form:"address" json:"address"`
IdCard string `gorm:"column:id_card;type:varchar(255);not null;default:''" form:"id_card" json:"id_card"`
Pid uint `gorm:"column:pid;type:int(11);not null;default:0" form:"pid" json:"pid"`
Parent *User `gorm:"foreignKey:Pid;references:id" form:"parent" json:"parent"`
Children []User `gorm:"foreignKey:Pid;references:id" form:"children" json:"children"`
orm.SoftDeletes
}

func Test_HttpRequest() {

}

0 comments on commit 9924119

Please sign in to comment.