NonInventPurchasingSystem/CPRNIMS.Domain/UIServices/Items/Item.cs
2026-01-20 07:44:30 +08:00

208 lines
8.0 KiB
C#

using CPRNIMS.Domain.UIContracts.Common;
using CPRNIMS.Domain.UIContracts.Items;
using CPRNIMS.Infrastructure.Helper;
using CPRNIMS.Infrastructure.Models.Account;
using CPRNIMS.Infrastructure.Models.Common;
using CPRNIMS.Infrastructure.ViewModel.Items;
using Google.Apis.Drive.v3.Data;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using static Google.Apis.Requests.BatchRequest;
namespace CPRNIMS.Domain.UIServices.Items
{
public class Item : IItem
{
private readonly IConfiguration _configuration;
private readonly TokenHelper _tokenHelper;
private readonly IApiConfigurationService _apiConfigurationService;
public Item(IConfiguration configuration, TokenHelper tokenHelper,
IApiConfigurationService apiConfigurationService)
{
_configuration = configuration;
_tokenHelper = tokenHelper;
_apiConfigurationService = apiConfigurationService;
}
#region SendRequest service
public async Task<ItemVM> SendPostApiRequest(Infrastructure.Models.Account.User user,
ItemVM 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.message = responseObject.message;
viewModel.messCode =1;
viewModel.ItemCodeId= responseObject.itemCode;
return viewModel;
}
else
{
viewModel.message = responseObject.message;
viewModel.messCode = 0;
return viewModel;
}
}
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
public async Task<List<ItemVM>> SendGetApiRequest(Infrastructure.Models.Account.User user,
ItemVM 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 jsonResponse = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
viewModel.messCode = 1;
var myArtWork = JsonSerializer.Deserialize<List<ItemVM>>(jsonResponse, options);
return myArtWork;
}
else
{
var myArtWork = JsonSerializer.Deserialize<List<ItemVM>>(jsonResponse);
viewModel.messCode = 0;
viewModel.message = myArtWork[0].message;
return myArtWork;
}
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region Get Method
public async Task<List<ItemVM>> GetItemDetail(Infrastructure.Models.Account.User user,
ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemDetail"]);
}
public async Task<List<ItemVM>> GetItemCart(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemCart"]);
}
public async Task<List<ItemVM>> GetItemList(Infrastructure.Models.Account.User user,
ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemList"]);
}
public async Task<List<ItemVM>> GetItemCateg(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemCateg"]);
}
public async Task<List<ItemVM>> GetItemColor(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemColor"]);
}
public async Task<List<ItemVM>> GetItemUOM(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemUOM"]);
}
public async Task<List<ItemVM>> GetItemLocalization(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetItemLocalization"]);
}
public async Task<List<ItemVM>> GetDepartment(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendGetApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:GetDepartment"]);
}
#endregion
#region PostPut Method
public async Task<ItemVM> PostPutItem(Infrastructure.Models.Account.User user,
ItemVM viewModel)
{
return await SendPostApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:PostPutItem"]);
}
public async Task<ItemVM> PutItemDetail(Infrastructure.Models.Account.User user,
ItemVM viewModel)
{
return await SendPostApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:PutItemDetail"]);
}
public async Task<ItemVM> PostPutItemCart(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendPostApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:PostPutItemCart"]);
}
public async Task<ItemVM> PostPurchRequest(Infrastructure.Models.Account.User user, ItemVM viewModel)
{
return await SendPostApiRequest(user, viewModel,
_configuration["LLI:NonInvent:ItemMgmt:PostPurchRequest"]);
}
#endregion
}
}