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) }