Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product options #462

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
defmodule AdminAppWeb.OptionTypeController do
use AdminAppWeb, :controller
alias Snitch.Data.Model.OptionType, as: OTModel
alias Snitch.Data.Schema.OptionType, as: OTSchema
alias Snitch.Data.Model.Option, as: OptionModel
alias Snitch.Data.Schema.Option, as: OptionSchema

def index(conn, _params) do
option_types = OTModel.get_all()
option_types = OptionModel.get_all()
render(conn, "index.html", %{option_types: option_types})
end

def new(conn, _params) do
changeset = OTSchema.create_changeset(%OTSchema{}, %{})
changeset = OptionSchema.create_changeset(%OptionSchema{}, %{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"option_type" => params}) do
case OTModel.create(params) do
case OptionModel.create(params) do
{:ok, _} ->
option_types = OTModel.get_all()
option_types = OptionModel.get_all()
render(conn, "index.html", %{option_types: option_types})

{:error, changeset} ->
Expand All @@ -26,8 +26,8 @@ defmodule AdminAppWeb.OptionTypeController do

def edit(conn, %{"id" => id}) do
with {id, _} <- Integer.parse(id),
{:ok, %OTSchema{} = option_type} <- OTModel.get(id) do
changeset = OTSchema.update_changeset(option_type, %{})
{:ok, %OptionSchema{} = option_type} <- OptionModel.get(id) do
changeset = OptionSchema.update_changeset(option_type, %{})
render(conn, "edit.html", changeset: changeset)
else
err when err in [:error, nil] ->
Expand All @@ -39,9 +39,9 @@ defmodule AdminAppWeb.OptionTypeController do

def update(conn, %{"id" => id, "option_type" => params}) do
with {id, _} <- Integer.parse(id),
{:ok, option_type} <- OTModel.get(id),
{:ok, _} <- OTModel.update(option_type, params) do
option_types = OTModel.get_all()
{:ok, option_type} <- OptionModel.get(id),
{:ok, _} <- OptionModel.update(option_type, params) do
option_types = OptionModel.get_all()
render(conn, "index.html", %{option_types: option_types})
else
{:error, changeset} ->
Expand All @@ -57,8 +57,8 @@ defmodule AdminAppWeb.OptionTypeController do

def delete(conn, %{"id" => id}) do
with {id, _} <- Integer.parse(id),
false <- OTModel.is_theme_associated(id),
{:ok, option_type} <- OTModel.delete(id) do
false <- OptionModel.is_theme_associated(id),
{:ok, option_type} <- OptionModel.delete(id) do
conn
|> put_flash(:info, "Option type #{option_type.name} deleted successfully")
|> redirect(to: option_type_path(conn, :index))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule AdminAppWeb.ProductController do
end

def edit(conn, %{"id" => id} = params) do
preloads = [variants: [options: :option_type], images: [], taxon: [:variation_themes]]
preloads = [variants: [options: :option], images: [], taxon: [:variation_themes]]

with {:ok, %ProductSchema{} = product} <- ProductModel.get(id) do
product = product |> Repo.preload(preloads)
Expand Down Expand Up @@ -311,7 +311,7 @@ defmodule AdminAppWeb.ProductController do
|> String.trim()
|> String.split(",")
|> Enum.map(fn option_value ->
%{option_type_id: map["option_type_id"], value: option_value}
%{option_id: map["option_type_id"], value: option_value}
end)
end)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule AdminAppWeb.TemplateApi.OptionTypeController do

alias AdminAppWeb.TemplateApi.OptionTypeView
alias Snitch.Core.Tools.MultiTenancy.Repo
alias Snitch.Data.Model.{VariationTheme, ProductOptionValue}
alias Snitch.Data.Model.{VariationTheme, OptionValue}
import Phoenix.View, only: [render_to_string: 3]

def index(conn, %{"theme_id" => theme_id} = params) do
Expand All @@ -20,8 +20,8 @@ defmodule AdminAppWeb.TemplateApi.OptionTypeController do
end

def update(conn, %{"id" => id} = params) do
with {:ok, option_value} <- ProductOptionValue.get(id),
{:ok, option_value} <- ProductOptionValue.update(option_value, params) do
with {:ok, option_value} <- OptionValue.get(id),
{:ok, option_value} <- OptionValue.update(option_value, params) do
render(conn, "option_value.json", option_value: option_value)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule AdminAppWeb.VariationThemeController do

