-
Notifications
You must be signed in to change notification settings - Fork 0
/
playthrough_time_controller.ex
55 lines (39 loc) · 1.4 KB
/
playthrough_time_controller.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
defmodule SkaroWeb.PlaythroughTimeController do
use SkaroWeb, :controller
alias Skaro.Playthrough
action_fallback(SkaroWeb.FallbackController)
plug(Guardian.Plug.EnsureAuthenticated)
def show(conn, %{"id" => game_id, "name" => game_name, "release_date" => game_release_date}) do
with {:ok, date_parsed} <- Date.from_iso8601(game_release_date),
{game_id_parsed, _} <- Integer.parse(game_id),
{:ok, time} <-
Playthrough.find(%{id: game_id_parsed, name: game_name, release_date: date_parsed}) do
Playthrough.maybe_update(time, date_parsed)
render(conn, "show.json", playthrough_time: time)
else
{:error, :invalid_format} ->
{:error, :bad_request}
{:error, :invalid_date} ->
{:error, :bad_request}
{:error, :future_release} ->
{:error, :bad_request}
{:error, :no_name} ->
{:error, :bad_request}
{:error, :no_date} ->
{:error, :bad_request}
{:error, :times_not_available} ->
{:error, :not_found}
{:error, :game_id_not_found} ->
{:error, :not_found}
{:error, :not_found} ->
{:error, :not_found}
:error ->
{:error, :bad_request}
{:error, %Ecto.Changeset{} = changeset} ->
{:error, changeset}
{:error, reason} ->
{:error, :external_api, reason}
end
end
def show(_, _), do: {:error, :bad_request}
end