191 lines
7.3 KiB
C#
191 lines
7.3 KiB
C#
using CPRNIMS.Domain.UIContracts.Common;
|
|
using CPRNIMS.Domain.UIContracts.Receiving;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.Models.Account;
|
|
using CPRNIMS.Infrastructure.Models.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.Receiving;
|
|
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.Receiving
|
|
{
|
|
public class Receiving : IReceiving
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly TokenHelper _tokenHelper;
|
|
private readonly IApiConfigurationService _apiConfigurationService;
|
|
public Receiving(IConfiguration configuration, TokenHelper tokenHelper,
|
|
IApiConfigurationService apiConfigurationService)
|
|
{
|
|
_configuration = configuration;
|
|
_tokenHelper = tokenHelper;
|
|
_apiConfigurationService = apiConfigurationService;
|
|
}
|
|
#region SendRequest service
|
|
public async Task<ReceivingVM> SendPostApiRequest(User user,
|
|
ReceivingVM viewModel, string apiEndpoint)
|
|
{
|
|
var token = await _tokenHelper.GetJwtTokenAsync(user);
|
|
|
|
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);
|
|
var responseObject = JsonSerializer.Deserialize<ResponseObject>(await response.Content.ReadAsStringAsync());
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
viewModel.ErrCode = 1;
|
|
viewModel.ErrMessage = responseObject.message;
|
|
viewModel.StatusResponse = responseObject.statusResponse;
|
|
return viewModel;
|
|
}
|
|
else
|
|
{
|
|
viewModel.ErrCode = 0;
|
|
viewModel.Message = responseObject.message;
|
|
viewModel.StatusResponse = responseObject.statusResponse;
|
|
return viewModel;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<List<ReceivingVM>> SendGetApiRequest(User user,
|
|
ReceivingVM viewModel,
|
|
string apiEndpoint)
|
|
{
|
|
var token = await _tokenHelper.GetJwtTokenAsync(user);
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
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 myPR = JsonSerializer.Deserialize<List<ReceivingVM>>(jsonResponse, options);
|
|
if (myPR.Count > 0)
|
|
{
|
|
myPR[0].URL = _configuration["CommonEndpoints:ApiDefaultHeaders:ItemImages"];
|
|
}
|
|
|
|
return myPR;
|
|
}
|
|
else
|
|
{
|
|
// Handle API request failure
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
#region Get
|
|
public async Task<List<ReceivingVM>?> GetRRReport(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetRRReport"]);
|
|
}
|
|
public async Task<List<ReceivingVM>> GetForReceiving(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetForReceiving"]);
|
|
}
|
|
public async Task<List<ReceivingVM>> GetPRDetailByPRNo(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetPRDetailByPRNo"]);
|
|
}
|
|
public async Task<List<ReceivingVM>> GetRRDetailByPO(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetRRDetailByPO"]);
|
|
}
|
|
public async Task<List<ReceivingVM>?> GetLatestRRNo(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetLatestRRNo"]);
|
|
}
|
|
public async Task<List<ReceivingVM>?> GetRR(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetRR"]);
|
|
}
|
|
public async Task<List<ReceivingVM>?> GetRRDetail(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:GetRRDetail"]);
|
|
}
|
|
#endregion
|
|
#region POST PUT
|
|
public async Task<ReceivingVM> PutRRNoSeries(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:PutRRNoSeries"]);
|
|
}
|
|
|
|
public async Task<ReceivingVM> PostPutReceiving(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:PostPutReceiving"]);
|
|
}
|
|
|
|
public async Task<ReceivingVM> PutPOClose(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:PutPOClose"]);
|
|
}
|
|
public async Task<ReceivingVM> PostPutDeniedItem(User user, ReceivingVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:Receiving:PostPutDeniedItem"]);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|