3.1 - Ajout du combats controller

pull/1/head
Karim Bogtob 2 years ago
parent 054e32366a
commit cca68f8dd7

@ -0,0 +1,29 @@
class CombatsController < ApplicationController
COMBATS_RENDER_CONFIG = {
left_fighter: { only: CreaturesController::SHOWABLE_ATTRIBUTES },
right_fighter: { only: CreaturesController::SHOWABLE_ATTRIBUTES },
winner: { only: CreaturesController::SHOWABLE_ATTRIBUTES },
}
def create
left_fighter = Creature.find(params[:left_fighter_id])
right_fighter = Creature.find(params[:right_fighter_id])
@combat = Combat.new(create_params)
@combat.left_fighter = left_fighter
@combat.right_fighter = right_fighter
@combat.baston!
@combat.left_fighter.save!
@combat.right_fighter.save!
@combat.save!
render json: @combat.as_json(include: COMBATS_RENDER_CONFIG)
rescue ActiveRecord::RecordNotFound
render json: {}, status: 404
end
private
def create_params
params.require(:combat).permit(:name)
end
end

@ -3,7 +3,7 @@ class Combat < ApplicationRecord
belongs_to :left_fighter, class_name: 'Creature'
belongs_to :right_fighter, class_name: 'Creature'
belongs_to :winner, class_name: 'Creature'
belongs_to :winner, class_name: 'Creature', optional: true
def baston!
return if left_fighter.nil? || right_fighter.nil?

@ -9,6 +9,9 @@ Rails.application.routes.draw do
put '/creatures/:id', to: 'creatures#update'
delete '/creatures/:id', to: 'creatures#destroy'
# Combats
post '/combats', to: 'combats#create'
# Defines the root path route ("/")
root 'home#welcome'
end

Loading…
Cancel
Save