added alternative swagger @failure

This commit is contained in:
Wouter Groeneveld 2024-04-15 12:17:50 +02:00
parent 0e372a4e51
commit 315527ee72
5 changed files with 71 additions and 0 deletions

View File

@ -3,6 +3,10 @@
A simple Go-powered REST API kata.
## HTTP Server
- KISS: Use built-in `http` package. https://gin-gonic.com/ looks cooler but also adds dozens of dependencies 😮 No need for a "fully-featured" web framework.
## Swagger
- Exposed at `http://localhost:8080/docs`

View File

@ -38,6 +38,29 @@ const docTemplate = `{
}
}
}
},
"/pokemon/{name}": {
"get": {
"produces": [
"application/json"
],
"summary": "Find a specific Pokemon by name",
"operationId": "get-specific-pokemon",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/pokemon.Pokemon"
}
},
"500": {
"description": "error",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {

View File

@ -32,6 +32,29 @@
}
}
}
},
"/pokemon/{name}": {
"get": {
"produces": [
"application/json"
],
"summary": "Find a specific Pokemon by name",
"operationId": "get-specific-pokemon",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/pokemon.Pokemon"
}
},
"500": {
"description": "error",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {

View File

@ -48,4 +48,19 @@ paths:
schema:
$ref: '#/definitions/pokemon.Pokemon'
summary: get all Pokemon
/pokemon/{name}:
get:
operationId: get-specific-pokemon
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/pokemon.Pokemon'
"500":
description: error
schema:
type: string
summary: Find a specific Pokemon by name
swagger: "2.0"

View File

@ -7,6 +7,12 @@ import (
"pokedex/rest"
)
// @Summary Find a specific Pokemon by name
// @ID get-specific-pokemon
// @Produce json
// @Success 200 {object} Pokemon
// @failure 500 {string} string "error"
// @Router /pokemon/{name} [get]
func HandleFindSingle(db *gorm.DB, rw http.ResponseWriter, req *http.Request) {
repo := NewRepo(db)
name := req.PathValue("name")