Initial commit - ECommerce.RCL
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Commerce.Contracts.DTOs;
|
||||
using Generic.Contracts.Generics;
|
||||
using Generic.Services;
|
||||
|
||||
namespace ECommerceModule.Services;
|
||||
|
||||
public class ProductAPIConsumer
|
||||
{
|
||||
private readonly HttpClient client;
|
||||
|
||||
public ProductAPIConsumer(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
this.client = httpClientFactory.CreateClient("Commerce");
|
||||
}
|
||||
|
||||
public virtual async Task<ProductVM> GetProduct(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName ?? "ar";
|
||||
|
||||
var response = await client.GetFromJsonAsync<ProductVM>($"product/{id}");
|
||||
return response!;
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
// _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<(List<ProductVM>, int)> FetchProduct(ProductFilter filter, Pagination pagination)
|
||||
{
|
||||
try
|
||||
{
|
||||
var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName ?? "ar";
|
||||
|
||||
var filterWithCulture = new ProductFilter
|
||||
{
|
||||
Name = filter?.Name,
|
||||
Price = filter?.Price,
|
||||
Discount = filter?.Discount,
|
||||
IsBundle = filter?.IsBundle,
|
||||
CategoryId = filter?.CategoryId,
|
||||
Random = filter?.Random,
|
||||
Language = culture
|
||||
};
|
||||
|
||||
var query = new AdvancedQueryBuilder()
|
||||
.AddPreText("products?")
|
||||
.AddModels(filterWithCulture, pagination)
|
||||
.ToString();
|
||||
|
||||
var products = new List<ProductVM>();
|
||||
var response = await client.GetAsync(query);
|
||||
var itemsCount = 0;
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
if (response.Headers.TryGetValues("x-pagination", out var values))
|
||||
{
|
||||
itemsCount = int.Parse(values.First());
|
||||
}
|
||||
products = await response.Content.ReadFromJsonAsync<List<ProductVM>>();
|
||||
}
|
||||
|
||||
return (products ?? new List<ProductVM>(), itemsCount);
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
// public virtual async Task<List<ProductVM>> GetRandomProducts()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var response = await client.GetFromJsonAsync<List<ProductVM>>($"product/random");
|
||||
// return response;
|
||||
// }
|
||||
// catch (HttpRequestException ex)
|
||||
// {
|
||||
// // _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint);
|
||||
// throw;
|
||||
// }
|
||||
// }
|
||||
|
||||
public virtual async Task<bool> UpdateProduct(ProductVM product)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await client.PutAsJsonAsync<ProductVM>(
|
||||
"product",
|
||||
product,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
|
||||
);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
// _logger.LogError(ex, "GET request failed for endpoint: Endpoint", endpoint);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<bool> CreateProduct(ProductVM product)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await client.PostAsJsonAsync<ProductVM>(
|
||||
"product",
|
||||
product,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
|
||||
);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
// _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<bool> DeleteProduct(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await client.DeleteAsync($"product/{id}");
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
// _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user