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.Advertisement; using MarketingDAL.Query.Advertisement; using Newtonsoft.Json; namespace MarketingDAL.CustomCode.Advertisement; public class AdvertisementDAL : IAdvertisementDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public AdvertisementDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetAdvertisement(int adId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QuerySingleAsync(loginDTO, AdvertisementQB.GET_ADVERTISEMENT, new { AdId = adId }, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task GetSelectListAdvertisement(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QueryAsync(loginDTO, AdvertisementQB.GET_SELECTLIST_ADVERTISEMENT, null!, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task SaveAdvertisement(AdvertisementDTO advertisementDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, AdvertisementQB.SAVE_ADVERTISEMENT, advertisementDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } public async Task UpdateAdvertisement(AdvertisementDTO advertisementDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, AdvertisementQB.UPDATE_ADVERTISEMENT, advertisementDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task DeleteAdvertisement(int adId, LoginDTO loginDTO, CancellationToken ct) { try { int result = await _QueryExecutor .ExecuteAsync(loginDTO, AdvertisementQB.DELETE_ADVERTISEMENT, new { AdId = adId }, 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); } } }