-
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.
[Aggregator] Added ShoppingController
- Loading branch information
1 parent
8091fc9
commit 2f70020
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/ApiGateways/Shopping.Aggregator/Controllers/ShoppingController.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,58 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Shopping.Aggregator.Models; | ||
using Shopping.Aggregator.Services; | ||
using System.Net; | ||
|
||
namespace Shopping.Aggregator.Controllers | ||
{ | ||
[Route("api/v1/[controller]")] | ||
[ApiController] | ||
public class ShoppingController : ControllerBase | ||
{ | ||
private readonly ICatalogService _catalogService; | ||
private readonly IBasketService _basketService; | ||
private readonly IOrderService _orderService; | ||
|
||
public ShoppingController(ICatalogService catalogService, IBasketService basketService, IOrderService orderService) | ||
{ | ||
_catalogService = catalogService ?? throw new ArgumentNullException(nameof(catalogService)); | ||
_basketService = basketService ?? throw new ArgumentNullException(nameof(basketService)); | ||
_orderService = orderService ?? throw new ArgumentNullException(nameof(orderService)); | ||
} | ||
|
||
[HttpGet("{userName}", Name = "GetShopping")] | ||
[ProducesResponseType(typeof(ShoppingModel), (int)HttpStatusCode.OK)] | ||
public async Task<ActionResult<ShoppingModel>> GetShopping(string userName) | ||
{ | ||
// get basket with username | ||
var basket = await _basketService.GetBasket(userName); | ||
|
||
// iterate basket items and consume products with basket item productId member | ||
// map product related members into basketitem dto with extended columns | ||
foreach (var item in basket.Items) | ||
{ | ||
var product = await _catalogService.GetCatalog(item.ProductId); | ||
// set additional product fields onto basket item | ||
item.ProductName = product.Name; | ||
item.Category = product.Category; | ||
item.Summary = product.Summary; | ||
item.Description = product.Description; | ||
item.ImageFile = product.ImageFile; | ||
} | ||
|
||
// consume ordering microservices in order to retrieve order list | ||
var orders = await _orderService.GetOrdersByUserName(userName); | ||
|
||
// return root ShoppinModel dto class which including all responses | ||
var shoppingModel = new ShoppingModel | ||
{ | ||
UserName = userName, | ||
BasketWithProducts = basket, | ||
Orders = orders | ||
}; | ||
|
||
return Ok(shoppingModel); | ||
} | ||
} | ||
} |