summaryrefslogtreecommitdiffstats
path: root/modules/context/api.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-03-13 23:20:22 -0400
committerUnknwon <u@gogs.io>2016-03-13 23:20:22 -0400
commit9bd9ad420582a7a34d18011847bb789f64271b1f (patch)
tree05b1191897ed5f56395faacd3c8bf1ab00a14c9c /modules/context/api.go
parentdd6faf7f9bd0a1dbf986e124ea0f4db249e1da48 (diff)
downloadgitea-9bd9ad420582a7a34d18011847bb789f64271b1f.tar.gz
gitea-9bd9ad420582a7a34d18011847bb789f64271b1f.zip
#1692 add CRUD issue APIs
- Fix go-gogs-client#10 - Related to #809
Diffstat (limited to 'modules/context/api.go')
-rw-r--r--modules/context/api.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/modules/context/api.go b/modules/context/api.go
index 44232d71a6..8d036be9c0 100644
--- a/modules/context/api.go
+++ b/modules/context/api.go
@@ -5,10 +5,15 @@
package context
import (
+ "fmt"
+ "strings"
+
+ "github.com/Unknwon/paginater"
"gopkg.in/macaron.v1"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
+ "github.com/gogits/gogs/modules/setting"
)
type APIContext struct {
@@ -35,6 +40,27 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
})
}
+func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
+ page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
+ links := make([]string, 0, 4)
+ if page.HasNext() {
+ links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.Next()))
+ }
+ if !page.IsLast() {
+ links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.TotalPages()))
+ }
+ if !page.IsFirst() {
+ links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppUrl, ctx.Req.URL.Path[1:]))
+ }
+ if page.HasPrevious() {
+ links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.Previous()))
+ }
+
+ if len(links) > 0 {
+ ctx.Header().Set("Link", strings.Join(links, ","))
+ }
+}
+
func APIContexter() macaron.Handler {
return func(c *Context) {
ctx := &APIContext{