GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
frozendragon
/
pokemon-go-trade
Browse code
Added want in db
master
1 parent
ea381c2
commit
8f8df63bcafe90ef2ee8c0e6c00a978933b96a2d
Simon Lindgren
authored
on 22 Feb 2021
Patch
Showing
5 changed files
internal/db/db.go
internal/model/model.go
internal/pokemon/pokemon.go
internal/web/web.go
migration/1_initial.sql
Ignore Space
Show notes
View
internal/db/db.go
package db import ( "git.ssns.se/git/frozendragon/pokemon-go-trade/internal/model" "github.com/jmoiron/sqlx" "github.com/pkg/errors" ) type Package struct { Db *sqlx.DB } type Interface interface { GetPokemons() ([]Pokemon, error) StoreHavePokemons(havePokemons []HavePokemon) error GetHavePokemons() ([]Pokemon, error) CreateWantGroup(wantGroup model.WantGroup) error GetWantGroups() ([]model.WantGroup, error) DeleteWantGroup(id int) error CreateWantPokemon(wantPokemon model.WantPokemon) error GetWantPokemonByWantGroup(wantGroupID int) ([]Pokemon, error) } type Pokemon struct { ID int `db:"id"` DexNumber int `db:"dex_number"` Region string `db:"region"` Name string `db:"name"` ShinyEnabled bool `db:"shiny_enabled"` ShinyImageName string `db:"shiny_image_name"` ImageName string `db:"image_name"` } type HavePokemon struct { ID int `db:"id"` PokemonID int `db:"pokemon_id"` Shiny bool `db:"shiny"` } func (p *Package) GetPokemons() ([]Pokemon, error) { var pokemons []Pokemon err := p.Db.Select(&pokemons, "SELECT * FROM pokemon") if err != nil { return nil, errors.WithStack(err) } return pokemons, nil } func (p *Package) GetHavePokemons() ([]Pokemon, error) { var pokemons []Pokemon err := p.Db.Select(&pokemons, "SELECT pokemon.id, pokemon.dex_number, pokemon.region, "+ "have_pokemon.shiny as shiny_enabled, pokemon.name, pokemon.shiny_image_name, pokemon.image_name "+ "FROM pokemon JOIN have_pokemon ON have_pokemon.pokemon_id = pokemon.id") if err != nil { return nil, errors.WithStack(err) } return pokemons, nil } func (p *Package) GetWantGroups() ([]model.WantGroup, error) { var wantGroups []model.WantGroup err := p.Db.Select(&wantGroups, "SELECT * FROM want_group_pokemon") if err != nil { return nil, errors.WithStack(err) } return wantGroups, nil } func (p *Package) GetWantPokemonByWantGroup(wantGroupID int) ([]Pokemon, error) { pokemons := []Pokemon{} err := p.Db.Select(&pokemons, "SELECT pokemon.* FROM want_pokemon JOIN pokemon ON want_pokemon.pokemon_id = pokemon.id AND want_pokemon.want_group_id = $1", wantGroupID) if err != nil { return nil, errors.WithStack(err) } return pokemons, nil } func (p *Package) CreateWantGroup(wantGroup model.WantGroup) error { insertQuery := `INSERT INTO want_group_pokemon(group_name) VALUES (:group_name)` _, err := p.Db.NamedExec(insertQuery, wantGroup) if err != nil { return errors.WithStack(err) } return nil } func (p *Package) CreateWantPokemon(wantPokemon model.WantPokemon) error { insertQuery := `INSERT INTO want_pokemon(pokemon_id, want_group_id, shiny) VALUES (:pokemon_id, :want_group_id, :shiny)` _, err := p.Db.NamedExec(insertQuery, wantPokemon) if err != nil { return errors.WithStack(err) } return nil } func (p *Package) DeleteWantGroup(id int) error { insertQuery := "DELETE FROM want_group_pokemon WHERE id = $1" _, err := p.Db.Exec(insertQuery, id) if err != nil { return errors.WithStack(err) } return nil } func (p *Package) StoreHavePokemons(havePokemons []HavePokemon) error { insertQuery := `INSERT INTO have_pokemon (pokemon_id, shiny) VALUES (:pokemon_id, :shiny)` for _, havePokemon := range havePokemons { _, err := p.Db.NamedExec(insertQuery, havePokemon) if err != nil { return errors.WithStack(err) } } return nil }
package db import ( "github.com/jmoiron/sqlx" "github.com/pkg/errors" ) type Package struct { Db *sqlx.DB } type Interface interface { GetPokemons() ([]Pokemon, error) StoreHavePokemons(havePokemons []HavePokemon) error GetHavePokemons() ([]Pokemon, error) } type Pokemon struct { ID int `db:"id"` DexNumber int `db:"dex_number"` Region string `db:"region"` Name string `db:"name"` ShinyEnabled bool `db:"shiny_enabled"` ShinyImageName string `db:"shiny_image_name"` ImageName string `db:"image_name"` } type HavePokemon struct { ID int `db:"id"` PokemonID int `db:"pokemon_id"` Shiny bool `db:"shiny"` } func (p *Package) GetPokemons() ([]Pokemon, error) { var pokemons []Pokemon err := p.Db.Select(&pokemons, "SELECT * FROM pokemon") if err != nil { return nil, errors.WithStack(err) } return pokemons, nil } func (p *Package) GetHavePokemons() ([]Pokemon, error) { var pokemons []Pokemon err := p.Db.Select(&pokemons, "SELECT pokemon.id, pokemon.dex_number, pokemon.region, "+ "have_pokemon.shiny as shiny_enabled, pokemon.name, pokemon.shiny_image_name, pokemon.image_name "+ "FROM pokemon JOIN have_pokemon ON have_pokemon.pokemon_id = pokemon.id") if err != nil { return nil, errors.WithStack(err) } return pokemons, nil } func (p *Package) StoreHavePokemons(havePokemons []HavePokemon) error { insertQuery := `INSERT INTO have_pokemon (pokemon_id, shiny) VALUES (:pokemon_id, :shiny)` for _, havePokemon := range havePokemons { _, err := p.Db.NamedExec(insertQuery, havePokemon) if err != nil { return errors.WithStack(err) } } return nil }
Ignore Space
Show notes
View
internal/model/model.go
package model import "fmt" type Pokemon struct { ID int `json:"id"` DexNumber int `json:"dex_number"` Name string `json:"name"` Shiny bool `json:"shiny"` Url string `json:"url"` Variations []Pokemon `json:"variations, omitempty"` } func (p *Pokemon) FormatUrl() { p.Url = fmt.Sprintf("image/%s", p.Url) } type WantGroup struct { ID int `db:"id" json:"id"` GroupName string `db:"group_name" json:"group_name"` Pokemons []Pokemon `json:"pokemons"` } type WantPokemon struct { ID int `db:"id"` PokemonID int `db:"pokemon_id"` WantGroupID int `db:"want_group_id"` Shiny bool `db:"shiny"` }
package model import "fmt" type Pokemon struct { ID int `json:"id"` DexNumber int `json:"dex_number"` Name string `json:"name"` Shiny bool `json:"shiny"` Url string `json:"url"` Variations []Pokemon `json:"variations, omitempty"` } func (p *Pokemon) FormatUrl() { p.Url = fmt.Sprintf("image/%s", p.Url) }
Ignore Space
Show notes
View
internal/pokemon/pokemon.go
package pokemon import ( "fmt" "git.ssns.se/git/frozendragon/pokemon-go-trade/internal/db" "git.ssns.se/git/frozendragon/pokemon-go-trade/internal/model" ) type Package struct { Db db.Interface } type Interface interface { GetAvailablePokemons() (*Pokemons, error) StoreHavePokemons(pokemons []model.Pokemon) error GetHavePokemons() ([]model.Pokemon, error) CreateWantGroup() error GetWantGroups() ([]model.WantGroup, error) DeleteWantGroups(id int) error AddPokemonWantGroup(wantGroupID int, pokemon model.Pokemon) error } type Pokemons struct { Pokemons []model.Pokemon `json:"pokemons"` } func (p *Package) GetAvailablePokemons() (*Pokemons, error) { dbPokemons, err := p.Db.GetPokemons() if err != nil { return nil, err } var pokemons Pokemons for _, pokemon := range dbPokemons { dexFound := false if pokemon.Region != "" { for i, p := range pokemons.Pokemons { if p.DexNumber == pokemon.DexNumber { pokemons.Pokemons[i].Variations = append(pokemons.Pokemons[i].Variations, model.Pokemon{ ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: true, Url: fmt.Sprintf("image/%s", pokemon.ShinyImageName), }) pokemons.Pokemons[i].Variations = append(pokemons.Pokemons[i].Variations, model.Pokemon{ ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: false, Url: fmt.Sprintf("image/%s", pokemon.ImageName), }) dexFound = true break } } } if dexFound { continue } if pokemon.ShinyEnabled { pokemons.Pokemons = append(pokemons.Pokemons, model.Pokemon{ ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: true, Url: fmt.Sprintf("image/%s", pokemon.ShinyImageName), Variations: []model.Pokemon{ { ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: false, Url: fmt.Sprintf("image/%s", pokemon.ImageName), }, }, }) } else { pokemons.Pokemons = append(pokemons.Pokemons, model.Pokemon{ ID: pokemon.ID, DexNumber: pokemon.DexNumber, Shiny: false, Url: fmt.Sprintf("image/%s", pokemon.ImageName), Variations: []model.Pokemon{}, }) } } return &pokemons, nil } func (p *Package) GetHavePokemons() ([]model.Pokemon, error) { pokemons := []model.Pokemon{} dbPokemons, err := p.Db.GetHavePokemons() if err != nil { return nil, err } for _, dbPokemon := range dbPokemons { pokemon := model.Pokemon{ ID: dbPokemon.ID, DexNumber: dbPokemon.DexNumber, Name: dbPokemon.Name, Shiny: dbPokemon.ShinyEnabled, } if dbPokemon.ShinyEnabled { pokemon.Url = dbPokemon.ShinyImageName } else { pokemon.Url = dbPokemon.ImageName } pokemon.FormatUrl() pokemons = append(pokemons, pokemon) } return pokemons, nil } func (p *Package) StoreHavePokemons(pokemons []model.Pokemon) error { var havePokemons []db.HavePokemon for _, pokemon := range pokemons { havePokemons = append(havePokemons, db.HavePokemon{ PokemonID: pokemon.ID, Shiny: pokemon.Shiny, }) } return p.Db.StoreHavePokemons(havePokemons) } func (p *Package) CreateWantGroup() error { return p.Db.CreateWantGroup(model.WantGroup{ GroupName: "want group", }) } func (p *Package) GetWantGroups() ([]model.WantGroup, error) { wantGroups, err := p.Db.GetWantGroups() if err != nil { return nil, err } for i, wantGroup := range wantGroups { dbPokemons, err := p.Db.GetWantPokemonByWantGroup(wantGroup.ID) if err != nil { return nil, err } pokemons := []model.Pokemon{} for _, dbPokemon := range dbPokemons { pokemon := model.Pokemon{ ID: dbPokemon.ID, Name: dbPokemon.Name, DexNumber: dbPokemon.DexNumber, Shiny: true, Url: fmt.Sprintf("image/%s", dbPokemon.ShinyImageName), } pokemons = append(pokemons, pokemon) } wantGroups[i].Pokemons = pokemons } return wantGroups, nil } func (p *Package) DeleteWantGroups(id int) error { return p.Db.DeleteWantGroup(id) } func (p *Package) AddPokemonWantGroup(wantGroupID int, pokemon model.Pokemon) error { return p.Db.CreateWantPokemon(model.WantPokemon{ PokemonID: pokemon.ID, WantGroupID: wantGroupID, Shiny: pokemon.Shiny, }) }
package pokemon import ( "fmt" "git.ssns.se/git/frozendragon/pokemon-go-trade/internal/db" "git.ssns.se/git/frozendragon/pokemon-go-trade/internal/model" ) type Package struct { Db db.Interface } type Interface interface { GetAvailablePokemons() (*Pokemons, error) StoreHavePokemons(pokemons []model.Pokemon) error GetHavePokemons() ([]model.Pokemon, error) } type Pokemons struct { Pokemons []model.Pokemon `json:"pokemons"` } func (p *Package) GetAvailablePokemons() (*Pokemons, error) { dbPokemons, err := p.Db.GetPokemons() if err != nil { return nil, err } var pokemons Pokemons for _, pokemon := range dbPokemons { dexFound := false if pokemon.Region != "" { for i, p := range pokemons.Pokemons { if p.DexNumber == pokemon.DexNumber { pokemons.Pokemons[i].Variations = append(pokemons.Pokemons[i].Variations, model.Pokemon{ ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: true, Url: fmt.Sprintf("image/%s", pokemon.ShinyImageName), }) pokemons.Pokemons[i].Variations = append(pokemons.Pokemons[i].Variations, model.Pokemon{ ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: false, Url: fmt.Sprintf("image/%s", pokemon.ImageName), }) dexFound = true break } } } if dexFound { continue } if pokemon.ShinyEnabled { pokemons.Pokemons = append(pokemons.Pokemons, model.Pokemon{ ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: true, Url: fmt.Sprintf("image/%s", pokemon.ShinyImageName), Variations: []model.Pokemon{ { ID: pokemon.ID, Name: pokemon.Name, DexNumber: pokemon.DexNumber, Shiny: false, Url: fmt.Sprintf("image/%s", pokemon.ImageName), }, }, }) } else { pokemons.Pokemons = append(pokemons.Pokemons, model.Pokemon{ ID: pokemon.ID, DexNumber: pokemon.DexNumber, Shiny: false, Url: fmt.Sprintf("image/%s", pokemon.ImageName), Variations: []model.Pokemon{}, }) } } return &pokemons, nil } func (p *Package) GetHavePokemons() ([]model.Pokemon, error) { pokemons := []model.Pokemon{} dbPokemons, err := p.Db.GetHavePokemons() if err != nil { return nil, err } for _, dbPokemon := range dbPokemons { pokemon := model.Pokemon{ ID: dbPokemon.ID, DexNumber: dbPokemon.DexNumber, Name: dbPokemon.Name, Shiny: dbPokemon.ShinyEnabled, } if dbPokemon.ShinyEnabled { pokemon.Url = dbPokemon.ShinyImageName } else { pokemon.Url = dbPokemon.ImageName } pokemon.FormatUrl() pokemons = append(pokemons, pokemon) } return pokemons, nil } func (p *Package) StoreHavePokemons(pokemons []model.Pokemon) error { var havePokemons []db.HavePokemon for _, pokemon := range pokemons { havePokemons = append(havePokemons, db.HavePokemon{ PokemonID: pokemon.ID, Shiny: pokemon.Shiny, }) } return p.Db.StoreHavePokemons(havePokemons) }
Ignore Space
Show notes
View
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) }
package web import ( "encoding/json" "fmt" "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"` } var availablePokemonResponse = PokemonsResponse{ Pokemons: []model.Pokemon{}, } var pokemonsWantResponse = []PokemonsWantResponse{ { ID: 1, GroupName: "want group 1", Pokemons: availablePokemonResponse.Pokemons, }, } 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 } pokemonsWantResponse[wantGroupID-1].Pokemons = append(pokemonsWantResponse[wantGroupID-1].Pokemons, postStoredPokemonRequest.Pokemons...) } else { err := wp.Pokemon.StoreHavePokemons(postStoredPokemonRequest.Pokemons) if err != nil { w.WriteHeader(500) _, _ = w.Write([]byte(err.Error())) return } availablePokemonResponse.Pokemons = append(availablePokemonResponse.Pokemons, postStoredPokemonRequest.Pokemons...) } 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) { 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{ ID: len(pokemonsWantResponse), GroupName: fmt.Sprintf("want group %d", len(pokemonsWantResponse)), Pokemons: []model.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-1], pokemonsWantResponse[deleteStoredPokemonWantRequest.ID:]...) bodyBytes, err = json.Marshal(pokemonsWantResponse) if err != nil { w.WriteHeader(500) _, _ = w.Write([]byte(err.Error())) return } w.WriteHeader(200) _, _ = w.Write(bodyBytes) }
Ignore Space
Show notes
View
migration/1_initial.sql
-- +migrate Up -- SQL in section 'Up' is executed when this migration is applied CREATE TABLE pokemon ( id INT PRIMARY KEY, dex_number INT, region varchar(200), name VARCHAR(200), shiny_enabled boolean, shiny_image_name VARCHAR(200), image_name VARCHAR(200) ); CREATE TABLE have_pokemon ( id SERIAL PRIMARY KEY, pokemon_id INT, shiny bool ); CREATE TABLE want_group_pokemon ( id SERIAL PRIMARY KEY, group_name VARCHAR(200) ); CREATE TABLE want_pokemon ( id SERIAL PRIMARY KEY, pokemon_id INT, want_group_id int, shiny bool ); -- +migrate Down -- SQL section 'Down' is executed when this migration is rolled back DROP TABLE pokemon;
-- +migrate Up -- SQL in section 'Up' is executed when this migration is applied CREATE TABLE pokemon ( id INT PRIMARY KEY, dex_number INT, region varchar(200), name VARCHAR(200), shiny_enabled boolean, shiny_image_name VARCHAR(200), image_name VARCHAR(200) ); CREATE TABLE have_pokemon ( id SERIAL PRIMARY KEY, pokemon_id INT, shiny bool ); -- +migrate Down -- SQL section 'Down' is executed when this migration is rolled back DROP TABLE pokemon;
Show line notes below