feat: add Customer area
All checks were successful
continuous-integration/drone/push Build is passing

* controller
* views
* SessionExtensions class
This commit is contained in:
surtur 2021-02-10 12:01:59 +01:00
parent 9bed70352e
commit 4b42658671
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
8 changed files with 300 additions and 0 deletions

View File

@ -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<OrderItem> orderItems = HttpContext.Session.GetObject<List<OrderItem>>(s_order_items);
OrderItem orderItemInSession = null;
if (orderItems != null)
orderItemInSession = orderItems.Find(oi => oi.Product_id == order_item.Product_id);
else
orderItems = new List<OrderItem>();
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<IActionResult> ApproveOrderInSession()
{
if (HttpContext.Session.IsAvailable)
{
int price_total = 0;
List<OrderItem> order_items = HttpContext.Session.GetObject<List<OrderItem>>(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 = "" });
}
}
}

View File

@ -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<IActionResult> Index()
{
if (User.Identity.IsAuthenticated)
{
User currentUser = await isas.gimme_current_user(User);
if (currentUser != null)
{
IList<Order> 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();
}
}
}

View File

@ -0,0 +1,76 @@
@model IList<Order>;
@{
ViewData["Title"] = "My Orders";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>
<br />
@{
if (Model != null && Model != null && Model.Count > 0)
{
foreach (var item in Model)
{
<table style="width:100%" class="table table-responsive table-striped table-bordered">
<tr>
<th class="col-sm-1">@nameof(Order.id)</th>
<th class="col-sm-2">@nameof(Order.Order_Number)</th>
<th class="col-sm-2">@nameof(Order.Price_total)</th>
<th class="col-sm-2">@nameof(Order.Created)</th>
<th class="col-sm-2">@nameof(Order.Updated)</th>
<th class="col-sm-3">@nameof(Order.usr.UserName)</th>
</tr>
<tr>
<td class="col-sm-1">@item.id</td>
<td class="col-sm-2">@item.Order_Number</td>
<td class="col-sm-2">@item.Price_total</td>
<td class="col-sm-2">@item.Created</td>
<td class="col-sm-2">@item.Updated</td>
<td class="col-sm-3">@item.usr.UserName</td>
</tr>
</table>
<button id="order_items_button_@item.id" class="btn-group btn-link" onclick="ViewHideDetails( 'order_items_' + @item.id, 'order_items_button_' + @item.id)">View Details</button>
<div id="order_items_@item.id" style="display:none">
<h4>Order Items</h4>
<table style="width:41.667%" class="table table-responsive table-bordered">
<tr>
<th class="col-sm-3">@nameof(Product.Name)</th>
<th class="col-sm-1">@nameof(OrderItem.Amount)</th>
<th class="col-sm-1">@nameof(OrderItem.Price)</th>
</tr>
@{
foreach (var order_items_item in item.OrderItems)
{
<tr>
<td class="col-sm-3">@order_items_item.Product.Name</td>
<td class="col-sm-1">@order_items_item.Amount</td>
<td class="col-sm-1">@order_items_item.Price</td>
</tr>
}
}
</table>
</div>
<br />
<br />
<br />
}
}
else
{
<h2>Orders are empty!</h2>
}
}
@section Scripts {
<environment include="Development">
<script defer src="~/js/Details.js"></script>
</environment>
<environment exclude="Development">
<script defer src="~/js/Details.min.js"></script>
</environment>
}

View File

@ -0,0 +1,3 @@
@using pwt_0x01_ng
@using pwt_0x01_ng.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}

View File

@ -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<T>(this ISession session, string key)
{
var data = session.GetString(key);
if (data == null)
{
return default(T);
}
return JsonConvert.DeserializeObject<T>(data);
}
public static void SetObject(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
}

View File

@ -29,6 +29,7 @@
<Folder Include="Areas\Security\Data" />
<Folder Include="Areas\Security\Models" />
<Folder Include="Areas\Security\Views" />
<Folder Include="Areas\Customer\Views" />
<Folder Include="Migrations\pgsql" />
<Folder Include="Models\ApplicationServices" />
<Folder Include="Models\Database" />

16
wwwroot/js/Details.js Normal file
View File

@ -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";
}
}
}