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

Api for sports list for a particular city #20

Open
wants to merge 25 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1676ecb
Added Api for sports for a city
MayurDeshmukh10 Feb 3, 2020
3c3ffae
Added sports list for city
MayurDeshmukh10 Feb 3, 2020
c266abd
removed index method
MayurDeshmukh10 Feb 4, 2020
5856699
added test cases
MayurDeshmukh10 Feb 4, 2020
75ad997
added factory bot
MayurDeshmukh10 Feb 5, 2020
fc152d8
add .env file
MayurDeshmukh10 Feb 5, 2020
724ecb5
add dotenv-rails gem
MayurDeshmukh10 Feb 5, 2020
bf166f3
added dotenv-rails gem
MayurDeshmukh10 Feb 5, 2020
58686cc
removed hardcoded jwt secret key
MayurDeshmukh10 Feb 5, 2020
42ffe0b
added jwt class
MayurDeshmukh10 Feb 5, 2020
51f45e9
Used factory bot for new user
MayurDeshmukh10 Feb 6, 2020
952ab0e
changed name to first and last name
MayurDeshmukh10 Feb 6, 2020
ef2372b
removed token validation
MayurDeshmukh10 Feb 7, 2020
846abdb
added validation token method for before action
MayurDeshmukh10 Feb 7, 2020
dfb363c
changed post to get
MayurDeshmukh10 Feb 7, 2020
d638a79
added test cases for token validation
MayurDeshmukh10 Feb 9, 2020
2e86bcd
Resolved merge conflicts with development
MayurDeshmukh10 Feb 10, 2020
635f821
Handled query exceptions
MayurDeshmukh10 Feb 12, 2020
744a5fa
Handled query exceptions
MayurDeshmukh10 Feb 13, 2020
7402ad6
Handled jwt exceptions
MayurDeshmukh10 Feb 13, 2020
d49091e
Added error messages
MayurDeshmukh10 Feb 13, 2020
e343ed9
Handled jwt exceptions
MayurDeshmukh10 Feb 13, 2020
69cee42
Added context for test cases
MayurDeshmukh10 Feb 13, 2020
b29ceae
Resolved merge conflicts with development
MayurDeshmukh10 Feb 14, 2020
80e0ae2
Resolved merge conflicts with development
MayurDeshmukh10 Feb 24, 2020
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
19 changes: 19 additions & 0 deletions app/controllers/api/v1/city_sports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Api
module V1
class CitySportsController < ApplicationController
def displaysports
city_id = @current_user.city_id
sport_list = []
city_sports = CitySport.where(city_id: city_id)
render json: {"error": I18n.t(city_sports.id_not_found)}, status: 422 if city_sports.nil?
city_sports.find_each do |citysport|
sport_list += Sport.where(id: citysport.sport_id)
end

Choose a reason for hiding this comment

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

@MayurDeshmukh10 Remove this loop

render json: {"error": I18n.t(city_sports.sport_list_empty)}, status: 422 if sport_list.empty?

Choose a reason for hiding this comment

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

@MayurDeshmukh10 Sports List Empty does not necesaarily mean there is some error right?

render json: sport_list.as_json
end
end
end
end
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Application < Rails::Application
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

config.autoload_paths << Rails.root.join("lib")
config.middleware.insert_before 0, Rack::Cors do
allow do
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

en:
hello: "Hello world"
city_sports:
sport_list_empty: "Cannot fetch Sports"
id_not_found: "Id not found"
levels:
id_not_found: "Id not found"
city_sports_not_found: "could not fetch city sports"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get "/city_sports/display", to: "city_sports#displaysports"
resources :city_sports

Choose a reason for hiding this comment

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

@MayurDeshmukh10 Why "resources :city_sports"?

get "/city_sport_levels", to: "levels#all_levels"
resources :levels
post "/level_sports", to: "level_sports#create"
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
Dir[Rails.root.join("spec", "support", "**", "*.rb")].sort.each {|f| require f }

# Checks for pending migrations and applies them before tests are run.
Expand Down
31 changes: 31 additions & 0 deletions spec/requests/post_display_sports_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "rails_helper"

describe "should display all available sports for particular city", type: :request do
context "should return all available sport" do
it "returns a ok status" do
@sport = Sport.create(name: "Badminton")
@city = City.create(name: "Pune")
@user = FactoryBot.create(:user, city_id: @city.id)
@cs = CitySport.create(city_id: @city.id, sport_id: @sport.id, number_of_boxes: 5, number_of_players: 10)
@token1 = JsonWebToken.encode(user_id: @user.id)
get "/api/v1/city_sports/display", headers: {"user-auth-token" => @token1}
expect(response).to have_http_status(:ok)
end
end

context "should return error", type: :request do
it "returns a unauthorized status due user not present" do
@token2 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiOCJ9.9V492uKokeLSu9F62ck-CX2XvrquAsTNc9_agkPcYzk"
get "/api/v1/city_sports/display", headers: {"user-auth-token" => @token2}
expect(response).to have_http_status(:unauthorized)
end

it "returns a unauthorized status due to jwt invalidation" do
@token3 = "eyJhbGciOiJIUzI1NiIsInR5cuKokeLSu9F62ck-CX2XvrquAsTNc9_agkPcYzk"

Choose a reason for hiding this comment

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

@MayurDeshmukh10 Dont pass the hard coded token. Generate it. You can write a method and call it everywhere in your test cases

get "/api/v1/city_sports/display", headers: {"user-auth-token" => @token3}
expect(response).to have_http_status(:unauthorized)
end
end
end