260 lines
10 KiB
C#
260 lines
10 KiB
C#
using CPRNIMS.Domain.UIContracts.Common;
|
|
using CPRNIMS.Domain.UIContracts.Inventory;
|
|
using CPRNIMS.Infrastructure.Dto.Inventory;
|
|
using CPRNIMS.Infrastructure.Dto.Inventory.Request;
|
|
using CPRNIMS.Infrastructure.Dto.Inventory.Response;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.Models.Account;
|
|
using CPRNIMS.Infrastructure.Models.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.Inventory;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Domain.UIServices.Inventory
|
|
{
|
|
public class Inventory : IInventory
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly TokenHelper _tokenHelper;
|
|
private readonly IApiConfigurationService _apiConfigurationService;
|
|
public Inventory(IConfiguration configuration, TokenHelper tokenHelper,
|
|
IApiConfigurationService apiConfigurationService)
|
|
{
|
|
_configuration = configuration;
|
|
_tokenHelper = tokenHelper;
|
|
_apiConfigurationService = apiConfigurationService;
|
|
}
|
|
#region SendRequest service
|
|
public async Task<InventoryVM> SendPostApiRequest(User user,
|
|
InventoryVM viewModel, string apiEndpoint)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
// Handle token retrieval failure
|
|
return null;
|
|
}
|
|
|
|
viewModel.UserId = user.UserId;
|
|
var jsonContent = JsonSerializer.Serialize(viewModel);
|
|
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token))
|
|
{
|
|
HttpResponseMessage response;
|
|
|
|
response = await httpClient.PostAsync(apiEndpoint, content);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var responseObject = JsonSerializer.Deserialize<ResponseObject>(await response.Content.ReadAsStringAsync());
|
|
|
|
viewModel.Message = responseObject.message;
|
|
viewModel.StatusResponse = responseObject.statusResponse;
|
|
return viewModel;
|
|
}
|
|
else
|
|
{
|
|
// Handle API request failure
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<List<InventoryVM>> SendGetApiRequest(User user,
|
|
InventoryVM viewModel,
|
|
string apiEndpoint)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
// Handle token retrieval failure
|
|
return null;
|
|
}
|
|
|
|
viewModel.UserId = user.UserId;
|
|
var jsonContent = JsonSerializer.Serialize(viewModel);
|
|
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token))
|
|
{
|
|
HttpResponseMessage response;
|
|
|
|
response = await httpClient.PostAsync(apiEndpoint, content);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
};
|
|
var myInventory = JsonSerializer.Deserialize<List<InventoryVM>>(jsonResponse, options);
|
|
if (myInventory.Count > 0)
|
|
{
|
|
myInventory[0].URL = _configuration["CommonEndpoints:ApiDefaultHeaders:ItemImages"];
|
|
}
|
|
return myInventory;
|
|
}
|
|
else
|
|
{
|
|
// Handle API request failure
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<TransactContextDto?> GetTransactContextAsync(int inventoryId)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
return null;
|
|
|
|
var apiEndpoint =
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetTransactContext"] ?? "api/InventoryMgmt/GetTransactContext";
|
|
|
|
using var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
|
|
var response = await httpClient.GetAsync( $"{apiEndpoint}?inventoryId={inventoryId}");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
return JsonSerializer.Deserialize<TransactContextDto>(jsonResponse,
|
|
new JsonSerializerOptions{PropertyNameCaseInsensitive = true});
|
|
}
|
|
|
|
public async Task<Infrastructure.Dto.Inventory.PagedResult<InventoryResponse>> SendGetPageListApiRequest
|
|
(User user, InventoryRequest request, string apiEndpoint)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
request.UserId = user.UserId;
|
|
var jsonContent = JsonSerializer.Serialize(request);
|
|
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token))
|
|
{
|
|
HttpResponseMessage response;
|
|
|
|
response = await httpClient.PostAsync(apiEndpoint, content);
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
|
var result = JsonSerializer.Deserialize<PagedResult<InventoryResponse>>(jsonResponse, options);
|
|
return result ?? new PagedResult<InventoryResponse>();
|
|
}
|
|
else
|
|
{
|
|
return new PagedResult<InventoryResponse>
|
|
{
|
|
IsSuccess = false,
|
|
ErrorMessage = $"API returned {response.StatusCode}"
|
|
};
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
#region Get
|
|
public async Task<List<InventoryVM>> GetInventoryById(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetInventoryById"]);
|
|
}
|
|
public async Task<List<InventoryVM>> GetLotNo(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetLotNo"]);
|
|
}
|
|
|
|
public async Task<List<InventoryVM>> GetLotNoById(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetLotNoById"]);
|
|
}
|
|
public async Task<Infrastructure.Dto.Inventory.PagedResult<InventoryResponse>> GetInventory(User user, InventoryRequest reqquest)
|
|
{
|
|
return await SendGetPageListApiRequest(user, reqquest,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetInventory"]);
|
|
}
|
|
public async Task<List<InventoryVM>> GetInventoryByUserId(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetInventoryByUserId"]);
|
|
}
|
|
|
|
public async Task<List<InventoryVM>> GetRequestedItemByUserId(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetRequestedItemByUserId"]);
|
|
}
|
|
public async Task<List<InventoryVM>> GetLotQtyByItem(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetLotQtyByItem"]);
|
|
}
|
|
#endregion
|
|
#region Post Put
|
|
public async Task<InventoryVM> PostPutReqApproval(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:PostPutReqApproval"]);
|
|
}
|
|
public async Task<InventoryVM> PostPutLotNo(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:PostPutLotNo"]);
|
|
}
|
|
public async Task<InventoryVM> PostPutLotBin(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:PostPutLotBin"]);
|
|
}
|
|
|
|
public async Task<InventoryVM> PostPutReqItems(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:PostPutReqItems"]);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|