2018-02-27 14:32:25 +01:00
|
|
|
|
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;
|
2018-11-15 12:50:37 +01:00
|
|
|
|
using System.Net;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2019-08-27 09:40:31 +02:00
|
|
|
|
using Serilog;
|
2019-09-04 15:21:19 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/v1/[controller]")]
|
|
|
|
|
[Authorize]
|
2018-11-14 16:21:50 +01:00
|
|
|
|
[ApiController]
|
|
|
|
|
public class BasketController : ControllerBase
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly ICatalogService _catalog;
|
|
|
|
|
private readonly IBasketService _basket;
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2018-02-27 14:32:25 +01:00
|
|
|
|
public BasketController(ICatalogService catalogService, IBasketService basketService)
|
|
|
|
|
{
|
|
|
|
|
_catalog = catalogService;
|
|
|
|
|
_basket = basketService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[HttpPut]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(BasketData), (int)HttpStatusCode.OK)]
|
|
|
|
|
public async Task<ActionResult<BasketData>> UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
if (data.Items == null || !data.Items.Any())
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("Need to pass at least one basket line");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve the current basket
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var basket = await _basket.GetById(data.BuyerId) ?? new BasketData(data.BuyerId);
|
2018-11-15 12:50:37 +01:00
|
|
|
|
var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2019-09-04 15:21:19 +02:00
|
|
|
|
// group by product id to avoid duplicates
|
|
|
|
|
var itemsCalculated = data
|
|
|
|
|
.Items
|
|
|
|
|
.GroupBy(x => x.ProductId, x => x, (k, i) => new { productId = k, items = i })
|
|
|
|
|
.Select(groupedItem =>
|
|
|
|
|
{
|
|
|
|
|
var item = groupedItem.items.First();
|
|
|
|
|
item.Quantity = groupedItem.items.Sum(i => i.Quantity);
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
foreach (var bitem in itemsCalculated)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId);
|
|
|
|
|
if (catalogItem == null)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})");
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 15:21:19 +02:00
|
|
|
|
var itemInBasket = basket.Items.FirstOrDefault(x => x.ProductId == bitem.ProductId);
|
|
|
|
|
if (itemInBasket == null)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-09-04 15:21:19 +02:00
|
|
|
|
basket.Items.Add(new BasketDataItem()
|
|
|
|
|
{
|
|
|
|
|
Id = bitem.Id,
|
|
|
|
|
ProductId = catalogItem.Id,
|
|
|
|
|
ProductName = catalogItem.Name,
|
|
|
|
|
PictureUrl = catalogItem.PictureUri,
|
|
|
|
|
UnitPrice = catalogItem.Price,
|
|
|
|
|
Quantity = bitem.Quantity
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
itemInBasket.Quantity = bitem.Quantity;
|
|
|
|
|
}
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 14:21:41 -05:00
|
|
|
|
await _basket.UpdateAsync(basket);
|
2018-11-14 16:21:50 +01:00
|
|
|
|
|
2019-05-23 14:21:41 -05:00
|
|
|
|
return basket;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
|
[Route("items")]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(BasketData), (int)HttpStatusCode.OK)]
|
|
|
|
|
public async Task<ActionResult<BasketData>> UpdateQuantitiesAsync([FromBody] UpdateBasketItemsRequest data)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
if (!data.Updates.Any())
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("No updates sent");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve the current basket
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var currentBasket = await _basket.GetById(data.BasketId);
|
2018-02-27 14:32:25 +01:00
|
|
|
|
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
|
2018-11-15 12:50:37 +01:00
|
|
|
|
await _basket.UpdateAsync(currentBasket);
|
2018-11-14 16:21:50 +01:00
|
|
|
|
|
|
|
|
|
return currentBasket;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("items")]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
|
|
|
public async Task<ActionResult> AddBasketItemAsync([FromBody] AddBasketItemRequest data)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
if (data == null || data.Quantity == 0)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("Invalid payload");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Get the item from catalog
|
2018-11-15 12:50:37 +01:00
|
|
|
|
var item = await _catalog.GetCatalogItemAsync(data.CatalogItemId);
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
//item.PictureUri =
|
|
|
|
|
|
|
|
|
|
// Step 2: Get current basket status
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var currentBasket = (await _basket.GetById(data.BasketId)) ?? new BasketData(data.BasketId);
|
2019-08-06 16:10:39 +02:00
|
|
|
|
// Step 3: Search if exist product into basket
|
2019-09-04 15:21:19 +02:00
|
|
|
|
var product = currentBasket.Items.SingleOrDefault(i => i.ProductId == item.Id);
|
2019-08-29 13:13:10 +02:00
|
|
|
|
if (product != null)
|
|
|
|
|
{
|
2019-08-06 16:10:39 +02:00
|
|
|
|
// Step 4: Update quantity for product
|
|
|
|
|
product.Quantity += data.Quantity;
|
|
|
|
|
}
|
2019-08-29 13:13:10 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2019-08-06 16:10:39 +02:00
|
|
|
|
// Step 4: Merge current status with new product
|
|
|
|
|
currentBasket.Items.Add(new BasketDataItem()
|
|
|
|
|
{
|
|
|
|
|
UnitPrice = item.Price,
|
|
|
|
|
PictureUrl = item.PictureUri,
|
2019-09-04 15:21:19 +02:00
|
|
|
|
ProductId = item.Id,
|
2019-08-06 16:10:39 +02:00
|
|
|
|
ProductName = item.Name,
|
|
|
|
|
Quantity = data.Quantity,
|
|
|
|
|
Id = Guid.NewGuid().ToString()
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-08-29 13:13:10 +02:00
|
|
|
|
|
2019-08-06 16:10:39 +02:00
|
|
|
|
// Step 5: Update basket
|
2018-11-15 12:50:37 +01:00
|
|
|
|
await _basket.UpdateAsync(currentBasket);
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|