@ -0,0 +1,23 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Config | |||
{ | |||
public class UrlsConfig | |||
{ | |||
public class CatalogOperations | |||
{ | |||
public static string GetItemById(int id) => $"/api/v1/catalog/items/{id}"; | |||
} | |||
public class BasketOperations | |||
{ | |||
public static string GetItemById(string id) => $"/api/v1/basket/{id}"; | |||
} | |||
public string Basket { get; set; } | |||
public string Catalog { get; set; } | |||
} | |||
} |
@ -0,0 +1,18 @@ | |||
using Microsoft.AspNetCore.Mvc; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Controllers | |||
{ | |||
[Route("")] | |||
public class HomeController : Controller | |||
{ | |||
[HttpGet()] | |||
public IActionResult Index() | |||
{ | |||
return new RedirectResult("~/swagger"); | |||
} | |||
} | |||
} |
@ -0,0 +1,20 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Models | |||
{ | |||
public class AddBasketItemRequest | |||
{ | |||
public int CatalogItemId { get; set; } | |||
public string BasketId { get; set; } | |||
public int Quantity { get; set; } | |||
public AddBasketItemRequest() | |||
{ | |||
Quantity = 1; | |||
} | |||
} | |||
} |
@ -0,0 +1,31 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Models | |||
{ | |||
public class BasketData | |||
{ | |||
public string BuyerId { get; set; } | |||
public List<BasketDataItem> Items { get; set; } | |||
public BasketData(string buyerId) | |||
{ | |||
BuyerId = buyerId; | |||
Items = new List<BasketDataItem>(); | |||
} | |||
} | |||
public class BasketDataItem | |||
{ | |||
public string Id { get; set; } | |||
public string ProductId { get; set; } | |||
public string ProductName { get; set; } | |||
public decimal UnitPrice { get; set; } | |||
public decimal OldUnitPrice { get; set; } | |||
public int Quantity { get; set; } | |||
public string PictureUrl { get; set; } | |||
} | |||
} |
@ -0,0 +1,20 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Models | |||
{ | |||
public class CatalogItem | |||
{ | |||
public int Id { get; set; } | |||
public string Name { get; set; } | |||
public decimal Price { get; set; } | |||
public string PictureUri { get; set; } | |||
} | |||
} |
@ -0,0 +1,34 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http; | |||
using Microsoft.Extensions.Logging; | |||
using Microsoft.Extensions.Options; | |||
using Newtonsoft.Json; | |||
using PurchaseBff.Config; | |||
using PurchaseBff.Models; | |||
namespace PurchaseBff.Services | |||
{ | |||
public class BasketService : IBasketService | |||
{ | |||
private readonly IHttpClient _apiClient; | |||
private readonly ILogger<BasketService> _logger; | |||
private readonly UrlsConfig _urls; | |||
public BasketService(IHttpClient httpClient, ILogger<BasketService> logger, IOptionsSnapshot<UrlsConfig> config) | |||
{ | |||
_apiClient = httpClient; | |||
_logger = logger; | |||
_urls = config.Value; | |||
} | |||
public async Task<BasketData> GetById(string id) | |||
{ | |||
var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id)); | |||
var basket = JsonConvert.DeserializeObject<BasketData>(data); | |||
return basket; | |||
} | |||
} | |||
} |
@ -0,0 +1,35 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http; | |||
using Microsoft.Extensions.Logging; | |||
using Microsoft.Extensions.Options; | |||
using Newtonsoft.Json; | |||
using PurchaseBff.Config; | |||
using PurchaseBff.Models; | |||
namespace PurchaseBff.Services | |||
{ | |||
public class CatalogService : ICatalogService | |||
{ | |||
private readonly IHttpClient _apiClient; | |||
private readonly ILogger<CatalogService> _logger; | |||
private readonly UrlsConfig _urls; | |||
public CatalogService(IHttpClient httpClient, ILogger<CatalogService> logger, IOptionsSnapshot<UrlsConfig> config) | |||
{ | |||
_apiClient = httpClient; | |||
_logger = logger; | |||
_urls = config.Value; | |||
} | |||
public async Task<CatalogItem> GetCatalogItem(int id) | |||
{ | |||
var data = await _apiClient.GetStringAsync(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id)); | |||
var item = JsonConvert.DeserializeObject<CatalogItem>(data); | |||
return item; | |||
} | |||
} | |||
} |
@ -0,0 +1,13 @@ | |||
using PurchaseBff.Models; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Services | |||
{ | |||
public interface IBasketService | |||
{ | |||
Task<BasketData> GetById(string id); | |||
} | |||
} |
@ -0,0 +1,13 @@ | |||
using PurchaseBff.Models; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace PurchaseBff.Services | |||
{ | |||
public interface ICatalogService | |||
{ | |||
Task<CatalogItem> GetCatalogItem(int id); | |||
} | |||
} |
@ -1,10 +0,0 @@ | |||
{ | |||
"Logging": { | |||
"IncludeScopes": false, | |||
"LogLevel": { | |||
"Default": "Debug", | |||
"System": "Information", | |||
"Microsoft": "Information" | |||
} | |||
} | |||
} |
@ -0,0 +1,6 @@ | |||
{ | |||
"urls": { | |||
"basket": "http://localhost:55105", | |||
"catalog": "http://localhost:55101" | |||
} | |||
} |