NonInventPurchasingSystem/CPRNIMS.Domain/UIServices/Attachment/Attachment.cs
2026-01-26 14:21:31 +08:00

129 lines
4.6 KiB
C#

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<List<AttachResponseDto>> GetAttachmentByType(User user, int fileType)
{
try
{
var token = await _tokenHelper.GetValidTokenAsync();
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<List<AttachResponseDto>>(jsonResponse, options);
return attachmentDto;
}
}
// Handle token retrieval failure or API call failure
return null;
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
public async Task<string> GetAllAttachment(User user)
{
var token = await _tokenHelper.GetValidTokenAsync();
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<AttachResponseDto>(jsonResponse, options);
return attachmentDto.URL;
}
}
// Handle token retrieval failure or API call failure
return null;
}
public Task<Infrastructure.Entities.Account.Attachment> CreateUpdateAttachment(Infrastructure.Entities.Account.Attachment attachment)
{
throw new NotImplementedException();
}
public Task DeleteSignatureAsync(int id)
{
throw new NotImplementedException();
}
public async Task<string> GetAttachmentById(User user)
{
var token = await _tokenHelper.GetValidTokenAsync();
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<AttachResponseDto>(jsonResponse, options);
return attachmentDto.URL;
}
}
// Handle token retrieval failure or API call failure
return null;
}
}
}