using GB5Shared.DTO.Framework.Login; using GB5Shared.Resource.Response; using Microsoft.Extensions.Logging; using SwDAL.CustomCode.Tag; using SwDAL.DTO.Tag; using System.Text.Json; namespace SwBLL.Tag; public class TagBLL : ITagBLL { private readonly ITagDAL _dal; private readonly ILogger _logger; public TagBLL(ITagDAL dal, ILogger logger) { _dal = dal; _logger = logger; } public async Task GetById(int tagId, LoginDTO login, CancellationToken ct) { try { return await _dal.GetById(tagId, login, ct).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "GetById failed for TagId {TagId}", tagId); throw; } } public async Task GetList(string? searchText, int pageOffset, int pageSize, LoginDTO login, CancellationToken ct) { try { var (rows, total) = await _dal.GetList(searchText, pageOffset, pageSize, login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(new { rows, total }); } catch (Exception ex) { _logger.LogError(ex, "GetList failed"); throw new InvalidOperationException(); } } public async Task GetSelectList(int firstNumber, int maxResult, LoginDTO login, CancellationToken ct) { try { var rows = await _dal.GetSelectList(firstNumber, maxResult, login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(rows); } catch (Exception ex) { _logger.LogError(ex, "GetSelectList failed"); throw; } } public async Task Save(TagDTO dto, LoginDTO login, CancellationToken ct) { try { if (string.IsNullOrWhiteSpace(dto.TagName)) throw new InvalidOperationException("Tag Name is required."); if (string.IsNullOrWhiteSpace(dto.ColorHex)) dto.ColorHex = "#6B7280"; if (dto.TagId == 0) { int newId = await _dal.Insert(dto, login, ct).ConfigureAwait(false); return $"{SuccessResponse.SaveSuccessMessage} {newId}"; } await _dal.Update(dto, login, ct).ConfigureAwait(false); return SuccessResponse.UpdateSuccess; } catch (Exception ex) { _logger.LogError(ex, "Save failed for TagName {TagName}", dto.TagName); throw new InvalidOperationException(); } } public async Task Delete(int tagId, LoginDTO login, CancellationToken ct) { try { await _dal.Delete(tagId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } catch (Exception ex) { _logger.LogError(ex, "Delete failed for TagId {TagId}", tagId); throw new InvalidOperationException(); } } public async Task GetCrTags(int changeRequestId, LoginDTO login, CancellationToken ct) { try { var tags = await _dal.GetByCr(changeRequestId, login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(tags); } catch (Exception ex) { _logger.LogError(ex, "GetCrTags failed for ChangeRequestId {CRId}", changeRequestId); throw new InvalidOperationException(); } } public async Task SaveCrTags(int changeRequestId, IEnumerable tagIds, LoginDTO login, CancellationToken ct) { try { await _dal.ReplaceCrTags(changeRequestId, tagIds, login, ct).ConfigureAwait(false); return SuccessResponse.UpdateSuccess; } catch (Exception ex) { _logger.LogError(ex, "SaveCrTags failed for ChangeRequestId {CRId}", changeRequestId); throw; } } }