Skip to content

Commit

Permalink
remove Person & Profile code #118
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Sep 8, 2023
1 parent c11fb5c commit bd65456
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 537 deletions.
2 changes: 1 addition & 1 deletion BUILDIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ A `timer` is associated with an `item`
to track how long it takes to ***complete***.

+ `id`: `Int`
+ `item_id` (Foreign Key `item.id`)mvvvvyvyvy
+ `item_id` (Foreign Key `item.id`)
+ `start`: `NaiveDateTime` - start time for the timer
+ `stop`: `NaiveDateTime` - stop time for the timer
+ `inserted_at`: `NaiveDateTime` - record insertion time
Expand Down
28 changes: 3 additions & 25 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ defmodule App.Item do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias App.{Repo, Tag, ListItem, ItemTag, Person}
alias App.List, as: L
alias App.{Repo, Tag, ItemTag}
alias __MODULE__

schema "items" do
field :status, :integer
field :text, :string
field :item_lists, {:array, :string}, virtual: true
field :person_id, :integer

belongs_to :people, Person, references: :person_id, foreign_key: :person_id
many_to_many(:tags, Tag, join_through: ItemTag, on_replace: :delete)
many_to_many(:lists, L, join_through: ListItem, on_replace: :delete)

timestamps()
end

@doc false
def changeset(item, attrs \\ %{}) do
item
|> cast(attrs, [:person_id, :status, :text, :item_lists])
|> cast(attrs, [:person_id, :status, :text])
|> validate_required([:text, :person_id])
end

Expand All @@ -30,14 +27,6 @@ defmodule App.Item do
|> put_assoc(:tags, Tag.parse_and_create_tags(attrs))
end

def changeset_with_lists(item, list_ids) do
lists = Repo.all(from l in L, where: l.id in ^list_ids)

item
|> change()
|> put_assoc(:lists, lists)
end

@doc """
Creates an `item`.
Expand Down Expand Up @@ -80,7 +69,6 @@ defmodule App.Item do
Item
|> Repo.get!(id)
|> Repo.preload(tags: from(t in Tag, order_by: t.text))
|> Repo.preload(lists: from(l in L, order_by: l.name))
end

@doc """
Expand All @@ -104,7 +92,6 @@ defmodule App.Item do
|> where(person_id: ^person_id)
|> Repo.all()
|> Repo.preload(tags: from(t in Tag, order_by: t.text))
|> Repo.preload(lists: from(l in L, order_by: l.name))
end

@doc """
Expand Down Expand Up @@ -134,12 +121,6 @@ defmodule App.Item do
|> Repo.update()
end

def update_item_with_lists(%Item{} = item, list_ids) do
item
|> Item.changeset_with_lists(list_ids)
|> Repo.update()
end

def delete_item(id) do
get_item!(id)
|> Item.changeset(%{status: 6})
Expand Down Expand Up @@ -182,9 +163,6 @@ defmodule App.Item do
|> Enum.map(fn t ->
Map.put(t, :tags, items_tags[t.id].tags)
end)
|> Enum.map(fn t ->
Map.put(t, :lists, items_tags[t.id].lists)
end)
end

@doc """
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/app/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ defmodule App.Tag do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias App.{Item, ItemTag, Person, Repo}
alias App.{Item, ItemTag, Repo}
alias __MODULE__

schema "tags" do
field :text, :string
field :color, :string
field :person_id, :integer

belongs_to :people, Person, references: :person_id, foreign_key: :person_id
many_to_many(:items, Item, join_through: ItemTag)
timestamps()
end
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,7 @@
<%= live_patch tag.text, to: Routes.live_path(@socket, AppWeb.AppLive, %{filter_by: @filter, filter_by_tag: tag.text}), style: "background-color:#{tag.color}", class: " text-white font-bold py-1 px-2 rounded-full" %>
<% end %>
</div>

<div class="ml-2 my-3">
<%= for list <- item.lists do %>
<.badge color="info" label={list.name} />
<% end %>
<div class="ml-2 my-1">
<.a to={Routes.item_path(@socket, :edit, item.id)} class="" label="Edit lists" />
</div>
</div>

<div class="ml-2 my-2">
</div>
<% end %>
<% end %><!-- end for item <- @items -->
</ul>
Expand Down
61 changes: 22 additions & 39 deletions lib/app_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule AppWeb.Router do
use AppWeb, :router
alias App.Person

pipeline :browser do
plug :accepts, ["html"]
Expand All @@ -15,53 +14,37 @@ defmodule AppWeb.Router do
scope "/", AppWeb do
pipe_through :browser
get "/init", InitController, :index
get "/login", AuthController, :login
end

pipeline :authOptional, do: plug(AuthPlugOptional)
pipeline :verify_loggedin, do: plug(:loggedin)
pipeline :check_profile_name, do: plug(:profile_name)
pipeline :authOptional do
plug(AuthPlugOptional)
end

scope "/", AppWeb do
pipe_through [:browser, :authOptional, :verify_loggedin]

resources "/profile", ProfileController,
only: [:edit, :update],
param: "person_id"

pipe_through [:check_profile_name]
pipe_through [:browser, :authOptional]
live "/", AppLive
resources "/tags", TagController, except: [:show]
resources "/lists", ListController, except: [:show]
get "/login", AuthController, :login
get "/logout", AuthController, :logout

