-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generated timer + check DateTime
Signed-off-by: WoodenMaiden <yann.pomie@laposte.net>
- Loading branch information
1 parent
e576bd0
commit 4524a2a
Showing
9 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule TimecopsyncProjectsApi.Projects.Timer do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
require Logger | ||
|
||
@primary_key {:id, :binary_id, autogenerate: true} | ||
@foreign_key_type :binary_id | ||
schema "timers" do | ||
field :description, :string | ||
field :notes, :string | ||
field :start_time, :utc_datetime | ||
field :end_time, :utc_datetime | ||
field :project_id, :binary_id | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
|
||
defp check_chronology(_, ended) when is_nil(ended), do: :lt | ||
|
||
defp check_chronology(started, ended) do | ||
DateTime.compare(started, ended) | ||
end | ||
|
||
def validate_datetime_chronology(changeset) do | ||
start_time = get_field(changeset, :start_time) | ||
end_time = get_field(changeset, :end_time) | ||
|
||
case check_chronology(start_time, end_time) do | ||
:gt -> add_error(changeset, :end_time, "A timer cannot end before it started!") | ||
_ -> changeset | ||
end | ||
end | ||
|
||
@doc false | ||
def changeset(timer, attrs) do | ||
timer | ||
|> cast(attrs, [:description, :notes, :start_time, :end_time]) | ||
|> validate_required([:start_time]) | ||
|> validate_datetime_chronology() | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
lib/timecopsync_projects_api_web/controllers/timer_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule TimecopsyncProjectsApiWeb.TimerController do | ||
use TimecopsyncProjectsApiWeb, :controller | ||
|
||
alias TimecopsyncProjectsApi.Projects | ||
alias TimecopsyncProjectsApi.Projects.Timer | ||
|
||
action_fallback TimecopsyncProjectsApiWeb.FallbackController | ||
|
||
def index(conn, _params) do | ||
timers = Projects.list_timers() | ||
render(conn, :index, timers: timers) | ||
end | ||
|
||
def create(conn, %{"timer" => timer_params}) do | ||
with {:ok, %Timer{} = timer} <- Projects.create_timer(timer_params) do | ||
conn | ||
|> put_status(:created) | ||
|> put_resp_header("location", ~p"/api/v1/timers/#{timer}") | ||
|> render(:show, timer: timer) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
timer = Projects.get_timer!(id) | ||
render(conn, :show, timer: timer) | ||
end | ||
|
||
def update(conn, %{"id" => id, "timer" => timer_params}) do | ||
timer = Projects.get_timer!(id) | ||
|
||
with {:ok, %Timer{} = timer} <- Projects.update_timer(timer, timer_params) do | ||
render(conn, :show, timer: timer) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
timer = Projects.get_timer!(id) | ||
|
||
with {:ok, %Timer{}} <- Projects.delete_timer(timer) do | ||
send_resp(conn, :no_content, "") | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
lib/timecopsync_projects_api_web/controllers/timer_json.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule TimecopsyncProjectsApiWeb.TimerJSON do | ||
alias TimecopsyncProjectsApi.Projects.Timer | ||
|
||
@doc """ | ||
Renders a list of timers. | ||
""" | ||
def index(%{timers: timers}) do | ||
%{ | ||
metadata: %{ | ||
total: length(timers) | ||
# page: page, | ||
}, | ||
data: for(timer <- timers, do: data(timer))} | ||
end | ||
|
||
@doc """ | ||
Renders a single timer. | ||
""" | ||
def show(%{timer: timer}) do | ||
%{data: data(timer)} | ||
end | ||
|
||
defp data(%Timer{} = timer) do | ||
%{ | ||
id: timer.id, | ||
description: timer.description, | ||
notes: timer.notes, | ||
start_time: timer.start_time, | ||
end_time: timer.end_time | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule TimecopsyncProjectsApi.Repo.Migrations.CreateTimers do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:timers, primary_key: false) do | ||
add :id, :binary_id, primary_key: true | ||
add :description, :string | ||
add :notes, :string | ||
add :start_time, :utc_datetime | ||
add :end_time, :utc_datetime | ||
add :project_id, references(:projects, on_delete: :nothing, type: :binary_id) | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
create index(:timers, [:project_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.