wedding-planner/app/controllers/groups_controller.rb

34 lines
789 B
Ruby

# Copyright (C) 2024 Manuel Bustillo
class GroupsController < ApplicationController
def index
render json: Groups::SummaryQuery.new.call.as_json
end
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
end