public async Task<ActionResult> Index(string[] SelectedCities, Notification notification) { if (Session["User"] == null) { return RedirectToAction("LogOff", "Account"); } User user = Session["User"] as User; if (SelectedCities == null) { TempData["Message"] = "Seleccione al menos una ciudad."; var response = await cities.GetCities(); ViewBag.CitiesList = response.Data; return View(notification); } try { List<Notification> notifications = new List<Notification>(); foreach (string city in SelectedCities) { notifications.Add(new Notification() { ID_CITY = city, ID_DEPENDENCY =user.ID_DEPENDENCY, NOTIFICATION_TEXT = notification.NOTIFICATION_TEXT, CREATED_DATE = DateTime.Now }); } NotificationRepository notificationRespository = new NotificationRepository(); RepositoryResponse<List<Notification>> response = await notificationRespository.AddNotifications(notifications); if (response.Success) { TempData["Message"] = MvcHtmlString.Create("Notificación enviada exitosamente."); } else { TempData["Message"] = "Se produjo un error, por favor intente nuevamente."; } } catch(Exception ex){ TempData["Message"] = "Se produjo un error, por favor intente nuevamente."; Trace.TraceError(ex.Message); } return RedirectToAction("Index"); }
public void AddNotifications_Success() { NotificationRepository repository = new NotificationRepository(); Notification notification = new Notification { NOTIFICATION_TEXT="Unit Test notification", ID_DEPENDENCY = "iIAW2gamws", ID_CITY = "vxwnP2GrHn" }; RepositoryResponse<Notification> expected = new RepositoryResponse<Notification> { Success = true, Data= notification }; RepositoryResponse<Notification> result = repository.AddNotification(notification).GetAwaiter().GetResult(); Assert.IsTrue(result.Success, "Notifications not created"); Assert.IsNotNull(result.Data); Assert.IsNull(result.Error,"Some error ocurred"); }
public async Task<RepositoryResponse<Notification>> AddNotification(Notification notification) { var response = new RepositoryResponse<Notification> { }; try { var Noti = Parse.ParseObject.Create("Notification"); #region Add Dependency Relationship DependencyRepository DependencyContext = new DependencyRepository(); RepositoryResponse<ParseObject> Dependency = await DependencyContext.GetDependencyById(notification.ID_DEPENDENCY); ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Dependency"); IEnumerable<ParseObject> result = await query.FindAsync(); #endregion #region Add City Relationship CityRepository CityContext = new CityRepository(); RepositoryResponse<ParseObject> City = await CityContext.GetCityById(notification.ID_CITY); query = new ParseQuery<ParseObject>("City"); result = await query.FindAsync(); #endregion var relation = Noti.GetRelation<ParseObject>("ID_Dependency"); relation.Add(Dependency.Data); relation = Noti.GetRelation<ParseObject>("ID_City"); relation.Add(City.Data); var message = string.Format("{0} > {1}: {2}", Dependency.Data["Name"].ToString(), City.Data["Name"].ToString(), notification.NOTIFICATION_TEXT); Noti.Add("NotificationText", message); await Noti.SaveAsync(); await Noti.SaveAsync(); var push = new ParsePush(); push.Query = from installation in ParseInstallation.Query where installation.Get<string>("City").Contains(notification.ID_CITY) select installation; push.Alert = message; await push.SendAsync(); notification.ID = Noti.ObjectId; response.Data = notification; response.Success = true; } catch (Exception ex) { response.Success = false; response.Error = ex; } return response; }