-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81c5118
commit 9924119
Showing
9 changed files
with
163 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¤tPage=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 + "¤tPage=1", | ||
Last: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "¤tPage=" + strconv.Itoa(int(total)/pageSizeInt), | ||
Prev: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "¤tPage=" + strconv.Itoa(currentPageInt-1), | ||
Next: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "¤tPage=" + 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} |