-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed my comments for cleaner code
- Loading branch information
1 parent
e12ad80
commit 62cb703
Showing
2 changed files
with
110 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,88 +4,148 @@ | |
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace GroupProject.Controllers | ||
{ | ||
public class ContactController : Controller | ||
{ | ||
private ContactContext context { get; set; } | ||
private readonly ILogger<ContactController> _logger; | ||
private readonly ContactContext _context; | ||
|
||
public ContactController(ContactContext ctx) | ||
public ContactController(ILogger<ContactController> logger, ContactContext context) | ||
{ | ||
context = ctx; | ||
_logger = logger; | ||
_context = context; | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Add() | ||
// Action to list contacts (Simulated data) | ||
public IActionResult SimulatedIndex() | ||
{ | ||
ViewBag.Action = "Add"; | ||
ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList(); | ||
return View("Edit", new Contact()); | ||
_logger.LogInformation("Contact list accessed (Simulated data)."); | ||
|
||
// Simulating data with Categories included | ||
var contacts = new List<Contact> | ||
{ | ||
new Contact | ||
{ | ||
ContactID = 1, FirstName = "John", LastName = "Doe", | ||
Phone = "123-456-7890", Email = "[email protected]", | ||
Organization = "Company A", CategoryID = 1, | ||
Category = new Category { Name = "Friends" } | ||
}, | ||
new Contact | ||
{ | ||
ContactID = 2, FirstName = "Jane", LastName = "Smith", | ||
Phone = "987-654-3210", Email = "[email protected]", | ||
Organization = "Company B", CategoryID = 2, | ||
Category = new Category { Name = "Colleagues" } | ||
} | ||
}; | ||
|
||
return View("Index", contacts); | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Details(int id) | ||
|
||
public IActionResult Index() | ||
{ | ||
var contact = context.Contacts.Find(id); | ||
if (contact == null) | ||
_logger.LogInformation("Contact list accessed from the database."); | ||
|
||
var contacts = _context.Contacts | ||
.Include(c => c.Category) | ||
.OrderBy(c => c.LastName) | ||
.ToList(); | ||
|
||
if (contacts == null || contacts.Count == 0) | ||
{ | ||
return NotFound(); | ||
return NotFound("No contacts found."); | ||
} | ||
return View(contact); | ||
|
||
return View(contacts); | ||
} | ||
|
||
|
||
|
||
[HttpGet] | ||
public IActionResult Edit(int id) | ||
public IActionResult Add() | ||
{ | ||
ViewBag.Action = "Edit"; | ||
ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList(); | ||
var contact = context.Contacts.Find(id); | ||
return View(contact); | ||
|
||
_logger.LogInformation("Add contact form accessed."); | ||
return View(); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult Edit(Contact contact) | ||
public IActionResult Add(Contact model) | ||
{ | ||
if (ModelState.IsValid) | ||
if (ModelState.IsValid) | ||
{ | ||
if (contact.ContactID == 0) | ||
{ | ||
context.Contacts.Add(contact); | ||
} | ||
else | ||
{ | ||
context.Contacts.Update(contact); | ||
} | ||
|
||
context.SaveChanges(); | ||
return RedirectToAction("Index", "Home"); | ||
|
||
model.DateAdded = DateTime.Now; | ||
_logger.LogInformation("New contact added."); | ||
_context.Contacts.Add(model); | ||
_context.SaveChanges(); | ||
return RedirectToAction("Index"); | ||
} | ||
|
||
_logger.LogWarning("Failed to add contact. Model state is invalid."); | ||
return View(model); | ||
} | ||
|
||
public IActionResult Edit(int id) | ||
{ | ||
_logger.LogInformation($"Edit contact form accessed for contact with ID: {id}"); | ||
|
||
var contact = _context.Contacts | ||
.Include(c => c.Category) | ||
.FirstOrDefault(c => c.ContactID == id); | ||
if (contact == null) | ||
{ | ||
return NotFound("Contact not found."); | ||
} | ||
else | ||
|
||
return View(contact); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult Edit(Contact model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
ViewBag.Action = (contact.ContactID == 0) ? "Add" : "Edit"; | ||
ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList(); | ||
return View(contact); | ||
_context.Contacts.Update(model); | ||
_context.SaveChanges(); | ||
_logger.LogInformation($"Contact with ID: {model.ContactID} updated."); | ||
return RedirectToAction("Index"); | ||
} | ||
|
||
_logger.LogWarning($"Failed to update contact with ID: {model.ContactID}. Model state is invalid."); | ||
return View(model); | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Delete(int id) | ||
{ | ||
var contact = context.Contacts.Find(id); | ||
return View(contact); | ||
_logger.LogInformation($"Delete contact form accessed for contact with ID: {id}"); | ||
|
||
var contact = _context.Contacts | ||
.Include(c => c.Category) | ||
.FirstOrDefault(c => c.ContactID == id); | ||
|
||
if (contact == null) | ||
{ | ||
return NotFound("Contact not found."); | ||
} | ||
|
||
return View(contact); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult Delete(Contact contact) | ||
[HttpPost, ActionName("Delete")] | ||
public IActionResult DeleteConfirmed(int id) | ||
{ | ||
context.Contacts.Remove(contact); | ||
context.SaveChanges(); | ||
return RedirectToAction("Index", "Home"); | ||
var contact = _context.Contacts.Find(id); | ||
|
||
if (contact != null) | ||
{ | ||
_context.Contacts.Remove(contact); | ||
_context.SaveChanges(); | ||
_logger.LogInformation($"Contact with ID: {id} deleted."); | ||
} | ||
|
||
return RedirectToAction("Index"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters