mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-17 19:26:36 -07:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
47
engine/handlers/search.go
Normal file
47
engine/handlers/search.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/models"
|
||||
"github.com/gabehf/koito/internal/utils"
|
||||
)
|
||||
|
||||
type SearchResults struct {
|
||||
Artists []*models.Artist `json:"artists"`
|
||||
Albums []*models.Album `json:"albums"`
|
||||
Tracks []*models.Track `json:"tracks"`
|
||||
}
|
||||
|
||||
func SearchHandler(store db.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
l := logger.FromContext(ctx)
|
||||
q := r.URL.Query().Get("q")
|
||||
artists, err := store.SearchArtists(ctx, q)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to search for artists")
|
||||
utils.WriteError(w, "failed to search in database", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
albums, err := store.SearchAlbums(ctx, q)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to search for albums")
|
||||
utils.WriteError(w, "failed to search in database", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tracks, err := store.SearchTracks(ctx, q)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to search for tracks")
|
||||
utils.WriteError(w, "failed to search in database", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
utils.WriteJSON(w, http.StatusOK, SearchResults{
|
||||
Artists: artists,
|
||||
Albums: albums,
|
||||
Tracks: tracks,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue