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 StaffController : Controller { private readonly EventsDbContext _context; public StaffController(EventsDbContext context) { _context = context; } // GET: Staff public async Task Index() { return View(await _context.Staff.ToListAsync()); } // GET: Staff/Details/5 public async Task Details(int? id) { if (id == null) { return NotFound(); } var staff = await _context.Staff.Include(s => s.Staffings).FirstOrDefaultAsync(m => m.Id == id); //Get staffings for that staff, to show what events they are staffing. staff.Staffings = await _context.Staffing.Include(m => m.Event).Where(m => m.StaffId == id).ToListAsync(); if (staff == null) { return NotFound(); } return View(staff); } // GET: Staff/Create public IActionResult Create() { return View(); } // POST: Staff/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,FirstAid")] Staff staff) { if (ModelState.IsValid) { _context.Add(staff); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(staff); } // GET: Staff/Edit/5 public async Task Edit(int? id) { if (id == null) { return NotFound(); } var staff = await _context.Staff.FindAsync(id); if (staff == null) { return NotFound(); } return View(staff); } // POST: Staff/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,FirstAid")] Staff staff) { if (id != staff.Id) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(staff); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StaffExists(staff.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(staff); } // GET: Staff/Delete/5 public async Task Delete(int? id) { if (id == null) { return NotFound(); } var staff = await _context.Staff .FirstOrDefaultAsync(m => m.Id == id); if (staff == null) { return NotFound(); } return View(staff); } // POST: Staff/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { var staff = await _context.Staff.FindAsync(id); _context.Staff.Remove(staff); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool StaffExists(int id) { return _context.Staff.Any(e => e.Id == id); } } }