From e2157179145a4faab24d7c9cc0aa60229151f6b5 Mon Sep 17 00:00:00 2001 From: jcwimer Date: Wed, 10 Feb 2016 15:16:58 +0000 Subject: [PATCH] Start of api --- app/assets/javascripts/api.js.coffee | 3 +++ app/assets/stylesheets/api.css | 4 ++++ app/controllers/api_controller.rb | 10 ++++++++++ app/helpers/api_helper.rb | 2 ++ app/views/api/tournament.jbuilder | 1 + app/views/api/tournaments.jbuilder | 4 ++++ config/routes.rb | 5 +++++ test/controllers/api_controller_test.rb | 7 +++++++ 8 files changed, 36 insertions(+) create mode 100644 app/assets/javascripts/api.js.coffee create mode 100644 app/assets/stylesheets/api.css create mode 100644 app/controllers/api_controller.rb create mode 100644 app/helpers/api_helper.rb create mode 100644 app/views/api/tournament.jbuilder create mode 100644 app/views/api/tournaments.jbuilder create mode 100644 test/controllers/api_controller_test.rb diff --git a/app/assets/javascripts/api.js.coffee b/app/assets/javascripts/api.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/api.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api.css b/app/assets/stylesheets/api.css new file mode 100644 index 0000000..afad32d --- /dev/null +++ b/app/assets/stylesheets/api.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb new file mode 100644 index 0000000..8f3a604 --- /dev/null +++ b/app/controllers/api_controller.rb @@ -0,0 +1,10 @@ +class ApiController < ApplicationController + + def tournaments + @tournaments = Tournament.all + end + + def tournament + @tournament = Tournament.where(:id => params[:tournament]).includes(:schools,:weights,:mats,:matches,:user,:wrestlers).first + end +end diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb new file mode 100644 index 0000000..1f82fcf --- /dev/null +++ b/app/helpers/api_helper.rb @@ -0,0 +1,2 @@ +module ApiHelper +end diff --git a/app/views/api/tournament.jbuilder b/app/views/api/tournament.jbuilder new file mode 100644 index 0000000..171625c --- /dev/null +++ b/app/views/api/tournament.jbuilder @@ -0,0 +1 @@ +json.(@tournament, :id, :name, :address, :director, :director_email, :schools, :weights, :mats, :matches) \ No newline at end of file diff --git a/app/views/api/tournaments.jbuilder b/app/views/api/tournaments.jbuilder new file mode 100644 index 0000000..d53a86a --- /dev/null +++ b/app/views/api/tournaments.jbuilder @@ -0,0 +1,4 @@ +json.array!(@tournaments) do |tournament| + json.extract! tournament, :id, :name, :address, :director, :director_email + json.url tournament_url(tournament, format: :json) +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2b49bab..799b3de 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,6 +51,11 @@ Wrestling::Application.routes.draw do get 'tournaments/:id/error' => 'tournaments#error' post "/tournaments/:id/swap" => "tournaments#swap", :as => :swap_wrestlers + + #API + get "/api/tournaments" => "api#tournaments" + get "/api/tournaments/:tournament" => "api#tournament" + # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/test/controllers/api_controller_test.rb b/test/controllers/api_controller_test.rb new file mode 100644 index 0000000..5ba1ef0 --- /dev/null +++ b/test/controllers/api_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ApiControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end