# edit item lists
resources "/items", ItemController, only: [:edit, :update]
# live "/stats", StatsLive
resources "/tags", TagController, except: [:show]
# resources "/lists", ListController, except: [:show]
end

# assign to conn the loggedin value used in templates
defp loggedin(conn, _opts) do
if not is_nil(conn.assigns[:jwt]) do
assign(conn, :loggedin, true)
else
assign(conn, :loggedin, false)
end
end
# pipeline :api do
# plug :accepts, ["json"]
# plug :fetch_session
# end

# Redirect to edit profile to force user to
# add name to their profile for sharing items feature
defp profile_name(conn, _opts) do
person_id = conn.assigns[:person][:id] || 0
# scope "/api", API, as: :api do
# pipe_through [:api, :authOptional]

person = Person.get_or_insert(person_id)
# resources "/items", Item, only: [:create, :update, :show]

if is_nil(person.name) do
conn
|> put_flash(:info, "Add a name to your profile to allow sharing items")
|> redirect(to: "/profile/#{person.person_id}/edit")
|> halt()
else
conn
end
end
# resources "/items/:item_id/timers", Timer,
# only: [:create, :update, :show, :index]

# put "/timers/:id", Timer, :stop

# resources "/tags", Tag, only: [:create, :update, :show]
# end
end
File renamed without changes.
2 changes: 0 additions & 2 deletions lib/app_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
<div class="container flex flex-wrap justify-between items-center mx-auto">

<%= if @loggedin do %>
<a href={Routes.profile_path(@conn, :edit, @person.id)}>
<img src={@person.picture} class="mr-3 h-6 sm:h-9 rounded-full" alt="avatar image">
</a>
<% else %>
<h1 class="text-white">Hi Friend!</h1>
<% end %>
Expand Down
19 changes: 0 additions & 19 deletions lib/app_web/templates/profile/edit.html.heex

This file was deleted.

File renamed without changes.
3 changes: 0 additions & 3 deletions lib/app_web/views/profile_view.ex

This file was deleted.

File renamed without changes.
57 changes: 39 additions & 18 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ defmodule App.MixProject do
[
app: :app,
version: "1.0.0",
elixir: "~> 1.12",
elixir: "~> 1.14",
elixirc_paths: elixirc_paths(Mix.env()),
compilers: Mix.compilers(),
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps(),
Expand Down Expand Up @@ -41,44 +40,66 @@ defmodule App.MixProject do
# Type `mix help deps` for examples and options.
defp deps do
[
{:phoenix, "~> 1.6.10"},
# Phoenix deps:
{:phoenix, "~> 1.7.0"},
{:phoenix_ecto, "~> 4.4"},
{:ecto_sql, "~> 3.6"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 3.0"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.18.0"},
{:phoenix_live_view, "~> 0.18.3"},
{:phoenix_view, "~> 2.0"},
{:floki, ">= 0.30.0", only: :test},
{:esbuild, "~> 0.4", runtime: Mix.env() == :dev},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},

# Check/get Environment Variables: https://github.com/dwyl/envar
{:envar, "~> 1.0.8"},
# Auth with ONE Environment Variable™: github.com/dwyl/auth_plug
{:auth_plug, "~> 1.4.14"},
# Easily Encrypt Senstive Data: github.com/dwyl/fields
{:fields, "~> 2.9.1"},
{:auth_plug, "~> 1.5.1"},

# Check/get Environment Variables: github.com/dwyl/envar
{:envar, "~> 1.1.0", override: true},

# Universally Unique Deterministic Content IDs: github.com/dwyl/cid
{:excid, "~> 1.0.1"},

# Easily Encrypt Sensitive Data: github.com/dwyl/fields
{:fields, "~> 2.10.3"},

# Database changes tracking:
# github.com/dwyl/phoenix-papertrail-demo
{:paper_trail, "~> 1.0.0"},

# Time string parsing: github.com/bitwalker/timex
{:timex, "~> 3.7"},

# Useful functions: github.com/dwyl/useful
{:useful, "~> 1.0.8", override: true},
# https://github.com/dwyl/useful/issues/17
{:useful, "~> 1.12.1", override: true},

# See: github.com/dwyl/useful/issues/17
{:atomic_map, "~> 0.9.3"},

# Decimal precision: github.com/ericmj/decimal
{:decimal, "~> 2.0"},

# Statuses: github.com/dwyl/statuses
{:statuses, "~> 1.1.1"},

# create docs on localhost by running "mix docs"
{:ex_doc, "~> 0.28.4", only: :dev, runtime: false},
# Track test coverage
{:excoveralls, "~> 0.14.5", only: [:test, :dev]},
# git pre-commit hook runs tests before allowing commits
{:pre_commit, "~> 0.3.4"},
{:credo, "~> 1.6.4", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.29", only: :dev, runtime: false},
# Track test coverage: github.com/parroty/excoveralls
{:excoveralls, "~> 0.15", only: [:test, :dev]},

# Git pre-commit hook runs tests before allowing commits:
# github.com/dwyl/elixir-pre-commit
{:pre_commit, "~> 0.3.4", only: :dev},
{:credo, "~> 1.7.0", only: [:dev, :test], runtime: false},

# Ref: github.com/dwyl/learn-tailwind
{:tailwind, "~> 0.1", runtime: Mix.env() == :dev},
{:petal_components, "~> 0.18"}
{:petal_components, "~> 1.0"}
]
end

Expand Down
Loading

0 comments on commit bd65456

Please sign in to comment.