public SendInfo Update(SendInfo sendInfo) { SendInfo result = null; try { using (var dbCtx = new WeatherAppDbEntities()) { result = dbCtx.SendInfo.SingleOrDefault(info => info.UserName == sendInfo.UserName); if (result != null) { result.Merge(sendInfo); dbCtx.SaveChanges(); return result; } else throw new DaoException("No user " + sendInfo.UserName + " found in database"); } } catch (Exception e) { throw new DaoException("Error updating user " + sendInfo.UserName + " in database. " + e.Message, e); } }
/// <summary> /// Deletes token from database /// </summary> /// <param name="token"></param> public void DeleteToken(string token, long userId) { try { using (var dbCtx = new WeatherAppDbEntities()) { //var singleOrDefault = dbCtx.DeviceInfo.SingleOrDefault(info => info.token == token); //var userId = dbCtx.SendInfo.FirstOrDefault(info => info.UserName == userName).Id; var singleOrDefault = dbCtx.DeviceInfo.SingleOrDefault(info => info.userId == userId && info.token == token); if (singleOrDefault != null) { dbCtx.DeviceInfo.Remove(singleOrDefault); dbCtx.SaveChanges(); logger.InfoFormat(string.Format("Token {0} successfully removed from database", token)); } else { logger.InfoFormat(string.Format("Token {0} is already removed from database", token)); throw new DaoException(string.Format("Token {0} is already removed from database", token)); } } } catch (Exception e) { throw new DaoException(string.Format("Error deleting device token {0}. {1}.", token, e.Message), e); } }
public Recommendation Update(Recommendation rec) { Recommendation result = null; try { using (var dbCtx = new WeatherAppDbEntities()) { result = dbCtx.Recommendations.SingleOrDefault(info => info.GroupItemId == rec.GroupItemId); if (result != null) { result.Condition = rec.Condition; result.Forecast = rec.Forecast; result.Language = rec.Language; result.MelanomaRiskId = rec.MelanomaRiskId; result.VitaminDId = rec.VitaminDId; dbCtx.SaveChanges(); return result; } else throw new DaoException("No recommendation groupId" + rec.GroupItemId + " found in database"); } } catch (Exception e) { throw new DaoException("Error updating recommendation " + rec.GroupItemId + " in database. " + e.Message, e); } }
public SendInfo Insert(SendInfo info) { try { using (var dbCtx = new WeatherAppDbEntities()) { dbCtx.SendInfo.Add(info); dbCtx.SaveChanges(); } return info; } catch (Exception e) { throw new DaoException("Error inserting user " + info.UserName + " in database", e); } }
public Recommendation Insert(Recommendation rec) { try { using (var dbCtx = new WeatherAppDbEntities()) { dbCtx.Recommendations.Add(rec); dbCtx.SaveChanges(); } return rec; } catch (Exception e) { throw new DaoException("Error inserting recommendation " + rec.Forecast + " in database", e); } }
public UserInfo SaveUser(UserInfo info) { try { using (var dbCtx = new WeatherAppDbEntities()) { var firstOrDefault = dbCtx.UserInfo.FirstOrDefault(user => user.UserName == info.UserName); if (firstOrDefault == null) { dbCtx.UserInfo.Add(info); dbCtx.SaveChanges(); return info; } return firstOrDefault; } } catch (Exception e) { throw new DaoException("Error inserting new user token info " + info, e); } }
/// <summary> /// Updates entry /// </summary> /// <param name="ui"></param> /// <returns></returns> public UserInfo UpdateToken(UserInfo ui) { using (var _ctx = new WeatherAppDbEntities()) { var _userInfo = _ctx.UserInfo.FirstOrDefault(info => info.Id == ui.Id); _userInfo.AuthToken = ui.AuthToken; _ctx.SaveChanges(); return _userInfo; } }
public Forecast10Root GetForecast10(string city) { if (!string.IsNullOrEmpty(userName)) { using (var _ctx = new WeatherAppDbEntities()) { var _si = _ctx.SendInfo.FirstOrDefault(info => info.UserName == userName); if (_si == null) { _si = new SendInfo(userName); _ctx.SendInfo.Add(_si); } if (_si.WeatherUpdateDt.HasValue && DateTime.Now.Subtract(_si.WeatherUpdateDt.Value).TotalSeconds < WEATHER_UPD_SECONDS && _si.City == city && _si.LastWeatherUpdate != null) { var _deserializeObject = JsonConvert.DeserializeObject<Forecast10Root>(_si.LastWeatherUpdate); if (_deserializeObject.forecast != null) return _deserializeObject; } var _forecast10Impl = GetForecast10Impl(city); if (_forecast10Impl == null) return null; _si.LastWeatherUpdate = JsonConvert.SerializeObject(_forecast10Impl); _si.WeatherUpdateDt = DateTime.Now; _si.City = city; _ctx.SaveChanges(); return _forecast10Impl; } } return GetForecast10Impl(city); }
public void UpdateToken(string oldId, string newId) { try { using (var dbCtx = new WeatherAppDbEntities()) { var singleOrDefault = dbCtx.DeviceInfo.SingleOrDefault(info => info.token == oldId); if (singleOrDefault != null) { singleOrDefault.token = newId; dbCtx.SaveChanges(); logger.ErrorFormat("Old Token {0} successfully updated with new {1} ", oldId, newId); } else throw new DaoException(string.Format("No device token {0} found in database. ", oldId)); } } catch (Exception e) { throw new DaoException(string.Format("Error updating device token {0}. {1}", oldId, e.Message), e); } }
public void SaveToken(DeviceInfo tokenInfo) { try { using (var dbCtx = new WeatherAppDbEntities()) { dbCtx.DeviceInfo.Add(tokenInfo); dbCtx.SaveChanges(); } } catch (Exception e) { throw new DaoException(string.Format("Error inserting device token {0}. "+e.Message, tokenInfo.token), e); } }
/// <summary> /// Checks all users and sends emails/sms/on-site-alerts when appropriate /// </summary> public void CheckAllAndSendEmails() { using (var _ctx = new WeatherAppDbEntities()) { var _api = new MandrillApi(Options.MandrillApi); var _infos = _ctx.SendInfo.Where(info => (info.SendEmail ?? false) || (info.SendSms ?? false)).ToList(); foreach (var _info in _infos) { var _mode = _info.Temperature ?? TemperatureMode.F; var _rrb = new PersonalizedForecastResultBuilder(_info.UserName, _mode); LogManager.GetLogger(GetType()).Debug("Processing sendInfo:"+_info.Id); if (IsRightTime(_info)) { LogManager.GetLogger(GetType()).Debug("Processing sendInfo, time was right:" + _info.Id); var _weatherWorker = new WeatherWorker(_info.UserName); var _forecastRoot = _weatherWorker.GetForecast10(_info.City); LogManager.GetLogger(GetType()).Debug("Received forecast:" + _info.Id); if (string.IsNullOrEmpty(_info.DataFileId)) continue; var _jobId = GetJobId(_info); LogManager.GetLogger(GetType()).Debug("Started job:" + _info.Id); var _riskValue = _rrb.GetAppChainResultingRisks(_jobId.Item1.ToString(), _jobId.Item2.ToString()); var _alertCode = _forecastRoot.alerts.Count == 0 ? "--" : _forecastRoot.alerts[0].type; var _riskDescription = _rrb.GetPersonalizedRiskDescription(_forecastRoot.forecast.simpleforecast.forecastday[0].conditions, _alertCode, _riskValue, _info.UserName, Options.ApplicationName); var _subj = string.Format("Forecast for " + DateTime.Now.ToString("dddd MMMM d")); var _city = WeatherWorker.ConvertFromIDToName(_info.City); var _time = _forecastRoot.current_observation.observation_time; var _todayForecast = _mode == TemperatureMode.F ? _forecastRoot.forecast.txt_forecast.forecastday[0].fcttext : _forecastRoot.forecast.txt_forecast.forecastday[0].fcttext_metric; var _currentObservation = _forecastRoot.current_observation.weather + " and " + (_mode == TemperatureMode.F ? _forecastRoot.current_observation.temp_f + "F" : _forecastRoot.current_observation.temp_c + "C"); if (_info.SendSms ?? false) { LogManager.GetLogger(GetType()).Debug("Sending sms:" + _info.Id); SendSmsNotification(_info, _city, _todayForecast, _currentObservation, _riskDescription); } if (notificationService.IsUserSubscribed(_info.Id)) { LogManager.GetLogger(GetType()).Debug("Sending push:" + _info.Id); SendPushNotification(_info, _city, _todayForecast, _currentObservation, _riskDescription); } if (_info.SendEmail ?? false) { LogManager.GetLogger(GetType()).Debug("Sending email:" + _info.Id); SendEmailNotification(_info, _city, _todayForecast, _currentObservation, _riskDescription, _forecastRoot, _mode, _api, _subj); } _info.LastSendDt = DateTime.Now; _ctx.SaveChanges(); } } } }