using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Microsoft.Extensions.Logging; using MMDAL.DTO.Inspection; using MMDAL.Query.InspectionParameter; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MMDAL.CustomCode.InspectionParameter { public class InspectionParameterDAL : IInspectionParameterDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; private readonly ILogger _logger; public InspectionParameterDAL(IQueryExecutor QueryExecutor, IValidation Validation, ILogger logger) { _QueryExecutor = QueryExecutor; _Validation = Validation; _logger = logger; } public async Task GetInspectionParameter( int InspectionParameterId, LoginDTO LoginDTO) { try { string Sql = InspectionParameterQB.GET_INSPECTIONPARAMETER; var Parameters = new { InspectionParameterId = InspectionParameterId }; InspectionParameterDTO InspectionParameterDTO = await _QueryExecutor.QuerySingleAsync( LoginDTO, Sql, Parameters); return JsonConvert.SerializeObject(InspectionParameterDTO); } catch (Exception) { throw; } } public async Task GetSelectListInspectionParameter( int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO, bool IsCount = false) { try { string Json = ""; if (!IsCount) { string Sql = InspectionParameterQB.GET_SELECTLIST_INSPECTIONPARAMETER; var Parameters = new { firstnumber = FirstNumber, maxresult = MaxResult }; IEnumerable InspectionParameterDTO = await _QueryExecutor.QueryAsync( LoginDTO, Sql, Parameters); Json = JsonConvert.SerializeObject(InspectionParameterDTO); } else { int Count = 0; Json = Count.ToString(); } return Json; } catch (Exception) { throw; } } public async Task SaveInspectionParameter( InspectionParameterDTO InspectionParameterDTO, LoginDTO LoginDTO) { try { string Sql = InspectionParameterQB.SAVE_INSPECTIONPARAMETER; return await _QueryExecutor.ExecuteAsync( LoginDTO, Sql, InspectionParameterDTO); } catch (Exception ex) { string Error = await _Validation.HandleException( ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(Error); } } public async Task UpdateInspectionParameter( InspectionParameterDTO InspectionParameterDTO, LoginDTO LoginDTO) { try { string Sql = InspectionParameterQB.UPDATE_INSPECTIONPARAMETER; return await _QueryExecutor.ExecuteAsync( LoginDTO, Sql, InspectionParameterDTO); } catch (Exception ex) { string Error = await _Validation.HandleException( ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } public async Task DeleteInspectionParameter( int InspectionParameterId, LoginDTO LoginDTO) { try { string Sql = InspectionParameterQB.DELETE_INSPECTIONPARAMETER; var Parameters = new { InspectionParameterId = InspectionParameterId }; 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 GetInspectionParameterList( CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { string sql = InspectionParameterQB.GET_INSPECTIONPARAMETER_LIST; var result = await _QueryExecutor.QueryAsync( LoginDTO, sql, null!); return JsonConvert.SerializeObject(result); } catch (Exception ex) { _logger.LogError(ex, "GetInspectionParameterList failed"); throw; } } } }