From 6bff66b31bbc485521adbab4c0180a2de3bd041c Mon Sep 17 00:00:00 2001 From: zardzul Date: Sat, 14 Mar 2026 20:00:36 +0100 Subject: [PATCH] Implement artist GetByID --- api/docs/docs.go | 38 +++++++++++++++++++++++++++++ api/docs/swagger.json | 38 +++++++++++++++++++++++++++++ api/docs/swagger.yaml | 25 +++++++++++++++++++ api/handlers/artist_handler.go | 25 +++++++++++++++++++ api/repository/artist_repository.go | 7 ++++++ api/routes/router.go | 1 + 6 files changed, 134 insertions(+) diff --git a/api/docs/docs.go b/api/docs/docs.go index f07f8af..b6b7a46 100644 --- a/api/docs/docs.go +++ b/api/docs/docs.go @@ -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": [ diff --git a/api/docs/swagger.json b/api/docs/swagger.json index 40c54f6..8703629 100644 --- a/api/docs/swagger.json +++ b/api/docs/swagger.json @@ -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": [ diff --git a/api/docs/swagger.yaml b/api/docs/swagger.yaml index cb1f1d9..7690790 100644 --- a/api/docs/swagger.yaml +++ b/api/docs/swagger.yaml @@ -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: diff --git a/api/handlers/artist_handler.go b/api/handlers/artist_handler.go index 576ab9d..039a683 100644 --- a/api/handlers/artist_handler.go +++ b/api/handlers/artist_handler.go @@ -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) +} diff --git a/api/repository/artist_repository.go b/api/repository/artist_repository.go index f1d77b6..0e568da 100644 --- a/api/repository/artist_repository.go +++ b/api/repository/artist_repository.go @@ -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) +} diff --git a/api/routes/router.go b/api/routes/router.go index 987cf8e..67a8592 100644 --- a/api/routes/router.go +++ b/api/routes/router.go @@ -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) } }