You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Koito/engine/handlers/get_album.go

46 lines
1.3 KiB

package handlers
import (
"net/http"
"strconv"
"github.com/gabehf/koito/internal/db"
"github.com/gabehf/koito/internal/logger"
"github.com/gabehf/koito/internal/utils"
)
func GetAlbumHandler(store db.DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
l := logger.FromContext(ctx)
l.Debug().Msg("GetAlbumHandler: Received request to retrieve album")
idStr := r.URL.Query().Get("id")
if idStr == "" {
l.Debug().Msg("GetAlbumHandler: Missing album ID in request")
utils.WriteError(w, "id must be provided", http.StatusBadRequest)
return
}
id, err := strconv.Atoi(idStr)
if err != nil {
l.Debug().AnErr("error", err).Msg("GetAlbumHandler: Invalid album ID")
utils.WriteError(w, "id is invalid", http.StatusBadRequest)
return
}
l.Debug().Msgf("GetAlbumHandler: Retrieving album with ID %d", id)
album, err := store.GetAlbum(ctx, db.GetAlbumOpts{ID: int32(id)})
if err != nil {
l.Err(err).Msgf("GetAlbumHandler: Failed to retrieve album with ID %d", id)
utils.WriteError(w, "album with specified id could not be found", http.StatusNotFound)
return
}
l.Debug().Msgf("GetAlbumHandler: Successfully retrieved album with ID %d", id)
utils.WriteJSON(w, http.StatusOK, album)
}
}