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

@@ -9,9 +9,9 @@
<PackageReference Include="Ardalis.EFCore.Extensions" Version="1.1.0" /> <PackageReference Include="Ardalis.EFCore.Extensions" Version="1.1.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" /> <PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4" PrivateAssets="All" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.5" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" /> <ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />

View File

@@ -0,0 +1,20 @@
using System;
namespace Microsoft.eShopWeb.Web.API
{
/// <summary>
/// Base class used by API requests
/// </summary>
public abstract class BaseMessage
{
/// <summary>
/// Unique Identifier used by logging
/// </summary>
protected Guid _correlationId = Guid.NewGuid();
public Guid CorrelationId() => _correlationId;
}
public abstract class BaseRequest : BaseMessage
{
}
}

View File

@@ -0,0 +1,19 @@
using System;
namespace Microsoft.eShopWeb.Web.API
{
/// <summary>
/// Base class used by API responses
/// </summary>
public abstract class BaseResponse : BaseMessage
{
public BaseResponse(Guid correlationId) : base()
{
base._correlationId = correlationId;
}
public BaseResponse()
{
}
}
}

View File

@@ -0,0 +1,13 @@
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class CatalogItemDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string PictureUri { get; set; }
public int CatalogTypeId { get; set; }
public int CatalogBrandId { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class CreateCatalogItemRequest : BaseRequest
{
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; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class CreateCatalogItemResponse : BaseResponse
{
public CreateCatalogItemResponse(Guid correlationId) : base(correlationId)
{
}
public CatalogItemDto CatalogItem { get; set; }
}
}

View File

@@ -0,0 +1,48 @@
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 Create : BaseAsyncEndpoint<CreateCatalogItemRequest, CreateCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
public Create(IAsyncRepository<CatalogItem> itemRepository)
{
_itemRepository = itemRepository;
}
[HttpPost("api/catalog-items")]
[SwaggerOperation(
Summary = "Creates a new Catalog Item",
Description = "Creates a new Catalog Item",
OperationId = "catalog-items.create",
Tags = new[] { "CatalogItemEndpoints" })
]
public override async Task<ActionResult<CreateCatalogItemResponse>> HandleAsync(CreateCatalogItemRequest request)
{
var response = new CreateCatalogItemResponse(request.CorrelationId());
CatalogItem newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri);
newItem = await _itemRepository.AddAsync(newItem);
var dto = new CatalogItemDto
{
Id = newItem.Id,
CatalogBrandId = newItem.CatalogBrandId,
CatalogTypeId = newItem.CatalogTypeId,
Description = newItem.Description,
Name = newItem.Name,
PictureUri = newItem.PictureUri,
Price = newItem.Price
};
response.CatalogItem = dto;
return response;
}
}
}

View File

@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc;
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class DeleteCatalogItemRequest : BaseRequest
{
//[FromRoute]
public int CatalogItemId { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class DeleteCatalogItemResponse : BaseResponse
{
public DeleteCatalogItemResponse(Guid correlationId) : base(correlationId)
{
}
public string Status { get; set; } = "Deleted";
}
}

View File

@@ -0,0 +1,38 @@
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 Delete : BaseAsyncEndpoint<DeleteCatalogItemRequest, DeleteCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
public Delete(IAsyncRepository<CatalogItem> itemRepository)
{
_itemRepository = itemRepository;
}
[HttpDelete("api/catalog-items/{CatalogItemId}")]
[SwaggerOperation(
Summary = "Deletes a Catalog Item",
Description = "Deletes a Catalog Item",
OperationId = "catalog-items.Delete",
Tags = new[] { "CatalogItemEndpoints" })
]
public override async Task<ActionResult<DeleteCatalogItemResponse>> HandleAsync([FromRoute]DeleteCatalogItemRequest request)
{
var response = new DeleteCatalogItemResponse(request.CorrelationId());
var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId);
if (itemToDelete is null) return NotFound();
await _itemRepository.DeleteAsync(itemToDelete);
return Ok(response);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class GetByIdCatalogItemRequest : BaseRequest
{
public int CatalogItemId { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class GetByIdCatalogItemResponse : BaseResponse
{
public GetByIdCatalogItemResponse(Guid correlationId) : base(correlationId)
{
}
public CatalogItemDto CatalogItem { get; set; }
}
}

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);
}
}
}

View File

@@ -0,0 +1,17 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Microsoft.eShopWeb.Web.API
{
public class CustomSchemaFilters : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
var excludeProperties = new[] { "CorrelationId" };
foreach (var prop in excludeProperties)
if (schema.Properties.ContainsKey(prop))
schema.Properties.Remove(prop);
}
}
}

4
src/Web/API/README.md Normal file
View File

@@ -0,0 +1,4 @@
# API Endpoints
This folder demonstrates how to configure API endpoints as individual classes. You can compare it to the traditional controller-based approach found in /Controllers/Api.

View File

@@ -13,6 +13,7 @@ using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Infrastructure.Identity; using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Infrastructure.Logging; using Microsoft.eShopWeb.Infrastructure.Logging;
using Microsoft.eShopWeb.Infrastructure.Services; using Microsoft.eShopWeb.Infrastructure.Services;
using Microsoft.eShopWeb.Web.API;
using Microsoft.eShopWeb.Web.Interfaces; using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.eShopWeb.Web.Services; using Microsoft.eShopWeb.Web.Services;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@@ -24,6 +25,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Mime; using System.Net.Mime;
using static Microsoft.eShopWeb.Web.API.BaseRequest;
namespace Microsoft.eShopWeb.Web namespace Microsoft.eShopWeb.Web
{ {
@@ -129,7 +131,12 @@ namespace Microsoft.eShopWeb.Web
services.AddHttpContextAccessor(); services.AddHttpContextAccessor();
services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" })); services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" });
c.EnableAnnotations();
c.SchemaFilter<CustomSchemaFilters>();
});
services.AddHealthChecks(); services.AddHealthChecks();

View File

@@ -13,23 +13,24 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" Version="1.0.0" />
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" /> <PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" /> <PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="MediatR" Version="8.0.1" /> <PackageReference Include="MediatR" Version="8.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" /> <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" /> <PackageReference Include="BuildBundlerMinifier" Version="2.9.406" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.5.0" /> <PackageReference Include="Microsoft.CodeAnalysis" Version="3.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.76" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.4.1" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.5">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>

View File

@@ -13,14 +13,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup> </ItemGroup>
@@ -33,4 +33,8 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Web\ApiEndpoints\" />
</ItemGroup>
</Project> </Project>

View File

@@ -7,11 +7,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.14.1" /> <PackageReference Include="Moq" Version="4.14.4" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>

View File

@@ -9,9 +9,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.14.1" /> <PackageReference Include="Moq" Version="4.14.4" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>