alias Snitch.Data.Model.VariationTheme, as: VTModel
alias Snitch.Data.Schema.VariationTheme, as: VTSchema
alias Snitch.Data.Model.OptionType
alias Snitch.Data.Model.Option, as: OptionModel

plug(:load_resources when action in [:new, :edit, :update, :create])

Expand Down Expand Up @@ -72,6 +72,6 @@ defmodule AdminAppWeb.VariationThemeController do
end

defp load_resources(conn, _opts) do
assign(conn, :option_types, OptionType.get_all())
assign(conn, :option_types, OptionModel.get_all())
end
end
2 changes: 1 addition & 1 deletion apps/admin_app/lib/admin_app_web/views/product_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule AdminAppWeb.ProductView do
variant = product.variants |> List.first()

variant.options
|> Enum.map(fn x -> x.option_type end)
|> Enum.map(fn x -> x.option end)
end

def get_brand_options(brands) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule SnitchApiWeb.ProductOptionValueController do
use SnitchApiWeb, :controller

alias Snitch.Data.Model.ProductOptionValue
alias Snitch.Data.Model.OptionValue

def update(conn, %{"id" => id} = params) do
with option_value <- ProductOptionValue.get(id),
{:ok, option_value} <- ProductOptionValue.update(option_value, params) do
with option_value <- OptionValue.get(id),
{:ok, option_value} <- OptionValue.update(option_value, params) do
render(conn, "option_value.json", option_value: option_value)
end
end
Expand Down
44 changes: 22 additions & 22 deletions apps/snitch_core/lib/core/data/model/option_type.ex
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
defmodule Snitch.Data.Model.OptionType do
defmodule Snitch.Data.Model.Option do
@moduledoc """
OptionType API
Option API
"""
use Snitch.Data.Model
alias Snitch.Data.Schema.OptionType
alias Snitch.Data.Schema.Option
alias Snitch.Data.Schema.Product
alias Snitch.Core.Tools.MultiTenancy.Repo
import Ecto.Query

@doc """
Create a OptionType with supplied params
Create a Option with supplied params
"""
@spec create(map) :: {:ok, OptionType.t()} | {:error, Ecto.Changeset.t()}
@spec create(map) :: {:ok, Option.t()} | {:error, Ecto.Changeset.t()}
def create(params) do
QH.create(OptionType, params, Repo)
QH.create(Option, params, Repo)
end

@doc """
Returns all OptionTypes
Returns all Options
"""
@spec get_all() :: [OptionType.t()]
@spec get_all() :: [Option.t()]
def get_all do
Repo.all(OptionType)
Repo.all(Option)
end

@doc """
Returns an OptionType
Returns an Option

Takes OptionType id as input
"""
@spec get(integer) :: {:ok, OptionType.t()} | {:error, atom}
@spec get(integer) :: {:ok, Option.t()} | {:error, atom}
def get(id) do
QH.get(OptionType, id, Repo)
QH.get(Option, id, Repo)
end

@doc """
Update the OptionType with supplied params and OptionType instance
Update the Option with supplied params and Option instance
"""
@spec update(OptionType.t(), map) :: {:ok, OptionType.t()} | {:error, Ecto.Changeset.t()}
@spec update(Option.t(), map) :: {:ok, Option.t()} | {:error, Ecto.Changeset.t()}
def update(model, params) do
QH.update(OptionType, params, model, Repo)
QH.update(Option, params, model, Repo)
end

@doc """
Deletes the OptionType
Deletes the Option
"""
@spec delete(non_neg_integer | struct()) ::
{:ok, OptionType.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found}
{:ok, Option.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found}
def delete(id) when is_integer(id) do
QH.delete(OptionType, id, Repo)
QH.delete(Option, id, Repo)
end

def delete(%OptionType{} = instance) do
QH.delete(OptionType, instance, Repo)
def delete(%Option{} = instance) do
QH.delete(Option, instance, Repo)
end

@doc """
Checks whether the OptionType is associated to any product's variation theme.
Checks whether the Option is associated to any product's variation theme.
"""
@spec is_theme_associated(non_neg_integer) :: true | false
def is_theme_associated(option_type_id) do
option_preloader = from(ot in OptionType, where: ot.id == ^option_type_id)
option_preloader = from(ot in Option, where: ot.id == ^option_type_id)

