|
|
@ -15,9 +15,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints; |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Creates a new Catalog Item
|
|
|
/// Creates a new Catalog Item
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class CreateCatalogItemEndpoint : IEndpoint<IResult, CreateCatalogItemRequest> |
|
|
public class CreateCatalogItemEndpoint : IEndpoint<IResult, CreateCatalogItemRequest, IRepository<CatalogItem>> |
|
|
{ |
|
|
{ |
|
|
private IRepository<CatalogItem> _itemRepository; |
|
|
|
|
|
private readonly IUriComposer _uriComposer; |
|
|
private readonly IUriComposer _uriComposer; |
|
|
|
|
|
|
|
|
public CreateCatalogItemEndpoint(IUriComposer uriComposer) |
|
|
public CreateCatalogItemEndpoint(IUriComposer uriComposer) |
|
|
@ -31,26 +30,25 @@ public class CreateCatalogItemEndpoint : IEndpoint<IResult, CreateCatalogItemReq |
|
|
[Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async |
|
|
[Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async |
|
|
(CreateCatalogItemRequest request, IRepository<CatalogItem> itemRepository) => |
|
|
(CreateCatalogItemRequest request, IRepository<CatalogItem> itemRepository) => |
|
|
{ |
|
|
{ |
|
|
_itemRepository = itemRepository; |
|
|
return await HandleAsync(request, itemRepository); |
|
|
return await HandleAsync(request); |
|
|
|
|
|
}) |
|
|
}) |
|
|
.Produces<CreateCatalogItemResponse>() |
|
|
.Produces<CreateCatalogItemResponse>() |
|
|
.WithTags("CatalogItemEndpoints"); |
|
|
.WithTags("CatalogItemEndpoints"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task<IResult> HandleAsync(CreateCatalogItemRequest request) |
|
|
public async Task<IResult> HandleAsync(CreateCatalogItemRequest request, IRepository<CatalogItem> itemRepository) |
|
|
{ |
|
|
{ |
|
|
var response = new CreateCatalogItemResponse(request.CorrelationId()); |
|
|
var response = new CreateCatalogItemResponse(request.CorrelationId()); |
|
|
|
|
|
|
|
|
var catalogItemNameSpecification = new CatalogItemNameSpecification(request.Name); |
|
|
var catalogItemNameSpecification = new CatalogItemNameSpecification(request.Name); |
|
|
var existingCataloogItem = await _itemRepository.CountAsync(catalogItemNameSpecification); |
|
|
var existingCataloogItem = await itemRepository.CountAsync(catalogItemNameSpecification); |
|
|
if (existingCataloogItem > 0) |
|
|
if (existingCataloogItem > 0) |
|
|
{ |
|
|
{ |
|
|
throw new DuplicateException($"A catalogItem with name {request.Name} already exists"); |
|
|
throw new DuplicateException($"A catalogItem with name {request.Name} already exists"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri); |
|
|
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); |
|
|
|
|
|
|
|
|
if (newItem.Id != 0) |
|
|
if (newItem.Id != 0) |
|
|
{ |
|
|
{ |
|
|
@ -59,7 +57,7 @@ public class CreateCatalogItemEndpoint : IEndpoint<IResult, CreateCatalogItemReq |
|
|
// In production, we recommend uploading to a blob storage and deliver the image via CDN after a verification process.
|
|
|
// In production, we recommend uploading to a blob storage and deliver the image via CDN after a verification process.
|
|
|
|
|
|
|
|
|
newItem.UpdatePictureUri("eCatalog-item-default.png"); |
|
|
newItem.UpdatePictureUri("eCatalog-item-default.png"); |
|
|
await _itemRepository.UpdateAsync(newItem); |
|
|
await itemRepository.UpdateAsync(newItem); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var dto = new CatalogItemDto |
|
|
var dto = new CatalogItemDto |
|
|
|