Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Categories context #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions lib/ex_money/categories/categories.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
defmodule ExMoney.Categories do
import Ecto.Query, warn: false

alias ExMoney.Categories.Category
alias ExMoney.Repo

def category_changeset() do
Category.changeset(%Category{})
end

def category_changeset(category) do
Category.changeset(category)
end

def create_category(attrs \\ %{}) do
%Category{}
|> Category.changeset(attrs)
|> Repo.insert()
end

def create_category!(attrs \\ %{}) do
%Category{}
|> Category.changeset(attrs)
|> Repo.insert!()
end

def update_category(category, attrs \\ %{}) do
category
|> Category.changeset(attrs)
|> Repo.update()
end

def update_category!(category, attrs \\ %{}) do
category
|> Category.changeset(attrs)
|> Repo.update!()
end

def all do
Repo.all(Category)
end

def get_category(id), do: Repo.get(Category, id)
def get_category!(id), do: Repo.get!(Category, id)

def delete_category!(id) do
category = get_category!(id)

Repo.delete!(category)
end

def visible_categories() do
query = from c in Category, where: c.hidden != true

Repo.all(query)
end

def list_with_hidden do
query = from c in Category, order_by: c.name, preload: [:parent]

Repo.all(query)
end

def list do
query =
from c in Category,
where: c.hidden != true,
order_by: c.name, preload: [:parent]

Repo.all(query)
end

def find_or_create_category_by_name(name) do
case get_category_by(name: name) do
nil ->
create_category!(%{name: name})

existing_category ->
existing_category
end
end

def get_category_by(params) do
Repo.get_by(Category, params)
end

def get_category_by!(params) do
Repo.get_by!(Category, params)
end

def parents_with_hidden do
query =
from c in Category,
where: is_nil(c.parent_id),
order_by: c.name,
select: {c.humanized_name, c.id}

Repo.all(query)
end

def parents do
query =
from c in Category,
where: is_nil(c.parent_id),
where: c.hidden != true,
order_by: c.name,
select: {c.humanized_name, c.id}

Repo.all(query)
end

def get_categories_by_ids(ids) do
query = from c in Category, where: c.id in ^(ids)

Repo.all(query)
end

def get_sub_categories(parent_category_id) do
query =
from c in Category,
where: c.parent_id == ^parent_category_id,
where: c.hidden != true,
select: c.id

Repo.all(query)
end
end
47 changes: 47 additions & 0 deletions lib/ex_money/categories/category.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule ExMoney.Categories.Category do
use ExMoney.Web, :model

alias ExMoney.Categories.Category

schema "categories" do
field :name, :string
field :humanized_name, :string
field :css_color, :string
field :hidden, :boolean

timestamps()

has_many :transactions, ExMoney.Transaction
belongs_to :parent, Category
end

def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(name parent_id humanized_name)a)
|> validate_required(~w(name)a)
|> Ecto.Changeset.put_change(:css_color, generate_color())
|> put_humanized_name
end

def update_changeset(model, params \\ %{}) do
model
|> cast(params, ~w(name humanized_name parent_id hidden css_color)a)
end

def generate_color do
red = div((:rand.uniform(256) + 255), 2)
green = div((:rand.uniform(256) + 255), 2)
blue = div((:rand.uniform(256) + 255), 2)

"rgb(#{red}, #{green}, #{blue})"
end

defp put_humanized_name(changeset) do
case Ecto.Changeset.fetch_change(changeset, :name) do
{:ok, name} ->
humanized_name = String.replace(name, "_", " ") |> String.capitalize
Ecto.Changeset.put_change(changeset, :humanized_name, humanized_name)
:error -> changeset
end
end
end
4 changes: 2 additions & 2 deletions lib/ex_money/rule_processor.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExMoney.RuleProcessor do
use GenServer

alias ExMoney.{Repo, Rule, Transaction, Category, Account}
alias ExMoney.{Repo, Rule, Transaction, Categories, Account}

import Ecto.Query

Expand Down Expand Up @@ -54,7 +54,7 @@ defmodule ExMoney.RuleProcessor do

defp withdraw_to_cash(rule, transaction) do
{:ok, re} = Regex.compile(rule.pattern, "i")
withdraw_category = Category.by_name_with_hidden("withdraw") |> Repo.one
withdraw_category = Categories.get_category_by(name: "withdraw")

account = Repo.get(Account, rule.target_id)

Expand Down
14 changes: 2 additions & 12 deletions lib/ex_money/saltedge/transactions_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule ExMoney.Saltedge.TransactionsWorker do

require Logger

alias ExMoney.{Repo, Transaction, Category, Account}
alias ExMoney.{Repo, Transaction, Categories, Account}

def start_link(_opts \\ []) do
GenServer.start_link(__MODULE__, :ok, name: :transactions_worker)
Expand Down Expand Up @@ -140,21 +140,11 @@ defmodule ExMoney.Saltedge.TransactionsWorker do
end

defp set_category_id(transaction) do
category = find_or_create_category(transaction["category"])
category = Categories.find_or_create_category_by_name(transaction["category"])

Map.put(transaction, "category_id", category.id)
end

defp find_or_create_category(name) do
case Category.by_name_with_hidden(name) |> Repo.one do
nil ->
changeset = Category.changeset(%Category{}, %{name: name})
Repo.insert!(changeset)

existing_category -> existing_category
end
end

