Skip to content

Commit

Permalink
fix list
Browse files Browse the repository at this point in the history
  • Loading branch information
yasinsaee committed Jun 4, 2024
1 parent 6b2f978 commit 892af4f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
39 changes: 32 additions & 7 deletions api_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,48 @@ func (viewService ViewServiceRequest) View(a APIService) error {
}

// List handles listing models with pagination and filtering.
func (a APIService) List(c echo.Context, model Model, filter Filter) error {
func (listService ListServiceRequest) List(a APIService) error {
var (
err error
)

pfilter, _ := SetPagination(c, false)
pfilter, _ := SetPagination(listService.Context)

if err := c.Bind(filter); err != nil {
return a.ErrorResponse(c, http.StatusBadRequest, err, fmt.Sprintf("cannot bind %s filter", a.Name))
if listService.Security.Authenticator != nil {
if authenticated, err := listService.Security.Authenticator(listService.Context); err != nil || !authenticated {
return a.ErrorResponse(listService.Context, http.StatusUnauthorized, err, "authentication failed")
}
}

// Step 3: Authorization
if listService.Security.Authorizer != nil {
if authorized, err := listService.Security.Authorizer(listService.Context); err != nil || !authorized {
return a.ErrorResponse(listService.Context, http.StatusForbidden, err, "authorization failed")
}
}

if err := listService.Context.Bind(listService.Filters); err != nil {
return a.ErrorResponse(listService.Context, http.StatusBadRequest, err, fmt.Sprintf("cannot bind %s filter", a.Name))
}

totalCounts, totalPages, list, err := model.List(filter, pfilter)
if listService.BeforeGetList.Function != nil {
if err = listService.BeforeGetList.Function(listService.Model, listService.BeforeGetList.Params...); err != nil {
return a.ErrorResponse(listService.Context, http.StatusBadRequest, err, fmt.Sprintf("cannot use function before get list, error : %s ", err.Error()))
}
}

totalCounts, totalPages, list, err := listService.Model.List(listService.Filters, pfilter)
if err != nil {
return a.ErrorResponse(c, http.StatusBadRequest, err, fmt.Sprintf("cannot find any %s", a.Name))
return a.ErrorResponse(listService.Context, http.StatusBadRequest, err, fmt.Sprintf("cannot find any %s", a.Name))
}

if listService.AfterGetList.Function != nil {
if err = listService.AfterGetList.Function(listService.Model, listService.AfterGetList.Params...); err != nil {
return a.ErrorResponse(listService.Context, http.StatusBadRequest, err, fmt.Sprintf("cannot use function after get list, error : %s ", err.Error()))
}
}

return SuccessResponse(c, http.StatusOK, fmt.Sprintf("successfully loaded %s list", a.Name), echo.Map{
return SuccessResponse(listService.Context, http.StatusOK, fmt.Sprintf("successfully loaded %s list", a.Name), echo.Map{
a.Name + "s": list,
"total_counts": totalCounts,
"total_pages": totalPages,
Expand Down
12 changes: 8 additions & 4 deletions pagination.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package apimaker

import "github.com/labstack/echo/v4"
import (
"strconv"

"github.com/labstack/echo/v4"
)

type Pagination struct {
Limit int `query:"limit"`
Page int `query:"page"`
Sort string `query:"sort"`
Unlimited bool
Unlimited string `query:"unlimited"`
}

func SetPagination(c echo.Context, unlimited bool) (Pagination, error) {
func SetPagination(c echo.Context) (Pagination, error) {
pag := new(Pagination)
if err := c.Bind(pag); err != nil {
return *pag, err
Expand All @@ -19,7 +23,7 @@ func SetPagination(c echo.Context, unlimited bool) (Pagination, error) {
pag.Limit = 10
}

if unlimited {
if ok, _ := strconv.ParseBool(pag.Unlimited); ok {
pag.Limit = -1
}

Expand Down
19 changes: 18 additions & 1 deletion sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,24 @@ func main() {
pro := new(product.Product)
filter := new(product.ProductFilter)

if err := apiService.List(c, pro, filter); err != nil {
listServiceReq := apimaker.ListServiceRequest{
BaseServiceRequest: apimaker.BaseServiceRequest{
Context: c,
Model: pro,
Security: apimaker.Security{},
},
Pagination: apimaker.Pagination{
Limit: 10, //default is 10
Page: 1,
Sort: "",
Unlimited: "false",
},
Filters: filter,
BeforeGetList: apimaker.CreateFunc{}, //optional
AfterGetList: apimaker.CreateFunc{}, //optional
}.List(*apiService)

if err := listServiceReq; err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion sample/product/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func (a AddProductForm) Bind(model apimaker.Model) error {
}

// Bind the form data to the Product model
product.Name = a.Name
product.Name = "yasin"
return nil
}
6 changes: 4 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type UpdateServiceRequest struct {
// ListServiceRequest defines the structure for a service request used for listing resources.
type ListServiceRequest struct {
BaseServiceRequest
Pagination Pagination
Filters Filter
Pagination Pagination
Filters Filter
BeforeGetList CreateFunc
AfterGetList CreateFunc
}

// ViewServiceRequest defines the structure for a service request used for viewing a single resource.
Expand Down

0 comments on commit 892af4f

Please sign in to comment.