Export an event though ics
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
a1f60cf386
commit
8414f9a098
19
lib/eventos/export/icalendar.ex
Normal file
19
lib/eventos/export/icalendar.ex
Normal file
@ -0,0 +1,19 @@
|
||||
defmodule Eventos.Export.ICalendar do
|
||||
@moduledoc """
|
||||
Export an event to iCalendar format
|
||||
"""
|
||||
|
||||
alias Eventos.Events.Event
|
||||
|
||||
@spec export_event(%Event{}) :: String
|
||||
def export_event(%Event{} = event) do
|
||||
events = [%ICalendar.Event{
|
||||
summary: event.title,
|
||||
dtstart: event.begins_on,
|
||||
dtend: event.ends_on,
|
||||
description: event.description
|
||||
}]
|
||||
%ICalendar{events: events}
|
||||
|> ICalendar.to_ics()
|
||||
end
|
||||
end
|
@ -6,6 +6,7 @@ defmodule EventosWeb.EventController do
|
||||
|
||||
alias Eventos.Events
|
||||
alias Eventos.Events.Event
|
||||
alias Eventos.Export.ICalendar
|
||||
|
||||
action_fallback EventosWeb.FallbackController
|
||||
|
||||
@ -28,6 +29,13 @@ defmodule EventosWeb.EventController do
|
||||
render(conn, "show.json", event: event)
|
||||
end
|
||||
|
||||
def export_to_ics(conn, %{"id" => id}) do
|
||||
event = id
|
||||
|> Events.get_event!()
|
||||
|> ICalendar.export_event()
|
||||
send_resp(conn, 200, event)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "event" => event_params}) do
|
||||
event = Events.get_event!(id)
|
||||
|
||||
|
@ -44,6 +44,7 @@ defmodule EventosWeb.Router do
|
||||
resources "/users", UserController, except: [:new, :edit, :show]
|
||||
resources "/accounts", AccountController, except: [:new, :edit]
|
||||
resources "/events", EventController
|
||||
get "/events/:id/ics", EventController, :export_to_ics
|
||||
resources "/categories", CategoryController
|
||||
resources "/tags", TagController
|
||||
resources "/event_accounts", EventAccountsController
|
||||
|
@ -4,6 +4,7 @@ defmodule EventosWeb.EventControllerTest do
|
||||
|
||||
alias Eventos.Events
|
||||
alias Eventos.Events.Event
|
||||
alias Eventos.Export.ICalendar
|
||||
|
||||
@create_attrs %{begins_on: "2010-04-17 14:00:00.000000Z", description: "some description", ends_on: "2010-04-17 14:00:00.000000Z", title: "some title"}
|
||||
@update_attrs %{begins_on: "2011-05-18 15:01:01.000000Z", description: "some updated description", ends_on: "2011-05-18 15:01:01.000000Z", title: "some updated title"}
|
||||
@ -51,6 +52,17 @@ defmodule EventosWeb.EventControllerTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "export event" do
|
||||
setup [:create_event]
|
||||
|
||||
test "renders ics export of event", %{conn: conn, event: %Event{id: id} = event, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = get conn, event_path(conn, :export_to_ics, id)
|
||||
exported_event = ICalendar.export_event(event)
|
||||
assert exported_event == response(conn, 200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "update event" do
|
||||
setup [:create_event]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user