defp date_to_string(date) do
{:ok, str_date} = Timex.format(date, "%Y-%m-%d", :strftime)
str_date
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_money_web/controllers/api/v2/category_controller.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule ExMoney.Web.Api.V2.CategoryController do
use ExMoney.Web, :controller

alias ExMoney.Category
alias ExMoney.Categories

plug Guardian.Plug.EnsureAuthenticated, handler: ExMoney.Guardian.ApiUnauthenticated

def index(conn, _params) do
categories = Repo.all(Category)
categories = Categories.all()

render conn, :index, categories: categories
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExMoney.Web.Mobile.BudgetHistoryController do
plug Guardian.Plug.EnsureAuthenticated, handler: ExMoney.Guardian.Mobile.Unauthenticated
plug :put_layout, "mobile.html"

alias ExMoney.{Repo, Budget, Account, Category}
alias ExMoney.{Repo, Budget, Account, Categories}

def index(conn, _params) do
user = Guardian.Plug.current_resource(conn)
Expand All @@ -29,7 +29,7 @@ defmodule ExMoney.Web.Mobile.BudgetHistoryController do
items =
budget.items
|> Enum.map(fn({id, v}) ->
name = Repo.get(Category, id).humanized_name
name = Categories.get_category!(id).humanized_name
{amount, _} = Integer.parse(v)
{name, amount}
end)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExMoney.Web.Mobile.FavouriteTransactionController do
use ExMoney.Web, :controller
alias ExMoney.{Repo, FavouriteTransaction, Category, Account}
alias ExMoney.{Repo, FavouriteTransaction, Categories, Account}

plug Guardian.Plug.EnsureAuthenticated, handler: ExMoney.Guardian.Mobile.Unauthenticated
plug :put_layout, "mobile.html"
Expand Down Expand Up @@ -67,7 +67,7 @@ defmodule ExMoney.Web.Mobile.FavouriteTransactionController do
end

defp categories_list do
categories_dict = Repo.all(Category)
categories_dict = Categories.all()

Enum.reduce(categories_dict, %{}, fn(category, acc) ->
if is_nil(category.parent_id) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExMoney.Web.Mobile.Setting.BudgetController do
use ExMoney.Web, :controller

alias ExMoney.{Account, Repo, Category, BudgetTemplate, BudgetItem, DateHelper, Budget}
alias ExMoney.{Account, Repo, Categories, BudgetTemplate, BudgetItem, DateHelper, Budget}

plug Guardian.Plug.EnsureAuthenticated, handler: ExMoney.Guardian.Mobile.Unauthenticated
plug :put_layout, "mobile.html"
Expand Down Expand Up @@ -155,7 +155,7 @@ defmodule ExMoney.Web.Mobile.Setting.BudgetController do
end

defp categories_list do
categories_dict = Category.visible |> Repo.all
categories_dict = Categories.visible_categories()

Enum.reduce(categories_dict, %{}, fn(category, acc) ->
if is_nil(category.parent_id) do
Expand Down
25 changes: 11 additions & 14 deletions lib/ex_money_web/controllers/mobile/setting/category_controller.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule ExMoney.Web.Mobile.Setting.CategoryController do
use ExMoney.Web, :controller

alias ExMoney.{Category, Repo}
alias ExMoney.Categories

plug Guardian.Plug.EnsureAuthenticated, handler: ExMoney.Guardian.Mobile.Unauthenticated
plug :put_layout, "mobile.html"

def index(conn, _params) do
categories = Repo.all(Category.list_with_hidden)
categories = Categories.list_with_hidden()

parent_categories = Enum.filter(categories, fn(c) -> is_nil(c.parent_id) end)

Expand All @@ -22,24 +22,22 @@ defmodule ExMoney.Web.Mobile.Setting.CategoryController do
end

def show(conn, %{"id" => id}) do
category = Repo.get!(Category, id)
category = Categories.get_category!(id)

render conn, :show, category: category
end

def new(conn, _params) do
parent_categories = Repo.all(Category.parents_with_hidden)
changeset = Category.changeset(%Category{})
parent_categories = Categories.parents_with_hidden()
changeset = Categories.category_changeset()

render conn, :new,
changeset: changeset,
parent_categories: parent_categories
end

def create(conn, %{"category" => category_params}) do
changeset = Category.changeset(%Category{}, category_params)

case Repo.insert(changeset) do
case Categories.create_category(category_params) do
{:ok, _category} ->
conn
|> put_flash(:info, "Category created successfully.")
Expand All @@ -50,9 +48,9 @@ defmodule ExMoney.Web.Mobile.Setting.CategoryController do
end

def edit(conn, %{"id" => id}) do
parent_categories = Category.parents_with_hidden |> Repo.all
category = Repo.get(Category, id)
changeset = Category.update_changeset(category)
parent_categories = Categories.parents_with_hidden()
category = Categories.get_category!(id)
changeset = Categories.category_changeset(category)

render conn, :edit,
category: category,
Expand All @@ -61,10 +59,9 @@ defmodule ExMoney.Web.Mobile.Setting.CategoryController do
end

def update(conn, %{"id" => id, "category" => category_params}) do
category = Repo.get!(Category, id)
changeset = Category.update_changeset(category, category_params)
category = Categories.get_category!(id)

case Repo.update(changeset) do
case Categories.update_category(category, category_params) do
{:ok, _category} ->
send_resp(conn, 200, "")
{:error, _changeset} ->
Expand Down
Loading