Newer
Older
pokemon-go-trade / internal / web / web.go
package web

import (
	"encoding/json"
	"git.ssns.se/git/frozendragon/pokemon-go-trade/internal/model"
	"git.ssns.se/git/frozendragon/pokemon-go-trade/internal/pokemon"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"io/ioutil"
	"net/http"
	"strconv"
)

type WebPackage struct {
	Config  *viper.Viper
	Log     *logrus.Logger
	Pokemon pokemon.Interface
}

type PokemonsWantResponse struct {
	ID        int             `json:"id"`
	GroupName string          `json:"group_name"`
	Pokemons  []model.Pokemon `json:"pokemons"`
}

type PokemonsResponse struct {
	Pokemons []model.Pokemon `json:"pokemons"`
}

type DeleteStoredPokemonWantRequest struct {
	ID int `json:"id"`
}

func (wp *WebPackage) GetAvailablePokemon(w http.ResponseWriter, r *http.Request) {
	availablePokemonResponse, err := wp.Pokemon.GetAvailablePokemons()
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	bodyBytes, err := json.Marshal(availablePokemonResponse)
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	w.WriteHeader(200)
	_, _ = w.Write(bodyBytes)
}

type PostStoredPokemonRequest struct {
	Pokemons []model.Pokemon `json:"pokemons"`
}

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 postStoredPokemonRequest PostStoredPokemonRequest
	err = json.Unmarshal(bodyBytes, &postStoredPokemonRequest)
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	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
		}

		for _, p := range postStoredPokemonRequest.Pokemons {
			err := wp.Pokemon.AddPokemonWantGroup(wantGroupID, p)
			if err != nil {
				w.WriteHeader(500)
				_, _ = w.Write([]byte(err.Error()))
				return
			}
		}
	} else {
		err := wp.Pokemon.StoreHavePokemons(postStoredPokemonRequest.Pokemons)
		if err != nil {
			w.WriteHeader(500)
			_, _ = w.Write([]byte(err.Error()))
			return
		}
	}

	w.WriteHeader(200)
	_, _ = 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) {
	pokemons, err := wp.Pokemon.GetHavePokemons()
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	bodyBytes, err := json.Marshal(PokemonsResponse{Pokemons: pokemons})
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	w.WriteHeader(200)
	_, _ = w.Write(bodyBytes)
}

func (wp *WebPackage) GetStoredPokemonWant(w http.ResponseWriter, r *http.Request) {
	pokemons, err := wp.Pokemon.GetWantGroups()
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	bodyBytes, err := json.Marshal(pokemons)
	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) {
	err := wp.Pokemon.CreateWantGroup()
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	wantGroups, err := wp.Pokemon.GetWantGroups()
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	bodyBytes, err := json.Marshal(wantGroups)
	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
	}

	err = wp.Pokemon.DeleteWantGroups(deleteStoredPokemonWantRequest.ID)
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	wantGroups, err := wp.Pokemon.GetWantGroups()
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	bodyBytes, err = json.Marshal(wantGroups)
	if err != nil {
		w.WriteHeader(500)
		_, _ = w.Write([]byte(err.Error()))
		return
	}

	w.WriteHeader(200)
	_, _ = w.Write(bodyBytes)
}