using CPRNIMS.Domain.UIContracts.Attachment; using CPRNIMS.Domain.UIContracts.Common; using CPRNIMS.Infrastructure.Dto.Attachement; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.Models.Account; 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.Attachment { public class Attachment : IAttachment { private readonly IConfiguration _configuration; private readonly TokenHelper _tokenHelper; private readonly IApiConfigurationService _apiConfigurationService; public Attachment(IConfiguration configuration, TokenHelper tokenHelper, IApiConfigurationService apiConfigurationService) { _configuration = configuration; _tokenHelper = tokenHelper; _apiConfigurationService = apiConfigurationService; } public async Task> GetAttachmentByType(User user, int fileType) { try { var token = await _tokenHelper.GetJwtTokenAsync(user); if (!string.IsNullOrEmpty(token)) { var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token); var response = await httpClient.GetAsync($"{_configuration["LLI:Artwork:GetAttachmentByType"]}/{user.UserId}?fileType={fileType}"); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var attachmentDto = JsonSerializer.Deserialize>(jsonResponse, options); return attachmentDto; } } // Handle token retrieval failure or API call failure return null; } catch (Exception ex) { ex.ToString(); throw; } } public async Task GetAllAttachment(User user) { var token = await _tokenHelper.GetJwtTokenAsync(user); if (!string.IsNullOrEmpty(token)) { var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token); var response = await httpClient.GetAsync(_configuration["LLI:Artwork:GetAttachmentByType"] + user.UserId); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var attachmentDto = JsonSerializer.Deserialize(jsonResponse, options); return attachmentDto.URL; } } // Handle token retrieval failure or API call failure return null; } public Task CreateUpdateAttachment(Infrastructure.Entities.Account.Attachment attachment) { throw new NotImplementedException(); } public Task DeleteSignatureAsync(int id) { throw new NotImplementedException(); } public async Task GetAttachmentById(User user) { var token = await _tokenHelper.GetJwtTokenAsync(user); if (!string.IsNullOrEmpty(token)) { var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token); var response = await httpClient.GetAsync($"api/Attachment/GetAttachmentById/{user.UserId}"); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var attachmentDto = JsonSerializer.Deserialize(jsonResponse, options); return attachmentDto.URL; } } // Handle token retrieval failure or API call failure return null; } } }