forked from dotnet-architecture/eShopOnContainers
-
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.
- Loading branch information
Showing
34 changed files
with
970 additions
and
7 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
31 changes: 31 additions & 0 deletions
31
src/Aggregators/Web.Shopping.HttpAggregator/Config/UrlsConfig.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,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config | ||
{ | ||
public class UrlsConfig | ||
{ | ||
public class CatalogOperations | ||
{ | ||
public static string GetItemById(int id) => $"/api/v1/catalog/items/{id}"; | ||
public static string GetItemsById(IEnumerable<int> ids) => $"/api/v1/catalog/items?ids={string.Join(',', ids)}"; | ||
} | ||
|
||
public class BasketOperations | ||
{ | ||
public static string GetItemById(string id) => $"/api/v1/basket/{id}"; | ||
public static string UpdateBasket() => "/api/v1/basket"; | ||
} | ||
|
||
public class OrdersOperations | ||
{ | ||
public static string GetOrderDraft() => "/api/v1/orders/draft"; | ||
} | ||
|
||
public string Basket { get; set; } | ||
public string Catalog { get; set; } | ||
public string Orders { get; set; } | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
src/Aggregators/Web.Shopping.HttpAggregator/Controllers/BasketController.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,133 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models; | ||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers | ||
{ | ||
[Route("api/v1/[controller]")] | ||
[Authorize] | ||
public class BasketController : Controller | ||
{ | ||
private readonly ICatalogService _catalog; | ||
private readonly IBasketService _basket; | ||
public BasketController(ICatalogService catalogService, IBasketService basketService) | ||
{ | ||
_catalog = catalogService; | ||
_basket = basketService; | ||
} | ||
|
||
[HttpPost] | ||
[HttpPut] | ||
public async Task<IActionResult> UpdateAllBasket([FromBody] UpdateBasketRequest data) | ||
{ | ||
|
||
if (data.Items == null || !data.Items.Any()) | ||
{ | ||
return BadRequest("Need to pass at least one basket line"); | ||
} | ||
|
||
// Retrieve the current basket | ||
var currentBasket = await _basket.GetById(data.BuyerId); | ||
if (currentBasket == null) | ||
{ | ||
currentBasket = new BasketData(data.BuyerId); | ||
} | ||
|
||
var catalogItems = await _catalog.GetCatalogItems(data.Items.Select(x => x.ProductId)); | ||
var newBasket = new BasketData(data.BuyerId); | ||
|
||
foreach (var bitem in data.Items) | ||
{ | ||
var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId); | ||
if (catalogItem == null) | ||
{ | ||
return BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})"); | ||
} | ||
|
||
newBasket.Items.Add(new BasketDataItem() | ||
{ | ||
Id = bitem.Id, | ||
ProductId = catalogItem.Id.ToString(), | ||
ProductName = catalogItem.Name, | ||
PictureUrl = catalogItem.PictureUri, | ||
UnitPrice = catalogItem.Price, | ||
Quantity = bitem.Quantity | ||
}); | ||
} | ||
|
||
await _basket.Update(newBasket); | ||
return Ok(newBasket); | ||
} | ||
|
||
[HttpPut] | ||
[Route("items")] | ||
public async Task<IActionResult> UpdateQuantities([FromBody] UpdateBasketItemsRequest data) | ||
{ | ||
if (!data.Updates.Any()) | ||
{ | ||
return BadRequest("No updates sent"); | ||
} | ||
|
||
// Retrieve the current basket | ||
var currentBasket = await _basket.GetById(data.BasketId); | ||
if (currentBasket == null) | ||
{ | ||
return BadRequest($"Basket with id {data.BasketId} not found."); | ||
} | ||
|
||
// Update with new quantities | ||
foreach (var update in data.Updates) | ||
{ | ||
var basketItem = currentBasket.Items.SingleOrDefault(bitem => bitem.Id == update.BasketItemId); | ||
if (basketItem == null) | ||
{ | ||
return BadRequest($"Basket item with id {update.BasketItemId} not found"); | ||
} | ||
basketItem.Quantity = update.NewQty; | ||
} | ||
|
||
// Save the updated basket | ||
await _basket.Update(currentBasket); | ||
return Ok(currentBasket); | ||
} | ||
|
||
[HttpPost] | ||
[Route("items")] | ||
public async Task<IActionResult> AddBasketItem([FromBody] AddBasketItemRequest data) | ||
{ | ||
if (data == null || data.Quantity == 0) | ||
{ | ||
return BadRequest("Invalid payload"); | ||
} | ||
|
||
// Step 1: Get the item from catalog | ||
var item = await _catalog.GetCatalogItem(data.CatalogItemId); | ||
|
||
//item.PictureUri = | ||
|
||
// Step 2: Get current basket status | ||
var currentBasket = (await _basket.GetById(data.BasketId)) ?? new BasketData(data.BasketId); | ||
// Step 3: Merge current status with new product | ||
currentBasket.Items.Add(new BasketDataItem() | ||
{ | ||
UnitPrice = item.Price, | ||
PictureUrl = item.PictureUri, | ||
ProductId = item.Id.ToString(), | ||
ProductName = item.Name, | ||
Quantity = data.Quantity, | ||
Id = Guid.NewGuid().ToString() | ||
}); | ||
|
||
// Step 4: Update basket | ||
await _basket.Update(currentBasket); | ||
|
||
|
||
return Ok(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Aggregators/Web.Shopping.HttpAggregator/Controllers/HomeController.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,18 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers | ||
{ | ||
[Route("")] | ||
public class HomeController : Controller | ||
{ | ||
[HttpGet()] | ||
public IActionResult Index() | ||
{ | ||
return new RedirectResult("~/swagger"); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Aggregators/Web.Shopping.HttpAggregator/Controllers/OrderController.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,42 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers | ||
{ | ||
[Route("api/v1/[controller]")] | ||
[Authorize] | ||
public class OrderController : Controller | ||
{ | ||
private readonly IBasketService _basketService; | ||
private readonly IOrderApiClient _orderClient; | ||
public OrderController(IBasketService basketService, IOrderApiClient orderClient) | ||
{ | ||
_basketService = basketService; | ||
_orderClient = orderClient; | ||
} | ||
|
||
[Route("draft/{basketId}")] | ||
[HttpGet] | ||
public async Task<IActionResult> GetOrderDraft(string basketId) | ||
{ | ||
if (string.IsNullOrEmpty(basketId)) | ||
{ | ||
return BadRequest("Need a valid basketid"); | ||
} | ||
// Get the basket data and build a order draft based on it | ||
var basket = await _basketService.GetById(basketId); | ||
if (basket == null) | ||
{ | ||
return BadRequest($"No basket found for id {basketId}"); | ||
} | ||
|
||
var orderDraft = await _orderClient.GetOrderDraftFromBasket(basket); | ||
return Ok(orderDraft); | ||
} | ||
} | ||
} |
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,18 @@ | ||
FROM microsoft/aspnetcore:2.0.5 AS base | ||
WORKDIR /app | ||
EXPOSE 80 | ||
|
||
FROM microsoft/aspnetcore-build:2.0 AS build | ||
WORKDIR /src | ||
COPY . . | ||
RUN dotnet restore -nowarn:msb3202,nu1503 | ||
WORKDIR /src/src/Aggregators/Web.Shopping.HttpAggregator | ||
RUN dotnet build --no-restore -c Release -o /app | ||
|
||
FROM build AS publish | ||
RUN dotnet publish --no-restore -c Release -o /app | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app . | ||
ENTRYPOINT ["dotnet", "Web.Shopping.HttpAggregator.dll"] |
33 changes: 33 additions & 0 deletions
33
src/Aggregators/Web.Shopping.HttpAggregator/Filters/AuthorizeCheckOperationFilter.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,33 @@ | ||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Filters | ||
{ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Basket.API.Infrastructure.Filters | ||
{ | ||
public class AuthorizeCheckOperationFilter : IOperationFilter | ||
{ | ||
public void Apply(Operation operation, OperationFilterContext context) | ||
{ | ||
// Check for authorize attribute | ||
var hasAuthorize = context.ApiDescription.ControllerAttributes().OfType<AuthorizeAttribute>().Any() || | ||
context.ApiDescription.ActionAttributes().OfType<AuthorizeAttribute>().Any(); | ||
|
||
if (hasAuthorize) | ||
{ | ||
operation.Responses.Add("401", new Response { Description = "Unauthorized" }); | ||
operation.Responses.Add("403", new Response { Description = "Forbidden" }); | ||
|
||
operation.Security = new List<IDictionary<string, IEnumerable<string>>>(); | ||
operation.Security.Add(new Dictionary<string, IEnumerable<string>> | ||
{ | ||
{ "oauth2", new [] { "Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator" } } | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.