diff --git a/app/controllers/conflicts_controller.rb b/app/controllers/conflicts_controller.rb new file mode 100644 index 0000000..92650e1 --- /dev/null +++ b/app/controllers/conflicts_controller.rb @@ -0,0 +1,11 @@ +class ConflictsController < ApplicationController + def show + @conflicts_table = ConflictsTable.new conflicts_table_params + end + + private + + def conflicts_table_params + params.require(:conflicts_table).permit(:talk_id, other_talks_ids: []) + end +end diff --git a/app/models/conflicts.rb b/app/models/conflicts.rb new file mode 100644 index 0000000..ce729b8 --- /dev/null +++ b/app/models/conflicts.rb @@ -0,0 +1,12 @@ +class Conflicts + include ActiveModel::Model + + attr_accessor :left, :right + + def conflicts + TalkPreference.joins('INNER JOIN "selected_talks" AS left + ON "left"."talk_preference_id" = "talk_preferences"."unique_id" + INNER JOIN "selected_talks" AS right + ON "right"."talk_preference_id" = "talk_preferences"."unique_id"').where(left: {talk_id: @left}, right: {talk_id: @right}).count + end +end diff --git a/app/models/conflicts_table.rb b/app/models/conflicts_table.rb new file mode 100644 index 0000000..464ce1d --- /dev/null +++ b/app/models/conflicts_table.rb @@ -0,0 +1,18 @@ +class ConflictsTable + include ActiveModel::Model + + attr_accessor :talk_id, :other_talks_ids + + def other_talks_ids + @other_talks_ids ||= [] + end + + def conflicts + other_talks_ids.map do |right| + { + talk_id: right, + number_of_conflicts: Conflicts.new(left: talk_id, right: right).conflicts + } + end + end +end diff --git a/app/views/conflicts/show.json.jbuilder b/app/views/conflicts/show.json.jbuilder new file mode 100644 index 0000000..281c29e --- /dev/null +++ b/app/views/conflicts/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @conflicts_table, :talk_id, :conflicts diff --git a/config/routes.rb b/config/routes.rb index b3b5bf3..7ae80e6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,6 @@ Rails.application.routes.draw do resources :talk_preferences, only: [:index, :show, :create, :update] root to: 'home#index' resource :summary, only: :show + resource :conflicts, only: :show # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end