Skip to content

Commit

Permalink
[Aggregator] Added ShoppingController
Browse files Browse the repository at this point in the history
  • Loading branch information
hidayatarg committed Oct 15, 2023
1 parent 8091fc9 commit 2f70020
Showing 1 changed file with 58 additions and 0 deletions.
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);
}
}
}

0 comments on commit 2f70020

Please sign in to comment.