Skip to content

Commit 354190a

Browse files
committed
dovrebbe essere finito
1 parent 79dc621 commit 354190a

File tree

7 files changed

+41
-3
lines changed

7 files changed

+41
-3
lines changed

app/controllers/api/events/participants_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def create
1010
head 201
1111
end
1212

13+
def telegram_participants
14+
@event.participants << User.find_by(telegram_id: params[:telegram_id].to_i)
15+
render json: @event, serializer: ::EventsSerializer, status: :ok
16+
end
17+
1318
private
1419

1520
def set_event
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Api::TelegramEventsController < ApplicationController
2+
def create
3+
event = Event.new
4+
event.subject = params[:event][:subject]
5+
event.topic_id = params[:event][:topic_id].to_i
6+
event.starting_at = Time.now + [1, 2, 3].sample.days
7+
event.place_name = params[:event][:place_name]
8+
event.place_address = "Via Maria Vittoria, 38, 10123 Torino TO"
9+
event.max_participants = [3, 6, 12].sample
10+
event.owner_id = User.find_by(telegram_id: params[:event][:telegram_id]).id
11+
12+
if event.save
13+
render json: event, serializer: ::EventsSerializer, status: :ok
14+
else
15+
render json: {errors: event.errors}, status: 400
16+
end
17+
end
18+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Api::TopicsController < ApplicationController
22
def index
3-
render json: Topic.all, status: :ok
3+
render json: Topic.all.order(:name), status: :ok
44
end
55
end

app/serializers/events_serializer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class EventsSerializer < ActiveModel::Serializer
22
attributes :id, :subject, :place_address, :place_name, :starting_at,
3-
:max_participants, :owner, :participants, :topic
3+
:max_participants, :owner, :participants, :topic, :starting_at_formatted
44

55
def owner
66
object.owner
@@ -13,4 +13,8 @@ def participants
1313
def topic
1414
object.topic
1515
end
16+
17+
def starting_at_formatted
18+
object.starting_at.strftime("%d/%m/%Y %H:%M")
19+
end
1620
end

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
resources :events, only: [:index] do
1919
resources :participants, only: [:create, :index], controller: "events/participants"
20+
post "telegram_participants", to: "events/participants#telegram_participants"
2021
end
2122

23+
post "telegram_events", to: "telegram_events#create", controller: "telegram_events"
24+
2225
post "search", to: "events#search"
2326
end
2427
end

db/seeds.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
1414

1515
Topic.create name: name, code: code
1616
end
17+
18+
100.times do
19+
FactoryBot.create :event
20+
end
21+
22+
Topic.all.order(:name).first.destroy

test/factories/events.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
subject { Faker::Job.title }
44
owner { FactoryBot.create :user }
55
topic { Topic.all.sample }
6-
place_name "Rinascimenti Sociali"
6+
place_name { ["Rinascimenti Sociali", Faker::LordOfTheRings.location].sample }
77
place_address "Via Maria Vittoria, 38, 10123 Torino TO"
8+
starting_at { Time.now + 2.days }
9+
max_participants { [3, 6, 12].sample }
810
end
911
end

0 commit comments

Comments
 (0)