public async Task <IActionResult> Edit(int id, [Bind("Topic,Id,Name,StartDate,EndDate,ConferenceVersionId,FileId,RoomId,Exhibitor,AvailableTags")] PracticalSession practicalSession) { if (id != practicalSession.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var eventTags = new List <EventTag>(); if (practicalSession.AvailableTags != null) { for (var i = 0; i < practicalSession.AvailableTags.Count; i++) { var existingEventTag = await _context.EventTags.FirstOrDefaultAsync(x => x.TagId == practicalSession.AvailableTags[i].TagId && x.EventId == practicalSession.Id); if (practicalSession.AvailableTags[i].IsChecked) { // si el tag está checkeado y ya exitía este eventTag, no hacer nada, sino crearlo if (existingEventTag == null) { var tag = await _context.Tags.FirstOrDefaultAsync(x => x.Id == practicalSession.AvailableTags[i].TagId); var newEventTag = new EventTag() { Event = practicalSession, EventId = practicalSession.Id, Tag = tag, TagId = tag.Id }; _context.Add(newEventTag); eventTags.Add(newEventTag); } } else { // si no está checkeado y ya exitía este eventTag, eliminarlo, sino no hacer nada if (existingEventTag != null) { _context.EventTags.Remove(existingEventTag); } } } } practicalSession.EventTags = eventTags; // actualizamos los eventTags del chat _context.Update(practicalSession); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PracticalSessionExists(practicalSession.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details), new { id = practicalSession.Id.ToString() })); } return(View(practicalSession)); }
public async Task <IActionResult> Create([Bind("Topic,Id,Name,StartDate,EndDate,ConferenceVersionId,RoomId,Exhibitor,AvailableTags")] PracticalSession practicalSession) { // var conferenceVersion = await _context.ConferenceVersions.Where(x => x.Id == practicalSession.ConferenceVersionId).FirstOrDefaultAsync(); // if (conferenceVersion.StartDate > practicalSession.StartDate || conferenceVersion.EndDate < practicalSession.EndDate) // { // // hay problemas con la fecha // TempData["DateError"] = "Valor temporal"; // } // else // { // if (ModelState.IsValid) // { // _context.Add(practicalSession); // await _context.SaveChangesAsync(); // return RedirectToAction(nameof(Details), new { id = practicalSession.Id.ToString() }); // } // } // return RedirectToAction("Index", "Event"); //} var conferenceVersion = await _context.ConferenceVersions.Where(x => x.Id == practicalSession.ConferenceVersionId).FirstOrDefaultAsync(); var events = await _context.Events.Where(x => x.ConferenceVersionId == conferenceVersion.Id).ToListAsync(); var room = await _context.Rooms.Where(x => x.Id == practicalSession.RoomId).FirstOrDefaultAsync(); var isOccupied = 0; var sharedRoomEvents = await _context.Events.Where(x => x.ConferenceVersionId == practicalSession.ConferenceVersionId && x.RoomId == practicalSession.RoomId).ToListAsync(); foreach (var even in sharedRoomEvents) { if (practicalSession.StartDate <= even.StartDate && practicalSession.EndDate >= even.StartDate) { isOccupied = 1; } else if (practicalSession.StartDate <= even.EndDate && practicalSession.EndDate >= even.EndDate) { isOccupied = 1; } else if (practicalSession.StartDate >= even.StartDate && practicalSession.EndDate <= even.EndDate) { isOccupied = 1; } else if (practicalSession.StartDate <= even.StartDate && practicalSession.EndDate >= even.EndDate) { isOccupied = 1; } if (isOccupied == 1) { TempData["RoomError"] = "Valor Temporal"; break; } } if (isOccupied == 0) { if (conferenceVersion.StartDate > practicalSession.StartDate || conferenceVersion.EndDate < practicalSession.EndDate) { // hay problemas con la fecha TempData["DateError"] = "Valor temporal"; } else { if (ModelState.IsValid) { var eventTags = new List <EventTag>(); if (practicalSession.AvailableTags != null) { for (var i = 0; i < practicalSession.AvailableTags.Count; i++) { if (practicalSession.AvailableTags[i].IsChecked) { var tag = await _context.Tags.FirstOrDefaultAsync(m => m.Id == practicalSession.AvailableTags[i].TagId); var eventTag = new EventTag() { Event = practicalSession, EventId = practicalSession.Id, Tag = tag, TagId = tag.Id }; _context.Add(eventTag); eventTags.Add(eventTag); } } } practicalSession.EventTags = eventTags; _context.Add(practicalSession); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Details), new { id = practicalSession.Id.ToString() })); } } } return(RedirectToAction("Index", "Event")); }