189 lines
7.1 KiB
C#
189 lines
7.1 KiB
C#
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<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;
|
|
}
|
|
}
|
|
#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<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"]);
|
|
}
|
|
|
|
#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"]);
|
|
}
|
|
|
|
public async Task<List<InventoryVM>> GetLotQtyByItem(User user, InventoryVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:InventoryMgmt:GetLotQtyByItem"]);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|