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 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; } } public async Task 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(jsonResponse, new JsonSerializerOptions{PropertyNameCaseInsensitive = true}); } public async Task> 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>(jsonResponse, options); return result ?? new PagedResult(); } else { return new PagedResult { IsSuccess = false, ErrorMessage = $"API returned {response.StatusCode}" }; } } } 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> GetInventory(User user, InventoryRequest reqquest) { return await SendGetPageListApiRequest(user, reqquest, _configuration["LLI:NonInvent:InventoryMgmt:GetInventory"]); } 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"]); } public async Task> GetLotQtyByItem(User user, InventoryVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:InventoryMgmt:GetLotQtyByItem"]); } #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"]); } #endregion } }