From e94f9ad850c147e6ff340df26ba208e44c2dd399 Mon Sep 17 00:00:00 2001
From: Cybersepp <georg.sumailov@ut.ee>
Date: Tue, 6 Dec 2022 01:30:45 +0200
Subject: [PATCH] [#51]: customer order history

---
 .../customer_history_controller.ex            | 26 +++++++++++++++++++
 lib/volt_web/router.ex                        |  1 +
 .../restaurant_menu.html.heex                 |  2 +-
 .../customer_history/index.html.heex          | 25 ++++++++++++++++++
 .../templates/layout/_customer_menu.html.heex |  1 +
 lib/volt_web/views/customer_history_view.ex   |  3 +++
 6 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 lib/volt_web/controllers/customer_history_controller.ex
 create mode 100644 lib/volt_web/templates/customer_history/index.html.heex
 create mode 100644 lib/volt_web/views/customer_history_view.ex

diff --git a/lib/volt_web/controllers/customer_history_controller.ex b/lib/volt_web/controllers/customer_history_controller.ex
new file mode 100644
index 0000000..c3149d0
--- /dev/null
+++ b/lib/volt_web/controllers/customer_history_controller.ex
@@ -0,0 +1,26 @@
+defmodule VoltWeb.CustomerHistoryController do
+  use VoltWeb, :controller
+  import Ecto.Query, only: [from: 2]
+  import Ecto.Changeset
+  alias Volt.Sales.Basket
+  alias Volt.{Repo, Accounts}
+  alias VoltWeb.RestaurantAuth
+  alias Volt.Sales.Order
+  alias Volt.Sales
+  alias Volt.Accounts.Customer
+  alias Volt.Accounts
+  alias Volt.Sales.{Order}
+
+  require Logger
+
+
+  def index(conn, _params) do
+    usr = conn.assigns.current_customer
+    orders =
+      from(order in Order, where: order.customer_id == ^usr.id and order.order_status == "DELIVERED")
+      |> Repo.all()
+      |> Repo.preload([:restaurant, :customer, :courier])
+
+    render(conn, "index.html", orders: orders)
+  end
+end
diff --git a/lib/volt_web/router.ex b/lib/volt_web/router.ex
index c2165f3..6e2b157 100644
--- a/lib/volt_web/router.ex
+++ b/lib/volt_web/router.ex
@@ -190,6 +190,7 @@ defmodule VoltWeb.Router do
     put "/customers/dashboard", CustomerDashboardController, :update
     get "/customers/deposit", CustomerBalanceController, :index
     put "/customers/deposit", CustomerBalanceController, :submit
+    get "/customers/history", CustomerHistoryController, :index
   end
 
   scope "/", VoltWeb do
diff --git a/lib/volt_web/templates/customer_dashboard/restaurant_menu.html.heex b/lib/volt_web/templates/customer_dashboard/restaurant_menu.html.heex
index 6999e6d..2e1e7d0 100644
--- a/lib/volt_web/templates/customer_dashboard/restaurant_menu.html.heex
+++ b/lib/volt_web/templates/customer_dashboard/restaurant_menu.html.heex
@@ -3,7 +3,7 @@
 <h2> Opens from
   <time><%= @restaurant.opening_time %></time>
     • Closes at
-  <time><%= @restaurant.opening_time %></time></h2>
+  <time><%= @restaurant.closing_time %></time></h2>
 </div>
 
 <h1>Menu</h1>
diff --git a/lib/volt_web/templates/customer_history/index.html.heex b/lib/volt_web/templates/customer_history/index.html.heex
new file mode 100644
index 0000000..7b8c1a9
--- /dev/null
+++ b/lib/volt_web/templates/customer_history/index.html.heex
@@ -0,0 +1,25 @@
+<h1>Your order history! ًں“‌</h1>
+
+<table>
+  <thead>
+    <tr>
+      <th>Restaurant name</th>
+      <th>Order status</th>
+      <th>Total price</th>
+      <th>Order prepared</th>
+      <th>Order delivered</th>
+      <th></th>
+    </tr>
+  </thead>
+  <tbody>
+<%= for order <- @orders do %>
+    <tr>
+      <td><%= order.restaurant.name %></td>
+      <td><%= order.order_status %></td>
+      <td><%=  order.total_price %> €</td>
+      <td><%= order.preparation_time %></td>
+      <td><%= order.delivery_time %></td>
+    </tr>
+<% end %>
+  </tbody>
+</table>
diff --git a/lib/volt_web/templates/layout/_customer_menu.html.heex b/lib/volt_web/templates/layout/_customer_menu.html.heex
index 3545204..9bb4cce 100644
--- a/lib/volt_web/templates/layout/_customer_menu.html.heex
+++ b/lib/volt_web/templates/layout/_customer_menu.html.heex
@@ -3,6 +3,7 @@
   <h3>Greetings, <%= @current_customer.last_name%>!</h3>
   <h5>Your account balance: <%= @current_customer.balance %> €</h5>
   <li><%= link "Add money", to: Routes.customer_balance_path(@conn, :index), method: :get, id: "deposit_button" %> </li>
+  <li><%= link "Order history", to: Routes.customer_history_path(@conn, :index), method: :get, id: "history_button" %> </li>
   <li><%= link "Settings", to: Routes.customer_settings_path(@conn, :edit) %></li>
   <li><%= link "Log out", to: Routes.customer_session_path(@conn, :delete), method: :delete %></li>
 <% end %>
diff --git a/lib/volt_web/views/customer_history_view.ex b/lib/volt_web/views/customer_history_view.ex
new file mode 100644
index 0000000..aa7e5f0
--- /dev/null
+++ b/lib/volt_web/views/customer_history_view.ex
@@ -0,0 +1,3 @@
+defmodule VoltWeb.CustomerHistoryView do
+  use VoltWeb, :view
+end
-- 
GitLab