/// <summary>
        /// 1- Check input consitency.
        /// 2- Convert String to enum and call CoreCountriesService
        /// </summary>
        /// <param name="service">a Countryservice</param>
        /// <param name="continentId">a contientID (e.g : Africa)</param>
        /// <returns>Countries. Does not throw any exception. </returns>
        public async Task <(int, IEnumerable <HRCountry>)> GetHRCountriesByContinentAsync(ICoreCountriesService service, String continentId)
        {
            //1-
            Region region;

            if (service == null)
            {
                return(StatusCodes.Status500InternalServerError, null);
            }
            //2-
            if (!Enum.TryParse(continentId, out region))
            {
                return(StatusCodes.Status400BadRequest, null);
            }
            else
            {
                try
                {
                    using (Task <IEnumerable <HRCountry> > task = service.GetHRCountriesByContinentAsync(region))
                    {
                        await task;
                        return(StatusCodes.Status200OK, task.Result);
                    }
                }
                catch (Exception)
                {
                    return(StatusCodes.Status500InternalServerError, null);
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Version 1 : Get HRCountry then HRBorder one by one.
 /// 1- Get Corresponding HRCountries
 /// 2- Foreach HRCountry get single HRBorders and push it in retrun Enumerable
 /// </summary>
 /// <param name="region"></param>
 /// <returns></returns>
 public async Task <IEnumerable <HRBorder> > GetHRBordersByContinentAsync(Region region)
 {
     if (_hrCountriesService == null || _bordersRepository == null)
     {
         if (_logger != null)
         {
             _logger.LogError("_hrCountriesService or _bordersRepository is null in HRCoreBordersServices");
         }
         throw new MemberAccessException();
     }
     //1-
     using (Task <IEnumerable <HRCountry> > countriesTask = _hrCountriesService.GetHRCountriesByContinentAsync(region))
     {
         await countriesTask;
         if (countriesTask.IsCompleted)
         {
             //!TODO add robustness and UT
             List <HRBorder>             retour        = new List <HRBorder>();
             Dictionary <String, Region> idsAndRegions = new Dictionary <string, Region>();
             //2-
             foreach (HRCountry iter in countriesTask.Result)
             {
                 idsAndRegions.Add(iter.Alpha2Code, iter.Region);
             }
             using (Task <IEnumerable <HRBorder> > bordersTask = _bordersRepository.GetAsync(idsAndRegions.Keys))
             {
                 await bordersTask;
                 if (bordersTask.IsCompleted)
                 {
                     foreach (HRBorder borderIter in bordersTask.Result)
                     {
                         borderIter.BorderRegion = idsAndRegions[borderIter.ISO2].ToString();
                         retour.Add(borderIter);
                     }
                     return(retour);
                 }
                 else
                 {
                     throw new Exception("GetAsync (HRBorders) fail.");
                 }
             }
         }
         else
         {
             throw new Exception("GetHRCountriesByContinentAsync fail.");
         }
     }
 }