wedding-planner/app/controllers/guests_controller.rb

97 lines
2.5 KiB
Ruby
Raw Normal View History

2024-10-27 21:42:45 +00:00
# Copyright (C) 2024 Manuel Bustillo
2024-07-25 10:47:49 +02:00
require 'csv'
2024-07-11 20:08:26 +02:00
class GuestsController < ApplicationController
2024-07-12 15:08:30 +02:00
before_action :set_guest, only: %i[show edit update destroy]
2024-07-11 20:08:26 +02:00
# GET /guests or /guests.json
def index
2024-07-12 15:08:30 +02:00
@guests = Guest.all
.joins(:group)
.order('groups.name' => :asc, first_name: :asc, last_name: :asc)
render jsonapi: @guests
2024-07-11 20:08:26 +02:00
end
# GET /guests/1 or /guests/1.json
2024-07-12 15:08:30 +02:00
def show; end
2024-07-11 20:08:26 +02:00
# GET /guests/new
def new
@guest = Guest.new
end
# GET /guests/1/edit
2024-07-12 15:08:30 +02:00
def edit; end
2024-07-11 20:08:26 +02:00
# POST /guests or /guests.json
def create
@guest = Guest.new(guest_params)
respond_to do |format|
if @guest.save
2024-07-12 15:08:30 +02:00
format.html { redirect_to guest_url(@guest), notice: 'Guest was successfully created.' }
2024-07-11 20:08:26 +02:00
format.json { render :show, status: :created, location: @guest }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guests/1 or /guests/1.json
def update
respond_to do |format|
if @guest.update(guest_params)
2024-07-12 15:08:30 +02:00
format.html { redirect_to guest_url(@guest), notice: 'Guest was successfully updated.' }
2024-07-11 20:08:26 +02:00
format.json { render :show, status: :ok, location: @guest }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guests/1 or /guests/1.json
def destroy
@guest.destroy!
respond_to do |format|
2024-07-12 15:08:30 +02:00
format.html { redirect_to guests_url, notice: 'Guest was successfully destroyed.' }
2024-07-11 20:08:26 +02:00
format.json { head :no_content }
end
end
2024-07-25 10:47:49 +02:00
def import
csv = CSV.parse(params[:file].read, headers: true)
ActiveRecord::Base.transaction do
csv.each do |row|
guest = Guest.create!(first_name: row['name'])
guest.affinity_group_list.add(row['affinity_group'])
guest.save!
end
end
redirect_to guests_url
end
def bulk_update
2024-10-27 19:01:12 +01:00
Guest.where(id: params[:guest_ids]).update!(params.require(:properties).permit(:status))
render json: {}, status: :ok
end
2024-07-11 20:08:26 +02:00
private
2024-07-12 15:08:30 +02:00
# Use callbacks to share common setup or constraints between actions.
def set_guest
@guest = Guest.find(params[:id])
end
# Only allow a list of trusted parameters through.
def guest_params
params.require(:guest).permit(:first_name, :last_name, :email, :phone)
end
2024-07-11 20:08:26 +02:00
end