-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: development
Are you sure you want to change the base?
Changes from all commits
1676ecb
3c3ffae
c266abd
5856699
75ad997
fc152d8
724ecb5
bf166f3
58686cc
42ffe0b
51f45e9
952ab0e
ef2372b
846abdb
dfb363c
d638a79
2e86bcd
635f821
744a5fa
7402ad6
d49091e
e343ed9
69cee42
b29ceae
80e0ae2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
render json: {"error": I18n.t(city_sports.sport_list_empty)}, status: 422 if sport_list.empty? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MayurDeshmukh10 Remove this loop