using GB5Shared.QueryExecutor; using FrameworkDAL.DTO.Checking; using FrameworkDAL.Query.CheckingOption; using GB5Shared.DTO.Framework.Login; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using Newtonsoft.Json; namespace FrameworkDAL.CustomCode.CheckingOption { public class CheckingOptionDAL : ICheckingOptionDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public CheckingOptionDAL(IQueryExecutor QueryExecutor, IValidation IValidation) { _QueryExecutor = QueryExecutor; _Validation = IValidation; } public async Task GetCheckingOption(int CheckingOptionId, LoginDTO loginDTO) { try { string sql = CheckingOptionQB.GET_CHECKINGOPTION; var parameters = new { CheckingOptionid = CheckingOptionId }; var CheckingOptionDict = new Dictionary(); var result = await _QueryExecutor.QueryAsyncArray( loginDTO, sql, (parent, child) => { if (!CheckingOptionDict.TryGetValue(parent.CheckingOptionId, out var existingParent)) { existingParent = parent; existingParent.CheckingOptionDetailArray = new List(); CheckingOptionDict[parent.CheckingOptionId] = existingParent; } if (child != null && child.CheckingOptionDetailId != 0) { existingParent.CheckingOptionDetailArray.Add(child); } return existingParent; }, parameters, splitOn: "CheckingOptionDetailId" ); var finalResult = CheckingOptionDict.Values.FirstOrDefault(); return JsonConvert.SerializeObject(finalResult); } catch (Exception ex) { throw new Exception($"Failed to retrieve CheckingOption data: {ex.Message}", ex); } } public async Task SaveCheckingOption(CheckingOptionDTO CheckingOptionDTO, LoginDTO LoginDTO) { try { string Sql = CheckingOptionQB.SAVE_CHECKINGOPTION; return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, CheckingOptionDTO); } catch (Exception ex) { string userError = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(userError); } } public async Task UpdateCheckingOption(CheckingOptionDTO CheckingOptionDTO, LoginDTO LoginDTO) { try { string Sql = CheckingOptionQB.UPDATE_CHECKINGOPTION; return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, CheckingOptionDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } public async Task DeleteCheckingOption(int CheckingOptionId, LoginDTO LoginDTO) { try { string Sql = CheckingOptionQB.DELETE_CHECKINGOPTION; var Parameters = new { CheckingOptionid = CheckingOptionId }; int result = await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, Parameters); if (result > 0) { return $"{SuccessResponse.DeleteSuccessMessage}"; } else { return $"{ErrorResponse.DeleteNotFoundMessage}"; } } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.DeleteErrorMessage}"); throw new Exception(Error); } } } }