47 lines
973 B
Go
47 lines
973 B
Go
package pokemon
|
|
|
|
import (
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"net/http"
|
|
"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")
|
|
fmt.Println("Path " + name)
|
|
|
|
pk, err := repo.Find(name)
|
|
if err != nil {
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
rest.Json(rw, []Pokemon{
|
|
pk,
|
|
})
|
|
}
|
|
|
|
// @Summary get all Pokemon
|
|
// @ID get-all-pokemon
|
|
// @Produce json
|
|
// @Success 200 {object} Pokemon
|
|
// @Router /pokemon [get]
|
|
func HandleFindAll(db *gorm.DB, rw http.ResponseWriter, req *http.Request) {
|
|
repo := NewRepo(db)
|
|
pokemons, err := repo.FindAll()
|
|
if err != nil {
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
rest.Json(rw, pokemons)
|
|
}
|