public async Task InputHolidays() { string json = string.Empty; using (var httpClient = new HttpClient()) { json = await httpClient.GetStringAsync(_configuration["UrlNationalHolidaysJSON"]); } if (!string.IsNullOrEmpty(json)) { var holidays = JsonConvert.DeserializeObject <List <HolidayInputDTO> >(json); foreach (var holiday in holidays) { if (!string.IsNullOrEmpty(holiday.Date)) { var exists = await _holidayRepository.ExistsHolidayByDate(holiday.Date); if (!exists) { await _holidayRepository.AddAsync(_mapper.Map <Holiday>(holiday)); } } else if (holiday.VariableDates.Count > 0) { var existingHoliday = await _holidayRepository.GetHolidayByTitle(holiday.Title); if (existingHoliday == null) { await _holidayRepository.AddAsync(_mapper.Map <Holiday>(holiday)); } else { foreach (var variableDate in holiday.VariableDates) { var exists = existingHoliday.HolidayVariableDates.Any(x => x.Year == variableDate.Key && x.Date == variableDate.Value); if (!exists) { var holidayVariableDate = _mapper.Map <HolidayVariableDate>(variableDate); holidayVariableDate.Holiday = existingHoliday; await _holidayVariableDateRepository.AddAsync(holidayVariableDate); } } } } } await _unitOfWork.CommitAsync(); } }
public async Task <HolidayModel> AddAsync(HolidayModel model, int clientId, int userId) { if (model == null) { throw new ArgumentNullException(); } if (!await _permissionManager.HasPermission(clientId, userId, Permission.CanAddHoliday)) { throw new Exception("User has not permission to perform this operation"); } var result = await _holidayRepository.IsHolidayExistsAsync(clientId, model.Id, model.StartDate, model.EndDate); if (result) { throw new Exception("Already inserted holiday"); } var holiday = _holidayMapper.ConvertToDataModel(model); holiday.ClientId = clientId; holiday = await _holidayRepository.AddAsync(holiday); return(_holidayMapper.ConvertToModel(holiday)); }