Skip to content

Commit

Permalink
migrate from status id to status_code ref: dwyl/statuses#4 #89
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 11, 2022
1 parent 53a85f1 commit 0fd80f1
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 44 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# @dwyl App MVP `Phoenix` 💡⏳ ✅

A `Phoenix` implementation
of the @dwyl App
[MVP feature set](https://github.com/dwyl/app/issues/266).
A **`Phoenix`** implementation
of the **@dwyl App**
[**MVP** feature set](https://github.com/dwyl/app/issues/266).

[![Build Status](https://img.shields.io/travis/com/dwyl/app-mvp-phoenix/master?color=bright-green&style=flat-square)](https://travis-ci.org/dwyl/app-mvp-phoenix)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/app-mvp-phoenix/master.svg?style=flat-square)](https://codecov.io/github/dwyl/app-mvp-phoenix?branch=master)
Expand Down Expand Up @@ -614,8 +614,8 @@ Run the following
commands:

```sh
mix phx.gen.schema Status status text:string
mix phx.gen.schema Person people givenName:binary auth_provider:string key_id:integer status_id:references:status picture:binary locale:string
mix phx.gen.schema Status status text:string status_code:integer
mix phx.gen.schema Person people givenName:binary auth_provider:string key_id:integer status_code:integer picture:binary locale:string
mix phx.gen.schema Item items text:string person_id:references:people status:references:status
mix phx.gen.schema Timer timers item_id:references:items start:naive_datetime end:naive_datetime person_id:references:people
```
Expand Down
8 changes: 4 additions & 4 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule App.Item do

schema "items" do
field :person_id, :id
field :status, :id
field :status_code, :integer
field :text, :string

timestamps()
Expand All @@ -16,7 +16,7 @@ defmodule App.Item do
@doc false
def changeset(item, attrs) do
item
|> cast(attrs, [:text, :person_id, :status])
|> cast(attrs, [:text, :person_id, :status_code])
|> validate_required([:text, :person_id])
end

Expand Down Expand Up @@ -66,7 +66,7 @@ defmodule App.Item do
def list_items do
Item
|> order_by(desc: :inserted_at)
|> where([a], is_nil(a.status) or a.status != 6)
|> where([a], is_nil(a.status_code) or a.status_code != 6)
|> Repo.all()
end

Expand All @@ -91,7 +91,7 @@ defmodule App.Item do
# "soft" delete
def delete_item(id) do
get_item!(id)
|> Item.changeset(%{status: 6})
|> Item.changeset(%{status_code: 6})
|> Repo.update()
end
end
4 changes: 2 additions & 2 deletions lib/app/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ defmodule App.Person do
field :key_id, :integer
field :locale, :string
field :picture, Fields.Encrypted
field :status_id, :id
field :status_code, :integer

timestamps()
end

@doc false
def changeset(person, attrs) do
person
|> cast(attrs, [:givenName, :auth_provider, :key_id, :picture, :locale, :status_id])
|> cast(attrs, [:givenName, :auth_provider, :key_id, :picture, :locale, :status_code])
|> validate_required([:givenName, :auth_provider])
end

Expand Down
7 changes: 4 additions & 3 deletions lib/app/status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ defmodule App.Status do

schema "status" do
field :text, :string
field :status_code, :integer

timestamps()
end

@doc false
def changeset(status, attrs) do
status
|> cast(attrs, [:text])
|> cast(attrs, [:text, :status_code])
|> validate_required([:text])
end

def create(attrs) do
%Status{}
|> changeset(attrs)
|> Repo.insert!()
|> Repo.insert()
end

def upsert(attrs) do
Expand All @@ -30,7 +31,7 @@ defmodule App.Status do
create(attrs)

status ->
status
{:ok, status}
end
end

Expand Down
26 changes: 24 additions & 2 deletions lib/app/timer.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule App.Timer do
use Ecto.Schema
import Ecto.Changeset
# import Ecto.Query
alias App.Repo
alias __MODULE__

schema "timers" do
field :end, :naive_datetime
Expand All @@ -14,7 +17,26 @@ defmodule App.Timer do
@doc false
def changeset(timer, attrs) do
timer
|> cast(attrs, [:start, :end])
|> validate_required([:start, :end])
|> cast(attrs, [:item_id, :start, :end, :person_id])
|> validate_required([:item_id, :start])
end


@doc """
`start/1` starts a timer.
## Examples
iex> start(%{item_id: 1, })
{:ok, %Timer{}}
iex> create_item(%{item_id: nil})
{:error, %Ecto.Changeset{}}
"""
def start(attrs \\ %{}) do
%Timer{}
|> changeset(attrs)
|> Repo.insert()
end
end
19 changes: 7 additions & 12 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ defmodule AppWeb.AppLive do

@impl true
def handle_event("create", %{"text" => text}, socket) do
Item.create_item(%{text: text, person_id: 1, status_id: 2})
Item.create_item(%{text: text, person_id: 1, status_code: 2})
socket = assign(socket, items: Item.list_items(), active: %Item{})
AppWeb.Endpoint.broadcast_from(self(), @topic, "update", socket.assigns)
{:noreply, socket}
end

@impl true
def handle_event("toggle", data, socket) do
# IO.inspect(data)
# Toggle the status of the item between 3 (:active) and 4 (:done)
status = if Map.has_key?(data, "value"), do: 4, else: 3
item = Item.get_item!(Map.get(data, "id"))
Item.update_item(item, %{id: item.id, status: status})
Item.update_item(item, %{id: item.id, status_code: status})
socket = assign(socket, items: Item.list_items(), active: %Item{})
AppWeb.Endpoint.broadcast_from(self(), @topic, "update", socket.assigns)
{:noreply, socket}
Expand All @@ -34,17 +34,12 @@ defmodule AppWeb.AppLive do
def handle_event("delete", data, socket) do
Item.delete_item(Map.get(data, "id"))
socket = assign(socket, items: Item.list_items(), active: %Item{})
AppWeb.Endpoint.broadcast_from(self(), @topic, "update", socket.assigns)
AppWeb.Endpoint.broadcast_from(self(), @topic, "delete", socket.assigns)
{:noreply, socket}
end

def checked?(item) do
not is_nil(item.status) and item.status == 4
end

def completed?(item) do
# IO.inspect(item)
if not is_nil(item.status) and item.status == 4, do: "line-through text-green", else: ""
# helper function that checks for status 4 (:done)
def done?(item) do
not is_nil(item.status_code) and item.status_code == 4
end

end
12 changes: 8 additions & 4 deletions lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
<form phx-submit="create"
class="w-full lg:w-3/4 lg:max-w-lg text-center mx-auto">
<textarea
class="w-full py-2 px-2 text-slate-800 text-3xl"
class="w-full py-2 px-2 text-slate-800 text-3xl
bg-white bg-clip-padding
transition ease-in-out
focus:border-none focus:outline-none"
name="text"
placeholder="What is on your mind?"
autofocus=""
required="required" />
required="required"
></textarea>
<button
class="rounded-xl px-4 py-2 mt-1
bg-green-600 hover:bg-green-700 hover:shadow-lg text-white">
Expand All @@ -17,8 +21,8 @@
<ul class="w-full">
<%= for item <- @items do %>
<li data-id={item.id}
class="mt-2 flex w-full border-t border-slate-400 py-2">
<%= if checked?(item) do %>
class="mt-2 flex w-full border-t border-slate-200 py-2">
<%= if done?(item) do %>
<input type="checkbox" phx-value-id={item.id} phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox text-slate-400"
checked
Expand Down
1 change: 1 addition & 0 deletions priv/repo/migrations/20220627162141_create_status.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule App.Repo.Migrations.CreateStatus do
def change do
create table(:status) do
add :text, :string
add :status_code, :integer

timestamps()
end
Expand Down
4 changes: 2 additions & 2 deletions priv/repo/migrations/20220627162148_create_people.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ defmodule App.Repo.Migrations.CreatePeople do
add :key_id, :integer
add :picture, :binary
add :locale, :string
add :status_id, references(:status, on_delete: :nothing)
add :status_code, :integer

timestamps()
end

create index(:people, [:status_id])
create index(:people, [:status_code])
end
end
4 changes: 2 additions & 2 deletions priv/repo/migrations/20220627162154_create_items.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule App.Repo.Migrations.CreateItems do
create table(:items) do
add :text, :string
add :person_id, references(:people, on_delete: :nothing)
add :status, references(:status, on_delete: :nothing)
add :status_code, :integer

timestamps()
end

create index(:items, [:person_id])
create index(:items, [:status])
create index(:items, [:status_code])
end
end
5 changes: 2 additions & 3 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ Statuses.get_statuses()
text: Atom.to_string(s.text)
|> String.split("_", trim: true)
|> Enum.join(" "),
id: s.code
status_code: s.code
})
end)


Person.create(%{
givenName: "Beyoncé",
auth_provider: "Google",
status_id: 1
status_code: 1
})
4 changes: 2 additions & 2 deletions test/app/item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule App.ItemTest do

describe "items" do
@valid_attrs %{text: "some text", person_id: 1}
@update_attrs %{text: "some updated text", status: 1}
@update_attrs %{text: "some updated text", status_code: 1}
@invalid_attrs %{text: nil}


Expand Down Expand Up @@ -51,7 +51,7 @@ defmodule App.ItemTest do
test "delete_item/1 soft-deltes an item" do
item = item_fixture()
assert {:ok, %Item{} = deleted_item} = Item.delete_item(item.id)
assert deleted_item.status == 6
assert deleted_item.status_code == 6
end
end
end
22 changes: 22 additions & 0 deletions test/app/person_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule App.PersonTest do
use App.DataCase
alias App.Person

describe "person/people" do
# test "get!/1 returns the person with given id" do
# person = item_fixture(@valid_attrs)
# assert Item.get_item!(item.id) == item
# end

test "create/1 with valid data creates a person" do
person = Person.create(%{
givenName: "aLeX",
auth_provider: "dwyl",
status_code: 1,
local: "SoCal"
})
assert person.givenName == "aLeX"
assert person.status_code == 1
end
end
end
45 changes: 45 additions & 0 deletions test/app/status_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule App.StatusTest do
use App.DataCase
alias App.Status

describe "status" do
@valid_attrs %{text: "some text"}
# @update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}


def status_fixture(attrs \\ %{}) do
{:ok, status} =
attrs
|> Enum.into(@valid_attrs)
|> Status.create()

status
end

test "create/1 with valid data creates a status" do
{:ok, status} = Status.create(@valid_attrs)
assert status.text == "some text"
end

test "create/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Status.create(@invalid_attrs)
end

test "upsert/1 with valid data returns status" do
{:ok, status} = Status.upsert(@valid_attrs)
assert status.text == "some text"

# confirm that upsert/1 does not re-create the same status:
{:ok, status_again} = Status.upsert(@valid_attrs)
assert status_again.id == status.id
end


test "upsert/1 with new data returns the new status" do
new_status = %{text: "Hello Simon!"}
{:ok, status} = Status.upsert(new_status)
assert status.text == new_status.text
end
end
end
29 changes: 29 additions & 0 deletions test/app/timer_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule App.TimerTest do
use App.DataCase
alias App.{Item, Timer}

describe "timers" do
@valid_item_attrs %{text: "some text", person_id: 1}
# @update_attrs %{text: "some updated text", status: 1}
# @invalid_attrs %{text: nil}


def item_fixture(attrs \\ %{}) do
{:ok, item} =
attrs
|> Enum.into(@valid_item_attrs)
|> Item.create_item()

item
end

test "Timer.start!/1 returns timer that has been started" do
item = item_fixture(@valid_item_attrs)
assert Item.get_item!(item.id) == item

started = NaiveDateTime.utc_now
{:ok, timer} = Timer.start(%{item_id: item.id, person_id: 1, start: started})
assert NaiveDateTime.diff(timer.start, started) == 0
end
end
end
Loading

0 comments on commit 0fd80f1

Please sign in to comment.