Skip to content

Commit

Permalink
Add 'guest registration' and an endpoint to get current event data.
Browse files Browse the repository at this point in the history
  • Loading branch information
etopiei committed Sep 5, 2024
1 parent 9111e2e commit 95e351d
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 9 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ Example request to: `http://localhost:3000/api/events`
}
```

This will create an event with the data given in this request.
I've yet to add GET for the event, or to create an endpoint for people to register their repsonses, but
now that I have a handle on how to use Serializers, Operations and Actions I think this shouldn't be too bad to create.
^This will create an event with the data given in this request.


Example request to `http://localhost:3000/api/events/<event_uuid>/guest/`

```
{ name: "Guest A" }
```

I'm mostly saving this payload here in the README as a reminder of how to POST to the endpoint.
And now you can also 'GET' an event at: `http://localhost:3000/api/events/<event_uuid>`
and it will include the slots and responses.

I've yet to add an endpoint for people to register their repsonses, but
now that I have a handle on how to use Serializers, Operations and Actions I think this shouldn't be too bad to create.

### Setting up the project

Expand Down
13 changes: 13 additions & 0 deletions db/migrations/20240905074157_add_event_to_response.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddEventToResponse::V20240905074157 < Avram::Migrator::Migration::V1
def migrate
alter table_for(Response) do
add_belongs_to event : Event, on_delete: :cascade
end
end

def rollback
alter table_for(Response) do
remove :event
end
end
end
9 changes: 8 additions & 1 deletion src/actions/event/event.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
class Api::Event::Create < ApiAction
post "/api/events" do
post "/api/events/" do
serialized_event = EventCreateSerializer.from_json(params.body)
event = SaveEvent.create!(serialized_event)
json EventSerializer.new(event)
end
end

class Api::Event::Get < ApiAction
get "/api/events/:event_uuid" do
event = EventQuery.new.preload_slots.preload_responses.event_uuid(event_uuid).first
json EventDetailSerializer.new(event)
end
end
8 changes: 8 additions & 0 deletions src/actions/guest/guest.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Api::Guest::Create < ApiAction
post "/api/events/:event_uuid/guest" do
event = EventQuery.new.event_uuid(event_uuid).first
guest_data = GuestCreateSerializer.from_json(params.body)
guest = SaveGuest.create!(guest_data, event_id: event.id)
json({guest_id: guest.id})
end
end
1 change: 1 addition & 0 deletions src/models/event.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class Event < BaseModel
column host_name : String
column event_uuid : String = UUID.v7.to_s
has_many slots : Slot
has_many responses : Response
end
end
1 change: 1 addition & 0 deletions src/models/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Response < BaseModel
table do
belongs_to guest : Guest
belongs_to slot : Slot
belongs_to event : Event
column response : Response::ResponseValue
end
end
9 changes: 5 additions & 4 deletions src/operations/save_guest.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class SaveGuest < Guest::SaveOperation
# To save user provided params to the database, you must permit them
# https://luckyframework.org/guides/database/saving-records#perma-permitting-columns
#
# permit_columns column_1, column_2
needs serialized_guest : GuestCreateSerializer

before_save do
name.value = serialized_guest.name
end
end
14 changes: 14 additions & 0 deletions src/serializers/event_serializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ class EventCreateSerializer
property host_name : String
property slots : Array(SerializedSlot)
end

class EventDetailSerializer < BaseSerializer
def initialize(@event : Event)
end

def render
{
name: @event.name,
host_name: @event.host_name,
slots: SlotSerializer.for_collection(@event.slots),
responses: ResponseSerializer.for_collection(@event.responses)
}
end
end
15 changes: 15 additions & 0 deletions src/serializers/guest_serializer.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class GuestSerializer < BaseSerializer
def initialize(@guest : Guest)
end

def render
{
name: @guest.name
}
end
end

class GuestCreateSerializer
include JSON::Serializable
property name : String
end
11 changes: 11 additions & 0 deletions src/serializers/response_serializer.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

class ResponseSerializer < BaseSerializer
def initialize(@response : Response)
end
def render
{
response: @response.response,
guest: GuestSerializer.new(@response.guest)
}
end
end
11 changes: 11 additions & 0 deletions src/serializers/slot_serializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ class SerializedSlot
property slot_type : Slot::SlotType
property slot_start : Time
end

class SlotSerializer < BaseSerializer
def initialize(@slot : Slot)
end
def render
{
slot_type: @slot.slot_type,
slot_start: @slot.slot_start
}
end
end

0 comments on commit 95e351d

Please sign in to comment.