From b3f85275b52b49c7daad499bad7a6044b64d47e2 Mon Sep 17 00:00:00 2001 From: Adrien Date: Wed, 13 Oct 2021 18:02:59 +0200 Subject: [PATCH] feature: pokemon#index+show routes --- app/controllers/api/pokemons_controller.rb | 9 +++++++++ app/serializers/pokemon_serializer.rb | 11 +++++++++++ app/serializers/type_serializer.rb | 8 ++++++++ config/routes.rb | 3 +++ 4 files changed, 31 insertions(+) create mode 100644 app/controllers/api/pokemons_controller.rb create mode 100644 app/serializers/pokemon_serializer.rb create mode 100644 app/serializers/type_serializer.rb diff --git a/app/controllers/api/pokemons_controller.rb b/app/controllers/api/pokemons_controller.rb new file mode 100644 index 0000000..d5b4e02 --- /dev/null +++ b/app/controllers/api/pokemons_controller.rb @@ -0,0 +1,9 @@ +class Api::PokemonsController < ApplicationController + def index + render json: Pokemon.all, include: ['types'] + end + + def show + render json: Pokemon.find(params[:id]), include: ['types'] + end +end diff --git a/app/serializers/pokemon_serializer.rb b/app/serializers/pokemon_serializer.rb new file mode 100644 index 0000000..3d3fc6c --- /dev/null +++ b/app/serializers/pokemon_serializer.rb @@ -0,0 +1,11 @@ +class PokemonSerializer < ActiveModel::Serializer + attribute :id + attribute :name + attribute :base_experience + attribute :height + attribute :is_default + attribute :order + attribute :weight + + has_many :types +end diff --git a/app/serializers/type_serializer.rb b/app/serializers/type_serializer.rb new file mode 100644 index 0000000..547686f --- /dev/null +++ b/app/serializers/type_serializer.rb @@ -0,0 +1,8 @@ +class TypeSerializer < ActiveModel::Serializer + attribute :name + attribute :slot + + def slot + object.pokemon_types.slot + end +end diff --git a/config/routes.rb b/config/routes.rb index 1c65f1d..8593a0d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,7 @@ Rails.application.routes.draw do mount Sidekiq::Web => "/sidekiq" + + get '/api/pokemons', to: 'api/pokemons#index' + get '/api/pokemons/:id', to: 'api/pokemons#show' end