diff --git a/README_Lunchbot.md b/README_Lunchbot.md index e47f5e8..ef8e774 100644 --- a/README_Lunchbot.md +++ b/README_Lunchbot.md @@ -43,6 +43,7 @@ Below is the database model for Lunchbot displayed using mermaid.js. You can mod int id int order_id FK int item_id FK + int rating } ITEMS { int id diff --git a/lib/lunchbot/lunchbot_data/item.ex b/lib/lunchbot/lunchbot_data/item.ex index 79c0a31..4e6d86d 100644 --- a/lib/lunchbot/lunchbot_data/item.ex +++ b/lib/lunchbot/lunchbot_data/item.ex @@ -1,6 +1,10 @@ defmodule Lunchbot.LunchbotData.Item do use Ecto.Schema + import Ecto.Changeset + import Ecto.Query + + alias Lunchbot.Repo schema "items" do belongs_to :category, Lunchbot.LunchbotData.Category @@ -24,4 +28,13 @@ defmodule Lunchbot.LunchbotData.Item do |> cast(attrs, [:name, :description, :price, :category_id, :image_url, :item_image, :deleted]) |> validate_required([:name, :price, :category_id]) end + + def get_rating(item_id) do + query = + from o in "order_items", + where: o.item_id == ^item_id, + select: avg(o.rating) + + Repo.all(query) |> Enum.at(0) |> Decimal.round(1) + end end diff --git a/lib/lunchbot/lunchbot_data/order_item.ex b/lib/lunchbot/lunchbot_data/order_item.ex index 6c2df3e..02e1f06 100644 --- a/lib/lunchbot/lunchbot_data/order_item.ex +++ b/lib/lunchbot/lunchbot_data/order_item.ex @@ -6,6 +6,7 @@ defmodule Lunchbot.LunchbotData.OrderItem do belongs_to :item, Lunchbot.LunchbotData.Item field :order_id, :integer has_many :order_item_options, Lunchbot.LunchbotData.OrderItemOptions + field :rating, :integer timestamps() end @@ -13,7 +14,7 @@ defmodule Lunchbot.LunchbotData.OrderItem do @doc false def changeset(order_item, attrs) do order_item - |> cast(attrs, [:order_id, :item_id]) + |> cast(attrs, [:order_id, :item_id, :rating]) |> validate_required([:order_id, :item_id]) end diff --git a/lib/lunchbot_web/live/previous_orders_live.ex b/lib/lunchbot_web/live/previous_orders_live.ex index 094e60a..ce0f2fb 100644 --- a/lib/lunchbot_web/live/previous_orders_live.ex +++ b/lib/lunchbot_web/live/previous_orders_live.ex @@ -1,10 +1,13 @@ defmodule LunchbotWeb.PreviousOrdersLive do @moduledoc false use Phoenix.LiveView, layout: {LunchbotWeb.LayoutView, "live.html"} + # use LunchbotWeb, :view import Lunchbot.Repo + import Phoenix.HTML.Link alias Lunchbot.Accounts alias Lunchbot.LunchbotData.Order + alias Lunchbot.LunchbotData.OrderItem # Initial state for the live view; populates the relevant socket assignments def mount(_params, session, socket) do @@ -14,7 +17,17 @@ defmodule LunchbotWeb.PreviousOrdersLive do Accounts.get_user_by_session_token(user_token) |> preload(orders: [:menu, order_items: [:item, order_item_options: [:option]]]) - {:ok, assign(socket, user: user_with_preloaded_orders)} + {:ok, + assign(socket, + user: user_with_preloaded_orders, + show_rating: true, + show_rating_modifiers: true + )} + end + + def handle_event("rating-edit", %{"myvar1" => "@order_item", "myvar2" => "val2"}, socket) do + IO.inspect("TODO - HANDLE rating-edit EVENT") + {:noreply, socket} end def render(assigns) do @@ -24,7 +37,37 @@ defmodule LunchbotWeb.PreviousOrdersLive do <%= for an_order <- @user.orders do %>
  • ORDER # <%= an_order.id %> || $<%= Order.get_total(an_order) %>
  • <% end %> diff --git a/lib/lunchbot_web/templates/admin/order_item/form.html.heex b/lib/lunchbot_web/templates/admin/order_item/form.html.heex index 1964876..aae3fac 100644 --- a/lib/lunchbot_web/templates/admin/order_item/form.html.heex +++ b/lib/lunchbot_web/templates/admin/order_item/form.html.heex @@ -22,6 +22,14 @@ <%= error_tag f, :item_id %> + +
    + <%= label f, :rating %> +
    + <%= number_input f, :rating %> + <%= error_tag f, :rating %> +
    +
    <%= submit "Submit", class: "torch-submit-button" %> diff --git a/lib/lunchbot_web/templates/admin/order_item/index.html.heex b/lib/lunchbot_web/templates/admin/order_item/index.html.heex index 63f57db..19ab0a6 100644 --- a/lib/lunchbot_web/templates/admin/order_item/index.html.heex +++ b/lib/lunchbot_web/templates/admin/order_item/index.html.heex @@ -23,6 +23,12 @@ <%= number_filter_select(:order_item, :item_id, @conn.params) %> <%= filter_number_input(:order_item, :item_id, @conn.params) %>
    + +
    + + <%= number_filter_select(:order_item, :rating, @conn.params) %> + <%= filter_number_input(:order_item, :rating, @conn.params) %> +
    <%= link "Clear Filters", to: Routes.admin_order_item_path(@conn, :index) %> @@ -39,6 +45,8 @@ <%= table_link(@conn, "Order", :order_id) %> <%= table_link(@conn, "Item", :item_id) %> + + <%= table_link(@conn, "Rating", :rating) %> Actions @@ -50,6 +58,8 @@ <%= order_item.order_id %> <%= order_item.item_id %> + + <%= order_item.rating %> <%= link "Show", to: Routes.admin_order_item_path(@conn, :show, order_item) %> diff --git a/lib/lunchbot_web/templates/admin/order_item/show.html.heex b/lib/lunchbot_web/templates/admin/order_item/show.html.heex index 11c1e94..c657547 100644 --- a/lib/lunchbot_web/templates/admin/order_item/show.html.heex +++ b/lib/lunchbot_web/templates/admin/order_item/show.html.heex @@ -21,6 +21,11 @@
    Item:
    <%= @order_item.item_id %>
    + +
    +
    Rating:
    +
    <%= @order_item.rating %>
    +
    diff --git a/priv/repo/migrations/20220808164936_add_rating_col_to_order_item.exs b/priv/repo/migrations/20220808164936_add_rating_col_to_order_item.exs new file mode 100644 index 0000000..bde6931 --- /dev/null +++ b/priv/repo/migrations/20220808164936_add_rating_col_to_order_item.exs @@ -0,0 +1,9 @@ +defmodule Lunchbot.Repo.Migrations.AddRatingColToOrderItem do + use Ecto.Migration + + def change do + alter table(:order_items) do + add :rating, :integer + end + end +end