Refine endpoint to receive an affinity value and transform it into a discomfort equivalent
This commit is contained in:
parent
0780b17f4b
commit
5784c89b79
@ -8,16 +8,16 @@ class AffinitiesController < ApplicationController
|
|||||||
def index
|
def index
|
||||||
overridden_affinities = @group.affinities
|
overridden_affinities = @group.affinities
|
||||||
.each_with_object({}) { |affinity, acc| acc[affinity.another_group(@group).id] = affinity.discomfort }
|
.each_with_object({}) { |affinity, acc| acc[affinity.another_group(@group).id] = affinity.discomfort }
|
||||||
Group.pluck(:id).index_with { |group_id| overridden_affinities[group_id] || GroupAffinity::NEUTRAL }
|
Group.where.not(id: @group.id).pluck(:id).index_with { |group_id| GroupAffinity::MAX_DISCOMFORT - (overridden_affinities[group_id] || GroupAffinity::NEUTRAL) }
|
||||||
.then { |affinities| render json: affinities }
|
.then { |affinities| render json: affinities }
|
||||||
end
|
end
|
||||||
|
|
||||||
def bulk_update
|
def bulk_update
|
||||||
params.expect(affinities: [[:group_id, :discomfort]]).map(&:to_h).map do |affinity|
|
params.expect(affinities: [[:group_id, :affinity]]).map(&:to_h).map do |affinity|
|
||||||
{
|
{
|
||||||
group_a_id: @group.id,
|
group_a_id: @group.id,
|
||||||
group_b_id: affinity[:group_id],
|
group_b_id: affinity[:group_id],
|
||||||
discomfort: affinity[:discomfort]
|
discomfort: GroupAffinity::MAX_DISCOMFORT - affinity[:affinity]
|
||||||
}
|
}
|
||||||
end.then { |affinities| GroupAffinity.upsert_all(affinities) }
|
end.then { |affinities| GroupAffinity.upsert_all(affinities) }
|
||||||
render json: {}, status: :ok
|
render json: {}, status: :ok
|
||||||
@ -25,7 +25,7 @@ class AffinitiesController < ApplicationController
|
|||||||
rescue ActiveRecord::InvalidForeignKey
|
rescue ActiveRecord::InvalidForeignKey
|
||||||
render json: { error: 'At least one of the group IDs provided does not exist.' }, status: :bad_request
|
render json: { error: 'At least one of the group IDs provided does not exist.' }, status: :bad_request
|
||||||
rescue ActiveRecord::StatementInvalid
|
rescue ActiveRecord::StatementInvalid
|
||||||
render json: { error: 'Invalid group ID or discomfort provided.' }
|
render json: { error: 'Invalid group ID or discomfort provided.' }, status: :bad_request
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
Loading…
x
Reference in New Issue
Block a user