using System.ComponentModel.DataAnnotations; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using GB5Shared.Validation; using Microsoft.Extensions.Logging; using PartnerDAL.CustomCode.PartnerProduct; using PartnerDAL.DTO.PartnerProduct; namespace PartnerBLL.PartnerProduct { public class PartnerProductBLL : IPartnerProductBLL { private readonly IPartnerProductDAL _partnerProductDAL; private readonly IValidation _validation; private readonly ILogger _logger; public PartnerProductBLL(IPartnerProductDAL partnerProductDAL, IValidation validation, ILogger logger) { _partnerProductDAL = partnerProductDAL; _validation = validation; _logger = logger; } public async Task GetPartnerProduct(int partnerProductId, LoginDTO loginDTO) { try { return await _partnerProductDAL.GetPartnerProduct(partnerProductId, loginDTO).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetPartnerProductListByPartner(int partnerId, LoginDTO loginDTO) { try { return await _partnerProductDAL.GetPartnerProductListByPartner(partnerId, loginDTO).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetSelectListPartnerProduct(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { return await _partnerProductDAL.GetSelectListPartnerProduct(criteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task SavePartnerProduct(PartnerProductDTO partnerProductDTO, LoginDTO loginDTO) { if (partnerProductDTO == null) throw new ArgumentNullException(nameof(partnerProductDTO)); try { GB5Trace.Step("validate-partner-product", new { partnerProductDTO.PartnerProductId }); await _validation.NotEmpty(partnerProductDTO.ProductCode, nameof(partnerProductDTO.ProductCode)).ConfigureAwait(false); await _validation.NotEmpty(partnerProductDTO.ProductName, nameof(partnerProductDTO.ProductName)).ConfigureAwait(false); if (partnerProductDTO.PartnerId == 0) throw new ValidationException("PartnerId is required."); bool isNew = partnerProductDTO.PartnerProductId == 0; partnerProductDTO.PartnerProductModifiedById = loginDTO.UserId; partnerProductDTO.PartnerProductModifiedOn = DateTime.UtcNow; GB5Trace.Step("save-partner-product", new { partnerProductDTO.PartnerProductId, isNew }); if (isNew) { partnerProductDTO.PartnerProductCreatedById = loginDTO.UserId; partnerProductDTO.PartnerProductCreatedOn = DateTime.UtcNow; int newId = await _partnerProductDAL.SavePartnerProduct(partnerProductDTO, loginDTO).ConfigureAwait(false); return $"{SuccessResponse.SaveSuccessMessage} {newId}"; } else { await _partnerProductDAL.UpdatePartnerProduct(partnerProductDTO, loginDTO).ConfigureAwait(false); return $"{SuccessResponse.UpdateSuccessMessage} {partnerProductDTO.PartnerProductId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception ex) { GB5Trace.MarkFailed("save-partner-product-failed", ex); _logger.LogError(ex, "SavePartnerProduct failed for ProductCode {ProductCode}", partnerProductDTO.ProductCode); throw; } } public async Task DeletePartnerProduct(int partnerProductId, LoginDTO loginDTO) { try { int rows = await _partnerProductDAL.DeletePartnerProduct(partnerProductId, loginDTO).ConfigureAwait(false); return rows > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { GB5Trace.MarkFailed("delete-partner-product-failed", ex); _logger.LogError(ex, "DeletePartnerProduct failed for PartnerProductId {PartnerProductId}", partnerProductId); throw; } } } }