diff --git a/Areas/Customer/Controllers/CustomerOrderNotCartController.cs b/Areas/Customer/Controllers/CustomerOrderNotCartController.cs new file mode 100644 index 0000000..e6784d6 --- /dev/null +++ b/Areas/Customer/Controllers/CustomerOrderNotCartController.cs @@ -0,0 +1,117 @@ +using pwt_0x01_ng.Controllers; +using pwt_0x01_ng.Models; +using pwt_0x01_ng.Models.ApplicationServices; +using pwt_0x01_ng.Models.Database; +using pwt_0x01_ng.Models.Identity; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace pwt_0x01_ng.Areas.Customer.Controllers +{ + [Area("Customer")] + [Authorize(Roles = nameof(Roles.Customer))] + public class CustomerOrderNotCartController : Controller + { + const string s_price_total = "Price_total"; + const string s_order_items = "OrderItems"; + + ISecurityApplicationService isas; + DBContext ctx; + public CustomerOrderNotCartController(ISecurityApplicationService isas, DBContext ctx) + { + this.isas = isas; + this.ctx = ctx; + } + + [HttpPost] + public int AddOrderItemsToSession(int? productId) + { + int price_total = 0; + if (HttpContext.Session.IsAvailable) + { + price_total = HttpContext.Session.GetInt32(s_price_total).GetValueOrDefault(); + } + + Product product = ctx.Product.Where(prod => prod.id == productId).FirstOrDefault(); + + if (product != null) + { + OrderItem order_item = new OrderItem() + { + Product_id = product.id, + Product = product, + Amount = 1, + Price = product.Price + }; + + if (HttpContext.Session.IsAvailable) + { + List orderItems = HttpContext.Session.GetObject>(s_order_items); + OrderItem orderItemInSession = null; + if (orderItems != null) + orderItemInSession = orderItems.Find(oi => oi.Product_id == order_item.Product_id); + else + orderItems = new List(); + + if (orderItemInSession != null) + { + ++orderItemInSession.Amount; + orderItemInSession.Price += order_item.Product.Price; + } + else + { + orderItems.Add(order_item); + } + + HttpContext.Session.SetObject(s_order_items, orderItems); + + price_total += order_item.Product.Price; + HttpContext.Session.SetInt32(s_price_total, price_total); + } + } + return price_total; + } + public async Task ApproveOrderInSession() + { + if (HttpContext.Session.IsAvailable) + { + + int price_total = 0; + List order_items = HttpContext.Session.GetObject>(s_order_items); + if (order_items != null) + { + foreach (OrderItem order_item in order_items) + { + price_total += order_item.Product.Price * order_item.Amount; + order_item.Product = null; + } + + User currentUser = await isas.gimme_current_user(User); + + Order order = new Order() + { + Order_Number = Convert.ToBase64String(Guid.NewGuid().ToByteArray()), + Price_total = price_total, + OrderItems = order_items, + User_id = currentUser.Id + }; + + await ctx.AddAsync(order); + await ctx.SaveChangesAsync(); + + HttpContext.Session.Remove(s_order_items); + HttpContext.Session.Remove(s_price_total); + + return RedirectToAction(nameof(CustomerOrdersController.Index), nameof(CustomerOrdersController).Replace("Controller", ""), new { Area = nameof(Customer) }); + } + } + + return RedirectToAction(nameof(HomeController.Index), nameof(HomeController).Replace("Controller", ""), new { Area = "" }); + } + } +} diff --git a/Areas/Customer/Controllers/CustomerOrdersController.cs b/Areas/Customer/Controllers/CustomerOrdersController.cs new file mode 100644 index 0000000..dcf129e --- /dev/null +++ b/Areas/Customer/Controllers/CustomerOrdersController.cs @@ -0,0 +1,48 @@ +using pwt_0x01_ng.Models; +using pwt_0x01_ng.Models.ApplicationServices; +using pwt_0x01_ng.Models.Database; +using pwt_0x01_ng.Models.Identity; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace pwt_0x01_ng.Areas.Customer.Controllers +{ + [Area("Customer")] + [Authorize(Roles = nameof(Roles.Customer))] + public class CustomerOrdersController : Controller + { + + ISecurityApplicationService isas; + DBContext ctx; + + public CustomerOrdersController(ISecurityApplicationService isas, DBContext ctx) + { + this.isas = isas; + this.ctx = ctx; + } + + public async Task Index() + { + if (User.Identity.IsAuthenticated) + { + User currentUser = await isas.gimme_current_user(User); + if (currentUser != null) + { + IList userOrders = await this.ctx.Order + .Where(or => or.User_id == currentUser.Id) + .Include(o => o.usr) + .Include(o => o.OrderItems) + .ThenInclude(oi => oi.Product) + .ToListAsync(); + return View(userOrders); + } + } + + return NotFound(); + } + } +} diff --git a/Areas/Customer/Views/CustomerOrders/Index.cshtml b/Areas/Customer/Views/CustomerOrders/Index.cshtml new file mode 100644 index 0000000..7c2cca0 --- /dev/null +++ b/Areas/Customer/Views/CustomerOrders/Index.cshtml @@ -0,0 +1,76 @@ +@model IList; +@{ + ViewData["Title"] = "My Orders"; +} +

