Skip to content

Commit

Permalink
Adding API Endpoints for Catalog Items (dotnet-architecture#410)
Browse files Browse the repository at this point in the history
* Adding new Endpoints

* update nuget packages

* Modifying API to work well with Swagger.

* Remove Swashbuckle.Core
  • Loading branch information
ardalis authored Jun 25, 2020
1 parent 3b13397 commit b4d0f07
Show file tree
Hide file tree
Showing 20 changed files with 307 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<PackageReference Include="Ardalis.EFCore.Extensions" Version="1.1.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.5" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
Expand Down
20 changes: 20 additions & 0 deletions src/Web/API/BaseRequest.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
}
19 changes: 19 additions & 0 deletions src/Web/API/BaseResponse.cs
Original file line number Diff line number Diff line change
@@ -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()
{
}
}
}
13 changes: 13 additions & 0 deletions src/Web/API/CatalogItemEndpoints/CatalogItemDto.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
48 changes: 48 additions & 0 deletions src/Web/API/CatalogItemEndpoints/Create.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc;

namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class DeleteCatalogItemRequest : BaseRequest
{
//[FromRoute]
public int CatalogItemId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
38 changes: 38 additions & 0 deletions src/Web/API/CatalogItemEndpoints/Delete.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.eShopWeb.Web.API.CatalogItemEndpoints
{
public class GetByIdCatalogItemRequest : BaseRequest
{
public int CatalogItemId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
46 changes: 46 additions & 0 deletions src/Web/API/CatalogItemEndpoints/GetById.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
17 changes: 17 additions & 0 deletions src/Web/API/CustomSchemaFilters.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions src/Web/API/README.md
Original file line number Diff line number Diff line change
@@ -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.

9 changes: 8 additions & 1 deletion src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Infrastructure.Logging;
using Microsoft.eShopWeb.Infrastructure.Services;
using Microsoft.eShopWeb.Web.API;
using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.eShopWeb.Web.Services;
using Microsoft.Extensions.Configuration;
Expand All @@ -24,6 +25,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using static Microsoft.eShopWeb.Web.API.BaseRequest;

namespace Microsoft.eShopWeb.Web
{
Expand Down Expand Up @@ -129,7 +131,12 @@ public void ConfigureServices(IServiceCollection services)

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

Expand Down
21 changes: 11 additions & 10 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@


<ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" Version="1.0.0" />
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />

<PackageReference Include="MediatR" Version="8.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" />
<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.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4">
<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" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
10 changes: 7 additions & 3 deletions tests/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
</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="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>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</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" />
</ItemGroup>

Expand All @@ -33,4 +33,8 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<Folder Include="Web\ApiEndpoints\" />
</ItemGroup>

</Project>
Loading

0 comments on commit b4d0f07

Please sign in to comment.