Implement artist GetByID

This commit is contained in:
zardzul
2026-03-14 20:00:36 +01:00
parent d5e608feeb
commit 6bff66b31b
6 changed files with 134 additions and 0 deletions
+38
View File
@@ -44,6 +44,44 @@ const docTemplate = `{
}
}
},
"/artists/:id": {
"get": {
"produces": [
"application/json"
],
"tags": [
"artist"
],
"summary": "List artist by ID",
"parameters": [
{
"type": "string",
"description": "Artist ID (UUID)",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/handlers.ErrorResponse"
}
}
}
}
},
"/users/create": {
"post": {
"consumes": [
+38
View File
@@ -37,6 +37,44 @@
}
}
},
"/artists/:id": {
"get": {
"produces": [
"application/json"
],
"tags": [
"artist"
],
"summary": "List artist by ID",
"parameters": [
{
"type": "string",
"description": "Artist ID (UUID)",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/handlers.ErrorResponse"
}
}
}
}
},
"/users/create": {
"post": {
"consumes": [
+25
View File
@@ -71,6 +71,31 @@ paths:
summary: List all artists
tags:
- artist
/artists/:id:
get:
parameters:
- description: Artist ID (UUID)
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
additionalProperties: true
type: object
type: array
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/handlers.ErrorResponse'
summary: List artist by ID
tags:
- artist
/users/{id}:
get:
parameters:
+25
View File
@@ -5,6 +5,7 @@ import (
"zardzul/music-index/repository"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgtype"
)
type ArtistHandler struct {
@@ -31,3 +32,27 @@ func (handler *ArtistHandler) GetAll(c *gin.Context) {
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)
}
+7
View File
@@ -3,10 +3,13 @@ package repository
import (
"context"
db "zardzul/music-index/sqlc"
"github.com/jackc/pgx/v5/pgtype"
)
type ArtistRepository interface {
GetAll(ctx context.Context) ([]db.Artist, error)
GetByID(ctx context.Context, id pgtype.UUID) (db.Artist, error)
}
type SQLCArtistRepository struct {
@@ -20,3 +23,7 @@ func NewArtistRepository(queries *db.Queries) *SQLCArtistRepository {
func (r *SQLCArtistRepository) GetAll(ctx context.Context) ([]db.Artist, error) {
return r.q.GetAllArtists(ctx)
}
func (r *SQLCArtistRepository) GetByID(ctx context.Context, id pgtype.UUID) (db.Artist, error) {
return r.q.GetArtistByID(ctx, id)
}
+1
View File
@@ -35,5 +35,6 @@ func Routes(router *gin.Engine, userHandler *handlers.UserHandler, artistHandler
artist.Use(middleware.JWTAuth(jwtSecret))
{
artist.GET("/", artistHandler.GetAll)
artist.GET("/:id", artistHandler.GetByID)
}
}