From fdb4869c0ba37793c2c9cd5f28d8e0204e08ead8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Tue, 5 May 2020 16:23:57 +0200 Subject: [PATCH] add FirstAsync in respository (#370) * add FirstAsync in respository * use new FirstOrDefaultAsync() method from repository --- src/ApplicationCore/Interfaces/IAsyncRepository.cs | 2 ++ src/ApplicationCore/Services/BasketService.cs | 4 ++-- src/Infrastructure/Data/EfRepository.cs | 10 ++++++++++ src/Web/Services/BasketViewModelService.cs | 3 +-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ApplicationCore/Interfaces/IAsyncRepository.cs b/src/ApplicationCore/Interfaces/IAsyncRepository.cs index 77a67ab..e8033c2 100644 --- a/src/ApplicationCore/Interfaces/IAsyncRepository.cs +++ b/src/ApplicationCore/Interfaces/IAsyncRepository.cs @@ -13,5 +13,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces Task UpdateAsync(T entity); Task DeleteAsync(T entity); Task CountAsync(ISpecification spec); + Task FirstAsync(ISpecification spec); + Task FirstOrDefaultAsync(ISpecification spec); } } diff --git a/src/ApplicationCore/Services/BasketService.cs b/src/ApplicationCore/Services/BasketService.cs index f3f0dd8..979624a 100644 --- a/src/ApplicationCore/Services/BasketService.cs +++ b/src/ApplicationCore/Services/BasketService.cs @@ -39,7 +39,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services { Guard.Against.NullOrEmpty(userName, nameof(userName)); var basketSpec = new BasketWithItemsSpecification(userName); - var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault(); + var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec)); if (basket == null) { _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(userName, nameof(userName)); var basketSpec = new BasketWithItemsSpecification(anonymousId); - var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault(); + var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec)); if (basket == null) return; basket.SetNewBuyerId(userName); await _basketRepository.UpdateAsync(basket); diff --git a/src/Infrastructure/Data/EfRepository.cs b/src/Infrastructure/Data/EfRepository.cs index 830b89f..303019c 100644 --- a/src/Infrastructure/Data/EfRepository.cs +++ b/src/Infrastructure/Data/EfRepository.cs @@ -61,6 +61,16 @@ namespace Microsoft.eShopWeb.Infrastructure.Data await _dbContext.SaveChangesAsync(); } + public async Task FirstAsync(ISpecification spec) + { + return await ApplySpecification(spec).FirstAsync(); + } + + public async Task FirstOrDefaultAsync(ISpecification spec) + { + return await ApplySpecification(spec).FirstOrDefaultAsync(); + } + private IQueryable ApplySpecification(ISpecification spec) { return SpecificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), spec); diff --git a/src/Web/Services/BasketViewModelService.cs b/src/Web/Services/BasketViewModelService.cs index d0db78f..a23ed58 100644 --- a/src/Web/Services/BasketViewModelService.cs +++ b/src/Web/Services/BasketViewModelService.cs @@ -5,7 +5,6 @@ using Microsoft.eShopWeb.ApplicationCore.Specifications; using Microsoft.eShopWeb.Web.Interfaces; using Microsoft.eShopWeb.Web.Pages.Basket; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; namespace Microsoft.eShopWeb.Web.Services @@ -28,7 +27,7 @@ namespace Microsoft.eShopWeb.Web.Services public async Task GetOrCreateBasketForUser(string userName) { var basketSpec = new BasketWithItemsSpecification(userName); - var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault(); + var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec)); if (basket == null) {