Use cancellation token in repository and web fie system (http call can cancelled) (#471)

Co-authored-by: cmichel <cmichel@interparking.com>
This commit is contained in:
Cédric Michel
2020-11-30 18:21:19 +01:00
committed by GitHub
parent 2502e01efd
commit 6041a1f183
19 changed files with 95 additions and 97 deletions

View File

@@ -33,7 +33,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints
{
var response = new ListCatalogBrandsResponse();
var items = await _catalogBrandRepository.ListAllAsync();
var items = await _catalogBrandRepository.ListAllAsync(cancellationToken);
response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));

View File

@@ -1,12 +1,12 @@
using System.IO;
using System.Threading;
using Ardalis.ApiEndpoints;
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
@@ -39,15 +39,15 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
var newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri);
newItem = await _itemRepository.AddAsync(newItem);
newItem = await _itemRepository.AddAsync(newItem, cancellationToken);
if (newItem.Id != 0)
{
var picName = $"{newItem.Id}{Path.GetExtension(request.PictureName)}";
if (await _webFileSystem.SavePicture(picName, request.PictureBase64))
if (await _webFileSystem.SavePicture(picName, request.PictureBase64, cancellationToken))
{
newItem.UpdatePictureUri(picName);
await _itemRepository.UpdateAsync(newItem);
await _itemRepository.UpdateAsync(newItem, cancellationToken);
}
}
@@ -65,6 +65,6 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
return response;
}
}
}

View File

@@ -1,12 +1,11 @@
using System.Threading;
using Ardalis.ApiEndpoints;
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Constants;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
@@ -28,14 +27,14 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
OperationId = "catalog-items.Delete",
Tags = new[] { "CatalogItemEndpoints" })
]
public override async Task<ActionResult<DeleteCatalogItemResponse>> HandleAsync([FromRoute]DeleteCatalogItemRequest request, CancellationToken cancellationToken)
public override async Task<ActionResult<DeleteCatalogItemResponse>> HandleAsync([FromRoute] DeleteCatalogItemRequest request, CancellationToken cancellationToken)
{
var response = new DeleteCatalogItemResponse(request.CorrelationId());
var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId);
var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId, cancellationToken);
if (itemToDelete is null) return NotFound();
await _itemRepository.DeleteAsync(itemToDelete);
await _itemRepository.DeleteAsync(itemToDelete, cancellationToken);
return Ok(response);
}

View File

@@ -1,9 +1,9 @@
using System.Threading;
using Ardalis.ApiEndpoints;
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
@@ -30,7 +30,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
{
var response = new GetByIdCatalogItemResponse(request.CorrelationId());
var item = await _itemRepository.GetByIdAsync(request.CatalogItemId);
var item = await _itemRepository.GetByIdAsync(request.CatalogItemId, cancellationToken);
if (item is null) return NotFound();
response.CatalogItem = new CatalogItemDto

View File

@@ -34,12 +34,12 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
OperationId = "catalog-items.ListPaged",
Tags = new[] { "CatalogItemEndpoints" })
]
public override async Task<ActionResult<ListPagedCatalogItemResponse>> HandleAsync([FromQuery]ListPagedCatalogItemRequest request, CancellationToken cancellationToken)
public override async Task<ActionResult<ListPagedCatalogItemResponse>> HandleAsync([FromQuery] ListPagedCatalogItemRequest request, CancellationToken cancellationToken)
{
var response = new ListPagedCatalogItemResponse(request.CorrelationId());
var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId);
int totalItems = await _itemRepository.CountAsync(filterSpec);
int totalItems = await _itemRepository.CountAsync(filterSpec, cancellationToken);
var pagedSpec = new CatalogFilterPaginatedSpecification(
skip: request.PageIndex * request.PageSize,
@@ -47,7 +47,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
brandId: request.CatalogBrandId,
typeId: request.CatalogTypeId);
var items = await _itemRepository.ListAsync(pagedSpec);
var items = await _itemRepository.ListAsync(pagedSpec, cancellationToken);
response.CatalogItems.AddRange(items.Select(_mapper.Map<CatalogItemDto>));
foreach (CatalogItemDto item in response.CatalogItems)

View File

@@ -37,7 +37,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
{
var response = new UpdateCatalogItemResponse(request.CorrelationId());
var existingItem = await _itemRepository.GetByIdAsync(request.Id);
var existingItem = await _itemRepository.GetByIdAsync(request.Id, cancellationToken);
existingItem.UpdateDetails(request.Name, request.Description, request.Price);
existingItem.UpdateBrand(request.CatalogBrandId);
@@ -50,13 +50,13 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
else
{
var picName = $"{existingItem.Id}{Path.GetExtension(request.PictureName)}";
if (await _webFileSystem.SavePicture($"{picName}", request.PictureBase64))
if (await _webFileSystem.SavePicture($"{picName}", request.PictureBase64, cancellationToken))
{
existingItem.UpdatePictureUri(picName);
}
}
await _itemRepository.UpdateAsync(existingItem);
await _itemRepository.UpdateAsync(existingItem, cancellationToken);
var dto = new CatalogItemDto
{

View File

@@ -33,7 +33,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints
{
var response = new ListCatalogTypesResponse();
var items = await _catalogTypeRepository.ListAllAsync();
var items = await _catalogTypeRepository.ListAllAsync(cancellationToken);
response.CatalogTypes.AddRange(items.Select(_mapper.Map<CatalogTypeDto>));