Browse Source
* Refactoring ViewModels into Razor Pages models * Cleaning up Basket viewcomponent * Refactoring services. Fixed bug in basket item counter.main
committed by
GitHub
28 changed files with 215 additions and 227 deletions
@ -1,12 +1,11 @@ |
|||||
using Microsoft.eShopWeb.RazorPages.ViewModels; |
using System.Collections.Generic; |
||||
using System.Collections.Generic; |
|
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
|
|
||||
namespace Microsoft.eShopWeb.RazorPages.Interfaces |
namespace ApplicationCore.Interfaces |
||||
{ |
{ |
||||
public interface IBasketService |
public interface IBasketService |
||||
{ |
{ |
||||
Task<BasketViewModel> GetOrCreateBasketForUser(string userName); |
Task<int> GetBasketItemCountAsync(string userName); |
||||
Task TransferBasketAsync(string anonymousId, string userName); |
Task TransferBasketAsync(string anonymousId, string userName); |
||||
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity); |
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity); |
||||
Task SetQuantities(int basketId, Dictionary<string, int> quantities); |
Task SetQuantities(int basketId, Dictionary<string, int> quantities); |
||||
@ -1,15 +1,10 @@ |
|||||
using Microsoft.eShopWeb.ViewModels; |
using Microsoft.eShopWeb.ViewModels; |
||||
using System.Collections.Generic; |
|
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
|
|
||||
namespace Microsoft.eShopWeb.Interfaces |
namespace Microsoft.eShopWeb.Interfaces |
||||
{ |
{ |
||||
public interface IBasketService |
public interface IBasketViewModelService |
||||
{ |
{ |
||||
Task<BasketViewModel> GetOrCreateBasketForUser(string userName); |
Task<BasketViewModel> GetOrCreateBasketForUser(string userName); |
||||
Task TransferBasketAsync(string anonymousId, string userName); |
|
||||
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity); |
|
||||
Task SetQuantities(int basketId, Dictionary<string, int> quantities); |
|
||||
Task DeleteBasketAsync(int basketId); |
|
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,76 @@ |
|||||
|
using ApplicationCore.Interfaces; |
||||
|
using ApplicationCore.Specifications; |
||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities; |
||||
|
using Microsoft.eShopWeb.Interfaces; |
||||
|
using Microsoft.eShopWeb.ViewModels; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Microsoft.eShopWeb.Services |
||||
|
{ |
||||
|
public class BasketViewModelService : IBasketViewModelService |
||||
|
{ |
||||
|
private readonly IAsyncRepository<Basket> _basketRepository; |
||||
|
private readonly IUriComposer _uriComposer; |
||||
|
private readonly IRepository<CatalogItem> _itemRepository; |
||||
|
|
||||
|
public BasketViewModelService(IAsyncRepository<Basket> basketRepository, |
||||
|
IRepository<CatalogItem> itemRepository, |
||||
|
IUriComposer uriComposer) |
||||
|
{ |
||||
|
_basketRepository = basketRepository; |
||||
|
_uriComposer = uriComposer; |
||||
|
_itemRepository = itemRepository; |
||||
|
} |
||||
|
|
||||
|
public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName) |
||||
|
{ |
||||
|
var basketSpec = new BasketWithItemsSpecification(userName); |
||||
|
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault(); |
||||
|
|
||||
|
if(basket == null) |
||||
|
{ |
||||
|
return await CreateBasketForUser(userName); |
||||
|
} |
||||
|
return CreateViewModelFromBasket(basket); |
||||
|
} |
||||
|
|
||||
|
private BasketViewModel CreateViewModelFromBasket(Basket basket) |
||||
|
{ |
||||
|
var viewModel = new BasketViewModel(); |
||||
|
viewModel.Id = basket.Id; |
||||
|
viewModel.BuyerId = basket.BuyerId; |
||||
|
viewModel.Items = basket.Items.Select(i => |
||||
|
{ |
||||
|
var itemModel = new BasketItemViewModel() |
||||
|
{ |
||||
|
Id = i.Id, |
||||
|
UnitPrice = i.UnitPrice, |
||||
|
Quantity = i.Quantity, |
||||
|
CatalogItemId = i.CatalogItemId |
||||
|
|
||||
|
}; |
||||
|
var item = _itemRepository.GetById(i.CatalogItemId); |
||||
|
itemModel.PictureUrl = _uriComposer.ComposePicUri(item.PictureUri); |
||||
|
itemModel.ProductName = item.Name; |
||||
|
return itemModel; |
||||
|
}) |
||||
|
.ToList(); |
||||
|
return viewModel; |
||||
|
} |
||||
|
|
||||
|
private async Task<BasketViewModel> CreateBasketForUser(string userId) |
||||
|
{ |
||||
|
var basket = new Basket() { BuyerId = userId }; |
||||
|
await _basketRepository.AddAsync(basket); |
||||
|
|
||||
|
return new BasketViewModel() |
||||
|
{ |
||||
|
BuyerId = basket.BuyerId, |
||||
|
Id = basket.Id, |
||||
|
Items = new List<BasketItemViewModel>() |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using Microsoft.eShopWeb.RazorPages.ViewModels; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Microsoft.eShopWeb.RazorPages.Interfaces |
||||
|
{ |
||||
|
public interface IBasketViewModelService |
||||
|
{ |
||||
|
Task<BasketViewModel> GetOrCreateBasketForUser(string userName); |
||||
|
} |
||||
|
} |
||||
@ -1,7 +0,0 @@ |
|||||
namespace Microsoft.eShopWeb.RazorPages.ViewModels |
|
||||
{ |
|
||||
public class BasketComponentViewModel |
|
||||
{ |
|
||||
public int ItemsCount { get; set; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,18 +0,0 @@ |
|||||
namespace Microsoft.eShopWeb.RazorPages.ViewModels |
|
||||
{ |
|
||||
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; } |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,18 +0,0 @@ |
|||||
using ApplicationCore.Entities.OrderAggregate; |
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
|
|
||||
namespace Microsoft.eShopWeb.RazorPages.ViewModels |
|
||||
{ |
|
||||
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>(); |
|
||||
} |
|
||||
} |
|
||||
@ -1,24 +0,0 @@ |
|||||
using System; |
|
||||
using System.ComponentModel.DataAnnotations; |
|
||||
|
|
||||
namespace Microsoft.eShopWeb.RazorPages.ViewModels |
|
||||
{ |
|
||||
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; } |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue