module Trip
module Controls
class AttendsTripOffer
def initialize ( request )
Validator . validate! ( request )
@user = FacebookUserFetcher . get ( request . access_token )
@trip_offer = Repositories ::TripOffer . find_by_id ( request . trip_offer_id )
end
def call
Permissions ::TripOfferAttendingPolicy . enforce! ( @trip_offer , @user )
@trip_offer . add_passenger ( @user )
Repositories ::TripOffer . save ( @trip_offer )
Response . new ( :trip_offer => @trip_offer )
end
class Request
include Virtus
include Aequitas
attribute :access_token , String
attribute :trip_offer_id , Integer
validates_presence_of :access_token
validates_presence_of :trip_offer_id
validates_numericalness_of :trip_offer_id
end
class Response
include Virtus
attribute :trip_offer , TripOffer
end
end
end
end
include Trip ::Controls
post "/trip_offers/:trip_offer_id/attend" do
begin
req = AttendsTripOffer ::Request . new
req . access_token = access_token
req . trip_offer_id = params [ :trip_offer_id ]
res = AttendsTripOffer . new ( req ) . call
trip_offer = res . trip_offer
Serializers ::TripOffer . new ( trip_offer ) . to_json
rescue => exception
handle_error ( exception )
end
end
Please critique, all input appreciated