142 lines
4.0 KiB
C#
142 lines
4.0 KiB
C#
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;
|
|
using Microsoft.Extensions.Http;
|
|
|
|
namespace ECommerceModule.Services;
|
|
|
|
public class CategoryAPIConsumer
|
|
{
|
|
private readonly HttpClient client;
|
|
|
|
public CategoryAPIConsumer(IHttpClientFactory httpClientFactory)
|
|
{
|
|
this.client = httpClientFactory.CreateClient("Commerce");
|
|
}
|
|
|
|
public async Task<CategoryVM> GetCategory(string id)
|
|
{
|
|
try
|
|
{
|
|
var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName ?? "ar";
|
|
|
|
var response = await client.GetFromJsonAsync<CategoryVM>($"category/{id}");
|
|
return response!;
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
// _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<(List<CategoryVM>, int)> FetchCategory(CategoryFilter filter, Pagination pagination)
|
|
{
|
|
try
|
|
{
|
|
var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName ?? "ar";
|
|
|
|
var filterWithCulture = new CategoryFilter
|
|
{
|
|
Name = filter?.Name,
|
|
IsBundle = filter?.IsBundle,
|
|
Random = filter?.Random,
|
|
Language = culture
|
|
};
|
|
|
|
var query = new AdvancedQueryBuilder()
|
|
.AddPreText("categories?")
|
|
.AddModels(filterWithCulture, pagination)
|
|
.ToString();
|
|
|
|
var categories = new List<CategoryVM>();
|
|
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());
|
|
}
|
|
categories = await response.Content.ReadFromJsonAsync<List<CategoryVM>>();
|
|
}
|
|
|
|
return (categories ?? new List<CategoryVM>(), itemsCount);
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<CategoryVM>> FetchCategoryTree()
|
|
{
|
|
try
|
|
{
|
|
var response = await client.GetFromJsonAsync<List<CategoryVM>>("categories/tree");
|
|
return response ?? new List<CategoryVM>();
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<bool> CreateCategory(CategoryVM category)
|
|
{
|
|
try
|
|
{
|
|
var response = await client.PostAsJsonAsync<CategoryVM>(
|
|
"category",
|
|
category,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
|
|
);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateCategory(CategoryVM category)
|
|
{
|
|
try
|
|
{
|
|
var response = await client.PutAsJsonAsync<CategoryVM>(
|
|
"category",
|
|
category,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
|
|
);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
// _logger.LogError(ex, "GET request failed for endpoint: Endpoint", endpoint);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DeleteCategory(string id)
|
|
{
|
|
try
|
|
{
|
|
var response = await client.DeleteAsync($"category/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
// _logger.LogError(ex, "GET request failed for endpoint: Endpoint", endpoint);
|
|
throw;
|
|
}
|
|
}
|
|
}
|