using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using ThAmCo.Events.Data; namespace ThAmCo.Events.Controllers { public class GuestBookingsController : Controller { private readonly EventsDbContext _context; public GuestBookingsController(EventsDbContext context) { _context = context; } // GET: GuestBookings public async Task Index() { var eventsDbContext = _context.Guests.Include(g => g.Customer).Include(g => g.Event); return View(await eventsDbContext.ToListAsync()); } // GET: GuestBookings/Details/5 public async Task Details(int? id) { if (id == null) { return NotFound(); } var guestBooking = await _context.Guests .Include(g => g.Customer) .Include(g => g.Event) .FirstOrDefaultAsync(m => m.CustomerId == id); if (guestBooking == null) { return NotFound(); } return View(guestBooking); } // GET: GuestBookings/Create public IActionResult Create() { ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email"); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title"); return View(); } // POST: GuestBookings/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task 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, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return View(guestBooking); } // GET: GuestBookings/Edit/5 public async Task Edit(int? id) { if (id == null) { return NotFound(); } var guestBooking = await _context.Guests.FindAsync(id); if (guestBooking == null) { return NotFound(); } ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Email", guestBooking.CustomerId); ViewData["EventId"] = new SelectList(_context.Events, "Id", "Title", guestBooking.EventId); return View(guestBooking); } // POST: GuestBookings/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task 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); } // GET: GuestBookings/Delete/5 public async Task Delete(int? id) { if (id == null) { return NotFound(); } var guestBooking = await _context.Guests .Include(g => g.Customer) .Include(g => g.Event) .FirstOrDefaultAsync(m => m.CustomerId == id); if (guestBooking == null) { return NotFound(); } return View(guestBooking); } // POST: GuestBookings/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { var guestBooking = await _context.Guests.FindAsync(id); _context.Guests.Remove(guestBooking); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool GuestBookingExists(int id) { return _context.Guests.Any(e => e.CustomerId == id); } } }