using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Dapper; using GB5Shared.CriteriaHandler; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using PayRollDAL.DTO.EmployeeExperience; using PayRollDAL.Query.EmployeeExperience; namespace PayRollDAL.CustomeCode.EmployeeExperience { public class EmployeeExperienceDAL : IEmployeeExperienceDAL { private readonly IQueryExecutor _queryExecutor; private readonly Dapr.Client.DaprClient _daprClient; private readonly IValidation _Validation; public EmployeeExperienceDAL(IQueryExecutor queryExecutor, Dapr.Client.DaprClient DaprClient, IValidation IValidation) { _queryExecutor = queryExecutor; _daprClient = DaprClient; _Validation = IValidation; } public async Task SaveEmployeeExperience(EmployeeExperienceDTO EmployeeExperienceDTO, LoginDTO LoginDTO) { try { string Sql = EmployeeExperienceQB.SAVE_EMPLOYEEEXPERIENCE; return await _queryExecutor.ExecuteAsync(LoginDTO, Sql, EmployeeExperienceDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(Error); } } public async Task UpdateEmployeeExperience(EmployeeExperienceDTO EmployeeExperienceDTO, LoginDTO LoginDTO) { try { string Sql = EmployeeExperienceQB.UPDATE_EMPLOYEEEXPERIENCE; return await _queryExecutor.ExecuteAsync(LoginDTO, Sql, EmployeeExperienceDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } public async Task DeleteEmployeeExperience(int EmployeeId, LoginDTO LoginDTO) { try { string Sql = EmployeeExperienceQB.DELETE_EMPLOYEEEXPERIENCE; var Parameters = new { employeeid = EmployeeId }; 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); } } public async Task GetSelectListEmployeeExperience(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO, bool IsCount = false) { try { string SQL = EmployeeExperienceQB.GET_SELECTLIST_EMPLOYEEEXPERIENCE; // Build the criteria WHERE clause (parameterised) and inject it into the // CTE, so filtering happens before ROW_NUMBER()/paging. var parameters = new DynamicParameters(); parameters.Add("firstnumber", FirstNumber); parameters.Add("maxresult", MaxResult); var (whereClause, finalParameters) = CriteriaBuilder.Build(CriteriaDTO, parameters, SQL); SQL = SQL.Replace("{DYNAMIC_WHERE}", whereClause ?? string.Empty); string Json = ""; if (IsCount == false) { List EmployeeExperiencePicklistDTO = (await _queryExecutor.QueryAsync(LoginDTO, SQL, finalParameters)).ToList(); Json = JsonConvert.SerializeObject(EmployeeExperiencePicklistDTO); } else { int Count = 0; Json = Count.ToString(); } return Json; } catch (Exception) { throw; } } } }