All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 16m35s
78 lines
2.7 KiB
C#
78 lines
2.7 KiB
C#
using Domain.Entities;
|
|
using Domain.Generics;
|
|
using Microsoft.JSInterop;
|
|
using System.Net.Http.Json;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace phronCare.UIBlazor.Services.Stock
|
|
{
|
|
public class LSProductService
|
|
{
|
|
private readonly HttpClient _http;
|
|
private readonly IJSRuntime _js;
|
|
|
|
public LSProductService(HttpClient http, IJSRuntime js)
|
|
{
|
|
_http = http;
|
|
_js = js;
|
|
}
|
|
|
|
public async Task<PagedResult<ELSProduct>?> SearchAsync(LSProductSearchParams searchParams)
|
|
{
|
|
return await _http.PostAsJsonAsync("/api/LSProduct/Search", searchParams)
|
|
.ContinueWith(async t =>
|
|
await t.Result.Content.ReadFromJsonAsync<PagedResult<ELSProduct>>())
|
|
.Unwrap();
|
|
}
|
|
|
|
public async Task<ELSProduct?> GetByIdAsync(int id)
|
|
{
|
|
return await _http.GetFromJsonAsync<ELSProduct>($"/api/LSProduct/{id}");
|
|
}
|
|
|
|
public async Task<HttpResponseMessage> CreateAsync(ELSProduct product)
|
|
{
|
|
return await _http.PostAsJsonAsync("/api/LSProduct", product);
|
|
}
|
|
|
|
public async Task<HttpResponseMessage> UpdateAsync(ELSProduct product)
|
|
{
|
|
return await _http.PutAsJsonAsync("/api/LSProduct", product);
|
|
}
|
|
|
|
public async Task<HttpResponseMessage> DeleteAsync(int id)
|
|
{
|
|
return await _http.DeleteAsync($"/api/LSProduct/{id}");
|
|
}
|
|
|
|
public async Task ExportFilteredAsync(LSProductSearchParams searchParams)
|
|
{
|
|
try
|
|
{
|
|
var content = new StringContent(JsonSerializer.Serialize(searchParams), Encoding.UTF8, "application/json");
|
|
var response = await _http.PostAsync("/api/LSProduct/exportfiltered", content);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var errorContent = await response.Content.ReadAsStringAsync();
|
|
throw new Exception(errorContent);
|
|
}
|
|
var bytes = await response.Content.ReadAsByteArrayAsync();
|
|
var base64 = Convert.ToBase64String(bytes);
|
|
var timestamp = DateTime.Now.ToString("yyyyMMddHHmm");
|
|
|
|
var fileName = $"productos_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
|
|
await _js.InvokeVoidAsync("saveAsFile", fileName, base64);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
var message = ex.Message ?? "No message provided";
|
|
throw new Exception($"{message}", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|