public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { //Check if guest booking already exists if (_context.Guests.Any(g => g.CustomerId == guestBooking.CustomerId && g.EventId == guestBooking.EventId)) { ModelState.AddModelError(string.Empty, "Booking already exists"); } else { _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Events", new { id = guestBooking.EventId })); } } var @event = await _context.Events.FindAsync(guestBooking.EventId); if (@event == null) { return(BadRequest()); } guestBooking.Customer.Bookings.Add(guestBooking); guestBooking.Event.Bookings.Add(guestBooking); ViewData["EventId"] = guestBooking.EventId; return(View(guestBooking)); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { var existingGuest = _context.Guests.Where(g => g.CustomerId == guestBooking.CustomerId && g.EventId == guestBooking.EventId).ToList(); if ((existingGuest == null || existingGuest.Count == 0) && guestBooking.Attended == true) { var allGuests = _context.Guests.Where(g => g.EventId == guestBooking.EventId).ToList(); EventVM eventVM = new EventVM(await _context.Events.FindAsync(guestBooking.EventId)); if (allGuests.Count >= eventVM.VenueCapacity) { return(BadRequest()); } guestBooking.Attended = false; _context.Add(guestBooking); await _context.SaveChangesAsync(); return(Ok()); } else if (existingGuest != null && existingGuest.Count != 0 && guestBooking.Attended == false) { _context.Remove(existingGuest.FirstOrDefault()); await _context.SaveChangesAsync(); return(Ok()); } } return(BadRequest()); }
public async Task <IActionResult> Edit(int eventid, int customerid, [Bind("CustomerId, EventId, Attended")] GuestBooking GuestBooking) { if (eventid != GuestBooking.EventId || customerid != GuestBooking.CustomerId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(GuestBooking); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GuestExists(GuestBooking.EventId, GuestBooking.CustomerId)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Details", "Events", new { id = eventid })); } return(View(GuestBooking)); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { if (GuestBookingExists(guestBooking.CustomerId, guestBooking.EventId)) { ModelState.AddModelError(String.Empty, "This customer is already a guest at the event."); return(View(guestBooking)); } _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { id = guestBooking.EventId })); } var OnEvent = _context.Guests.Include(c => c.Customer).Where(e => e.EventId == guestBooking.EventId).Select(c => new Customer() { Id = c.CustomerId, Surname = c.Customer.Surname, FirstName = c.Customer.FirstName, Email = c.Customer.Email, Bookings = c.Customer.Bookings }).ToList(); var ToShow = _context.Customers.Where(x => !OnEvent.Contains(x)).ToList(); ViewData["CustomerCheck"] = ToShow == null || ToShow.Count == 0; ViewData["CustomerId"] = new SelectList(ToShow, "Id", "Email"); ViewData["EventId"] = guestBooking.EventId; return(View(guestBooking)); }
public async Task <IActionResult> Edit(int customerid, int eventid, [Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { //Checks to see if the guest booking exists. if (customerid != guestBooking.CustomerId || eventid != guestBooking.EventId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(guestBooking); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GuestBookingExists(guestBooking.CustomerId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } //Loads the viewbag with data that is to be used in the view, i used this method so that different data could be displayed based on the database even if it changes. ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return(View(guestBooking)); }
public async Task <IActionResult> Edit(int customerid, int eventid, [Bind("CustomerId,Customer,EventId,Event,Attended")] GuestBooking guest) { if (eventid != guest.EventId || customerid != guest.CustomerId) { return(NotFound()); } if (ModelState.IsValid) { try { _eventContext.Update(guest); await _eventContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_eventContext.Guests.Any(e => e.CustomerId == guest.CustomerId && e.EventId == guest.EventId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(GuestIndex))); } ViewData["CustomerId"] = new SelectList(_eventContext.Customers, "Id", "Fullname", guest.CustomerId); ViewData["EventId"] = new SelectList(_eventContext.Events, "Id", "Title", guest.EventId); return(View(guest)); }
public async Task <IActionResult> Create([Bind("CustomerId,Customer,EventId,Event,Attended")] GuestBooking guest) { var selectedGuest = _eventContext.Guests .Include(a => a.Event) .Include(a => a.Customer) .FirstOrDefault(a => a.CustomerId == guest.CustomerId && a.EventId == guest.EventId); if (ModelState.IsValid) { try { _eventContext.Add(guest); await _eventContext.SaveChangesAsync(); return(RedirectToAction(nameof(GuestIndex))); } catch (InvalidOperationException) { TempData["msg"] = $"Error : {selectedGuest.Customer.Fullname} and {selectedGuest.Event.Title} already exist!"; } catch (Exception ex) { TempData["msg"] = ex.Message; } } else { TempData["msg"] = "ModelState is not valid, please verify guestbooking model"; } ViewData["CustomerId"] = new SelectList(_eventContext.Customers, "Id", "Fullname", guest.Customer); ViewData["EventId"] = new SelectList(_eventContext.Events, "Id", "Title", guest.Event); return(View(guest)); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBookingCreateViewModel guestBooking) { if (ModelState.IsValid) { GuestBooking existing = await _context.Guests.FindAsync(guestBooking.EventId, guestBooking.CustomerId); if (existing != null) { // Exists ViewData["CustomerId"] = new SelectList(await _context.Customers.Where(x => !x.Deleted).ToListAsync(), "Id", "FullName", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(await _context.Events.Where(x => !x.Cancelled).ToListAsync(), "Id", "Title", guestBooking.EventId); ModelState.AddModelError(string.Empty, "The chosen guest is already booked into the chosen event."); return(View(guestBooking)); } GuestBooking booking = new GuestBooking() { CustomerId = guestBooking.CustomerId, EventId = guestBooking.EventId, Attended = guestBooking.Attended }; _context.Add(booking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CustomerId"] = new SelectList(await _context.Customers.Where(x => !x.Deleted).ToListAsync(), "Id", "FullName", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(await _context.Events.Where(x => !x.Cancelled).ToListAsync(), "Id", "Title", guestBooking.EventId); return(View(guestBooking)); }
/// <summary> /// HTTP GET endpoint for "/GuestBookings/Attend/<paramref name="id"/>?customerId=<paramref name="customerId"/>". <para/> /// Marks attendance for a <see cref="Customer"/> whose Id is <paramref name="customerId"/> on the <see cref="Event"/> /// whose Id is <paramref name="id"/>. /// </summary> /// <param name="id">The <see cref="Event"/>'s Id.</param> /// <param name="customerId">The <see cref="Customer"/>'s Id.</param> /// <returns>Directs the user to the <see cref="Index(int?)"/> view with the filter of <paramref name="id"/>.</returns> public async Task <IActionResult> Attend(int?id, int?customerId) { if (id == null || customerId == null) { return(NotFound()); } GuestBooking booking = await _context.Guests.FindAsync(customerId, id); if (booking == null) { return(NotFound()); } if (booking.Attended == true) // Already attended { return(RedirectToAction(nameof(Index), new { id })); } booking.Attended = true; // Attend _context.Entry(booking).State = EntityState.Modified; await _context.SaveChangesAsync(); if (StringValues.IsNullOrEmpty(Request.Headers["Referer"])) { return(RedirectToAction(nameof(Index))); } return(Redirect(Request.Headers["Referer"].ToString())); }
public async Task <IActionResult> UpdateAttended(int id, [Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { try { _context.Update(guestBooking); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EventExists(guestBooking.EventId)) { return(NotFound()); } else { throw; } } //return RedirectToAction(nameof(Index)); } return(RedirectToAction(nameof(ListGuests), new { id = guestBooking.EventId })); }
public async Task <IActionResult> Edit(int id, [Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (id != guestBooking.CustomerId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(guestBooking); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GuestBookingExists(guestBooking.CustomerId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return(View(guestBooking)); }
public GuestBookingVM(GuestBooking booking, CustomerVM customer, EventVM @event) { CustomerId = booking.CustomerId; Customer = customer; EventId = booking.EventId; Event = @event; Attended = booking.Attended; }
public EventBookingVM(Event e, GuestBooking b) { EventId = e.Id; Title = e.Title; Date = e.Date; Duration = e.Duration; TypeId = e.TypeId; Attended = b.Attended; Type = e.Type; }
public async Task <IActionResult> DeleteConfirmed([Bind("CustomerId,EventId")] GuestBooking guestBooking) { var guestBookingDel = await _context.Guests .Where(g => g.CustomerId == guestBooking.CustomerId) .Where(g => g.EventId == guestBooking.EventId) .FirstOrDefaultAsync(); _context.Guests.Remove(guestBookingDel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
// GET: Guests /Create (Filtered by customer) public IActionResult CustomerFilteredCreate(int?cusId) { ViewData["CustomerId"] = new SelectList(_eventContext.Customers, "Id", "Fullname"); ViewData["EventId"] = new SelectList(_eventContext.Events, "Id", "Title"); var guest = new GuestBooking() { CustomerId = cusId.HasValue ? cusId.Value : 0 }; return(View("Create")); }
public async Task <IActionResult> DeleteConfirmed(int id, [Bind("CustomerId,EventId")] GuestBooking guestBooking) { guestBooking = await _context.Guests .Include(g => g.Customer) .Include(g => g.Event) .FirstOrDefaultAsync(m => m.CustomerId == id); _context.Guests.Remove(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Delete([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { var booking = await _context.Guests .Where(g => g.EventId == guestBooking.EventId) .Where(g => g.CustomerId == guestBooking.CustomerId) .FirstOrDefaultAsync(); _context.Guests.Remove(booking); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Events", new { id = guestBooking.EventId })); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Events")); } ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return(RedirectToAction("Index", "Events")); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CustomerId"] = new SelectList(_context.Customers.Where(c => c.FirstName != "anon"), "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_dataAccess.GetEvents(), "Id", "Title", guestBooking.EventId); return(View(guestBooking)); }
public async Task <IActionResult> Delete(Dictionary <string, int> pairIDs, [Bind("CustomerId,EventId")] GuestBooking guest) { int cusid = pairIDs["CustomerId"]; int eventid = pairIDs["EventId"]; guest = await _eventContext.Guests .Include(g => g.Customer) .Include(g => g.Event) .FirstOrDefaultAsync(m => m.EventId == eventid && m.CustomerId == cusid); try { _eventContext.Guests.Remove(guest); await _eventContext.SaveChangesAsync(); } catch (Exception) { throw; } return(RedirectToAction(nameof(GuestIndex))); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { try { if (ModelState.IsValid) { _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } //Loads the viewbag with data that is to be used in the view, i used this method so that different data could be displayed based on the database even if it changes. ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return(View(guestBooking)); } catch (DbUpdateException) { return(RedirectToAction("index")); } }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { if (!GuestBookingExists(guestBooking.CustomerId, guestBooking.EventId)) { _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { Debug.WriteLine("Guest Booking already Exists"); } } ViewData["CustomerId"] = new SelectList((from c in _context.Customers select new{ Id = c.Id, FullName = c.FirstName + " " + c.Surname }), "Id", "FullName", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return(View(guestBooking)); }
public async Task <IActionResult> Create([Bind("CustomerId,EventId,Attended")] GuestBooking guestBooking) { if (ModelState.IsValid) { var personExists = _context.Guests.Any(pe => pe.CustomerId == guestBooking.CustomerId && pe.EventId == guestBooking.EventId); if (personExists) { ModelState.AddModelError("CustomerId", "This guest is already booked onto this event"); } else { _context.Add(guestBooking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index), new { id = guestBooking.EventId })); } } ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return(View(guestBooking)); }
public CustomerBookingVM(Customer c, GuestBooking b) { Id = c.Id; FullName = c.FullName; Attended = b.Attended; }