public IActionResult Create(DActivity active) { if (ModelState.IsValid) { DateTime ActivityDate = DateTime.Parse(active.Date); DateTime CurrentDate = DateTime.Today; if (ActivityDate < CurrentDate) { User User1 = context.Users.FirstOrDefault(u => u.UserId == HttpContext.Session.GetInt32("UserId")); ViewBag.User = User1; ModelState.AddModelError("Date", "Date cannot be in the past"); return(View("New")); } active.UserId = (int)HttpContext.Session.GetInt32("UserId"); Join newJoin = new Join(); context.Activities.Add(active); context.SaveChanges(); newJoin.ActivityId = active.ActivityId; newJoin.UserId = active.UserId; context.Joins.Add(newJoin); context.SaveChanges(); // return Redirect("Dashboard"); return(Redirect($"activity/{active.ActivityId}")); } User User = context.Users.FirstOrDefault(u => u.UserId == HttpContext.Session.GetInt32("UserId")); ViewBag.User = User; return(View("New")); }
public IActionResult Delete(int id) { DActivity Activity = context.Activities.FirstOrDefault(a => a.ActivityId == id); context.Activities.Remove(Activity); context.SaveChanges(); return(RedirectToAction("Dashboard", "Activity")); }
public IActionResult Show(int id) { string[] keys = HttpContext.Session.Keys.ToArray(); if (keys.Contains("UserId")) { User User = context.Users.FirstOrDefault(u => u.UserId == HttpContext.Session.GetInt32("UserId")); DActivity Activity = context.Activities.Include(x => x.Planner).Include(y => y.Participants).ThenInclude(z => z.User).FirstOrDefault(act => act.ActivityId == id); ViewBag.Activity = Activity; ViewBag.User = User; return(View("Display")); } else { return(RedirectToAction("Index")); } }
public async Task <List <string> > CreateAvailableActivitiesAsync() { var activities = await _context.Activities.ToListAsync(); var activeUsers = new List <string>(); foreach (var activity in activities) { var users = GetAvailableUsers(); var selectedUsers = users .Where(user => user.UserActivities.Where(item => item.ActivityId == activity.Id && item.UserId == user.Id) .OrderByDescending(item => item.Probability) .Take(2) .Any(item => item.ActivityId == activity.Id)) .ToList(); if (selectedUsers.Count >= activity.MinUsersCount) { var dActivity = new DActivity { ActivityId = activity.Id, Start = DateTimeOffset.UtcNow }; await _context.DActivities.AddAsync(dActivity); await _context.SaveChangesAsync(); var count = Math.Min(selectedUsers.Count, activity.MaxUsersCount); foreach (var selectedUser in selectedUsers.Take(count)) { var dUser = new DUser { UserId = selectedUser.Id, DActivityId = dActivity.Id }; await _context.DUsers.AddAsync(dUser); await _context.SaveChangesAsync(); activeUsers.Add(selectedUser.Id); } } } return(activeUsers); }