add FirstAsync in respository (#370)
* add FirstAsync in respository * use new FirstOrDefaultAsync() method from repository
This commit is contained in:
@@ -13,5 +13,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
|||||||
Task UpdateAsync(T entity);
|
Task UpdateAsync(T entity);
|
||||||
Task DeleteAsync(T entity);
|
Task DeleteAsync(T entity);
|
||||||
Task<int> CountAsync(ISpecification<T> spec);
|
Task<int> CountAsync(ISpecification<T> spec);
|
||||||
|
Task<T> FirstAsync(ISpecification<T> spec);
|
||||||
|
Task<T> FirstOrDefaultAsync(ISpecification<T> spec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
{
|
{
|
||||||
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
||||||
var basketSpec = new BasketWithItemsSpecification(userName);
|
var basketSpec = new BasketWithItemsSpecification(userName);
|
||||||
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();
|
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
|
||||||
if (basket == null)
|
if (basket == null)
|
||||||
{
|
{
|
||||||
_logger.LogInformation($"No basket found for {userName}");
|
_logger.LogInformation($"No basket found for {userName}");
|
||||||
@@ -72,7 +72,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
|
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
|
||||||
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
||||||
var basketSpec = new BasketWithItemsSpecification(anonymousId);
|
var basketSpec = new BasketWithItemsSpecification(anonymousId);
|
||||||
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();
|
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
|
||||||
if (basket == null) return;
|
if (basket == null) return;
|
||||||
basket.SetNewBuyerId(userName);
|
basket.SetNewBuyerId(userName);
|
||||||
await _basketRepository.UpdateAsync(basket);
|
await _basketRepository.UpdateAsync(basket);
|
||||||
|
|||||||
@@ -61,6 +61,16 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
|
|||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<T> FirstAsync(ISpecification<T> spec)
|
||||||
|
{
|
||||||
|
return await ApplySpecification(spec).FirstAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec)
|
||||||
|
{
|
||||||
|
return await ApplySpecification(spec).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
||||||
{
|
{
|
||||||
return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
|||||||
using Microsoft.eShopWeb.Web.Interfaces;
|
using Microsoft.eShopWeb.Web.Interfaces;
|
||||||
using Microsoft.eShopWeb.Web.Pages.Basket;
|
using Microsoft.eShopWeb.Web.Pages.Basket;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.Web.Services
|
namespace Microsoft.eShopWeb.Web.Services
|
||||||
@@ -28,7 +27,7 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName)
|
public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName)
|
||||||
{
|
{
|
||||||
var basketSpec = new BasketWithItemsSpecification(userName);
|
var basketSpec = new BasketWithItemsSpecification(userName);
|
||||||
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();
|
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
|
||||||
|
|
||||||
if (basket == null)
|
if (basket == null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user