mirror of
https://github.com/netfun2000/hipudding-teslamate.git
synced 2026-02-27 09:44:28 +08:00
31 lines
731 B
Elixir
31 lines
731 B
Elixir
defmodule TeslaMateWeb.DriveController do
|
|
use TeslaMateWeb, :controller
|
|
|
|
require Logger
|
|
import Ecto.Query
|
|
|
|
alias TeslaMate.Log.{Drive, Position}
|
|
alias TeslaMate.Repo
|
|
|
|
def gpx(conn, %{"id" => id}) do
|
|
drive =
|
|
Drive
|
|
|> Repo.get(id)
|
|
|> Repo.preload(positions: from(p in Position, order_by: p.date))
|
|
|
|
case drive do
|
|
nil -> conn |> send_resp(404, "Drive not found")
|
|
drive -> send_gpx_file(conn, drive)
|
|
end
|
|
end
|
|
|
|
defp send_gpx_file(conn, drive) do
|
|
filename = "#{drive.start_date}.gpx"
|
|
|
|
conn
|
|
|> put_resp_content_type("application/xml")
|
|
|> put_resp_header("content-disposition", ~s(attachment; filename="#{filename}"))
|
|
|> render("gpx.xml", drive: drive)
|
|
end
|
|
end
|