using CMSDAL.DTO.Navigation; using CMSDAL.Query.Navigation; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using System.Data.Common; using System.Linq; namespace CMSDAL.CustomCode.Navigation; public class NavigationDAL : INavigationDAL { private readonly IQueryExecutor _qe; private readonly IValidation _validation; public NavigationDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _validation = validation; } public async Task GetNavigation(int navigationId, LoginDTO loginDTO, CancellationToken ct = default) { try { var result = await _qe.QueryAsync(loginDTO, NavigationQB.GET_NAVIGATION, new { NavigationId = navigationId, TenantId = loginDTO.ClientId }, cancellationToken: ct); return JsonConvert.SerializeObject(result.FirstOrDefault()); } catch { throw; } } public async Task GetBySiteAsync(int siteId, LoginDTO loginDTO, CancellationToken ct = default) { try { var result = await _qe.QueryAsync(loginDTO, NavigationQB.GET_BY_SITE, new { SiteId = siteId, TenantId = loginDTO.ClientId }, cancellationToken: ct); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task SaveNavigation(NavigationDTO dto, LoginDTO loginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { int newNavigationId = await _qe.ExecuteIdentityAsync(loginDTO, NavigationQB.SAVE_NAVIGATION, dto, tx); dto.NavigationId = newNavigationId; return newNavigationId; } catch (Exception ex) { string error = await _validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(error); } } public async Task UpdateNavigation(NavigationDTO dto, LoginDTO loginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { await _qe.ExecuteAsync(loginDTO, NavigationQB.UPDATE_NAVIGATION, dto, tx, ct); return dto.NavigationId; } catch (Exception ex) { string error = await _validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(error); } } public async Task DeleteNavigation(int navigationId, LoginDTO loginDTO, CancellationToken ct = default) { try { int rows = await _qe.ExecuteAsync(loginDTO, NavigationQB.DELETE_NAVIGATION, new { NavigationId = navigationId, TenantId = loginDTO.ClientId, ModifiedById = loginDTO.UserId }, cancellationToken: ct); return rows > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _validation.HandleException(ex, $"{ErrorResponse.DeleteErrorMessage}"); throw new Exception(error); } } public async Task GetNavigationTenantIdAsync(int navigationId, LoginDTO loginDTO, CancellationToken ct = default) { try { var result = await _qe.QueryAsync(loginDTO, NavigationQB.GET_NAVIGATION_TENANT, new { NavigationId = navigationId }, cancellationToken: ct); return result.FirstOrDefault(); } catch { throw; } } }