Define and seed an invitation model and controller #224

Merged
bustikiller merged 12 commits from invitations into main 2025-06-01 18:36:50 +00:00
5 changed files with 49 additions and 0 deletions
Showing only changes of commit 5fb26f42d6 - Show all commits

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