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.ContentRef; using MarketingDAL.Query.ContentRef; using Newtonsoft.Json; namespace MarketingDAL.CustomCode.ContentRef; public class ContentRefDAL : IContentRefDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public ContentRefDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetContentRef(int contentRefId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QuerySingleAsync(loginDTO, ContentRefQB.GET_CONTENTREF, new { ContentRefId = contentRefId }, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task GetSelectListContentRef(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QueryAsync(loginDTO, ContentRefQB.GET_SELECTLIST_CONTENTREF, null!, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task SaveContentRef(ContentRefDTO contentRefDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, ContentRefQB.SAVE_CONTENTREF, contentRefDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } public async Task UpdateContentRef(ContentRefDTO contentRefDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, ContentRefQB.UPDATE_CONTENTREF, contentRefDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task DeleteContentRef(int contentRefId, LoginDTO loginDTO, CancellationToken ct) { try { int result = await _QueryExecutor .ExecuteAsync(loginDTO, ContentRefQB.DELETE_CONTENTREF, new { ContentRefId = contentRefId }, 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); } } }