public List <PublicHoliday> GetAllHolidays(long year) { var holidaysToInsert = new List <PublicHoliday>(); var allHolidays = new List <PublicHoliday>(); var holidaysFromDb = _repository.GetPulbicHolidaysByYear(year); foreach (var enumValue in Enum.GetValues(typeof(CountryCodesEnum))) { var holidays = new List <PublicHoliday>(); if (holidaysFromDb.Any(x => x.CountryCode == enumValue.ToString() && x.Date.Year == year)) { holidays = holidaysFromDb.Where(x => x.CountryCode == enumValue.ToString()).ToList(); } else { holidays = HolidaysApiWrapper <List <PublicHoliday> > .GetPublicHolidays(year, enumValue.ToString()).Result; holidays.ForEach(x => x.EndDate = x.Date.AddHours(24)); holidaysToInsert.AddRange(holidays); } allHolidays.AddRange(holidays); //publicHolidaysByCountry.Add(enumValue.ToString(), holidays.Count); } _repository.InsertHolidaysAsync(holidaysToInsert); return(allHolidays); }
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 void Execute() { var year = DateTime.Now.Year; _repository.DeleteHolidaysByYear(year); foreach (var enumValue in Enum.GetValues(typeof(CountryCodesEnum))) { var holidays = HolidaysApiWrapper <List <PublicHoliday> > .GetPublicHolidays(year, enumValue.ToString()).Result; holidays.ForEach(x => x.EndDate = x.Date.AddHours(24)); _repository.InsertHolidays(holidays); } }
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); } }