|
|
|
@@ -3,7 +3,9 @@ package controller |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"log/slog" |
|
|
|
"net/http" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
"github.com/AFASystems/presence/internal/pkg/api/response" |
|
|
|
"github.com/AFASystems/presence/internal/pkg/model" |
|
|
|
@@ -34,12 +36,32 @@ func FloorAddController(db *gorm.DB, context context.Context) http.HandlerFunc { |
|
|
|
|
|
|
|
func FloorListController(db *gorm.DB, context context.Context) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
var floors []model.Floor |
|
|
|
if err := db.WithContext(context).Find(&floors).Error; err != nil { |
|
|
|
response.InternalError(w, "failed to list floors", err) |
|
|
|
query := r.URL.Query() |
|
|
|
|
|
|
|
lStr := query.Get("limit") |
|
|
|
if lStr == "" { |
|
|
|
lStr = "100" |
|
|
|
} |
|
|
|
limit, err := strconv.Atoi(lStr) |
|
|
|
if err != nil { |
|
|
|
slog.Error("invalid limit parameter", "value", lStr) |
|
|
|
response.BadRequest(w, "invalid limit parameter") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
oStr := query.Get("offset") |
|
|
|
if oStr == "" { |
|
|
|
oStr = "0" |
|
|
|
} |
|
|
|
offset, err := strconv.Atoi(oStr) |
|
|
|
if err != nil { |
|
|
|
slog.Error("invalid offset parameter", "value", oStr) |
|
|
|
response.BadRequest(w, "invalid offset parameter") |
|
|
|
return |
|
|
|
} |
|
|
|
if err := db.WithContext(context).Find(&floors).Error; err != nil { |
|
|
|
|
|
|
|
var floors []model.Floor |
|
|
|
if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&floors).Error; err != nil { |
|
|
|
response.InternalError(w, "failed to list floors", err) |
|
|
|
return |
|
|
|
} |
|
|
|
|