-
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.
- Loading branch information
Showing
23 changed files
with
593 additions
and
25 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -26,3 +26,5 @@ erl_crash.dump | |
habits-*.tar | ||
|
||
.direnv | ||
.nix-mix | ||
.erlang-history |
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
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,57 @@ | ||
defmodule Habits.Metrics do | ||
import Ecto.Query, warn: false | ||
alias Habits.Repo | ||
|
||
alias Habits.Metrics.Metric | ||
alias Habits.Chains.Chain | ||
|
||
def get_by_date(user_id, date) do | ||
case Date.from_iso8601(date) do | ||
{:ok, date} -> | ||
query = | ||
from c in Chain, | ||
left_join: m in Metric, | ||
on: m.chain_id == c.id and m.date == ^date, | ||
where: c.active == ^true, | ||
where: c.user_id == ^user_id, | ||
select: %{id: m.id, value: m.value, chain: c} | ||
|
||
Repo.all(query) | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
def upsert(chain, attrs) do | ||
metric_changeset = | ||
chain | ||
|> Ecto.build_assoc(:metrics) | ||
|> Metric.changeset(attrs) | ||
|
||
Repo.insert( | ||
metric_changeset, | ||
on_conflict: {:replace, [:value, :updated_at]}, | ||
conflict_target: [:chain_id, :date], | ||
returning: true | ||
) | ||
end | ||
|
||
def get_metric!(user_id, metric_id) do | ||
query = | ||
from m in Metric, | ||
join: c in Chain, | ||
on: m.chain_id == c.id, | ||
where: m.id == ^metric_id, | ||
where: c.user_id == ^user_id | ||
|
||
case Repo.one(query) do | ||
nil -> raise Ecto.NoResultsError, queryable: query | ||
result -> result | ||
end | ||
end | ||
|
||
def delete_metric(%Metric{} = metric) do | ||
Repo.delete(metric) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Habits.Metrics.Metric do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@derive {Jason.Encoder, only: [:value, :date, :chain, :updated_at]} | ||
schema "metrics" do | ||
field :value, :integer | ||
field :date, :date | ||
|
||
belongs_to :chain, Habits.Chains.Chain | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
def changeset(chain, attrs) do | ||
chain | ||
|> cast(attrs, [:value, :chain_id, :date]) | ||
|> validate_required([:value, :chain_id, :date]) | ||
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
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 |
---|---|---|
@@ -1,26 +1,21 @@ | ||
defmodule HabitsWeb.ChainJSON do | ||
alias Habits.Chains.Chain | ||
|
||
@doc """ | ||
Renders a list of chains. | ||
""" | ||
def index(%{chains: chains}) do | ||
%{data: for(chain <- chains, do: data(chain))} | ||
for(chain <- chains, do: data(chain)) | ||
end | ||
|
||
@doc """ | ||
Renders a single chain. | ||
""" | ||
def show(%{chain: chain}) do | ||
%{data: data(chain)} | ||
data(chain) | ||
end | ||
|
||
defp data(%Chain{} = chain) do | ||
%{ | ||
id: chain.id, | ||
name: chain.name, | ||
type: chain.type, | ||
active: chain.active | ||
active: chain.active, | ||
description: chain.description | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule HabitsWeb.MetricController do | ||
use HabitsWeb, :controller | ||
|
||
alias Habits.Metrics | ||
alias Habits.Chains | ||
|
||
action_fallback HabitsWeb.FallbackController | ||
|
||
def index(conn, params) do | ||
metrics = Metrics.get_by_date(conn.assigns.current_user.id, params["date"]) | ||
render(conn, :index, metrics: metrics) | ||
end | ||
|
||
def create(conn, %{"metric" => metric_params}) do | ||
chain = Chains.get_chain!(conn.assigns.current_user.id, metric_params["chain_id"]) | ||
|
||
with {:ok, metric} <- Metrics.upsert(chain, metric_params) do | ||
render(conn, :create, metric: metric) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
metric = Metrics.get_metric!(conn.assigns.current_user.id, id) | ||
|
||
with {:ok, _metric} <- Metrics.delete_metric(metric) do | ||
send_resp(conn, :no_content, "") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule HabitsWeb.MetricJSON do | ||
def index(%{metrics: metrics}) do | ||
for(metric <- metrics, do: data(metric)) | ||
end | ||
|
||
def show(%{metric: metric}) do | ||
data(metric) | ||
end | ||
|
||
def create(%{metric: metric}) do | ||
%{value: metric.value, updated_at: metric.updated_at, id: metric.id} | ||
end | ||
|
||
defp data(metric) do | ||
%{ | ||
id: metric.id, | ||
value: metric.value, | ||
chain: metric.chain.name, | ||
chain_id: metric.chain.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
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
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
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,15 @@ | ||
defmodule Habits.Repo.Migrations.CreateMetrics do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:metrics) do | ||
add :value, :integer, null: false | ||
add :chain_id, references(:chains, on_delete: :delete_all), null: false | ||
add :date, :date, null: false | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
create unique_index(:metrics, [:chain_id, :date]) | ||
end | ||
end |
Oops, something went wrong.