From 9bd9ad420582a7a34d18011847bb789f64271b1f Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 13 Mar 2016 23:20:22 -0400 Subject: #1692 add CRUD issue APIs - Fix go-gogs-client#10 - Related to #809 --- modules/context/api.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'modules/context') diff --git a/modules/context/api.go b/modules/context/api.go index 44232d71..8d036be9 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{ -- cgit v1.2.3