Refactoring Services (#61)
* Refactoring ViewModels into Razor Pages models * Cleaning up Basket viewcomponent * Refactoring services. Fixed bug in basket item counter.
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
{
|
||||
@@ -23,6 +24,24 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
[BindProperty]
|
||||
public RegisterViewModel UserDetails { get; set; }
|
||||
|
||||
public class RegisterViewModel
|
||||
{
|
||||
[Required]
|
||||
[EmailAddress]
|
||||
[Display(Name = "Email")]
|
||||
public string Email { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
|
||||
[DataType(DataType.Password)]
|
||||
[Display(Name = "Password")]
|
||||
public string Password { get; set; }
|
||||
|
||||
[DataType(DataType.Password)]
|
||||
[Display(Name = "Confirm password")]
|
||||
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
|
||||
public string ConfirmPassword { get; set; }
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPost(string returnUrl = "/Index")
|
||||
{
|
||||
@@ -38,7 +57,6 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
AddErrors(result);
|
||||
}
|
||||
return Page();
|
||||
|
||||
}
|
||||
|
||||
private void AddErrors(IdentityResult result)
|
||||
@@ -48,6 +66,5 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
ModelState.AddModelError("", error.Description);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using Microsoft.eShopWeb.RazorPages.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using ApplicationCore.Interfaces;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
{
|
||||
|
||||
@@ -22,8 +22,10 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Basket
|
||||
private readonly IAppLogger<IndexModel> _logger;
|
||||
private readonly IOrderService _orderService;
|
||||
private string _username = null;
|
||||
private readonly IBasketViewModelService _basketViewModelService;
|
||||
|
||||
public IndexModel(IBasketService basketService,
|
||||
IBasketViewModelService basketViewModelService,
|
||||
IUriComposer uriComposer,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
IAppLogger<IndexModel> logger,
|
||||
@@ -34,6 +36,7 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Basket
|
||||
_signInManager = signInManager;
|
||||
_logger = logger;
|
||||
_orderService = orderService;
|
||||
_basketViewModelService = basketViewModelService;
|
||||
}
|
||||
|
||||
public BasketViewModel BasketModel { get; set; } = new BasketViewModel();
|
||||
@@ -83,12 +86,12 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Basket
|
||||
{
|
||||
if (_signInManager.IsSignedIn(HttpContext.User))
|
||||
{
|
||||
BasketModel = await _basketService.GetOrCreateBasketForUser(User.Identity.Name);
|
||||
BasketModel = await _basketViewModelService.GetOrCreateBasketForUser(User.Identity.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
GetOrSetBasketCookieAndUserName();
|
||||
BasketModel = await _basketService.GetOrCreateBasketForUser(_username);
|
||||
BasketModel = await _basketViewModelService.GetOrCreateBasketForUser(_username);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
@model BasketComponentViewModel
|
||||
@using Microsoft.eShopWeb.RazorPages.ViewComponents
|
||||
@model Basket.BasketComponentViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "My Basket";
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using ApplicationCore.Interfaces;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using ApplicationCore.Entities.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Order
|
||||
{
|
||||
@@ -17,6 +19,27 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Order
|
||||
|
||||
public OrderViewModel OrderDetails { get; set; } = new OrderViewModel();
|
||||
|
||||
public class OrderViewModel
|
||||
{
|
||||
public int OrderNumber { get; set; }
|
||||
public DateTimeOffset OrderDate { get; set; }
|
||||
public decimal Total { get; set; }
|
||||
public string Status { get; set; }
|
||||
|
||||
public Address ShippingAddress { get; set; }
|
||||
|
||||
public List<OrderItemViewModel> OrderItems { get; set; } = new List<OrderItemViewModel>();
|
||||
}
|
||||
|
||||
public class OrderItemViewModel
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
public string ProductName { get; set; }
|
||||
public decimal UnitPrice { get; set; }
|
||||
public decimal Discount { get; set; }
|
||||
public int Units { get; set; }
|
||||
public string PictureUrl { get; set; }
|
||||
}
|
||||
|
||||
public async Task OnGet(int orderId)
|
||||
{
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using ApplicationCore.Interfaces;
|
||||
using ApplicationCore.Specifications;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Order
|
||||
{
|
||||
@@ -17,28 +17,25 @@ namespace Microsoft.eShopWeb.RazorPages.Pages.Order
|
||||
_orderRepository = orderRepository;
|
||||
}
|
||||
|
||||
public List<OrderViewModel> Orders { get; set; } = new List<OrderViewModel>();
|
||||
public List<OrderSummary> Orders { get; set; } = new List<OrderSummary>();
|
||||
|
||||
public class OrderSummary
|
||||
{
|
||||
public int OrderNumber { get; set; }
|
||||
public DateTimeOffset OrderDate { get; set; }
|
||||
public decimal Total { get; set; }
|
||||
public string Status { get; set; }
|
||||
}
|
||||
|
||||
public async Task OnGet()
|
||||
{
|
||||
var orders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name));
|
||||
|
||||
Orders = orders
|
||||
.Select(o => new OrderViewModel()
|
||||
.Select(o => new OrderSummary()
|
||||
{
|
||||
OrderDate = o.OrderDate,
|
||||
OrderItems = o.OrderItems?.Select(oi => new OrderItemViewModel()
|
||||
{
|
||||
Discount = 0,
|
||||
PictureUrl = oi.ItemOrdered.PictureUri,
|
||||
ProductId = oi.ItemOrdered.CatalogItemId,
|
||||
ProductName = oi.ItemOrdered.ProductName,
|
||||
UnitPrice = oi.UnitPrice,
|
||||
Units = oi.Units
|
||||
}).ToList(),
|
||||
OrderNumber = o.Id,
|
||||
ShippingAddress = o.ShipToAddress,
|
||||
Status = "Pending",
|
||||
Total = o.Total()
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
</section>
|
||||
|
||||
<section class="col-lg-1 col-xs-12">
|
||||
@await Component.InvokeAsync("Basket", User.Identity.Name)
|
||||
@await Component.InvokeAsync("Basket")
|
||||
</section>
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user