Skip to content

Commit

Permalink
Removed my comments for cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
prosilentgamer2 committed Oct 20, 2024
1 parent e12ad80 commit 62cb703
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 50 deletions.
156 changes: 108 additions & 48 deletions GroupProject/Controllers/ContactController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
4 changes: 2 additions & 2 deletions GroupProject/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public HomeController (ContactContext ctx)
public IActionResult Index()
{
var contacts = context.Contacts
.Include(c => c.Category) // Include the Category navigation property
.Include(c => c.Category)
.OrderBy(c => c.LastName)
.ToList();

return View(contacts); // Return the view with the contacts
return View(contacts);
}


Expand Down

0 comments on commit 62cb703

Please sign in to comment.