Exemplo n.º 1
0
        public async Task <IActionResult> GetHolidaysForYear(int year, string countryCode)
        {
            var response = new List <HolidaysForYear>();

            if (_ctx.HolidaysForYear.Any(x => x.CountryCode == countryCode && x.Date.Year == year))
            {
                response = _ctx.HolidaysForYear.Where(x => x.CountryCode == countryCode && x.Date.Year == year)
                           .ToList();
            }

            else
            {
                var holidayList = await _holidayService.GetHolidaysForYear(year, countryCode, "public_holiday");

                foreach (var item in holidayList)
                {
                    response.Add(new HolidaysForYear
                    {
                        Date        = new DateTime(item.Date.Year, item.Date.Month, item.Date.Day),
                        HolidayName = string.Join(", ", item.HolidayName.Select(x => x.Text)),
                        HolidayType = item.HolidayType,
                        CountryCode = countryCode,
                        DayOfWeek   = (DayOfWeek)item.Date.DayOfWeek
                    });
                }

                _ctx.HolidaysForYear.AddRange(response);
                await _ctx.SaveChangesAsync();
            }

            var resultGroupedByMonth = response.GroupBy(x => x.Date.Month)
                                       .Select(x => new { Month = x.Key, Holiday = x.ToList() })
                                       .ToList();

            return(Ok(resultGroupedByMonth));
        }