Adding API Endpoints for Catalog Items (#410)

* Adding new Endpoints

* update nuget packages

* Modifying API to work well with Swagger.

* Remove Swashbuckle.Core
This commit is contained in:
Steve Smith
2020-06-25 16:23:44 -04:00
committed by GitHub
parent 3b1339787f
commit b4d0f07c2e
20 changed files with 307 additions and 22 deletions

View File

@@ -0,0 +1,46 @@
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class GetById : BaseAsyncEndpoint<GetByIdCatalogItemRequest, GetByIdCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
public GetById(IAsyncRepository<CatalogItem> itemRepository)
{
_itemRepository = itemRepository;
}
[HttpGet("api/catalog-items/{CatalogItemId}")]
[SwaggerOperation(
Summary = "Get a Catalog Item by Id",
Description = "Gets a Catalog Item by Id",
OperationId = "catalog-items.GetById",
Tags = new[] { "CatalogItemEndpoints" })
]
public override async Task<ActionResult<GetByIdCatalogItemResponse>> HandleAsync([FromRoute]GetByIdCatalogItemRequest request)
{
var response = new GetByIdCatalogItemResponse(request.CorrelationId());
var item = await _itemRepository.GetByIdAsync(request.CatalogItemId);
if (item is null) return NotFound();
response.CatalogItem = new CatalogItemDto
{
Id = item.Id,
CatalogBrandId = item.CatalogBrandId,
CatalogTypeId = item.CatalogTypeId,
Description = item.Description,
Name = item.Name,
PictureUri = item.PictureUri,
Price = item.Price
};
return Ok(response);
}
}
}