using Microsoft.AspNetCore.Mvc; using ContactManager.Models; using ContactManager.Helpers; namespace ContactManager.Controllers { public class ContactsController : Controller { private readonly DbHelper _dbHelper; public ContactsController(DbHelper dbHelper) { _dbHelper = dbHelper; } public async Task Index() { var contacts = await _dbHelper.GetAllContacts(); return View(contacts); } // GET: Contacts/Create public IActionResult Create() { return View(); } // POST: Contacts/Create [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("FirstName,LastName")] Contact contact) { if (ModelState.IsValid) { await _dbHelper.AddContact(contact); return RedirectToAction(nameof(Index)); } return View(contact); } // GET: Contacts/Edit/5 public async Task Edit(int id) { var contact = await _dbHelper.GetContact(id); if (contact == null) { return NotFound(); } return View(contact); } // POST: Contacts/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, [Bind("Id,FirstName,LastName")] Contact contact) { if (id != contact.Id) { return NotFound(); } if (ModelState.IsValid) { await _dbHelper.UpdateContact(contact); return RedirectToAction(nameof(Index)); } return View(contact); } // GET: Contacts/Delete/5 public async Task Delete(int id) { var contact = await _dbHelper.GetContact(id); if (contact == null) { return NotFound(); } return View(contact); } // POST: Contacts/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { await _dbHelper.DeleteContact(id); return RedirectToAction(nameof(Index)); } } }