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) %>
-
+
+ From <%= an_order.menu.name %> @ <%= an_order.inserted_at %>
+
+ <%= for order_item <- an_order.order_items do %>
+
<%= order_item.item.name %>
+ <%= if !Enum.empty?(order_item.order_item_options) do %>
+ (
+ <%= for order_item_option <- order_item.order_item_options do %>
+ <%= order_item_option.option.name %>
+ <% end %>
+ )
+ <% end %>
+ <%= if !is_nil(OrderItem.get_total(order_item)) do %>
+ for $<%= OrderItem.get_total(order_item) %>
+ <% end %>
+ <%= if Map.has_key?(assigns, :show_rating) do %>
+ <%= if !is_nil(order_item.rating) do %>
+ [Rating: <%= order_item.rating %>/5]
+ <%= if Map.has_key?(assigns, :show_rating_modifiers) do %>
+ (Edit Rating)
+ <% end %>
+ <% else %>
+ <%= if Map.has_key?(assigns, :show_rating_modifiers) do %>
+ (<%= link "Add Rating", to: "#TODO-ADD-RATING" %>)
+ <% end %>
+ <% end %>
+ <% end %>
+
<%= 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