diff --git a/internal/web/web.go b/internal/web/web.go index 6f413eb..2963f87 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -2,10 +2,12 @@ import ( "encoding/json" + "fmt" "github.com/sirupsen/logrus" "github.com/spf13/viper" "io/ioutil" "net/http" + "strconv" ) type WebPackage struct { @@ -13,7 +15,13 @@ Log *logrus.Logger } -type AvailablePokemonResponse struct { +type PokemonsWantResponse struct { + ID int `json:"id"` + GroupName string `json:"group_name"` + Pokemons []Pokemon `json:"pokemons"` +} + +type PokemonsResponse struct { Pokemons []Pokemon `json:"pokemons"` } @@ -21,9 +29,26 @@ Url string `json:"url"` } +type DeleteStoredPokemonWantRequest struct { + ID int `json:"id"` +} + +var availablePokemonResponse = PokemonsResponse{ + Pokemons: []Pokemon{}, +} + +var pokemonsWantResponse = []PokemonsWantResponse{ + { + ID: 1, + GroupName: "want group 1", + Pokemons: availablePokemonResponse.Pokemons, + }, +} + + func (wp *WebPackage) GetAvailablePokemon(w http.ResponseWriter, r *http.Request) { - availablePokemonResponse := AvailablePokemonResponse{Pokemons: []Pokemon{ + availablePokemonResponse := PokemonsResponse{Pokemons: []Pokemon{ { Url: "/image/111-00-shiny.webp", }, @@ -46,20 +71,6 @@ _, _ = 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 { @@ -76,13 +87,25 @@ return } - availablePokemonResponse.Pokemons = append(availablePokemonResponse.Pokemons, Pokemon...) + if r.URL.Query().Get("type") == "want" { + wantGroupIDString := r.URL.Query().Get("want-group-id") + wantGroupID, err := strconv.Atoi(wantGroupIDString) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + pokemonsWantResponse[wantGroupID-1].Pokemons = append(pokemonsWantResponse[wantGroupID-1].Pokemons, Pokemon...) + } else { + availablePokemonResponse.Pokemons = append(availablePokemonResponse.Pokemons, Pokemon...) + } w.WriteHeader(200) _, _ = w.Write(bodyBytes) } -func (wp *WebPackage) GetStoredPokemon(w http.ResponseWriter, r *http.Request) { +func (wp *WebPackage) GetStoredPokemonHave(w http.ResponseWriter, r *http.Request) { bodyBytes, err := json.Marshal(availablePokemonResponse) if err != nil { w.WriteHeader(500) @@ -92,4 +115,63 @@ w.WriteHeader(200) _, _ = w.Write(bodyBytes) +} + +func (wp *WebPackage) GetStoredPokemonWant(w http.ResponseWriter, r *http.Request) { + bodyBytes, err := json.Marshal(pokemonsWantResponse) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(200) + _, _ = w.Write(bodyBytes) +} + +func (wp *WebPackage) CreateStoredPokemonWant(w http.ResponseWriter, r *http.Request) { + pokemonsWantResponse = append(pokemonsWantResponse, PokemonsWantResponse{ + GroupName: fmt.Sprintf("want group %d", len(pokemonsWantResponse)), + Pokemons: []Pokemon{}, + }) + + bodyBytes, err := json.Marshal(pokemonsWantResponse) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(200) + _, _ = w.Write(bodyBytes) +} + + +func (wp *WebPackage) DeleteStoredPokemonWant(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 deleteStoredPokemonWantRequest DeleteStoredPokemonWantRequest + err = json.Unmarshal(bodyBytes, &deleteStoredPokemonWantRequest) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + pokemonsWantResponse = append(pokemonsWantResponse[:deleteStoredPokemonWantRequest.ID], pokemonsWantResponse[deleteStoredPokemonWantRequest.ID+1:]...) + + bodyBytes, err = json.Marshal(pokemonsWantResponse) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(200) + _, _ = w.Write(bodyBytes) } \ No newline at end of file