Implement artist GetByID
This commit is contained in:
@@ -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": {
|
"/users/create": {
|
||||||
"post": {
|
"post": {
|
||||||
"consumes": [
|
"consumes": [
|
||||||
|
|||||||
@@ -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": {
|
"/users/create": {
|
||||||
"post": {
|
"post": {
|
||||||
"consumes": [
|
"consumes": [
|
||||||
|
|||||||
@@ -71,6 +71,31 @@ paths:
|
|||||||
summary: List all artists
|
summary: List all artists
|
||||||
tags:
|
tags:
|
||||||
- artist
|
- 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}:
|
/users/{id}:
|
||||||
get:
|
get:
|
||||||
parameters:
|
parameters:
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"zardzul/music-index/repository"
|
"zardzul/music-index/repository"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ArtistHandler struct {
|
type ArtistHandler struct {
|
||||||
@@ -31,3 +32,27 @@ func (handler *ArtistHandler) GetAll(c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(http.StatusOK, artists)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,10 +3,13 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
db "zardzul/music-index/sqlc"
|
db "zardzul/music-index/sqlc"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ArtistRepository interface {
|
type ArtistRepository interface {
|
||||||
GetAll(ctx context.Context) ([]db.Artist, error)
|
GetAll(ctx context.Context) ([]db.Artist, error)
|
||||||
|
GetByID(ctx context.Context, id pgtype.UUID) (db.Artist, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SQLCArtistRepository struct {
|
type SQLCArtistRepository struct {
|
||||||
@@ -20,3 +23,7 @@ func NewArtistRepository(queries *db.Queries) *SQLCArtistRepository {
|
|||||||
func (r *SQLCArtistRepository) GetAll(ctx context.Context) ([]db.Artist, error) {
|
func (r *SQLCArtistRepository) GetAll(ctx context.Context) ([]db.Artist, error) {
|
||||||
return r.q.GetAllArtists(ctx)
|
return r.q.GetAllArtists(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *SQLCArtistRepository) GetByID(ctx context.Context, id pgtype.UUID) (db.Artist, error) {
|
||||||
|
return r.q.GetArtistByID(ctx, id)
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,5 +35,6 @@ func Routes(router *gin.Engine, userHandler *handlers.UserHandler, artistHandler
|
|||||||
artist.Use(middleware.JWTAuth(jwtSecret))
|
artist.Use(middleware.JWTAuth(jwtSecret))
|
||||||
{
|
{
|
||||||
artist.GET("/", artistHandler.GetAll)
|
artist.GET("/", artistHandler.GetAll)
|
||||||
|
artist.GET("/:id", artistHandler.GetByID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user