| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package server
- import (
- "encoding/json"
- "net/http"
- "strconv"
- "github.com/felamaslen/go-music-player/pkg/database"
- "github.com/felamaslen/go-music-player/pkg/logger"
- "github.com/felamaslen/go-music-player/pkg/read"
- "github.com/felamaslen/go-music-player/pkg/repository"
- "github.com/go-redis/redis/v7"
- )
- type ArtistsResponse struct {
- Artists []string `json:"artists"`
- }
- func routeFetchArtists(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
- db := database.GetConnection()
- artists, err := repository.SelectAllArtists(db)
- if err != nil {
- return err
- }
- response, err := json.Marshal(ArtistsResponse{
- Artists: *artists,
- })
- if err != nil {
- return err
- }
- w.Write(response)
- return nil
- }
- type AlbumsResponse struct {
- Artist string `json:"artist"`
- Albums []string `json:"albums"`
- }
- func routeFetchAlbums(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
- artist := r.URL.Query().Get("artist")
- db := database.GetConnection()
- albums, err := repository.SelectAlbumsByArtist(db, artist)
- if err != nil {
- return err
- }
- response, err := json.Marshal(AlbumsResponse{
- Artist: artist,
- Albums: *albums,
- })
- if err != nil {
- return err
- }
- w.Write(response)
- return nil
- }
- type SongsResponse struct {
- Artist string `json:"artist"`
- Songs *[]*read.SongExternal `json:"songs"`
- }
- func routeFetchSongs(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
- artist := r.URL.Query().Get("artist")
- db := database.GetConnection()
- songs, err := repository.SelectSongsByArtist(db, artist)
- if err != nil {
- return err
- }
- response, err := json.Marshal(SongsResponse{
- Artist: artist,
- Songs: songs,
- })
- if err != nil {
- return err
- }
- w.Write(response)
- return nil
- }
- func routeFetchSongInfo(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
- idRaw := r.URL.Query().Get("id")
- idInt, err := strconv.Atoi(idRaw)
- if err != nil {
- http.Error(w, "Must provide a valid id", http.StatusBadRequest)
- return nil
- }
- if idInt < 1 {
- http.Error(w, "id must be non-negative", http.StatusBadRequest)
- return nil
- }
- db := database.GetConnection()
- song, err := repository.SelectSong(db, idInt)
- if err != nil {
- if err.Error() == "No such ID" {
- http.Error(w, "Song not found", http.StatusNotFound)
- return nil
- }
- return err
- }
- response, err := json.Marshal(read.SongExternal{
- Id: idInt,
- TrackNumber: song.TrackNumber,
- Title: song.Title,
- Artist: song.Artist,
- Album: song.Album,
- Duration: song.Duration,
- })
- if err != nil {
- return err
- }
- w.Write(response)
- return nil
- }
|