diff --git a/app/controllers/api/v1/grid_summons_controller.rb b/app/controllers/api/v1/grid_summons_controller.rb new file mode 100644 index 0000000..a039922 --- /dev/null +++ b/app/controllers/api/v1/grid_summons_controller.rb @@ -0,0 +1,32 @@ +class Api::V1::GridSummonsController < Api::V1::ApiController + def create + party = Party.find(summon_params[:party_id]) + canonical_summon = Summon.find(summon_params[:summon_id]) + + if current_user + if party.user != current_user + render_unauthorized_response + end + end + + if grid_summon = GridSummon.where( + party_id: party.id, + position: summon_params[:position] + ).first + GridSummon.destroy(grid_summon.id) + end + + @summon = GridSummon.create!(summon_params.merge(party_id: party.id, summon_id: canonical_summon.id)) + render :show, status: :created if @summon.save! + end + + def destroy + end + + private + + # Specify whitelisted properties that can be modified. + def summon_params + params.require(:summon).permit(:party_id, :summon_id, :position, :main, :friend) + end +end \ No newline at end of file diff --git a/app/models/grid_summon.rb b/app/models/grid_summon.rb new file mode 100644 index 0000000..cf6b177 --- /dev/null +++ b/app/models/grid_summon.rb @@ -0,0 +1,7 @@ +class GridSummon < ApplicationRecord + belongs_to :party + + def summon + Summon.find(self.summon_id) + end +end diff --git a/app/models/summon.rb b/app/models/summon.rb new file mode 100644 index 0000000..bccc20d --- /dev/null +++ b/app/models/summon.rb @@ -0,0 +1,11 @@ +class Summon < ApplicationRecord + include PgSearch::Model + + pg_search_scope :search, + against: [:name_en, :name_jp], + using: { + tsearch: { + prefix: true + } + } +end diff --git a/app/views/api/v1/grid_summons/base.json.rabl b/app/views/api/v1/grid_summons/base.json.rabl new file mode 100644 index 0000000..696d7be --- /dev/null +++ b/app/views/api/v1/grid_summons/base.json.rabl @@ -0,0 +1,9 @@ +attributes :id, + :party_id, + :main, + :friend, + :position + +node :summon do |w| + partial('summons/base', :object => w.summon) +end \ No newline at end of file diff --git a/app/views/api/v1/grid_summons/show.json.rabl b/app/views/api/v1/grid_summons/show.json.rabl new file mode 100644 index 0000000..07c4dcb --- /dev/null +++ b/app/views/api/v1/grid_summons/show.json.rabl @@ -0,0 +1,3 @@ +object @summon + +extends 'api/v1/grid_summons/base' \ No newline at end of file diff --git a/app/views/api/v1/search/summons.json.rabl b/app/views/api/v1/search/summons.json.rabl new file mode 100644 index 0000000..8fd7e1e --- /dev/null +++ b/app/views/api/v1/search/summons.json.rabl @@ -0,0 +1,3 @@ +collection @summons, :object_root => false + +extends 'summons/base' \ No newline at end of file diff --git a/app/views/api/v1/summons/base.json.rabl b/app/views/api/v1/summons/base.json.rabl new file mode 100644 index 0000000..75d103b --- /dev/null +++ b/app/views/api/v1/summons/base.json.rabl @@ -0,0 +1,39 @@ +object :summon + +attributes :id, + :granblue_id, + :element, + :rarity, + :max_level + +node :name do |w| + { + :en => w.name_en, + :jp => w.name_jp + } +end + +node :uncap do |w| + { + :flb => w.flb, + :ulb => w.ulb + } +end + +node :hp do |w| + { + :min_hp => w.min_hp, + :max_hp => w.max_hp, + :max_hp_flb => w.max_hp_flb, + :max_hp_ulb => w.max_hp_ulb + } +end + +node :atk do |w| + { + :min_atk => w.min_atk, + :max_atk => w.max_atk, + :max_atk_flb => w.max_atk_flb, + :max_atk_ulb => w.max_atk_ulb + } +end \ No newline at end of file diff --git a/db/migrate/20201017013942_create_summons.rb b/db/migrate/20201017013942_create_summons.rb new file mode 100644 index 0000000..dfb31f3 --- /dev/null +++ b/db/migrate/20201017013942_create_summons.rb @@ -0,0 +1,26 @@ +class CreateSummons < ActiveRecord::Migration[6.0] + def change + create_table :summons, id: :uuid, default: -> { "gen_random_uuid()" } do |t| + t.string :name_en + t.string :name_jp + t.integer :granblue_id + + t.integer :rarity + t.integer :element + t.string :series + + t.boolean :flb + t.boolean :ulb + + t.integer :max_level + t.integer :min_hp + t.integer :max_hp + t.integer :max_hp_flb + t.integer :max_hp_ulb + t.integer :min_atk + t.integer :max_atk + t.integer :max_atk_flb + t.integer :max_atk_ulb + end + end +end diff --git a/db/migrate/20201017013949_create_grid_summons.rb b/db/migrate/20201017013949_create_grid_summons.rb new file mode 100644 index 0000000..90220ca --- /dev/null +++ b/db/migrate/20201017013949_create_grid_summons.rb @@ -0,0 +1,15 @@ +class CreateGridSummons < ActiveRecord::Migration[6.0] + def change + create_table :grid_summons, id: :uuid, default: -> { "gen_random_uuid()" } do |t| + t.references :party, type: :uuid + t.references :summon, type: :uuid + + t.integer :uncap_level + t.boolean :main + t.boolean :friend + t.integer :position + + t.timestamps + end + end +end