using FrameworkDAL.DTO.MAction; using FrameworkDAL.Query.Action; using FrameworkDAL.DTO.Role; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using Newtonsoft.Json; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.Resource.Response; using GB5Shared.Validation; namespace FrameworkDAL.CustomCode.Action { public class ActionDAL : IActionDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public ActionDAL(IQueryExecutor QueryExecutor, IValidation Validation) { _QueryExecutor = QueryExecutor; _Validation = Validation; } public async Task GetAction(int MActionId, LoginDTO loginDTO) { string json = ""; try { // 1️⃣ Fetch a single MAction record string sqlAction = ActionQB.GET_ACTION; var parameters = new { mactionid = MActionId }; var action = await _QueryExecutor.QuerySingleAsync(loginDTO, sqlAction, parameters); // 2️⃣ Process and populate MActionTo for that record if (action != null && action.MActionToDeliveryType == 1 && !string.IsNullOrEmpty(action.MActionTo)) { string[] roleIds = action.MActionTo.Split(','); List roleNames = new List(); foreach (var roleId in roleIds) { if (short.TryParse(roleId, out short id)) { string sqlRole = @"SELECT ROLEID AS RoleId, ROLECODE AS RoleCode, ROLENAME AS RoleName FROM ROLE WHERE ROLEID = @roleId"; var roleParams = new { roleId = id }; var role = await _QueryExecutor.QuerySingleAsync(loginDTO, sqlRole, roleParams); if (role != null) { roleNames.Add(role.RoleName); } } } action.MActionTo = string.Join(",", roleNames); } json = JsonConvert.SerializeObject(action); return json; } catch (Exception) { throw; } finally { json = null; } } public async Task SaveAction(MActionDTO MActionDTO, LoginDTO LoginDTO) { try { string Sql = ActionQB.SAVE_ACTION; return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, MActionDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(Error); } } public async Task UpdateAction(MActionDTO MActionDTO, LoginDTO LoginDTO) { try { string Sql = ActionQB.UPDATE_ACTION; return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, MActionDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } public async Task DeleteAction(int MActionId, LoginDTO LoginDTO) { try { string Sql = ActionQB.DELETE_ACTION; var Parameters = new { mactionid = MActionId }; 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); } } } }