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 SendPostApiRequest(Infrastructure.Models.Account.User user, ItemVM 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); var responseObject = JsonSerializer.Deserialize(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> SendGetApiRequest(Infrastructure.Models.Account.User user, ItemVM 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); var jsonResponse = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; viewModel.messCode = 1; var myArtWork = JsonSerializer.Deserialize>(jsonResponse, options); return myArtWork; } else { var myArtWork = JsonSerializer.Deserialize>(jsonResponse); viewModel.messCode = 0; viewModel.message = myArtWork[0].message; return myArtWork; } } } catch (Exception ex) { throw; } } #endregion #region Get Method public async Task> GetProjectCodeByTerm(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetProjectCodeByTerm"]); } public async Task> GetItemDetail(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemDetail"]); } public async Task> GetItemCart(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemCart"]); } public async Task> GetItemList(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemList"]); } public async Task> GetItemCateg(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemCateg"]); } public async Task> GetItemColor(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemColor"]); } public async Task> GetItemUOM(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemUOM"]); } public async Task> GetItemLocalization(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:GetItemLocalization"]); } public async Task> 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 PostPutItem(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:PostPutItem"]); } public async Task PutItemDetail(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:PutItemDetail"]); } public async Task PostPutItemCart(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:PostPutItemCart"]); } public async Task PostPurchRequest(Infrastructure.Models.Account.User user, ItemVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:ItemMgmt:PostPurchRequest"]); } #endregion } }