diff --git a/MyShop/MyShop.Core/Contracts/IOrderService.cs b/MyShop/MyShop.Core/Contracts/IOrderService.cs index e6db503..d98847a 100644 --- a/MyShop/MyShop.Core/Contracts/IOrderService.cs +++ b/MyShop/MyShop.Core/Contracts/IOrderService.cs @@ -11,5 +11,8 @@ namespace MyShop.Core.Contracts public interface IOrderService { void CreateOrder(Order baseOrder, List basketItems); + List GetOrderList(); + Order GetOrder(string Id); + void UpdateOrder(Order updatedOrder); } } diff --git a/MyShop/MyShop.Services/OrderService.cs b/MyShop/MyShop.Services/OrderService.cs index 25c0a9d..6b43726 100644 --- a/MyShop/MyShop.Services/OrderService.cs +++ b/MyShop/MyShop.Services/OrderService.cs @@ -32,5 +32,18 @@ public void CreateOrder(Order baseOrder, List basketItems) orderContext.Insert(baseOrder); orderContext.Commit(); } + + public List GetOrderList() { + return orderContext.Collection().ToList(); + } + + public Order GetOrder(string Id) { + return orderContext.Find(Id); + } + + public void UpdateOrder(Order updatedOrder) { + orderContext.Update(updatedOrder); + orderContext.Commit(); + } } } diff --git a/MyShop/MyShop.WebUI.Tests/Controllers/BasketControllerTests.cs b/MyShop/MyShop.WebUI.Tests/Controllers/BasketControllerTests.cs index 15dfba9..91016c7 100644 --- a/MyShop/MyShop.WebUI.Tests/Controllers/BasketControllerTests.cs +++ b/MyShop/MyShop.WebUI.Tests/Controllers/BasketControllerTests.cs @@ -8,6 +8,7 @@ using MyShop.WebUI.Controllers; using System.Web.Mvc; using MyShop.Core.ViewModels; +using System.Security.Principal; namespace MyShop.WebUI.Tests.Controllers { @@ -21,13 +22,14 @@ public void CanAddBasketItem() IRepository baskets = new MockContext(); IRepository products = new MockContext(); IRepository orders = new MockContext(); + IRepository customers = new MockContext(); var httpContext = new MockHttpContext(); IBasketService basketService = new BasketService(products, baskets); IOrderService orderService = new OrderService(orders); - var controller = new BasketController(basketService, orderService); + var controller = new BasketController(basketService, orderService, customers); controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller); //Act @@ -48,6 +50,7 @@ public void CanGetSummaryViewModel() { IRepository baskets = new MockContext(); IRepository products = new MockContext(); IRepository orders = new MockContext(); + IRepository customers = new MockContext(); products.Insert(new Product() { Id = "1", Price = 10.00m }); products.Insert(new Product() { Id = "2", Price = 5.00m }); @@ -59,7 +62,7 @@ public void CanGetSummaryViewModel() { IBasketService basketService = new BasketService(products, baskets); IOrderService orderService = new OrderService(orders); - var controller = new BasketController(basketService, orderService); + var controller = new BasketController(basketService, orderService, customers); var httpContext = new MockHttpContext(); httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket") { Value = basket.Id }); @@ -77,6 +80,7 @@ public void CanGetSummaryViewModel() { [TestMethod] public void CanCheckoutAndCreateOrder() { + IRepository customers = new MockContext(); IRepository products = new MockContext(); products.Insert(new Product() { Id = "1", Price = 10.00m }); products.Insert(new Product() { Id = "2", Price = 5.00m }); @@ -93,8 +97,14 @@ public void CanCheckoutAndCreateOrder() { IRepository orders = new MockContext(); IOrderService orderService = new OrderService(orders); - var controller = new BasketController(basketService, orderService); + customers.Insert(new Customer() { Id = "1", Email = "brett.hargreaves@gmail.com", ZipCode = "90210" }); + + IPrincipal FakeUser = new GenericPrincipal(new GenericIdentity("brett.hargreaves@gmail.com", "Forms"), null); + + + var controller = new BasketController(basketService, orderService, customers); var httpContext = new MockHttpContext(); + httpContext.User = FakeUser; httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket") { Value = basket.Id diff --git a/MyShop/MyShop.WebUI.Tests/Mocks/MockHttpContext.cs b/MyShop/MyShop.WebUI.Tests/Mocks/MockHttpContext.cs index 6c0d3c2..694b847 100644 --- a/MyShop/MyShop.WebUI.Tests/Mocks/MockHttpContext.cs +++ b/MyShop/MyShop.WebUI.Tests/Mocks/MockHttpContext.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Principal; using System.Text; using System.Threading.Tasks; using System.Web; @@ -12,6 +13,7 @@ public class MockHttpContext : HttpContextBase private MockRequest request; private MockResponse response; private HttpCookieCollection cookies; + private IPrincipal FakeUser; public MockHttpContext() { cookies = new HttpCookieCollection(); @@ -19,6 +21,16 @@ public MockHttpContext() { this.response = new MockResponse(cookies); } + public override IPrincipal User { + get { + return this.FakeUser; + } + set { + this.FakeUser = value; + } + + } + public override HttpRequestBase Request { get { return request; diff --git a/MyShop/MyShop.WebUI/Controllers/AdminController.cs b/MyShop/MyShop.WebUI/Controllers/AdminController.cs new file mode 100644 index 0000000..012445c --- /dev/null +++ b/MyShop/MyShop.WebUI/Controllers/AdminController.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace MyShop.WebUI.Controllers +{ + [Authorize(Roles ="Admin")] + public class AdminController : Controller + { + // GET: Admin + public ActionResult Index() + { + return View(); + } + } +} \ No newline at end of file diff --git a/MyShop/MyShop.WebUI/Controllers/BasketController.cs b/MyShop/MyShop.WebUI/Controllers/BasketController.cs index db78e4b..69898ff 100644 --- a/MyShop/MyShop.WebUI/Controllers/BasketController.cs +++ b/MyShop/MyShop.WebUI/Controllers/BasketController.cs @@ -10,12 +10,14 @@ namespace MyShop.WebUI.Controllers { public class BasketController : Controller { + IRepository customers; IBasketService basketService; IOrderService orderService; - public BasketController(IBasketService BasketService, IOrderService OrderService) { + public BasketController(IBasketService BasketService, IOrderService OrderService, IRepository Customers) { this.basketService = BasketService; this.orderService = OrderService; + this.customers = Customers; } // GET: Basket2 public ActionResult Index() @@ -44,15 +46,38 @@ public PartialViewResult BasketSummary() { return PartialView(basketSummary); } + [Authorize] public ActionResult Checkout() { - return View(); + Customer customer = customers.Collection().FirstOrDefault(c => c.Email == User.Identity.Name); + + if (customer != null) + { + Order order = new Order() + { + Email = customer.Email, + City = customer.City, + State = customer.State, + Street = customer.Street, + FirstName = customer.FirstName, + Surname = customer.LastName, + ZipCode = customer.ZipCode + }; + + return View(order); + } + else { + return RedirectToAction("Error"); + } + } [HttpPost] + [Authorize] public ActionResult Checkout(Order order) { var basketItems = basketService.GetBasketItems(this.HttpContext); order.OrderStatus = "Order Created"; + order.Email = User.Identity.Name; //process payment diff --git a/MyShop/MyShop.WebUI/Controllers/ManageController.cs b/MyShop/MyShop.WebUI/Controllers/ManageController.cs index 76266fa..8d14d87 100644 --- a/MyShop/MyShop.WebUI/Controllers/ManageController.cs +++ b/MyShop/MyShop.WebUI/Controllers/ManageController.cs @@ -18,6 +18,7 @@ public class ManageController : Controller public ManageController() { + } public ApplicationSignInManager SignInManager diff --git a/MyShop/MyShop.WebUI/Controllers/OrderManagerController.cs b/MyShop/MyShop.WebUI/Controllers/OrderManagerController.cs new file mode 100644 index 0000000..42de4e9 --- /dev/null +++ b/MyShop/MyShop.WebUI/Controllers/OrderManagerController.cs @@ -0,0 +1,48 @@ +using MyShop.Core.Contracts; +using MyShop.Core.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace MyShop.WebUI.Controllers +{ + [Authorize(Roles = "Admin")] + public class OrderManagerController : Controller + { + IOrderService orderService; + + public OrderManagerController(IOrderService OrderService) { + this.orderService = OrderService; + } + // GET: OrderManager + public ActionResult Index() + { + List orders = orderService.GetOrderList(); + + return View(orders); + } + + public ActionResult UpdateOrder(string Id) { + ViewBag.StatusList = new List() { + "Order Created", + "Payment Processed", + "Order Shipped", + "Order Complete" + }; + Order order = orderService.GetOrder(Id); + return View(order); + } + + [HttpPost] + public ActionResult UpdateOrder(Order updatedOrder, string Id) { + Order order = orderService.GetOrder(Id); + + order.OrderStatus = updatedOrder.OrderStatus; + orderService.UpdateOrder(order); + + return RedirectToAction("Index"); + } + } +} \ No newline at end of file diff --git a/MyShop/MyShop.WebUI/Controllers/ProductCategoryManagerController.cs b/MyShop/MyShop.WebUI/Controllers/ProductCategoryManagerController.cs index bec7555..16eb0f7 100644 --- a/MyShop/MyShop.WebUI/Controllers/ProductCategoryManagerController.cs +++ b/MyShop/MyShop.WebUI/Controllers/ProductCategoryManagerController.cs @@ -9,6 +9,7 @@ namespace MyShop.WebUI.Controllers { + [Authorize(Roles = "Admin")] public class ProductCategoryManagerController : Controller { IRepository context; diff --git a/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs b/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs index e11c863..b3d1d80 100644 --- a/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs +++ b/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs @@ -11,6 +11,7 @@ namespace MyShop.WebUI.Controllers { + [Authorize(Roles = "Admin")] public class ProductManagerController : Controller { IRepository context; diff --git a/MyShop/MyShop.WebUI/MyShop.WebUI.csproj b/MyShop/MyShop.WebUI/MyShop.WebUI.csproj index 4013151..ffde975 100644 --- a/MyShop/MyShop.WebUI/MyShop.WebUI.csproj +++ b/MyShop/MyShop.WebUI/MyShop.WebUI.csproj @@ -208,9 +208,11 @@ + + @@ -306,10 +308,15 @@ + + + + + diff --git a/MyShop/MyShop.WebUI/Views/Admin/Index.cshtml b/MyShop/MyShop.WebUI/Views/Admin/Index.cshtml new file mode 100644 index 0000000..8138965 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/Admin/Index.cshtml @@ -0,0 +1,20 @@ +@{ + ViewBag.Title = "Admin"; +} + +

