using CPRNIMS.Domain.UIContracts.Common; using CPRNIMS.Domain.UIContracts.Inventory; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.Models.Account; using CPRNIMS.Infrastructure.Models.Common; using CPRNIMS.Infrastructure.ViewModel.Finance; 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 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(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> 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>(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; } } #endregion #region Get public async Task> GetInventoryById(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetInventoryById"]); } public async Task> GetLotNo(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetLotNo"]); } public async Task> GetLotNoById(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetLotNoById"]); } public async Task> GetInventoryByUserId(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetInventoryByUserId"]); } public async Task> GetRequestedItemByUserId(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetRequestedItemByUserId"]); } #endregion #region Post Put public async Task PostPutReqApproval(User user, InventoryVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:PostPutReqApproval"]); } public async Task PostPutLotNo(User user, InventoryVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:PostPutLotNo"]); } public async Task PostPutLotBin(User user, InventoryVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:PostPutLotBin"]); } public async Task PostPutReqItems(User user, InventoryVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:PostPutReqItems"]); } public async Task> GetLotQtyByItem(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetLotQtyByItem"]); } #endregion } }