products =
from(p in Product, preload: [theme: [option_types: ^option_preloader]]) |> Repo.all()
Expand Down
17 changes: 8 additions & 9 deletions apps/snitch_core/lib/core/data/model/product_option_value.ex
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
defmodule Snitch.Data.Model.ProductOptionValue do
defmodule Snitch.Data.Model.OptionValue do
@moduledoc """
Product Option Value API.
Option Value API.
"""

use Snitch.Data.Model

alias Snitch.Data.Schema.ProductOptionValue
alias Snitch.Data.Schema.OptionValue

@doc """
Update the Option Value with supplied params and Option Value instance
"""
@spec update(ProductOptionValue.t(), map) ::
{:ok, ProductOptionValue.t()} | {:error, Ecto.Changeset.t()}
@spec update(OptionValue.t(), map) :: {:ok, OptionValue.t()} | {:error, Ecto.Changeset.t()}
def update(model, params) do
QH.update(ProductOptionValue, params, model, Repo)
QH.update(OptionValue, params, model, Repo)
end

@doc """
Returns an Product Option Value
Returns an Option Value

Takes Product Option Value id as input
"""
@spec get(integer) :: {:ok, ProductOptionValue.t()} | {:error, atom}
@spec get(integer) :: {:ok, OptionValue.t()} | {:error, atom}
def get(id) do
QH.get(ProductOptionValue, id, Repo)
QH.get(OptionValue, id, Repo)
end
end
43 changes: 43 additions & 0 deletions apps/snitch_core/lib/core/data/schema/option.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Snitch.Data.Schema.Option do
@moduledoc """
Models an Option
"""
use Snitch.Data.Schema

alias Snitch.Data.Schema.TemplateOptionValue

@type t :: %__MODULE__{}

schema "snitch_options" do
field(:name, :string)
field(:display_name, :string)

has_many(:template_option_values, TemplateOptionValue)
timestamps()
end

@create_params ~w(name display_name)a

@doc """
Returns a changeset to create new Option
"""
@spec create_changeset(t, map) :: Ecto.Changeset.t()
def create_changeset(model, params) do
common_changeset(model, params)
end

@doc """
Returns a changeset to update a Option
"""
@spec update_changeset(t, map) :: Ecto.Changeset.t()
def update_changeset(model, params) do
common_changeset(model, params)
end

defp common_changeset(model, params) do
model
|> cast(params, @create_params)
|> validate_required(@create_params)
|> unique_constraint(:name)
end
end
35 changes: 2 additions & 33 deletions apps/snitch_core/lib/core/data/schema/option_type.ex
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
defmodule Snitch.Data.Schema.OptionType do
@moduledoc """
Models an OptionType
"""
use Snitch.Data.Schema

alias Snitch.Data.Schema.TemplateOptionValue

@type t :: %__MODULE__{}

schema "snitch_option_types" do
field(:name, :string)
field(:display_name, :string)

has_many(:template_option_values, TemplateOptionValue)
field(:config, :map)
field(:type, OptionTypeEnum, default: :rectangle)
timestamps()
end

@create_params ~w(name display_name)a

@doc """
Returns a changeset to create new OptionType
"""
@spec create_changeset(t, map) :: Ecto.Changeset.t()
def create_changeset(model, params) do
common_changeset(model, params)
end

@doc """
Returns a changeset to update a OptionType
"""
@spec update_changeset(t, map) :: Ecto.Changeset.t()
def update_changeset(model, params) do
common_changeset(model, params)
end

defp common_changeset(model, params) do
model
|> cast(params, @create_params)
|> validate_required(@create_params)
|> unique_constraint(:name)
end
end
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
defmodule Snitch.Data.Schema.ProductOptionValue do
defmodule Snitch.Data.Schema.OptionValue do
@moduledoc false

use Snitch.Data.Schema
alias Snitch.Data.Schema.{OptionType, Product}
alias Snitch.Data.Schema.{Option, Product}

schema "snitch_product_option_values" do
schema "snitch_option_values" do
field(:value, :string)
field(:value_data, :map)
field(:display_name, :string)

belongs_to(:option_type, OptionType)
belongs_to(:option, Option)
belongs_to(:product, Product)

timestamps()
end

def changeset(model, params) do
model
|> cast(params, [:option_type_id, :product_id, :value])
|> validate_required([:option_type_id, :value])
|> cast(params, [:option_id, :product_id, :value])
|> validate_required([:option_id, :value])
end

def update_changeset(model, params) do
Expand Down
Loading