From 5fb26f42d65fadef105a0e3862a440f6798f4a83 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 1 Jun 2025 17:58:27 +0200 Subject: [PATCH] Define a controller for invitations --- .gitignore | 1 + app/controllers/invitations_controller.rb | 42 +++++++++++++++++++++++ app/models/wedding.rb | 2 ++ config/routes.rb | 1 + db/seeds.rb | 3 ++ 5 files changed, 49 insertions(+) create mode 100644 app/controllers/invitations_controller.rb diff --git a/.gitignore b/.gitignore index 13e3051..945067b 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ # Ignore swagger generated documentation swagger/v1/swagger.yaml +wedding-planner.code-workspace diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb new file mode 100644 index 0000000..0dac059 --- /dev/null +++ b/app/controllers/invitations_controller.rb @@ -0,0 +1,42 @@ +class InvitationsController < ApplicationController + def index + render json: Invitation.includes(:guests).as_json( + only: :id, + include: { + guests: { + only: %i[id name] + } + } + ) + end + + def create + invitation = Invitation.create + + if invitation.persisted? + render json: invitation, only: :id, status: :created + else + render json: { errors: invitation.errors.full_messages }, status: :unprocessable_entity + end + end + + def update + invitation = Invitation.find(params[:id]) + + if invitation.update(guest_ids: params[:invitation][:guest_ids]) + render json: invitation, only: :id, include: { guests: { only: %i[id name] } }, status: :ok + else + render json: { errors: invitation.errors.full_messages }, status: :unprocessable_entity + end + end + + def destroy + invitation = Invitation.find(params[:id]) + + if invitation.destroy + head :no_content + else + render json: { errors: invitation.errors.full_messages }, status: :unprocessable_entity + end + end +end diff --git a/app/models/wedding.rb b/app/models/wedding.rb index 5f0dabf..e9bed42 100644 --- a/app/models/wedding.rb +++ b/app/models/wedding.rb @@ -21,4 +21,6 @@ class Wedding < ApplicationRecord validates :slug, presence: true, uniqueness: true, format: { with: /\A#{SLUG_REGEX}\z/ } has_many :guests, dependent: :delete_all + has_many :groups, dependent: :delete_all + has_many :invitations, dependent: :delete_all end diff --git a/config/routes.rb b/config/routes.rb index 39d0460..a4b7897 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,6 +39,7 @@ Rails.application.routes.draw do end resources :tables_arrangements, only: %i[index show create] resources :summary, only: :index + resources :invitations, only: %i[index create update destroy] root to: redirect("/%{slug}") end diff --git a/db/seeds.rb b/db/seeds.rb index a693f64..f69b6e9 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,6 +5,7 @@ NUMBER_OF_GUESTS = 200 ActsAsTenant.without_tenant do + GroupAffinity.delete_all Wedding.delete_all end @@ -78,6 +79,8 @@ ActsAsTenant.with_tenant(wedding) do guests.shift(rand(1..3)).each do |guest| guest.update!(invitation:) end + guests.shift(1) if rand < 0.3 # Leave a percentage of guests without an invitation + end end