public ActionResult SaveGroupTask(TaskForGroupsViewModel taskForGroupsViewModel) { _context = new ApplicationDbContext(); string id = User.Identity.GetUserId(); if (taskForGroupsViewModel.taskForGroups.description == null || taskForGroupsViewModel.taskForGroups.dateTimeEnd == null || taskForGroupsViewModel.taskForGroups.dateTimeBegin == null) { ApplicationUser employee = _context.Users.SingleOrDefault(emp => emp.Id == id); List <Groups> groupList = _context.Groups.Select(g => g).ToList <Groups>(); var viewModel = new TaskForGroupsViewModel { taskForGroups = new TasksForGroups(), user = employee, groups = groupList }; return(View("CreateTaskForGroups", viewModel)); } if (_context.TasksForGroups.FirstOrDefault(c => c.Id == taskForGroupsViewModel.taskForGroups.Id) == null) { string AdminID = User.Identity.GetUserId(); taskForGroupsViewModel.taskForGroups.fromUserId = taskForGroupsViewModel.user.Id; taskForGroupsViewModel.taskForGroups.toGroupId = taskForGroupsViewModel.group.Id; List <UsersGroups> userGroups = (from gr in _context.UsersGroups.ToList() where gr.GroupId == taskForGroupsViewModel.taskForGroups.toGroupId select gr).ToList(); foreach (UsersGroups userID in userGroups) { if (userID.Id.ToString() != null) { ApplicationUser employeeFrom = _context.Users.SingleOrDefault(emp => emp.Id == AdminID); string Title = employeeFrom.DisplayName; ApplicationUser employeeTo = _context.Users.SingleOrDefault(emp => emp.Id == userID.UserId.ToString()); if (employeeTo.FCMToken != null) { var FCMToken = employeeTo.FCMToken; string token = FCMToken.ToString(); string TouserId = employeeTo.Id; string Message = taskForGroupsViewModel.taskForGroups.title; string Type = "Group"; firebase.FirebaseNotification(token, TouserId, Title, Message, Type); firebase.PushFleet(token, Message, Type); } } var result = taskForGroupsViewModel.taskForGroups; _context.TasksForGroups.Add(result); } } _context.SaveChanges(); return(RedirectToAction("Index", "Tasks")); }
public ActionResult CreateTaskForGroups() { _context = new ApplicationDbContext(); string id = User.Identity.GetUserId(); ApplicationUser employee = _context.Users.SingleOrDefault(emp => emp.Id == id); List <Groups> groupList = _context.Groups.Select(g => g).ToList <Groups>(); var viewModel = new TaskForGroupsViewModel { taskForGroups = new TasksForGroups(), user = employee, groups = groupList }; return(View(viewModel)); }