public BaseReponse <string> GetCountryWithMostUniqueHolidays(long year) { try { var publicHolidaysByCountry = new Dictionary <string, List <PublicHoliday> >(); var holidays = _helper.GetAllHolidays(year); foreach (var holiday in holidays) { if (!publicHolidaysByCountry.ContainsKey(holiday.CountryCode)) { publicHolidaysByCountry[holiday.CountryCode] = new List <PublicHoliday>(); } publicHolidaysByCountry[holiday.CountryCode].Add(holiday); } var uniqueHolidaysByCountry = new Dictionary <string, List <PublicHoliday> >(); foreach (KeyValuePair <string, List <PublicHoliday> > element in publicHolidaysByCountry) { uniqueHolidaysByCountry[element.Key] = new List <PublicHoliday>(); uniqueHolidaysByCountry[element.Key].AddRange(element.Value.Where(x => !holidays.Any(y => y.Date == x.Date && y.CountryCode != element.Key)).ToList()); } uniqueHolidaysByCountry = uniqueHolidaysByCountry.OrderByDescending(x => x.Value.Count).ToDictionary(z => z.Key, y => y.Value); var countryWithMostUniqueHolidays = uniqueHolidaysByCountry.FirstOrDefault(); var countryInfo = HolidaysApiWrapper <Country> .GetCountryInfo(countryWithMostUniqueHolidays.Key).Result; countryWithMostUniqueHolidays.Value.ForEach(x => x.Id = 0); var response = new BaseReponse <string> { Response = countryInfo.OfficialName, StatusCode = HttpStatusCode.OK, Exception = null }; return(response); } catch (Exception ex) { BaseReponse <string> response = new BaseReponse <string> { Response = null, StatusCode = HttpStatusCode.BadRequest, Exception = ex }; return(response); } }
public BaseReponse <string> GetCountryWithMostHolidays(long year) { try { var publicHolidaysByCountry = new Dictionary <string, int>(); var holidays = _helper.GetAllHolidays(year); foreach (var holiday in holidays) { if (!publicHolidaysByCountry.ContainsKey(holiday.CountryCode)) { publicHolidaysByCountry[holiday.CountryCode] = 0; } publicHolidaysByCountry[holiday.CountryCode] += 1; } publicHolidaysByCountry = publicHolidaysByCountry.OrderByDescending(x => x.Value).ToDictionary(z => z.Key, y => y.Value); var countryWithMostHolidaysCode = publicHolidaysByCountry.FirstOrDefault().Key; var countryInfo = HolidaysApiWrapper <Country> .GetCountryInfo(countryWithMostHolidaysCode).Result; BaseReponse <string> response = new BaseReponse <string> { Response = countryInfo.CommonName, StatusCode = HttpStatusCode.OK, Exception = null }; return(response); } catch (Exception ex) { BaseReponse <string> response = new BaseReponse <string> { Response = null, StatusCode = HttpStatusCode.BadRequest, Exception = ex }; return(response); } }