package web import ( "encoding/json" "github.com/sirupsen/logrus" "github.com/spf13/viper" "io/ioutil" "net/http" ) type WebPackage struct { Config *viper.Viper Log *logrus.Logger } type AvailablePokemonResponse struct { Pokemons []Pokemon `json:"pokemons"` } type Pokemon struct { Url string `json:"url"` } func (wp *WebPackage) GetAvailablePokemon(w http.ResponseWriter, r *http.Request) { availablePokemonResponse := AvailablePokemonResponse{Pokemons: []Pokemon{ { Url: "/image/111-00-shiny.webp", }, { Url: "/image/129-00-shiny.webp", }, { Url: "/image/243-00-shiny.webp", }, }} bodyBytes, err := json.Marshal(availablePokemonResponse) if err != nil { w.WriteHeader(500) _, _ = w.Write([]byte(err.Error())) return } w.WriteHeader(200) _, _ = w.Write(bodyBytes) } var availablePokemonResponse = AvailablePokemonResponse{ Pokemons: []Pokemon{ { Url: "/image/111-00-shiny.webp", }, { Url: "/image/129-00-shiny.webp", }, { Url: "/image/243-00-shiny.webp", }, }, } func (wp *WebPackage) PostStoredPokemon(w http.ResponseWriter, r *http.Request) { bodyBytes, err := ioutil.ReadAll(r.Body) if err != nil { w.WriteHeader(500) _, _ = w.Write([]byte(err.Error())) return } var Pokemon []Pokemon err = json.Unmarshal(bodyBytes, &Pokemon) if err != nil { w.WriteHeader(500) _, _ = w.Write([]byte(err.Error())) return } availablePokemonResponse.Pokemons = append(availablePokemonResponse.Pokemons, Pokemon...) w.WriteHeader(200) _, _ = w.Write(bodyBytes) } func (wp *WebPackage) GetStoredPokemon(w http.ResponseWriter, r *http.Request) { bodyBytes, err := json.Marshal(availablePokemonResponse) if err != nil { w.WriteHeader(500) _, _ = w.Write([]byte(err.Error())) return } w.WriteHeader(200) _, _ = w.Write(bodyBytes) }