Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add limit param for resources #535

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading