Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions app/controllers/pokemons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class PokemonsController < ApplicationController
before_action :authenticate_user!

def create
@pokemon = Pokemon.new(pokemon_params)
if @pokemon.save
render json: @pokemon, status: :created
else
render json: @pokemon.errors, status: :unprocessable_entity
end
@pokemon = Pokemon.new(pokemon_params)
if @pokemon.save
render json: @pokemon, status: :created
else
render json: @pokemon.errors, status: :unprocessable_entity
end
end

def search
Expand All @@ -21,38 +21,37 @@ def list
render json: Pokemon.all
end

def update
if @pokemon.update(pokemon_params)
render json: @pokemon
else
render json: @pokemon.errors, status: :unprocessable_entity
end
end
def update
if @pokemon.update(pokemon_params)
render json: @pokemon
else
render json: @pokemon.errors, status: :unprocessable_entity
end
end

def destroy
@pokemon.destroy
render json: { message: 'Pokemon was successfully destroyed.' }
end
def destroy
@pokemon.destroy
render json: { message: 'Pokemon was successfully destroyed.' }
end

def fetch_all_pokemon_data
response = HTTParty.get("#{POKEMON_API}?limit=10000")
if response.code == 200
render json: JSON.parse(response.body)
end
end
def fetch_all_pokemon_data
response = pokemons_service.all_pokemons
render json: response
end

def species
name = params[:name]
data = fetch_pokemon_species_data(name)
if data[:error]
render json: { error: data[:error] }, status: :not_found
else
description = find_english_description(data)
render json: { description: description }
end
end
def species
name = params[:name]
data = fetch_pokemon_species_data(name)
if data[:error]
render json: { error: data[:error] }, status: :not_found
else
description = find_english_description(data)
render json: { description: description }
end
end

private

def fetch_pokemon_data(pokemon_name)
response = HTTParty.get("#{POKEMON_API}/#{pokemon_name}")
if response.code == 200
Expand Down Expand Up @@ -87,4 +86,8 @@ def set_pokemon
def pokemon_params
params.require(:pokemon).permit(:nome, :tipo, :imagem, moves: [])
end

def pokemons_service
PokemonsService.new(PokemonsApiRepository.new)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isso aqui seria um exemplo, que teriamos 2 repos, um que procuraria na base, outro na api

end
end
12 changes: 12 additions & 0 deletions app/presenters/pokemons_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class PokemonsPresenter
def initialize(pokemon)
@pokemon = pokemon
end

def as_json
{
name: @pokemon.nome,
url: @pokemon.imagem
}
Comment on lines +6 to +10
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aqui é como queremos mostrar a info, separei num presenter para separar a logica

end
end
17 changes: 17 additions & 0 deletions app/repositories/pokemons_api_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class PokemonsApiRepository
POKEMON_API = "https://pokeapi.co/api/v2/pokemon"

attr_reader :url, :limit

def initialize(limit = 10000)
@url = POKEMON_API
@limit = limit
end

def fetch_all_as_json
response = HTTParty.get("#{url}?limit=#{limit}")
if response.code == 200
response.body
end
end
end
30 changes: 30 additions & 0 deletions app/repositories/pokemons_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class PokemonsRepository
require_relative "../presenters/pokemons_presenter"

def initialize(model = Pokemon)
@model = model
@presenter = ::PokemonsPresenter
end

def fetch_all_as_json
pokemons = all
{
count: pokemons.count,
next: nil,
previous: nil,
results: pokemons.map { |pokemon| @presenter.new(pokemon).as_json },
}.to_json
end

def all
@model.all
end

def find_by_name(name)
@model.find_by(nome: name)
end

def find_by_id(id)
@model.find(id)
end
end
11 changes: 11 additions & 0 deletions app/services/pokemons_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class PokemonsService

def initialize(repository = PokemonsRepository.new)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Por padrão, será o PokemonsRepository (que procura na base), o controller está procurando o da API, isso é legal pra gente isolar a logica das barreiras de acesso a banco de dados, logo podemos mudar como acessamos os dados sem mudar a interface

@repository = repository
end

def all_pokemons
JSON.parse(@repository.fetch_all_as_json)
end

end