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.Collateral; using MarketingDAL.Query.Collateral; using Newtonsoft.Json; namespace MarketingDAL.CustomCode.Collateral; public class CollateralDAL : ICollateralDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public CollateralDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetCollateral(int collateralId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QuerySingleAsync(loginDTO, CollateralQB.GET_COLLATERAL, new { CollateralId = collateralId }, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task GetSelectListCollateral(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QueryAsync(loginDTO, CollateralQB.GET_SELECTLIST_COLLATERAL, null!, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task SaveCollateral(CollateralDTO collateralDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, CollateralQB.SAVE_COLLATERAL, collateralDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } public async Task UpdateCollateral(CollateralDTO collateralDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, CollateralQB.UPDATE_COLLATERAL, collateralDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task DeleteCollateral(int collateralId, LoginDTO loginDTO, CancellationToken ct) { try { int result = await _QueryExecutor .ExecuteAsync(loginDTO, CollateralQB.DELETE_COLLATERAL, new { CollateralId = collateralId }, 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); } } }