using CMSDAL.DTO.BlockDataBinding; using CMSDAL.Query.BlockDataBinding; 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.BlockDataBinding; public class BlockDataBindingDAL : IBlockDataBindingDAL { private readonly IQueryExecutor _qe; private readonly IValidation _validation; public BlockDataBindingDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _validation = validation; } public async Task GetByContentBlockIdAsync(int contentBlockId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _qe.QueryAsync(loginDTO, BlockDataBindingQB.GET_BY_CONTENTBLOCKID, new { ContentBlockId = contentBlockId }, cancellationToken: ct); return JsonConvert.SerializeObject(result.FirstOrDefault()); } catch { throw; } } public async Task SaveAsync(BlockDataBindingDTO dto, LoginDTO loginDTO, DbTransaction? tx, CancellationToken ct) { try { // TenantId bound from loginDTO.ClientId, never dto.TenantId directly — same // reasoning as DataSourceDAL.SaveAsync/UpdateAsync. BlockDataBindingBLL already sets // dto.TenantId to the owning ContentBlock's real tenant (which CrossTenantGuard has // already asserted equals loginDTO.ClientId) before calling this, so this is a // second, independent guarantee rather than the only one. int newId = await _qe.ExecuteIdentityAsync(loginDTO, BlockDataBindingQB.SAVE_BLOCKDATABINDING, new { dto.ContentBlockId, dto.DataSourceId, dto.BindingMode, dto.ParamsJson, dto.Status, TenantId = loginDTO.ClientId, dto.CreatedById, dto.CreatedOn, dto.ModifiedById, dto.ModifiedOn }, tx); dto.BlockDataBindingId = newId; dto.TenantId = loginDTO.ClientId; return newId; } catch (Exception ex) { string error = await _validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(error); } } public async Task UpdateAsync(BlockDataBindingDTO dto, LoginDTO loginDTO, DbTransaction? tx, CancellationToken ct) { try { await _qe.ExecuteAsync(loginDTO, BlockDataBindingQB.UPDATE_BLOCKDATABINDING, new { dto.BlockDataBindingId, dto.DataSourceId, dto.BindingMode, dto.ParamsJson, dto.Status, TenantId = loginDTO.ClientId, dto.ModifiedById, dto.ModifiedOn }, tx, ct); dto.TenantId = loginDTO.ClientId; return dto.BlockDataBindingId; } catch (Exception ex) { string error = await _validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(error); } } }