Define a controller for invitations
Some checks failed
Run unit tests / copyright_notice (pull_request) Successful in 57s
Run unit tests / check-licenses (pull_request) Failing after 2m13s
Run unit tests / rubocop (pull_request) Failing after 3m35s
Run unit tests / unit_tests (pull_request) Failing after 5m6s
Run unit tests / build-static-assets (pull_request) Has been skipped

This commit is contained in:
Manuel Bustillo 2025-06-01 17:58:27 +02:00
parent 3e6afcc048
commit 5fb26f42d6
5 changed files with 49 additions and 0 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@
# Ignore swagger generated documentation
swagger/v1/swagger.yaml
wedding-planner.code-workspace

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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