280 lines
11 KiB
C#
280 lines
11 KiB
C#
using CPRNIMS.Domain.UIContracts.Common;
|
|
using CPRNIMS.Domain.UIContracts.PR;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.Models.Account;
|
|
using CPRNIMS.Infrastructure.Models.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.PR;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Domain.UIServices.PR
|
|
{
|
|
public class PRequest : IPRequest
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly TokenHelper _tokenHelper;
|
|
private readonly IApiConfigurationService _apiConfigurationService;
|
|
public PRequest(IConfiguration configuration, TokenHelper tokenHelper,
|
|
IApiConfigurationService apiConfigurationService)
|
|
{
|
|
_configuration = configuration;
|
|
_tokenHelper = tokenHelper;
|
|
_apiConfigurationService = apiConfigurationService;
|
|
}
|
|
#region SendRequest service
|
|
public async Task<PRVM> SendPostApiRequest(User user,
|
|
PRVM viewModel, string apiEndpoint)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
|
|
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);
|
|
var responseObject = JsonSerializer.Deserialize<ResponseObject>(await response.Content.ReadAsStringAsync());
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
viewModel.messCode = responseObject.messCode;
|
|
viewModel.Message = responseObject.message;
|
|
viewModel.StatusResponse = responseObject.statusResponse;
|
|
return viewModel;
|
|
}
|
|
else
|
|
{
|
|
viewModel.messCode = 0;
|
|
viewModel.Message = responseObject.message;
|
|
viewModel.StatusResponse = responseObject.statusResponse;
|
|
return viewModel;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<List<PRVM>> SendGetApiRequest(User user,
|
|
PRVM viewModel,string apiEndpoint)
|
|
{
|
|
try
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
|
|
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<PRVM>>(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<PRVM>?> GetProjectCodes(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetProjectCodes"]);
|
|
}
|
|
public async Task<List<PRVM>?> GetApproverName(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetApproverName"]);
|
|
}
|
|
public async Task<List<PRVM>?> GetApproverNameByPRNo(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetApproverNameByPRNo"]);
|
|
}
|
|
public async Task<List<PRVM>> GetAllPR(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetAllPR"]);
|
|
}
|
|
public async Task<List<PRVM>> GetMyPR(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetMyPR"]);
|
|
}
|
|
public async Task<List<PRVM>> GetForReceiving(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetForReceiving"]);
|
|
}
|
|
public async Task<List<PRVM>> GetItemDetailForReceiving(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetItemDetailForReceiving"]);
|
|
}
|
|
public async Task<List<PRVM>> GetPRDetailByPRNo(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetPRDetailByPRNo"]);
|
|
}
|
|
public async Task<List<PRVM>> GetPRByRRId(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetPRByRRId"]);
|
|
}
|
|
public async Task<List<PRVM>> GetRRDetailByPO(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetRRDetailByPO"]);
|
|
}
|
|
public async Task<List<PRVM>> GetPRStatusById(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetPRStatusById"]);
|
|
}
|
|
public async Task<List<PRVM>> GetDashBoard(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetDashBoard"]);
|
|
}
|
|
public async Task<List<PRVM>> GetPRArchived(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetPRArchived"]);
|
|
}
|
|
public async Task<List<PRVM>?> GetPRListByPRNo(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetPRListByPRNo"]);
|
|
}
|
|
public async Task<List<PRVM>?> GetDetailedPRTracking(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetDetailedPRTracking"]);
|
|
}
|
|
public async Task<List<PRVM>?> GetSupplierAlternativeOffer(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetSupplierAlternativeOffer"]);
|
|
}
|
|
public async Task<List<PRVM>?> GetSupplierAlterOfferDetails(User user, PRVM viewModel)
|
|
{
|
|
return await SendGetApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:GetSupplierAlterOfferDetails"]);
|
|
}
|
|
#endregion
|
|
#region POST PUT
|
|
public async Task<PRVM> PostPRApproveReject(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PostPRApproveReject"]);
|
|
}
|
|
public async Task<PRVM> PostPutItemReceiving(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PostPutItemReceiving"]);
|
|
}
|
|
|
|
public async Task<PRVM> PutItemDetail(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PutItemDetail"]);
|
|
}
|
|
|
|
public async Task<PRVM> PostPutReceiving(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PostPutReceiving"]);
|
|
}
|
|
|
|
public async Task<PRVM> PutPOClose(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PutPOClose"]);
|
|
}
|
|
public async Task<PRVM> PostPutDeniedItem(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PostPutDeniedItem"]);
|
|
}
|
|
|
|
public async Task<PRVM> PutAlternativeOffer(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PutAlternativeOffer"]);
|
|
}
|
|
|
|
public async Task<PRVM> PutSupplierAlterOffer(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PutSupplierAlterOffer"]);
|
|
}
|
|
public async Task<PRVM> PostPutPRItem(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PutSupplierAlterOffer"]);
|
|
}
|
|
public async Task<PRVM> PRItemRemoval(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PRItemRemoval"]);
|
|
}
|
|
public async Task<PRVM> ApprovedSelectedPRItem(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:ApprovedSelectedPRItem"]);
|
|
}
|
|
|
|
public async Task<PRVM> PostPutProjectCode(User user, PRVM viewModel)
|
|
{
|
|
return await SendPostApiRequest(user, viewModel,
|
|
_configuration["LLI:NonInvent:PRMgmt:PostPutProjectCode"]);
|
|
}
|
|
|
|
public async Task<PRVM> PostPutAttachment(User user, PRVM prVM)
|
|
{
|
|
return await SendPostApiRequest(user, prVM,
|
|
_configuration["LLI:NonInvent:PRMgmt:PostPutAttachment"]);
|
|
}
|
|
#endregion
|
|
}
|
|
} |