public async Task <IActionResult> Index() { var resApi = new WeatherApiResponse(); var displayResApi = new WeatherApiResponse(); try { if (SignInManager.IsSignedIn(User)) { var loggedInUser = HttpContext.Session.GetString(ApplicationVariables.SessionVariables.UserEmail); List <string> listOfCities = new List <string>(); List <WeatherApiResponse> weatherApiResponsesList = new List <WeatherApiResponse>(); List <Employee> listOfEmployees = _userRepositoriesInterface.GetEmployees(); //Fetch cities stored in database var cities = _userRepositoriesInterface.GetCities(); //populatelist of cities if (listOfEmployees != null && listOfEmployees.Count > 0) { foreach (var emp in listOfEmployees) { listOfCities.Add(_userRepositoriesInterface.GetEmployeeByEmail(emp.Email).City.CityName); } } //checks if city list is empty if (listOfCities != null && listOfEmployees.Count > 0) { List <string> cityDataFetched = new List <string>(); //populate list of weatherdata responses per city foreach (var city in listOfCities) { //Fetch data from api if (!cityDataFetched.Contains(city)) { resApi = await _weatherInterface.GetWeatherAsync(city); if (resApi.list != null) { weatherApiResponsesList.Add(resApi); cityDataFetched.Add(city); } } } } //checks if weatherlist is empty if (weatherApiResponsesList != null && weatherApiResponsesList.Count > 0) { //populate weather data to be displayed for current users city foreach (var emp in listOfEmployees) { if (emp.Email.ToLower().Trim() == loggedInUser.ToLower().Trim()) { foreach (var weather in weatherApiResponsesList) { int cityID = -1; cityID = cities.Where(x => x.CityName.Contains(weather.city.name.Trim())) .FirstOrDefault().ID; if (emp.CityID == cityID && emp.Email.ToLower().Trim() == loggedInUser.ToLower().Trim()) { displayResApi.list = new List <List>(); //Use data from db foreach (var item in weather.list) { if (Convert.ToDateTime(item.dt_txt).Date >= DateTime.Now.Date) { displayResApi.city = new City(); displayResApi.city.name = weather.city.name; List list = new List(); list.dt_txt = item.dt_txt; list.weather = new List <Weather>(); Weather weatherForList = new Weather(); weatherForList.main = item.weather[0].main; weatherForList.description = item.weather[0].description; list.weather.Add(weatherForList); displayResApi.list.Add(list); } } break; } } break; } } foreach (var weather in weatherApiResponsesList) { int cityID = -1; cityID = cities.Where(x => x.CityName.Contains(weather.city.name.Trim())) .FirstOrDefault().ID; //Fetch Weather Data from db var resFromDB = _weatherRepositoryInterface.GetWeatherDataByCityID(cityID); if (resFromDB != null && resFromDB.Count > 0) { var todaysDate = DateTime.Now.Date; var tommorrowsDate = DateTime.Now.AddDays(1).Date; //checks to se if there is a record of weather data in db for today var hasTodayWData = resFromDB.Find(x => x.Day.Date == todaysDate); //checks to se if there is a record of weather data in db for the next day var hasTomorrowsWData = resFromDB.Find(x => x.Day.Date == tommorrowsDate); //if there is no data in db for today and tomorrow fetch data from api if (hasTodayWData == null && hasTomorrowsWData == null) { //Fetch Weather Data from api resApi = await _weatherInterface.GetWeatherAsync(weather.city.name); //populate weather info to store in db foreach (var wData in resApi.list) { WeatherInfo weatherInfo = new WeatherInfo(); weatherInfo.Day = Convert.ToDateTime(wData.dt_txt); if (weatherInfo.Day.Hour == 6) { weatherInfo.WeatherDesc = wData.weather[0].main + ": " + wData.weather[0].description; weatherInfo.CityID = cityID; //Store Weather data _weatherRepositoryInterface.StoreWeatherData(weatherInfo); } } } } } } } }catch (Exception ex) { _logger.LogError("Error:" + ex.Message); } return(View(displayResApi)); }
public async Task <IActionResult> Register() { RegisterViewModel rvm = new RegisterViewModel(); try { rvm.city = _userRepositoriesInterface.GetCities(); rvm.position = _userRepositoriesInterface.GetPositions(); List <Role> roleList = new List <Role>(); Role role1 = new Role(); Role role2 = new Role(); Role role3 = new Role(); Role role4 = new Role(); role1.Name = ApplicationVariables.Roles.Employee; roleList.Add(role1); role2.Name = ApplicationVariables.Roles.Manager; roleList.Add(role2); role3.Name = ApplicationVariables.Roles.ITAdmin; roleList.Add(role3); role4.Name = ApplicationVariables.Roles.CEO; roleList.Add(role4); //Create Roles If Not Exist foreach (var roleItem in roleList) { var roleExist = await roleManager.FindByNameAsync(roleItem.Name); if (roleExist == null) { IdentityRole identityRole = new IdentityRole { Name = roleItem.Name }; IdentityResult roleResult = await roleManager.CreateAsync(identityRole); } } rvm.Role = new List <Role>(); foreach (var role in roleManager.Roles) { Role roleItem = new Role(); roleItem.Id = role.Id; roleItem.Name = role.Name; rvm.Role.Add(roleItem); } //Set roles session HttpContext.Session.SetString(ApplicationVariables.SessionVariables.Roles, JsonConvert.SerializeObject(rvm.Role)); var userCeatedSuccess = HttpContext.Session.GetString(ApplicationVariables.SessionVariables.AddUserSuccess); if (userCeatedSuccess != null) { ViewBag.AddUserSuccess = userCeatedSuccess; HttpContext.Session.SetString(ApplicationVariables.SessionVariables.AddUserSuccess, "none"); } return(View(rvm)); } catch (Exception ex) { _logger.LogError("Error:" + ex.Message); return(RedirectToAction("index", "home")); } }