Skip to content

Commit

Permalink
feat: add pagination for all objects (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlocksuper authored Aug 9, 2024
1 parent a6064c4 commit 55e690d
Show file tree
Hide file tree
Showing 44 changed files with 1,162 additions and 484 deletions.
38 changes: 31 additions & 7 deletions controllers/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package controllers
import (
"encoding/json"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)

// GetGlobalArticles
Expand Down Expand Up @@ -45,14 +47,36 @@ func (c *ApiController) GetGlobalArticles() {
// @router /get-articles [get]
func (c *ApiController) GetArticles() {
owner := c.Input().Get("owner")

articles, err := object.GetArticles(owner)
if err != nil {
c.ResponseError(err.Error())
return
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
articles, err := object.GetArticles(owner)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(object.GetMaskedArticles(articles, true))
} else {
limit := util.ParseInt(limit)
count, err := object.GetArticleCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := pagination.SetPaginator(c.Ctx, limit, count)
articles, err := object.GetPaginationArticles(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(articles, paginator.Nums())
}

c.ResponseOk(object.GetMaskedArticles(articles, true))
}

// GetArticle
Expand Down
37 changes: 31 additions & 6 deletions controllers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package controllers
import (
"encoding/json"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)
Expand All @@ -28,13 +29,37 @@ import (
// @Success 200 {array} object.Chat The Response object
// @router /get-global-chats [get]
func (c *ApiController) GetGlobalChats() {
chats, err := object.GetGlobalChats()
if err != nil {
c.ResponseError(err.Error())
return
}
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

c.ResponseOk(chats)
if limit == "" || page == "" {
chats, err := object.GetGlobalChats()
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(chats)
} else {
limit := util.ParseInt(limit)
count, err := object.GetChatCount("", field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := pagination.SetPaginator(c.Ctx, limit, count)
chats, err := object.GetPaginationChat("", paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(chats, paginator.Nums())
}
}

// GetChats
Expand Down
40 changes: 33 additions & 7 deletions controllers/factorset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package controllers
import (
"encoding/json"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)

// GetGlobalFactorsets
Expand Down Expand Up @@ -45,14 +47,38 @@ func (c *ApiController) GetGlobalFactorsets() {
// @router /get-factorsets [get]
func (c *ApiController) GetFactorsets() {
owner := c.Input().Get("owner")

factorsets, err := object.GetFactorsets(owner)
if err != nil {
c.ResponseError(err.Error())
return
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
factorsets, err := object.GetFactorsets(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(factorsets)
} else {
limit := util.ParseInt(limit)
count, err := object.GetFactorsetCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

paginator := pagination.SetPaginator(c.Ctx, limit, count)
factorsets, err := object.GetPaginationFactorsets(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(factorsets, paginator.Nums())
}

c.ResponseOk(factorsets)
}

// GetFactorset
Expand Down
37 changes: 31 additions & 6 deletions controllers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)
Expand All @@ -29,13 +30,37 @@ import (
// @Success 200 {array} object.Message The Response object
// @router /get-global-messages [get]
func (c *ApiController) GetGlobalMessages() {
messages, err := object.GetGlobalMessages()
if err != nil {
c.ResponseError(err.Error())
return
}
owner := "admin"
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
messages, err := object.GetGlobalMessages()
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(messages)
} else {
limit := util.ParseInt(limit)
count, err := object.GetMessageCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := pagination.SetPaginator(c.Ctx, limit, count)
messages, err := object.GetPaginationMessage(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(messages)
c.ResponseOk(messages, paginator.Nums())
}
}

// GetMessages
Expand Down
40 changes: 33 additions & 7 deletions controllers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package controllers
import (
"encoding/json"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)

// GetGlobalProviders
Expand All @@ -44,14 +46,38 @@ func (c *ApiController) GetGlobalProviders() {
// @router /get-providers [get]
func (c *ApiController) GetProviders() {
owner := "admin"

providers, err := object.GetProviders(owner)
if err != nil {
c.ResponseError(err.Error())
return
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
providers, err := object.GetProviders(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(providers)
} else {
limit := util.ParseInt(limit)
count, err := object.GetProviderCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

paginator := pagination.SetPaginator(c.Ctx, limit, count)
providers, err := object.GetPaginationProviders(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(providers, paginator.Nums())
}

c.ResponseOk(object.GetMaskedProviders(providers, true))
}

// GetProvider
Expand Down
39 changes: 33 additions & 6 deletions controllers/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package controllers
import (
"encoding/json"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)

// GetGlobalStores
Expand All @@ -27,13 +29,38 @@ import (
// @Success 200 {array} object.Store The Response object
// @router /get-global-stores [get]
func (c *ApiController) GetGlobalStores() {
stores, err := object.GetGlobalStores()
if err != nil {
c.ResponseError(err.Error())
return
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
stores, err := object.GetGlobalStores()
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(stores)
} else {
limit := util.ParseInt(limit)
count, err := object.GetStoreCount(field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

paginator := pagination.SetPaginator(c.Ctx, limit, count)
stores, err := object.GetPaginationStores(paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(stores, paginator.Nums())
}

c.ResponseOk(stores)
}

// GetStores
Expand Down
39 changes: 32 additions & 7 deletions controllers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package controllers
import (
"encoding/json"

"github.com/astaxie/beego/utils/pagination"
"github.com/casibase/casibase/object"
"github.com/casibase/casibase/util"
)

// GetGlobalTasks
Expand Down Expand Up @@ -45,14 +47,37 @@ func (c *ApiController) GetGlobalTasks() {
// @router /get-tasks [get]
func (c *ApiController) GetTasks() {
owner := c.Input().Get("owner")

tasks, err := object.GetTasks(owner)
if err != nil {
c.ResponseError(err.Error())
return
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
tasks, err := object.GetTasks(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(object.GetMaskedTasks(tasks, true))
} else {
limit := util.ParseInt(limit)
count, err := object.GetTaskCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

paginator := pagination.SetPaginator(c.Ctx, limit, count)
tasks, err := object.GetPaginationTasks(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(tasks, paginator.Nums())
}

c.ResponseOk(object.GetMaskedTasks(tasks, true))
}

// GetTask
Expand Down
Loading

0 comments on commit 55e690d

Please sign in to comment.