aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/api/photos.go15
-rw-r--r--internal/api/query.go34
2 files changed, 46 insertions, 3 deletions
diff --git a/internal/api/photos.go b/internal/api/photos.go
index 1e9db99..e777914 100644
--- a/internal/api/photos.go
+++ b/internal/api/photos.go
@@ -4,17 +4,26 @@ import (
"encoding/json"
"log"
"net/http"
-
- "github.com/RanXom/galleryd/internal/gallery"
)
func (s *Server) handlePhotos(
w http.ResponseWriter,
r *http.Request,
) {
+ query, err := parseQuery(r)
+ if err != nil {
+ writeError(
+ w,
+ http.StatusBadRequest,
+ "invalid query parameters",
+ )
+
+ return
+ }
+
photos, err := s.gallery.Gallery(
r.Context(),
- gallery.Query{},
+ query,
)
if err != nil {
log.Printf("build gallery: %v", err)
diff --git a/internal/api/query.go b/internal/api/query.go
new file mode 100644
index 0000000..cfb7435
--- /dev/null
+++ b/internal/api/query.go
@@ -0,0 +1,34 @@
+package api
+
+import (
+ "net/http"
+ "strconv"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+)
+
+func parseQuery(r *http.Request) (gallery.Query, error) {
+ query := gallery.Query{}
+
+ values := r.URL.Query()
+
+ if limit := values.Get("limit"); limit != "" {
+ n, err := strconv.Atoi(limit)
+ if err != nil {
+ return gallery.Query{}, err
+ }
+
+ query.Limit = n
+ }
+
+ if offset := values.Get("offset"); offset != "" {
+ n, err := strconv.Atoi(offset)
+ if err != nil {
+ return gallery.Query{}, err
+ }
+
+ query.Offset = n
+ }
+
+ return query, nil
+}