Refactor how GridCharacters are saved

This commit is contained in:
Justin Edmund 2022-02-23 13:49:40 -08:00
parent bc5d127ce7
commit 8362fcf770

View file

@ -9,14 +9,13 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
end end
end end
if grid_character = GridCharacter.where( if GridCharacter.where(party_id: party.id, position: character_params[:position]).exists?
party_id: party.id, @character = GridCharacter.where(party_id: party.id, position: character_params[:position]).limit(1)[0]
position: character_params[:position] @character.character_id = canonical_character.id
).first else
GridCharacter.destroy(grid_character.id) @character = GridCharacter.create!(character_params.merge(party_id: party.id, character_id: canonical_character.id))
end end
@character = GridCharacter.create!(character_params.merge(party_id: party.id, character_id: canonical_character.id))
render :show, status: :created if @character.save! render :show, status: :created if @character.save!
end end