diff --git a/internal/db/db.go b/internal/db/db.go index d9664ff..72b56ea 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -1,6 +1,7 @@ package db import ( + "git.ssns.se/git/frozendragon/pokemon-go-trade/internal/model" "github.com/jmoiron/sqlx" "github.com/pkg/errors" ) @@ -13,6 +14,11 @@ 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 { @@ -51,6 +57,51 @@ 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 {