Add customer communication system
This commit is contained in:
183
LittleShop.Client/Services/CustomerService.cs
Normal file
183
LittleShop.Client/Services/CustomerService.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using LittleShop.Client.Models;
|
||||
|
||||
namespace LittleShop.Client.Services;
|
||||
|
||||
public class CustomerService : ICustomerService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<CustomerService> _logger;
|
||||
|
||||
public CustomerService(HttpClient httpClient, ILogger<CustomerService> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<Customer>> GetCustomerByIdAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"api/customers/{id}");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var customer = await response.Content.ReadFromJsonAsync<Customer>();
|
||||
return ApiResponse<Customer>.Success(customer!);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<Customer>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get customer {CustomerId}", id);
|
||||
return ApiResponse<Customer>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<Customer>> GetCustomerByTelegramUserIdAsync(long telegramUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"api/customers/by-telegram/{telegramUserId}");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var customer = await response.Content.ReadFromJsonAsync<Customer>();
|
||||
return ApiResponse<Customer>.Success(customer!);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<Customer>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get customer by Telegram ID {TelegramUserId}", telegramUserId);
|
||||
return ApiResponse<Customer>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<Customer>> CreateCustomerAsync(CreateCustomerRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync("api/customers", request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var customer = await response.Content.ReadFromJsonAsync<Customer>();
|
||||
return ApiResponse<Customer>.Success(customer!);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<Customer>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to create customer");
|
||||
return ApiResponse<Customer>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<Customer>> GetOrCreateCustomerAsync(CreateCustomerRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync("api/customers/get-or-create", request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var customer = await response.Content.ReadFromJsonAsync<Customer>();
|
||||
return ApiResponse<Customer>.Success(customer!);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<Customer>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get or create customer");
|
||||
return ApiResponse<Customer>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<Customer>> UpdateCustomerAsync(Guid id, UpdateCustomerRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PutAsJsonAsync($"api/customers/{id}", request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var customer = await response.Content.ReadFromJsonAsync<Customer>();
|
||||
return ApiResponse<Customer>.Success(customer!);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<Customer>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to update customer {CustomerId}", id);
|
||||
return ApiResponse<Customer>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<bool>> BlockCustomerAsync(Guid id, string reason)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync($"api/customers/{id}/block", reason);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return ApiResponse<bool>.Success(true);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<bool>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to block customer {CustomerId}", id);
|
||||
return ApiResponse<bool>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<bool>> UnblockCustomerAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsync($"api/customers/{id}/unblock", null);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return ApiResponse<bool>.Success(true);
|
||||
}
|
||||
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return ApiResponse<bool>.Failure(error, response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to unblock customer {CustomerId}", id);
|
||||
return ApiResponse<bool>.Failure(
|
||||
ex.Message,
|
||||
System.Net.HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
LittleShop.Client/Services/ICustomerService.cs
Normal file
14
LittleShop.Client/Services/ICustomerService.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using LittleShop.Client.Models;
|
||||
|
||||
namespace LittleShop.Client.Services;
|
||||
|
||||
public interface ICustomerService
|
||||
{
|
||||
Task<ApiResponse<Customer>> GetCustomerByIdAsync(Guid id);
|
||||
Task<ApiResponse<Customer>> GetCustomerByTelegramUserIdAsync(long telegramUserId);
|
||||
Task<ApiResponse<Customer>> CreateCustomerAsync(CreateCustomerRequest request);
|
||||
Task<ApiResponse<Customer>> GetOrCreateCustomerAsync(CreateCustomerRequest request);
|
||||
Task<ApiResponse<Customer>> UpdateCustomerAsync(Guid id, UpdateCustomerRequest request);
|
||||
Task<ApiResponse<bool>> BlockCustomerAsync(Guid id, string reason);
|
||||
Task<ApiResponse<bool>> UnblockCustomerAsync(Guid id);
|
||||
}
|
||||
@@ -7,6 +7,6 @@ public interface IOrderService
|
||||
Task<ApiResponse<Order>> CreateOrderAsync(CreateOrderRequest request);
|
||||
Task<ApiResponse<List<Order>>> GetOrdersByIdentityAsync(string identityReference);
|
||||
Task<ApiResponse<Order>> GetOrderByIdAsync(Guid id);
|
||||
Task<ApiResponse<CryptoPayment>> CreatePaymentAsync(Guid orderId, string currency);
|
||||
Task<ApiResponse<CryptoPayment>> CreatePaymentAsync(Guid orderId, int currency);
|
||||
Task<ApiResponse<List<CryptoPayment>>> GetOrderPaymentsAsync(Guid orderId);
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public class OrderService : IOrderService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<CryptoPayment>> CreatePaymentAsync(Guid orderId, string currency)
|
||||
public async Task<ApiResponse<CryptoPayment>> CreatePaymentAsync(Guid orderId, int currency)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user