Skip to content

Commit

Permalink
feat: add limit param for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
calisio authored and PThorpe92 committed Nov 26, 2024
1 parent a4e8ef5 commit 8d31837
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions backend/src/database/leftmenulinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
log "github.com/sirupsen/logrus"
)

func (db *DB) GetLeftMenuLinks() ([]models.LeftMenuLink, error) {
func (db *DB) GetLeftMenuLinks(limit int) ([]models.LeftMenuLink, error) {
var links []models.LeftMenuLink
if err := db.Find(&links).Error; err != nil {
if err := db.Model(&models.LeftMenuLink{}).Limit(limit).Find(&links).Error; err != nil {
return nil, newGetRecordsDBError(err, "left_menu_links")
}
return links, nil
Expand Down
8 changes: 7 additions & 1 deletion backend/src/handlers/left_menu_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"UnlockEdv2/src/models"
"encoding/json"
"net/http"
"strconv"
)

func (srv *Server) registerLeftMenuRoutes() []routeDef {
Expand All @@ -16,7 +17,12 @@ func (srv *Server) registerLeftMenuRoutes() []routeDef {

func (srv *Server) handleGetLeftMenu(w http.ResponseWriter, r *http.Request, log sLog) error {
log.info("GET: /api/left-menu")
links, err := srv.Db.GetLeftMenuLinks()
var limit int
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
limit = -1
}
links, err := srv.Db.GetLeftMenuLinks(limit)
if err != nil {
return newDatabaseServiceError(err)
}
Expand Down
12 changes: 7 additions & 5 deletions backend/src/handlers/left_menu_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import (
"UnlockEdv2/src/models"
"bytes"
"encoding/json"
"fmt"
"net/http"
"slices"
"testing"
)

func TestHandleGetLeftMenu(t *testing.T) {
httpTests := []httpTest{
{"TestGetLeftMenuAsAdmin", "admin", nil, http.StatusOK, ""},
{"TestGetLeftMenuAsUser", "student", nil, http.StatusOK, ""},
{"TestGetLeftMenuAsAdmin", "admin", map[string]any{"limit": -1}, http.StatusOK, ""},
{"TestGetLeftMenuAsUser", "student", map[string]any{"limit": -1}, http.StatusOK, ""},
{"TestGetLeftMenuWithLimitAsUser", "student", map[string]any{"limit": 5}, http.StatusOK, "?limit=5"},
}
for _, test := range httpTests {
t.Run(test.testName, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/api/left-menu", nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/left-menu%v", test.queryParams), nil)
if err != nil {
t.Fatalf("unable to create new request, error is %v", err)
}
handler := getHandlerByRole(server.handleGetLeftMenu, test.role)
rr := executeRequest(t, req, handler, test)
menuLinks, err := server.Db.GetLeftMenuLinks()
menuLinks, err := server.Db.GetLeftMenuLinks(test.mapKeyValues["limit"].(int))
if err != nil {
t.Fatalf("unable to retrieve menu links, error is %v", err)
}
Expand Down Expand Up @@ -64,7 +66,7 @@ func TestHandlePostLeftMenuLinks(t *testing.T) {
handler := getHandlerByRoleWithMiddleware(server.handlePostLeftMenuLinks, test.role)
rr := executeRequest(t, req, handler, test)
if test.expectedStatusCode == http.StatusCreated {
menuLinks, err := server.Db.GetLeftMenuLinks()
menuLinks, err := server.Db.GetLeftMenuLinks(-1)
if err != nil {
t.Errorf("failed to retrieve menu links, error is %v", err)
}
Expand Down

0 comments on commit 8d31837

Please sign in to comment.