diff --git a/src/PublicApi/CatalogItemEndpoints/Create.CreateCatalogItemRequest.cs b/src/PublicApi/CatalogItemEndpoints/Create.CreateCatalogItemRequest.cs index 3a8e9a4..e3193d8 100644 --- a/src/PublicApi/CatalogItemEndpoints/Create.CreateCatalogItemRequest.cs +++ b/src/PublicApi/CatalogItemEndpoints/Create.CreateCatalogItemRequest.cs @@ -9,4 +9,5 @@ public string PictureUri { get; set; } public decimal Price { get; set; } } + } diff --git a/src/PublicApi/CatalogItemEndpoints/Create.cs b/src/PublicApi/CatalogItemEndpoints/Create.cs index f7a44af..27ce2ba 100644 --- a/src/PublicApi/CatalogItemEndpoints/Create.cs +++ b/src/PublicApi/CatalogItemEndpoints/Create.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints { + [Authorize(Roles = AuthorizationConstants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class Create : BaseAsyncEndpoint { diff --git a/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs b/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs new file mode 100644 index 0000000..c04de4a --- /dev/null +++ b/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs @@ -0,0 +1,14 @@ +namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints +{ + public class UpdateCatalogItemRequest : BaseRequest + { + public int Id { get; set; } + public int CatalogBrandId { get; set; } + public int CatalogTypeId { get; set; } + public string Description { get; set; } + public string Name { get; set; } + public string PictureUri { get; set; } + public decimal Price { get; set; } + } + +} diff --git a/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemResponse.cs b/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemResponse.cs new file mode 100644 index 0000000..12913b5 --- /dev/null +++ b/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemResponse.cs @@ -0,0 +1,17 @@ +using System; + +namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints +{ + public class UpdateCatalogItemResponse : BaseResponse + { + public UpdateCatalogItemResponse(Guid correlationId) : base(correlationId) + { + } + + public UpdateCatalogItemResponse() + { + } + + public CatalogItemDto CatalogItem { get; set; } + } +} diff --git a/src/PublicApi/CatalogItemEndpoints/Update.cs b/src/PublicApi/CatalogItemEndpoints/Update.cs new file mode 100644 index 0000000..9baa9ae --- /dev/null +++ b/src/PublicApi/CatalogItemEndpoints/Update.cs @@ -0,0 +1,54 @@ +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.Tasks; + +namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints +{ + [Authorize(Roles = AuthorizationConstants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + public class Update : BaseAsyncEndpoint + { + private readonly IAsyncRepository _itemRepository; + + public Update(IAsyncRepository itemRepository) + { + _itemRepository = itemRepository; + } + + [HttpPut("api/catalog-items")] + [SwaggerOperation( + Summary = "Updates a Catalog Item", + Description = "Updates a Catalog Item", + OperationId = "catalog-items.update", + Tags = new[] { "CatalogItemEndpoints" }) + ] + public override async Task> HandleAsync(UpdateCatalogItemRequest request) + { + var response = new UpdateCatalogItemResponse(request.CorrelationId()); + + var existingItem = await _itemRepository.GetByIdAsync(request.Id); + + existingItem.Update(request.Name, request.Price); + + await _itemRepository.UpdateAsync(existingItem); + + var dto = new CatalogItemDto + { + Id = existingItem.Id, + CatalogBrandId = existingItem.CatalogBrandId, + CatalogTypeId = existingItem.CatalogTypeId, + Description = existingItem.Description, + Name = existingItem.Name, + PictureUri = existingItem.PictureUri, + Price = existingItem.Price + }; + response.CatalogItem = dto; + return response; + } + } +}