Admin Pages

+ +
+

Product Management

+
+ @Html.ActionLink("Product Categories", "Index", "ProductCategoryManager", null, new { @class="list-group-item" }) + @Html.ActionLink("Product", "Index", "ProductManager", null, new { @class = "list-group-item" }) +
+
+
+

Order Management

+
+ @Html.ActionLink("Orders", "Index", "OrderManager", null, new { @class = "list-group-item" }) +
+
+
diff --git a/MyShop/MyShop.WebUI/Views/Basket/Checkout.cshtml b/MyShop/MyShop.WebUI/Views/Basket/Checkout.cshtml index 175c806..68e90b9 100644 --- a/MyShop/MyShop.WebUI/Views/Basket/Checkout.cshtml +++ b/MyShop/MyShop.WebUI/Views/Basket/Checkout.cshtml @@ -26,14 +26,6 @@ -
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) -
-
-
@Html.LabelFor(model => model.Street, htmlAttributes: new { @class = "control-label col-md-2" })
diff --git a/MyShop/MyShop.WebUI/Views/OrderManager/Index.cshtml b/MyShop/MyShop.WebUI/Views/OrderManager/Index.cshtml new file mode 100644 index 0000000..66c0497 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/OrderManager/Index.cshtml @@ -0,0 +1,48 @@ +@model IEnumerable + + + + + + + + + + + + +@foreach (var item in Model) { + + + + + + + + + +} + +
+ @Html.DisplayNameFor(model => model.CreatedAt) + + @Html.DisplayNameFor(model => model.FirstName) + + @Html.DisplayNameFor(model => model.Surname) + + @Html.DisplayNameFor(model => model.Email) + + @Html.DisplayNameFor(model => model.OrderStatus) +
+ @Html.DisplayFor(modelItem => item.CreatedAt) + + @Html.DisplayFor(modelItem => item.FirstName) + + @Html.DisplayFor(modelItem => item.Surname) + + @Html.DisplayFor(modelItem => item.Email) + + @Html.DisplayFor(modelItem => item.OrderStatus) + + @Html.ActionLink("Manage Order", "UpdateOrder", new { id=item.Id }) +
diff --git a/MyShop/MyShop.WebUI/Views/OrderManager/OrderItems.cshtml b/MyShop/MyShop.WebUI/Views/OrderManager/OrderItems.cshtml new file mode 100644 index 0000000..76b0f4b --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/OrderManager/OrderItems.cshtml @@ -0,0 +1,45 @@ +@model IEnumerable + + + + + + + + + + +@foreach (var item in Model) { + + + + + + + +} + + + + + + +
+ @Html.DisplayNameFor(model => model.ProductName) + + @Html.DisplayNameFor(model => model.Price) + + @Html.DisplayNameFor(model => model.Quanity) + Line Order
+ @Html.DisplayFor(modelItem => item.ProductName) + + @Html.DisplayFor(modelItem => item.Price) + + @Html.DisplayFor(modelItem => item.Quanity) + + @String.Format("{0:c}", item.Price * item.Quanity) +
+ Order Total + + @String.Format("{0:c}", (from p in Model select p.Quanity * p.Price).Sum()) +
diff --git a/MyShop/MyShop.WebUI/Views/OrderManager/UpdateOrder.cshtml b/MyShop/MyShop.WebUI/Views/OrderManager/UpdateOrder.cshtml new file mode 100644 index 0000000..5ef5889 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/OrderManager/UpdateOrder.cshtml @@ -0,0 +1,91 @@ +@model MyShop.Core.Models.Order + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
+

Order

+
+ @Html.HiddenFor(model => model.Id) + +
+
+ @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DropDownListFor(model => model.OrderStatus, new SelectList(ViewBag.StatusList), new { @class = "form-control" }) +
+
+ +
+ @Html.LabelFor(model => model.CreatedAt, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.CreatedAt, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+ @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+ @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+ @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+
+
+ @Html.LabelFor(model => model.Street, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.Street, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+ @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+ @Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } }) +
+
+ +
+ @Html.LabelFor(model => model.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DisplayFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control" } }) +
+
+
+ +
+
+ +
+
+
+ + @Html.Partial("OrderItems", Model.OrderItems) +} + +
+ @Html.ActionLink("Back to List", "Index") +
diff --git a/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml b/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml index 7a5ce9c..f9ad8b0 100644 --- a/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml +++ b/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml @@ -22,8 +22,10 @@