/// <summary> /// /// </summary> /// <param name="query"></param> /// <param name="pageModel"></param> /// <param name="orderBy"></param> /// <returns></returns> private async Task <uint> GetMainRecordsCountWithDapperAsync(HRBirdMainInput query, PagingParameterInModel pageModel, HRSortingParamModel orderBy) { String cxString = _config.GetConnectionString(CONNECTION_STRING_KEY); cxString = String.Format(cxString, _config[_DBUSER], _config[_DBPASSWORD]); using (var conn = new NpgsqlConnection(cxString)) { conn.Open(); try { using (Task <int> taskCount = conn.ExecuteScalarAsync <int>(GetSQLQueryMainRecordsCount(query, pageModel, orderBy))) { await taskCount; if (taskCount.IsCompleted) { return((uint)(taskCount.Result)); } else { throw new Exception("ExecuteScalarAsync : Can not complete Task."); } } } catch (Exception ex) { if (_logger != null) { _logger.LogError(ex.Message); } throw; } } }
/// <summary> /// /// </summary> /// <param name="query"></param> /// <param name="pageModel"></param> /// <param name="orderBy"></param> /// <returns></returns> private String GetSQLQueryMainRecordsCount(HRBirdMainInput query, PagingParameterInModel pageModel, HRSortingParamModel orderBy) { if (query == null || String.IsNullOrEmpty(query.Lang_Iso_Code)) { throw new ArgumentNullException("Lang_Iso_Code not set."); } return(String.Format(SQLQUERY_COUNT, query.Lang_Iso_Code.ToLower())); }
public async Task <ActionResult <PagingParameterOutModel <HRBirdMainOutput> > > Get( [FromQuery] HRBirdMainInput query, [FromQuery] PagingParameterInModel pageModel, [FromQuery] HRSortingParamModel orderBy) { using var resultTask = _birdService.GetMainRecordsAsync(query, pageModel, orderBy); await resultTask; return(resultTask.Result); }
/// <summary> /// /// </summary> /// <returns></returns> private HRBirdMainInput GetDefaultMainInput() { HRBirdMainInput retour = new HRBirdMainInput(); retour.Lat = 0.0F; retour.Lon = 0.0F; retour.Range = 5000; retour.Season = HRSeason.all; retour.Lang_Iso_Code = "fr"; return(retour); }
/// <summary> /// /// </summary> /// <param name="query"></param> /// <param name="pageModel"></param> /// <param name="orderBy"></param> /// <returns></returns> private async Task <IEnumerable <HRBirdMainOutput> > GetMainRecordsWithDapperAsync(HRBirdMainInput query, PagingParameterInModel pageModel, HRSortingParamModel orderBy) { String cxString = _config.GetConnectionString(CONNECTION_STRING_KEY); cxString = String.Format(cxString, _config[_DBUSER], _config[_DBPASSWORD]); using (var conn = new NpgsqlConnection(cxString)) { conn.Open(); try { using (Task <IEnumerable <HRBirdMainOutput> > retourTask = conn.QueryAsync <HRBirdMainOutput>(GetSQLQueryMainRecords(query, pageModel, orderBy))) { await retourTask; if (retourTask.IsCompleted) { return(retourTask.Result); } else { throw new Exception("QueryAsync : Can not complete Task."); } } } catch (Exception ex) { if (_logger != null) { _logger.LogError(ex.Message); } throw; } } }
/// <summary> /// 1- Check for consistency. /// </summary> /// <param name="query">Can not be null</param> /// <param name="pageModel">Can not be null</param> /// <param name="orderBy">Can be null</param> /// <returns></returns> public async Task <PagingParameterOutModel <HRBirdMainOutput> > GetMainRecordsAsync(HRBirdMainInput query, PagingParameterInModel pageModel, HRSortingParamModel orderBy) { //1- if (query == null || pageModel == null) { throw new ArgumentNullException("At least one of arguments is null"); } //2- PagingParameterOutModel <HRBirdMainOutput> retour = new PagingParameterOutModel <HRBirdMainOutput>(); using (Task <IEnumerable <HRBirdMainOutput> > mainRecordsAction = GetMainRecordsWithDapperAsync(query, pageModel, orderBy)) { await mainRecordsAction; if (mainRecordsAction.IsCompleted) { retour.CurrentPage = pageModel.PageNumber; retour.PageSize = pageModel.PageSize; retour.PageItems = mainRecordsAction.Result; using (var countTask = GetMainRecordsCountWithDapperAsync(query, pageModel, orderBy)) { await countTask; if (countTask.IsCompleted) { retour.TotalItemsCount = countTask.Result; } } } else { throw new Exception("Something get wrong in PostGisrepository : GetAsync(IEnumerable<string> borderIDs)"); } } return(retour); }
/// <summary> /// 1- Check for consistency and set default value if not supplied by caller. /// 2- Call repo. /// </summary> /// <param name="query"></param> /// <param name="pageModel"></param> /// <param name="orderBy"></param> /// <returns></returns> public async Task <PagingParameterOutModel <HRBirdMainOutput> > GetMainRecordsAsync(HRBirdMainInput query, PagingParameterInModel pageModel, HRSortingParamModel orderBy) { //1- if (_repo == null) { throw new Exception("repository not set."); } if (query == null) { query = GetDefaultMainInput(); } if (pageModel == null) { pageModel = GetDefaultMainPagination(); } //2- using (var taskRepo = _repo.GetMainRecordsAsync(query, pageModel, orderBy)) { await taskRepo; if (taskRepo.IsCompletedSuccessfully) { return(taskRepo.Result); } else { throw new Exception("Something goes wrong in _repo.GetMainRecordsAsync"); } } }