From e81ef81150ae34b06600c82d29cd8ed72b27044a Mon Sep 17 00:00:00 2001 From: Thor77 Date: Sun, 25 Nov 2018 20:04:05 +0100 Subject: [PATCH] Add TournamentsController --- app/controllers/tournaments_controller.rb | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 app/controllers/tournaments_controller.rb diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb new file mode 100644 index 0000000..a8710e1 --- /dev/null +++ b/app/controllers/tournaments_controller.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +class TournamentsController < ApplicationController + before_action :set_tournament, only: %i[show update destroy] + before_action :authenticate_user!, only: %i[create update destroy] + before_action -> { require_owner! @tournament.owner }, only: %i[update destroy] + + # GET /tournaments + def index + tournaments = Tournament.where(public: true).or(Tournament.where(owner: current_user)).order(:created_at) + render json: tournaments, each_serializer: SimpleTournamentSerializer + end + + # GET /tournaments/1 + def show + render json: @tournament + end + + # POST /tournaments + def create + tournament = current_user.tournaments.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 + + def set_tournament + @tournament = Tournament.find(params[:id]) + end + + def tournament_params + deserialize_params only: %i[name description public teams] + end +end