Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pokedex API #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Up and Running:
2. Run the command `bundle install`
3. Create the database with `rails db:create`
4. Run the migrations `rails db:migrate`
5. Seed the dabase `brake import:import_pokemon`
5. Seed the dabase `rake import:import_pokemon`


# API Docs
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/pokemons_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Api::V1::PokemonsController < ApplicationController
before_action :set_pokemon, only: [:show, :update, :destroy]

#CRUD POKEMON

def index
@pokemons = Pokemon.all.includes(:types)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/v1/types_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Api::V1::TypesController < ApplicationController

before_action :set_type, only: [:show, :update, :destroy]
#CRUD TYPES

def index
@types = Type.all
Expand Down
1 change: 1 addition & 0 deletions app/models/pokemon.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Pokemon < ApplicationRecord
#MODEL POKEMON, ASSOCIATED WITH TYPES AND VALIDATIONS
validates :name, presence: true, uniqueness: true
validates :order, presence: true, uniqueness: true

Expand Down
1 change: 1 addition & 0 deletions app/models/pokemon_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class PokemonType < ApplicationRecord
#INTERMEDIATE MODEL FOR RELATIONS BETWEEN POKEMON AND TYPE
belongs_to :pokemon
belongs_to :type

Expand Down
1 change: 1 addition & 0 deletions app/models/type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Type < ApplicationRecord
#MODEL TYPE WITH VALIDATIONS
has_many :pokemon_types
has_many :pokemons, through: :pokemon_types

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check

#ROUTES TO POKEMON AND TYPE

namespace :api do
namespace :v1 do
resources :pokemons
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require 'json'
require 'net/http'
require 'uri'

#RAKE TASK TO IMPORT DATA FROM POKEAPI

namespace :import do
desc "Import data from PokeApi"
task import_pokemon: :environment do
Expand Down
2 changes: 2 additions & 0 deletions test/controllers/api/v1/pokemons_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class Api::V1::PokemonsControllerTest < ActionDispatch::IntegrationTest

#TEST FOR POKEMON CRUD

test 'valid_pokemon' do
type = Type.create(name: 'grass')
Expand Down