Y2S1-Web_Apps_and_Services/ThAmCo.Events/Controllers/StaffController.cs
2020-06-07 22:36:12 +01:00

155 lines
4.4 KiB
C#

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<IActionResult> Index()
{
return View(await _context.Staff.ToListAsync());
}
// GET: Staff/Details/5
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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);
}
}
}