@ViewData["Title"]

+

@ViewData["Message"]

+ +
+ +@{ + if (Model != null && Model != null && Model.Count > 0) + { + foreach (var item in Model) + { + + + + + + + + + + + + + + + + + + + +
@nameof(Order.id)@nameof(Order.Order_Number)@nameof(Order.Price_total)@nameof(Order.Created)@nameof(Order.Updated)@nameof(Order.usr.UserName)
@item.id@item.Order_Number@item.Price_total@item.Created@item.Updated@item.usr.UserName
+ + + +
+
+
+ } + } + else + { +

Orders are empty!

+ } +} + +@section Scripts { + + + + + + +} \ No newline at end of file diff --git a/Areas/Customer/Views/_ViewImports.cshtml b/Areas/Customer/Views/_ViewImports.cshtml new file mode 100644 index 0000000..ef71eae --- /dev/null +++ b/Areas/Customer/Views/_ViewImports.cshtml @@ -0,0 +1,3 @@ +@using pwt_0x01_ng +@using pwt_0x01_ng.Models +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Areas/Customer/Views/_ViewStart.cshtml b/Areas/Customer/Views/_ViewStart.cshtml new file mode 100644 index 0000000..e1b65b4 --- /dev/null +++ b/Areas/Customer/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "_Layout"; +} diff --git a/Models/SessionExtensions.cs b/Models/SessionExtensions.cs new file mode 100644 index 0000000..efdc2f5 --- /dev/null +++ b/Models/SessionExtensions.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; + +public static class SessionExtensions +{ + public static double? GetDouble(this ISession session, string key) + { + var data = session.Get(key); + if (data == null) + { + return null; + } + return BitConverter.ToDouble(data, 0); + } + + public static void SetDouble(this ISession session, string key, double value) + { + session.Set(key, BitConverter.GetBytes(value)); + } + + public static T GetObject(this ISession session, string key) + { + var data = session.GetString(key); + if (data == null) + { + return default(T); + } + return JsonConvert.DeserializeObject(data); + } + + public static void SetObject(this ISession session, string key, object value) + { + session.SetString(key, JsonConvert.SerializeObject(value)); + } +} diff --git a/pwt-0x01-ng.csproj b/pwt-0x01-ng.csproj index 3bf3702..a919a7e 100644 --- a/pwt-0x01-ng.csproj +++ b/pwt-0x01-ng.csproj @@ -29,6 +29,7 @@ + diff --git a/wwwroot/js/Details.js b/wwwroot/js/Details.js new file mode 100644 index 0000000..4b06623 --- /dev/null +++ b/wwwroot/js/Details.js @@ -0,0 +1,16 @@ +function ViewHideDetails(groupElementId, buttonElementId) { + + var orderItemsGroup = document.getElementById(groupElementId); + var orderItemsButton = document.getElementById(buttonElementId); + + if (orderItemsGroup && orderItemsButton) { + if (orderItemsGroup.style.display === "none") { + orderItemsGroup.style.display = "block"; + orderItemsButton.textContent = "Hide Details"; + } + else { + orderItemsGroup.style.display = "none"; + orderItemsButton.textContent = "View Details"; + } + } +} \ No newline at end of file