forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/migrate to minimal api (dotnet-architecture#662)
* migrate from classic controller to minimal api * fix all PublicApi integration test * update all nuget package add forget project * fix pay now * Adapt readme use in memory database * undo AuthenticateEndpoint to use EndpointBaseAsync * Update README.md Co-authored-by: Steve Smith <steve@kentsmiths.com> Co-authored-by: Steve Smith <steve@kentsmiths.com>
- Loading branch information
1 parent
02b5097
commit 1e13733
Showing
63 changed files
with
842 additions
and
630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.eShopWeb.Infrastructure.Data; | ||
using Microsoft.eShopWeb.Infrastructure.Identity; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.eShopWeb.Infrastructure; | ||
|
||
public static class Dependencies | ||
{ | ||
public static void ConfigureServices(IConfiguration configuration, IServiceCollection services) | ||
{ | ||
var useOnlyInMemoryDatabase = false; | ||
if (configuration["UseOnlyInMemoryDatabase"] != null) | ||
{ | ||
useOnlyInMemoryDatabase = bool.Parse(configuration["UseOnlyInMemoryDatabase"]); | ||
} | ||
|
||
if (useOnlyInMemoryDatabase) | ||
{ | ||
services.AddDbContext<CatalogContext>(c => | ||
c.UseInMemoryDatabase("Catalog")); | ||
|
||
services.AddDbContext<AppIdentityDbContext>(options => | ||
options.UseInMemoryDatabase("Identity")); | ||
} | ||
else | ||
{ | ||
// use real database | ||
// Requires LocalDB which can be installed with SQL Server Express 2016 | ||
// https://www.microsoft.com/en-us/download/details.aspx?id=54284 | ||
services.AddDbContext<CatalogContext>(c => | ||
c.UseSqlServer(configuration.GetConnectionString("CatalogConnection"))); | ||
|
||
// Add Identity DbContext | ||
services.AddDbContext<AppIdentityDbContext>(options => | ||
options.UseSqlServer(configuration.GetConnectionString("IdentityConnection"))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
48 changes: 48 additions & 0 deletions
48
src/PublicApi/CatalogBrandEndpoints/CatalogBrandListEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using MinimalApi.Endpoint; | ||
|
||
namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints; | ||
|
||
/// <summary> | ||
/// List Catalog Brands | ||
/// </summary> | ||
public class CatalogBrandListEndpoint : IEndpoint<IResult> | ||
{ | ||
private IRepository<CatalogBrand> _catalogBrandRepository; | ||
private readonly IMapper _mapper; | ||
|
||
public CatalogBrandListEndpoint(IMapper mapper) | ||
{ | ||
_mapper = mapper; | ||
} | ||
|
||
public void AddRoute(IEndpointRouteBuilder app) | ||
{ | ||
app.MapGet("api/catalog-brands", | ||
async (IRepository<CatalogBrand> catalogBrandRepository) => | ||
{ | ||
_catalogBrandRepository = catalogBrandRepository; | ||
return await HandleAsync(); | ||
}) | ||
.Produces<ListCatalogBrandsResponse>() | ||
.WithTags("CatalogBrandEndpoints"); | ||
} | ||
|
||
public async Task<IResult> HandleAsync() | ||
{ | ||
var response = new ListCatalogBrandsResponse(); | ||
|
||
var items = await _catalogBrandRepository.ListAsync(); | ||
|
||
response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>)); | ||
|
||
return Results.Ok(response); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/PublicApi/CatalogItemEndpoints/CatalogItemGetByIdEndpoint.GetByIdCatalogItemRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints; | ||
|
||
public class GetByIdCatalogItemRequest : BaseRequest | ||
{ | ||
public int CatalogItemId { get; init; } | ||
|
||
public GetByIdCatalogItemRequest(int catalogItemId) | ||
{ | ||
CatalogItemId = catalogItemId; | ||
} | ||
} |
File renamed without changes.
56 changes: 56 additions & 0 deletions
56
src/PublicApi/CatalogItemEndpoints/CatalogItemGetByIdEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using MinimalApi.Endpoint; | ||
|
||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints; | ||
|
||
/// <summary> | ||
/// Get a Catalog Item by Id | ||
/// </summary> | ||
public class CatalogItemGetByIdEndpoint : IEndpoint<IResult, GetByIdCatalogItemRequest> | ||
{ | ||
private IRepository<CatalogItem> _itemRepository; | ||
private readonly IUriComposer _uriComposer; | ||
|
||
public CatalogItemGetByIdEndpoint(IUriComposer uriComposer) | ||
{ | ||
_uriComposer = uriComposer; | ||
} | ||
|
||
public void AddRoute(IEndpointRouteBuilder app) | ||
{ | ||
app.MapGet("api/catalog-items/{catalogItemId}", | ||
async (int catalogItemId, IRepository<CatalogItem> itemRepository) => | ||
{ | ||
_itemRepository = itemRepository; | ||
return await HandleAsync(new GetByIdCatalogItemRequest(catalogItemId)); | ||
}) | ||
.Produces<GetByIdCatalogItemResponse>() | ||
.WithTags("CatalogItemEndpoints"); | ||
} | ||
|
||
public async Task<IResult> HandleAsync(GetByIdCatalogItemRequest request) | ||
{ | ||
var response = new GetByIdCatalogItemResponse(request.CorrelationId()); | ||
|
||
var item = await _itemRepository.GetByIdAsync(request.CatalogItemId); | ||
if (item is null) | ||
return Results.NotFound(); | ||
|
||
response.CatalogItem = new CatalogItemDto | ||
{ | ||
Id = item.Id, | ||
CatalogBrandId = item.CatalogBrandId, | ||
CatalogTypeId = item.CatalogTypeId, | ||
Description = item.Description, | ||
Name = item.Name, | ||
PictureUri = _uriComposer.ComposePicUri(item.PictureUri), | ||
Price = item.Price | ||
}; | ||
return Results.Ok(response); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ublicApi/CatalogItemEndpoints/CatalogItemListPagedEndpoint.ListPagedCatalogItemRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints; | ||
|
||
public class ListPagedCatalogItemRequest : BaseRequest | ||
{ | ||
public int? PageSize { get; init; } | ||
public int? PageIndex { get; init; } | ||
public int? CatalogBrandId { get; init; } | ||
public int? CatalogTypeId { get; init; } | ||
|
||
public ListPagedCatalogItemRequest(int? pageSize, int? pageIndex, int? catalogBrandId, int? catalogTypeId) | ||
{ | ||
PageSize = pageSize ?? 0; | ||
PageIndex = pageIndex ?? 0; | ||
CatalogBrandId = catalogBrandId; | ||
CatalogTypeId = catalogTypeId; | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.