2024-10-27 21:42:45 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-08-11 16:29:10 +02:00
|
|
|
class GroupsController < ApplicationController
|
|
|
|
def index
|
2024-12-10 08:41:10 +01:00
|
|
|
query_result = Groups::SummaryQuery.new.call.as_json.map(&:deep_symbolize_keys).map do |group|
|
|
|
|
{
|
|
|
|
id: group[:id],
|
|
|
|
name: group[:name],
|
|
|
|
icon: group[:icon],
|
|
|
|
color: group[:color],
|
|
|
|
parent_id: group[:parent_id],
|
|
|
|
attendance: group.slice(:total, :considered, :invited, :confirmed, :declined, :tentative)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: query_result
|
2024-08-11 16:29:10 +02:00
|
|
|
end
|
2024-12-08 11:30:38 +01:00
|
|
|
|
|
|
|
def create
|
|
|
|
group = Group.create!(**group_params, parent:)
|
|
|
|
render json: group.as_json(only: %i[id name icon color parent_id]), status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
group = Group.find(params[:id])
|
|
|
|
group.update!(**group_params, parent:)
|
|
|
|
render json: group.as_json(only: %i[id name icon color parent_id]), status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
Group.find(params[:id]).destroy!
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parent
|
|
|
|
params[:group][:parent_id].present? ? Group.find(params[:group][:parent_id]) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_params
|
|
|
|
params.expect(group: [:name, :icon, :color])
|
|
|
|
end
|
2024-08-11 16:29:10 +02:00
|
|
|
end
|