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; using ThAmCo.Events.Models; namespace ThAmCo.Events.Controllers { public class CustomersController : Controller { private readonly EventsDbContext _context; public CustomersController(EventsDbContext context) { _context = context; } // GET: Customers public async Task Index() { return View(await _context.Customers.Where(c => c.IsDeleted == false).ToListAsync()); } // GET: Customers/Details/5 public async Task Details(int? id) { if (id == null) { return NotFound(); } var customer = await _context.Customers .FirstOrDefaultAsync(m => m.Id == id); customer.Bookings = _context.Guests.Include(m => m.Event).Where(m => m.CustomerId == id).ToList(); if (customer == null) { return NotFound(); } return View(customer); } // GET: Customers/Create public IActionResult Create() { ViewData["EventLists"] = new SelectList(_context.Events, "Id", "Title"); return View(); } // POST: Customers/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("Id,Surname,FirstName,Email,InitialEvent")] CustomerCreateModel customerCreate) { Customer customer = null; GuestBooking guestBooking = null; if (ModelState.IsValid) { customer = new Customer { Surname = customerCreate.Surname, FirstName = customerCreate.FirstName, Email = customerCreate.Email }; var customerContext = _context.Add(customer); int custID = customerContext.Entity.Id; //A booking is created at the same time as a customer. guestBooking = new GuestBooking { CustomerId = custID, EventId = customerCreate.InitialEvent, Attended = true }; _context.Add(guestBooking); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(customer); } // GET: Customers/Edit/5 public async Task Edit(int? id) { if (id == null) { return NotFound(); } var customer = await _context.Customers.FindAsync(id); if (customer == null) { return NotFound(); } return View(customer); } // POST: Customers/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("Id,Surname,FirstName,Email")] Customer customer) { if (id != customer.Id) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(customer); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(customer.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(customer); } // GET: Customers/Delete/5 public async Task Delete(int? id) { if (id == null) { return NotFound(); } var customer = await _context.Customers .FirstOrDefaultAsync(m => m.Id == id); if (customer == null) { return NotFound(); } return View(customer); } // POST: Customers/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { var customer = await _context.Customers.FindAsync(id); customer.IsDeleted = true; _context.Customers.Update(customer); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } [HttpPost, ActionName("AnonDelete")] [ValidateAntiForgeryToken] public async Task AnonDeleteConfirmed(int id) { var customer = await _context.Customers.FindAsync(id); customer.FirstName = "[Redacted]"; customer.Surname = "[Redacted]"; customer.Email = "[Redacted]"; customer.IsDeleted = true; _context.Customers.Update(customer); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool CustomerExists(int id) { return _context.Customers.Any(e => e.Id == id); } } }