Commit e63dab89 authored by Frank Rousseau's avatar Frank Rousseau
Browse files

Link trackers to users

parent 8968998e
......@@ -10,8 +10,9 @@ defmodule Gaja.Accounts.User do
field :email, :string
field :name, :string
field :password, :string, virtual: true
field :password_hash, :string
has_many :trackers, Gaja.Tracker
timestamps()
end
......@@ -61,7 +62,7 @@ defmodule Gaja.Accounts.User do
defp put_pass_hash(%Ecto.Changeset{valid?: true, changes:
%{password: password}} = changeset) do
change(changeset, Comeonin.Argon2.add_hash(password))
change(changeset, Argon2.add_hash(password))
end
defp put_pass_hash(changeset), do: changeset
......
......@@ -8,6 +8,8 @@ defmodule Gaja.Tracker do
schema "trackers" do
field :name, :string
has_many :tracker_entries, Gaja.TrackerEntry
belongs_to :user, Gaja.Accounts.User
timestamps()
end
......@@ -17,6 +19,7 @@ defmodule Gaja.Tracker do
changeset(tracker, attrs)
end
@doc false
def update_changeset(%__MODULE__{} = tracker, attrs) do
changeset(tracker, attrs)
end
......
......@@ -4,12 +4,11 @@ defmodule Gaja.TrackerEntry do
use Ecto.Schema
import Ecto.Changeset
schema "data_trackers" do
schema "tracker_entries" do
field :date, :naive_datetime
field :value, :float
field :value_string, :string
field :user_id, :id
field :tracker_id, :id
belongs_to :tracker, Gaja.Tracker
timestamps()
end
......
defmodule Gaja.Trackers do
@moduledoc false
import Ecto
import Ecto.Query, warn: false
alias Gaja.{Tracker, Repo}
alias Gaja.Accounts.{User}
def list_trackers do
Repo.all(Tracker)
def list_trackers(user) do
Repo.all(Ecto.assoc(user, :trackers))
end
def create_tracker(attrs) do
%Tracker{}
def create_tracker(user, attrs) do
user
|> build_assoc(:trackers)
|> Tracker.create_changeset(attrs)
|> Repo.insert()
end
def get_tracker(id) do
Repo.get(Tracker, id)
end
def get_by(%{"name" => name}) do
Repo.get_by!(Tracker, name: name)
def get_tracker(%User{id: user_id}, id) do
Tracker
|> where([t], t.id == ^id and t.user_id == ^user_id)
|> Repo.one()
end
def update_tracker(%Tracker{} = tracker, attrs) do
changeset = tracker
tracker
|> Tracker.update_changeset(attrs)
changeset
|> Repo.update()
end
def delete_tracker(%Tracker{} = tracker) do
Repo.delete(tracker)
end
def get_by(attrs) do
Repo.get_by(Tracker, attrs)
end
end
defmodule Gaja.Repo.Migrations.CleanTrackerRelation do
use Ecto.Migration
def change do
alter table(:trackers) do
add :user_id, references(:users, type: :uuid), null: false
end
end
end
......@@ -2,46 +2,49 @@ defmodule Gaja.TrackerController do
use Gaja.Web, :controller
import Gaja.Web.Authorize
alias Gaja.{Repo, Tracker, Trackers}
alias Gaja.{Tracker, Trackers}
action_fallback Gaja.Web.FallbackController
plug :user_check when action in [:index, :show, :create, :update, :delete]
def index(conn, _params) do
trackers = Repo.all(Tracker)
def index(%Plug.Conn{assigns: %{current_user: user}} = conn, _params) do
trackers = Trackers.list_trackers(user)
render(conn, "index.json", trackers: trackers)
end
def show(conn, %{"id" => uuid}) do
with tracker = %Tracker{} <- Repo.get(Tracker, uuid) do
def show(%Plug.Conn{assigns: %{current_user: user}} = conn, %{"id" => uuid}) do
with tracker = %Tracker{} <- Trackers.get_tracker(user, uuid) do
render(conn, "show.json", tracker: tracker)
end
end
def create(conn, params) do
with {:ok, tracker} <- Trackers.create_tracker(params) do
def create(%Plug.Conn{assigns: %{current_user: user}} = conn, params) do
with {:ok, tracker} <- Trackers.create_tracker(user, params) do
conn
|> put_status(201)
|> render("show.json", tracker: tracker)
end
end
def delete(conn, %{"id" => uuid}) do
with tracker = %Tracker{} <- Repo.get(Tracker, uuid) do
Repo.delete!(tracker)
def delete(%Plug.Conn{assigns: %{current_user: user}} = conn, %{"id" => uuid}) do
with tracker = %Tracker{} <- Trackers.get_tracker(user, uuid) do
Trackers.delete_tracker(tracker)
conn
|> conn.put_status(204)
|> conn.send_resp(:no_content, "")
|> put_status(204)
|> send_resp(:no_content, "")
end
end
def update(conn, params) do
with tracker = %Tracker{} <- Repo.get(Tracker, params["id"]) do
changeset = Trackers.update_tracker(tracker, params)
def update(
%Plug.Conn{assigns: %{current_user: user}} = conn,
%{"id" => uuid, "tracker" => tracker_params}
) do
with tracker = %Tracker{} <- Trackers.get_tracker(user, uuid) do
changeset = Trackers.update_tracker(tracker, tracker_params)
case changeset do
{:ok, user} -> render(conn, "show.json", tracker: tracker)
{:ok, tracker} -> render(conn, "show.json", tracker: tracker)
{:error, _} -> changeset
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment