Add catalogitem update endpoint to public api (#418)
* Initial update endpoint working * Updated CatalogItem to support more updates; added tests * Got MediatR domain events working to check for duplicate item names * Adding reference link * Remove domain events spike code * clean up usings
This commit is contained in:
@@ -9,4 +9,5 @@
|
||||
public string PictureUri { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<CreateCatalogItemRequest, CreateCatalogItemResponse>
|
||||
{
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class UpdateCatalogItemRequest : BaseRequest
|
||||
{
|
||||
[Range(1, 10000)]
|
||||
public int Id { get; set; }
|
||||
[Range(1, 10000)]
|
||||
public int CatalogBrandId { get; set; }
|
||||
[Range(1, 10000)]
|
||||
public int CatalogTypeId { get; set; }
|
||||
[Required]
|
||||
public string Description { get; set; }
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
public string PictureUri { get; set; }
|
||||
[Range(0.01, 10000)]
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
58
src/PublicApi/CatalogItemEndpoints/Update.cs
Normal file
58
src/PublicApi/CatalogItemEndpoints/Update.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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.Exceptions;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
[Authorize(Roles = AuthorizationConstants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
public class Update : BaseAsyncEndpoint<UpdateCatalogItemRequest, UpdateCatalogItemResponse>
|
||||
{
|
||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
||||
|
||||
public Update(IAsyncRepository<CatalogItem> 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<ActionResult<UpdateCatalogItemResponse>> HandleAsync(UpdateCatalogItemRequest request)
|
||||
{
|
||||
var response = new UpdateCatalogItemResponse(request.CorrelationId());
|
||||
|
||||
var existingItem = await _itemRepository.GetByIdAsync(request.Id);
|
||||
|
||||
existingItem.UpdateDetails(request.Name, request.Description, request.Price);
|
||||
existingItem.UpdateBrand(request.CatalogBrandId);
|
||||
existingItem.UpdateType(request.CatalogTypeId);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ardalis.ApiEndpoints" Version="1.0.0" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="MediatR" Version="8.0.2" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.0" />
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@@ -8,6 +11,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Microsoft.eShopWeb.Infrastructure.Data;
|
||||
@@ -119,9 +123,9 @@ namespace Microsoft.eShopWeb.PublicApi
|
||||
|
||||
|
||||
services.AddControllers();
|
||||
services.AddMediatR(typeof(CatalogItem).Assembly);
|
||||
|
||||
services.AddAutoMapper(typeof(Startup).Assembly);
|
||||
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
|
||||
@@ -157,7 +161,6 @@ namespace Microsoft.eShopWeb.PublicApi
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
||||
Reference in New Issue
Block a user