59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"zardzul/music-index/repository"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
type ArtistHandler struct {
|
|
repo repository.ArtistRepository
|
|
}
|
|
|
|
func NewArtistHandler(repo repository.ArtistRepository) *ArtistHandler {
|
|
return &ArtistHandler{repo: repo}
|
|
}
|
|
|
|
// GetAll godoc
|
|
// @Summary List all artists
|
|
// @Tags artist
|
|
// @Produce json
|
|
// @Success 200 {array} map[string]interface{}
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /artists [get]
|
|
func (handler *ArtistHandler) GetAll(c *gin.Context) {
|
|
artists, err := handler.repo.GetAll(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, artists)
|
|
}
|
|
|
|
// GetByID godoc
|
|
// @Summary List artist by ID
|
|
// @Tags artist
|
|
// @produce json
|
|
// @Param id path string true "Artist ID (UUID)"
|
|
// @Success 200 {array} map[string]interface{}
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /artists/:id [get]
|
|
func (handler *ArtistHandler) GetByID(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
var pgID pgtype.UUID
|
|
if err := pgID.Scan(idParam); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid artist id"})
|
|
return
|
|
}
|
|
|
|
artist, err := handler.repo.GetByID(c.Request.Context(), pgID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, artist)
|
|
}
|