using System.Data.Common; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using MarketingDAL.DTO.Communication; using MarketingDAL.Query.Communication; using Newtonsoft.Json; namespace MarketingDAL.CustomCode.Communication; public class CommunicationDAL : ICommunicationDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public CommunicationDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetCommunication(int communicationId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QuerySingleAsync(loginDTO, CommunicationQB.GET_COMMUNICATION, new { CommunicationId = communicationId }, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task GetSelectListCommunication(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QueryAsync(loginDTO, CommunicationQB.GET_SELECTLIST_COMMUNICATION, null!, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task SaveCommunication(CommunicationDTO communicationDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, CommunicationQB.SAVE_COMMUNICATION, communicationDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } public async Task UpdateCommunication(CommunicationDTO communicationDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, CommunicationQB.UPDATE_COMMUNICATION, communicationDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task DeleteCommunication(int communicationId, LoginDTO loginDTO, CancellationToken ct) { try { int result = await _QueryExecutor .ExecuteAsync(loginDTO, CommunicationQB.DELETE_COMMUNICATION, new { CommunicationId = communicationId }, cancellationToken: ct) .ConfigureAwait(false); return result > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } public async Task> GetCommunicationAudienceContacts( int segmentId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QueryAsync( loginDTO, CommunicationQB.GET_COMMUNICATION_AUDIENCE, new { SegmentId = segmentId }, cancellationToken: ct) .ConfigureAwait(false); return result.ToList(); } catch { throw; } } }