public async Task <ActionResult> Edit(int?id) { var currentUserId = User.Identity.GetUserId(); if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var kheechEditViewModels = new KheechEditViewModels(); kheechEditViewModels.KheechEvent = await _context.KheechEvents.Include(k => k.ApplicationUser) .Include(k => k.KheechComments) .Include(k => k.KheechUsers) .Include(k => k.Location) .FirstOrDefaultAsync(k => k.Id == id); if (kheechEditViewModels.KheechEvent == null) { return(HttpNotFound()); } var friends = await _context.Friendships .Include(f => f.Initiator) .Include(f => f.Recipient) .Include(f => f.FriendshipStatus) .Where(f => f.InitiatorId == currentUserId || f.RecipientId == currentUserId).Distinct().ToListAsync(); foreach (var item in friends) { if (item.RecipientId == currentUserId) { var friendViewModel1 = new FriendViewModel { Id = item.InitiatorId, Name = item.Initiator.FirstName }; kheechEditViewModels.Friends.Add(friendViewModel1); } else { var friendViewModel = new FriendViewModel { Id = item.RecipientId, Name = item.Recipient.FirstName }; kheechEditViewModels.Friends.Add(friendViewModel); } } kheechEditViewModels.WhereToMeet = kheechEditViewModels.KheechEvent.Location.Name; //ViewBag.ApplicationUserId = new SelectList(db.ApplicationUsers, "Id", "FirstName", kheechEvent.ApplicationUserId); ViewBag.GroupId = new SelectList(_context.Groups, "Id", "Name", kheechEditViewModels.KheechEvent.GroupId); //ViewBag.LocationId = new SelectList(_context.Locations, "Id", "Name", kheechEditViewModels.KheechEvent.LocationId); return(View(kheechEditViewModels)); }
public async Task <ActionResult> Edit(int id, KheechEditViewModels kheechEditViewModels) { if (ModelState.IsValid) { var kheechEventOld = await _context.KheechEvents.Include(k => k.Location).FirstOrDefaultAsync(k => k.Id == kheechEditViewModels.KheechEvent.Id); bool isKheechChanged = false; if ((kheechEventOld.StartDate != kheechEditViewModels.KheechEvent.StartDate) || (kheechEventOld.Location.Name != kheechEditViewModels.WhereToMeet)) { kheechEventOld.StartDate = kheechEditViewModels.KheechEvent.StartDate; kheechEventOld.EndDate = kheechEditViewModels.KheechEvent.StartDate.AddHours(2); kheechEventOld.InsertDate = DateTime.UtcNow; var googleLocation = kheechEditViewModels.WhereToMeet.Split(',').ToList(); var locationToMeet = googleLocation[0]; var Counter = googleLocation.Count; var location = await _context.Locations.FirstOrDefaultAsync(l => l.Name == locationToMeet); if (location == null) { location = new Location { Name = locationToMeet, address1 = googleLocation[Counter - 4], Country = googleLocation[Counter - 1], City = googleLocation[Counter - 3], State = googleLocation[Counter - 2], InsertDate = DateTime.UtcNow }; _context.Locations.Add(location); await _context.SaveChangesAsync(); } kheechEventOld.LocationId = location.Id; isKheechChanged = true; } foreach (var kuser in kheechEditViewModels.WhoToMeet) { var existingKheechUser = await _context.KheechUsers.FirstOrDefaultAsync(k => k.KheechEventId == id && k.ApplicationUserId == kuser); if (existingKheechUser == null) { var kheechUser = new KheechUser { KheechEventId = kheechEditViewModels.KheechEvent.Id, ApplicationUserId = kuser, IsAccepted = false }; _context.KheechUsers.Add(kheechUser); } } if (isKheechChanged) { var oldKheechUsers = await _context.KheechUsers.Where(k => k.KheechEventId == kheechEditViewModels.KheechEvent.Id).ToListAsync(); foreach (var kuser in oldKheechUsers) { kuser.IsAccepted = false; _context.Entry(kuser).State = EntityState.Modified; } } kheechEventOld.EventName = kheechEditViewModels.KheechEvent.EventName; _context.Entry(kheechEventOld).State = EntityState.Modified; await _context.SaveChangesAsync(); return(RedirectToRoute("SendKheechNotification", new { id = kheechEventOld.Id, returnFrom = "Edit" })); } //ViewBag.ApplicationUserId = new SelectList(db.ApplicationUsers, "Id", "FirstName", kheechEvent.ApplicationUserId); ViewBag.GroupId = new SelectList(_context.Groups, "Id", "Name", kheechEditViewModels.KheechEvent.GroupId); ViewBag.LocationId = new SelectList(_context.Locations, "Id", "Name", kheechEditViewModels.KheechEvent.LocationId); return(View(kheechEditViewModels)); }