diff --git a/internal/web/web.go b/internal/web/web.go index 2963f87..344ff2f 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -26,6 +26,7 @@ } type Pokemon struct { + ID int `json:"id"` Url string `json:"url"` } @@ -50,12 +51,15 @@ availablePokemonResponse := PokemonsResponse{Pokemons: []Pokemon{ { + ID: 111, Url: "/image/111-00-shiny.webp", }, { + ID:129, Url: "/image/129-00-shiny.webp", }, { + ID: 243, Url: "/image/243-00-shiny.webp", }, }} @@ -105,6 +109,61 @@ _, _ = w.Write(bodyBytes) } +type LinkedStoredPokemonResponse struct { + Links []LinkedStoredPokemonRequest `json:"links"` +} + +type LinkedStoredPokemonRequest struct { + GroupID int `json:"group_id"` + SelectedID int `json:"selected_id"` +} + +var linkedStoredPokemonResponse = LinkedStoredPokemonResponse{ + Links: []LinkedStoredPokemonRequest{}, +} + +func (wp *WebPackage) GetLinkedStoredPokemon(w http.ResponseWriter, r *http.Request) { + bodyBytes, err := json.Marshal(linkedStoredPokemonResponse) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(200) + _, _ = w.Write(bodyBytes) +} + + +func (wp *WebPackage) PostLinkedStoredPokemon(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 link LinkedStoredPokemonRequest + err = json.Unmarshal(bodyBytes, &link) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + linkedStoredPokemonResponse.Links = append(linkedStoredPokemonResponse.Links, link) + + bodyBytes, err = json.Marshal(linkedStoredPokemonResponse) + if err != nil { + w.WriteHeader(500) + _, _ = w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(200) + _, _ = w.Write(bodyBytes) +} + func (wp *WebPackage) GetStoredPokemonHave(w http.ResponseWriter, r *http.Request) { bodyBytes, err := json.Marshal(availablePokemonResponse) if err != nil { @@ -131,6 +190,7 @@ func (wp *WebPackage) CreateStoredPokemonWant(w http.ResponseWriter, r *http.Request) { pokemonsWantResponse = append(pokemonsWantResponse, PokemonsWantResponse{ + ID: len(pokemonsWantResponse), GroupName: fmt.Sprintf("want group %d", len(pokemonsWantResponse)), Pokemons: []Pokemon{}, }) @@ -163,7 +223,7 @@ return } - pokemonsWantResponse = append(pokemonsWantResponse[:deleteStoredPokemonWantRequest.ID], pokemonsWantResponse[deleteStoredPokemonWantRequest.ID+1:]...) + pokemonsWantResponse = append(pokemonsWantResponse[:deleteStoredPokemonWantRequest.ID-1], pokemonsWantResponse[deleteStoredPokemonWantRequest.ID:]...) bodyBytes, err = json.Marshal(pokemonsWantResponse) if err != nil {