using GB5Shared.QueryExecutor; using FrameworkDAL.DTO.ECMRights; using FrameworkDAL.Query.ECMRights; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace FrameworkDAL.CustomCode.ECMRights { public class ECMRightsDAL : IECMRightsDAL { private readonly IQueryExecutor _queryExecutor; private readonly IValidation _Validation; public ECMRightsDAL(IQueryExecutor queryExecutor, IValidation IValidation) { _queryExecutor = queryExecutor; _Validation = IValidation; } /// /// Retrieves a single ECMRights record by its unique identifier. /// /// The ID of the ECMRights to retrieve. /// The current user's login details. /// A JSON string representing the ECMRights details. public async Task GetECMRights(int ECMRightsId, LoginDTO LoginDTO) { try { string sql = ECMRightsQB.GET_ECMRIGHTS; var parameters = new { ecmrightsid = ECMRightsId }; var ecmRightsDict = new Dictionary(); var result = await _queryExecutor.QueryAsyncArray( LoginDTO, sql, (parent, child) => { if (!ecmRightsDict.TryGetValue(parent.ECMRightsId, out var existingParent)) { existingParent = parent; existingParent.ECMRightsDetailArray = new List(); ecmRightsDict[parent.ECMRightsId] = existingParent; } if (child != null && child.ECMRightsDetailId != 0) { existingParent.ECMRightsDetailArray.Add(child); } return existingParent; }, parameters, splitOn: "ECMRightsDetailId" ); var finalResult = ecmRightsDict.Values.FirstOrDefault(); return JsonConvert.SerializeObject(finalResult); } catch (Exception ex) { throw new Exception($"Failed to retrieve ECMRights data: {ex.Message}", ex); } } /// /// Retrieves a paginated list of ECMRights records based on specified filter criteria. /// Can optionally return the total count of records instead of the list. /// /// The starting index of records to fetch (for pagination). /// The maximum number of records to fetch. /// Filter criteria for selecting ECMRights. /// The current user's login details. /// If true, returns the total count of matching records instead of the list. /// A JSON string representing either the list of ECMRights records or the total count. public async Task GetSelectListECMRights(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO LoginDTO, bool isCount = false) { try { string json = ""; if (!isCount) { string sql = ECMRightsQB.GET_SELECTLIST_ECMRIGHTS; var parameters = new { firstnumber = firstNumber, maxresult = maxResult }; List list = (await _queryExecutor.QueryAsync(LoginDTO, sql, parameters)).ToList(); json = JsonConvert.SerializeObject(list); } else { int count = 0; json = count.ToString(); } return json; } catch (Exception) { throw; } } /// /// Inserts a new ECMRights record into the database. /// /// The ECMRights data to insert. /// The current user's login details. /// The number of rows affected (should be 1 if successful). public async Task SaveECMRights(ECMRightsDTO ECMRightsDTO, LoginDTO LoginDTO) { try { string sql = ECMRightsQB.SAVE_ECMRIGHTS; return await _queryExecutor.ExecuteAsync(LoginDTO, sql, ECMRightsDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(Error); } } /// /// Updates an existing ECMRights record in the database. /// /// The ECMRights data to update. /// The current user's login details. /// The number of rows affected (should be 1 if successful). public async Task UpdateECMRights(ECMRightsDTO ECMRightsDTO, LoginDTO LoginDTO) { try { string sql = ECMRightsQB.UPDATE_ECMRIGHTS; return await _queryExecutor.ExecuteAsync(LoginDTO, sql, ECMRightsDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } /// /// Deletes an ECMRights record by its unique identifier. /// /// The ID of the ECMRights to delete. /// The current user's login details. /// A message string indicating success or failure of the delete operation. public async Task DeleteECMRights(int ECMRightsId, LoginDTO LoginDTO) { try { string sql = ECMRightsQB.DELETE_ECMRIGHTS; var parameters = new { ecmrightsid = ECMRightsId }; 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); } } } }