From 1736fe9fce961176cf8f3adddb89feb6137bad61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=A4dler?= Date: Tue, 13 Nov 2018 14:14:58 +0100 Subject: [PATCH] Generate controllers --- app/controllers/match_controller.rb | 8 --- app/controllers/matches_controller.rb | 51 +++++++++++++++++++ app/controllers/teams_controller.rb | 51 +++++++++++++++++++ app/controllers/tournaments_controller.rb | 51 +++++++++++++++++++ test/controllers/matches_controller_test.rb | 38 ++++++++++++++ test/controllers/teams_controller_test.rb | 38 ++++++++++++++ .../tournaments_controller_test.rb | 38 ++++++++++++++ 7 files changed, 267 insertions(+), 8 deletions(-) delete mode 100644 app/controllers/match_controller.rb create mode 100644 app/controllers/matches_controller.rb create mode 100644 app/controllers/teams_controller.rb create mode 100644 app/controllers/tournaments_controller.rb create mode 100644 test/controllers/matches_controller_test.rb create mode 100644 test/controllers/teams_controller_test.rb create mode 100644 test/controllers/tournaments_controller_test.rb diff --git a/app/controllers/match_controller.rb b/app/controllers/match_controller.rb deleted file mode 100644 index 6d4b6fe..0000000 --- a/app/controllers/match_controller.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -class MatchController < ApplicationController - def get - id = params[:id] - Match - end -end diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb new file mode 100644 index 0000000..b760ec7 --- /dev/null +++ b/app/controllers/matches_controller.rb @@ -0,0 +1,51 @@ +class MatchesController < ApplicationController + before_action :set_match, only: [:show, :update, :destroy] + + # GET /matches + def index + @matches = Match.all + + render json: @matches + end + + # GET /matches/1 + def show + render json: @match + end + + # POST /matches + def create + @match = Match.new(match_params) + + if @match.save + render json: @match, status: :created, location: @match + else + render json: @match.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT /matches/1 + def update + if @match.update(match_params) + render json: @match + else + render json: @match.errors, status: :unprocessable_entity + end + end + + # DELETE /matches/1 + def destroy + @match.destroy + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_match + @match = Match.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def match_params + params.fetch(:match, {}) + end +end diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb new file mode 100644 index 0000000..7e02232 --- /dev/null +++ b/app/controllers/teams_controller.rb @@ -0,0 +1,51 @@ +class TeamsController < ApplicationController + before_action :set_team, only: [:show, :update, :destroy] + + # GET /teams + def index + @teams = Team.all + + render json: @teams + end + + # GET /teams/1 + def show + render json: @team + end + + # POST /teams + def create + @team = Team.new(team_params) + + if @team.save + render json: @team, status: :created, location: @team + else + render json: @team.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT /teams/1 + def update + if @team.update(team_params) + render json: @team + else + render json: @team.errors, status: :unprocessable_entity + end + end + + # DELETE /teams/1 + def destroy + @team.destroy + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_team + @team = Team.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def team_params + params.fetch(:team, {}) + end +end diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb new file mode 100644 index 0000000..ec0fe1b --- /dev/null +++ b/app/controllers/tournaments_controller.rb @@ -0,0 +1,51 @@ +class TournamentsController < ApplicationController + before_action :set_tournament, only: [:show, :update, :destroy] + + # GET /tournaments + def index + @tournaments = Tournament.all + + render json: @tournaments + end + + # GET /tournaments/1 + def show + render json: @tournament + end + + # POST /tournaments + def create + @tournament = Tournament.new(tournament_params) + + if @tournament.save + render json: @tournament, status: :created, location: @tournament + else + render json: @tournament.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT /tournaments/1 + def update + if @tournament.update(tournament_params) + render json: @tournament + else + render json: @tournament.errors, status: :unprocessable_entity + end + end + + # DELETE /tournaments/1 + def destroy + @tournament.destroy + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_tournament + @tournament = Tournament.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def tournament_params + params.fetch(:tournament, {}) + end +end diff --git a/test/controllers/matches_controller_test.rb b/test/controllers/matches_controller_test.rb new file mode 100644 index 0000000..5c82bdc --- /dev/null +++ b/test/controllers/matches_controller_test.rb @@ -0,0 +1,38 @@ +require 'test_helper' + +class MatchesControllerTest < ActionDispatch::IntegrationTest + setup do + @match = matches(:one) + end + + test "should get index" do + get matches_url, as: :json + assert_response :success + end + + test "should create match" do + assert_difference('Match.count') do + post matches_url, params: { match: { } }, as: :json + end + + assert_response 201 + end + + test "should show match" do + get match_url(@match), as: :json + assert_response :success + end + + test "should update match" do + patch match_url(@match), params: { match: { } }, as: :json + assert_response 200 + end + + test "should destroy match" do + assert_difference('Match.count', -1) do + delete match_url(@match), as: :json + end + + assert_response 204 + end +end diff --git a/test/controllers/teams_controller_test.rb b/test/controllers/teams_controller_test.rb new file mode 100644 index 0000000..8f5b105 --- /dev/null +++ b/test/controllers/teams_controller_test.rb @@ -0,0 +1,38 @@ +require 'test_helper' + +class TeamsControllerTest < ActionDispatch::IntegrationTest + setup do + @team = teams(:one) + end + + test "should get index" do + get teams_url, as: :json + assert_response :success + end + + test "should create team" do + assert_difference('Team.count') do + post teams_url, params: { team: { } }, as: :json + end + + assert_response 201 + end + + test "should show team" do + get team_url(@team), as: :json + assert_response :success + end + + test "should update team" do + patch team_url(@team), params: { team: { } }, as: :json + assert_response 200 + end + + test "should destroy team" do + assert_difference('Team.count', -1) do + delete team_url(@team), as: :json + end + + assert_response 204 + end +end diff --git a/test/controllers/tournaments_controller_test.rb b/test/controllers/tournaments_controller_test.rb new file mode 100644 index 0000000..fe57b00 --- /dev/null +++ b/test/controllers/tournaments_controller_test.rb @@ -0,0 +1,38 @@ +require 'test_helper' + +class TournamentsControllerTest < ActionDispatch::IntegrationTest + setup do + @tournament = tournaments(:one) + end + + test "should get index" do + get tournaments_url, as: :json + assert_response :success + end + + test "should create tournament" do + assert_difference('Tournament.count') do + post tournaments_url, params: { tournament: { } }, as: :json + end + + assert_response 201 + end + + test "should show tournament" do + get tournament_url(@tournament), as: :json + assert_response :success + end + + test "should update tournament" do + patch tournament_url(@tournament), params: { tournament: { } }, as: :json + assert_response 200 + end + + test "should destroy tournament" do + assert_difference('Tournament.count', -1) do + delete tournament_url(@tournament), as: :json + end + + assert_response 204 + end +end