Skip to content

Commit

Permalink
Project Functions Done
Browse files Browse the repository at this point in the history
  • Loading branch information
karthickpurush committed Jun 19, 2024
1 parent 48f9bda commit 83c5f2a
Show file tree
Hide file tree
Showing 25 changed files with 1,386 additions and 188 deletions.
33 changes: 32 additions & 1 deletion Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PropertyInventorySystem.Data;
using PropertyInventorySystem.Models;
using System.Diagnostics;

Expand All @@ -7,17 +9,46 @@ namespace PropertyInventorySystem.Controllers
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly AppDbContext _context; // Add the DbContext

public HomeController(ILogger<HomeController> logger)
public HomeController(ILogger<HomeController> logger, AppDbContext context) // Inject the DbContext
{
_logger = logger;
_context = context;
}

public IActionResult Index()
{
return View();
}

public async Task<IActionResult> Dashboard() // New Dashboard action
{
var soldProperties = await _context.SoldProperties
.Include(sp => sp.Property)
.Include(sp => sp.Contact)
.ToListAsync();

var dashboardData = soldProperties.Select(sp => new PropertyDashboardViewModel
{
PropertyName = sp.Property.Name,
AskingPriceEUR = sp.Property.Price, // Assuming this is the asking price
Owner = $"{sp.Contact.FirstName} {sp.Contact.LastName}",
ContactNumber = sp.Contact.PhoneNumber,
Email = sp.Contact.Email,
PropertyAddress = sp.Property.Address,
DateOfPurchase = sp.Property.DateOfRegistration, // Assuming this is the purchase date
SoldAtPriceEUR = sp.AcquisitionPrice, // Assuming SoldProperty has this field
SoldAtPriceUSD = ConvertEURtoUSD(sp.AcquisitionPrice) // Convert using your method
}).ToList();

return View(dashboardData); // Ensure you have a corresponding View at Views/Home/Dashboard.cshtml
}
private decimal ConvertEURtoUSD(decimal amountEUR)
{
decimal exchangeRate = 1.10M; // Placeholder exchange rate
return amountEUR * exchangeRate;
}
public IActionResult Privacy()
{
return View();
Expand Down
96 changes: 65 additions & 31 deletions Controllers/PropertyContactController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
using Microsoft.EntityFrameworkCore;
using PropertyInventorySystem.Data;
using PropertyInventorySystem.Models;
using Microsoft.Extensions.Logging;

namespace PropertyInventorySystem.Controllers
{
public class PropertyContactController : Controller
{
private readonly ILogger<PropertyContactController> _logger;
private readonly AppDbContext _context;

public PropertyContactController(AppDbContext context)
public PropertyContactController(ILogger<PropertyContactController> logger, AppDbContext context)
{
_logger = logger;
_context = context; // Assign the injected context to the private field
}


// Dependency injection for repositories or services if needed

[HttpGet]
[HttpGet]
public IActionResult Index()
{
var model = new PropertyContactViewModel();
return View(model);
}
public IActionResult Create()
{
// Initialize your ViewModel if needed
Expand All @@ -28,47 +37,72 @@ public IActionResult Create()
[HttpPost]
public async Task<IActionResult> Create(PropertyContactViewModel model)
{

if (ModelState.IsValid)
{
// return View(model);
return View("Dashboard");

}
foreach (var modelStateKey in ViewData.ModelState.Keys)
{
var modelStateVal = ViewData.ModelState[modelStateKey];
if (modelStateVal.Errors != null) // Added null check for modelStateVal.Errors
{
foreach (var error in modelStateVal.Errors)
{
var errorMessage = error.ErrorMessage;
// Log the error message or set a breakpoint here to inspect
_logger.LogError("Validation error for {ModelKey}: {ErrorMessage}", modelStateKey, errorMessage);
}
}
}
using (var transaction = _context.Database.BeginTransaction())
{
try
{
// Assuming you have a method to map your ViewModel to your Entity model
var (property, contact) = PropertyContactViewModel.MapToEntity(model);
// Assuming PropertyContactViewModel has Property and Contact objects
var property = model.Property;
var contact = model.Contact;

// Save Property
_context.Properties.Add(property);
await _context.SaveChangesAsync();

// Save Contact
_context.Contacts.Add(contact);
await _context.SaveChangesAsync();

// Create and Save SoldProperty
var soldProperty = new SoldProperty
{
PropertyId = property.Id,
ContactId = contact.Id,
AcquisitionPrice = property.Price,
DateOfRegistration = property.DateOfRegistration,
// Set EffectiveTill or other properties as needed
};
_context.SoldProperties.Add(soldProperty);
await _context.SaveChangesAsync();
TempData["SuccessMessage"] = "Property and contact information saved successfully!";
return RedirectToAction(nameof(Index));
}
catch (DbUpdateException ex)
{
// Log the exception details
// This can provide insights into issues like constraint violations
// Example: _logger.LogError(ex, "An error occurred while saving the data.");
ModelState.AddModelError("", "An error occurred while saving the data.");

// Commit transaction if all operations succeed
transaction.Commit();

TempData["SuccessMessage"] = "Property and Contact information saved successfully!";
// return RedirectToAction(nameof(Index));
return RedirectToAction("Dashboard", "Home");
}
catch (Exception ex)
{
// Log the exception and handle it
// Example: _logger.LogError(ex, "An unexpected error occurred.");
ModelState.AddModelError("", "An unexpected error occurred.");
}
}

// If we got this far, something failed; redisplay the form
// This includes handling model state errors and exceptions
foreach (var modelState in ViewData.ModelState.Values)
{
foreach (var error in modelState.Errors)
{
// Log or debug the error message
var errorMessage = error.ErrorMessage;
// Example: _logger.LogError("Model state error: {ErrorMessage}", errorMessage);
// Log the error (uncomment ex variable name and write a log.)
_logger.LogError(ex, "An error occurred while saving the data.");
transaction.Rollback();
ModelState.AddModelError("", "An error occurred while saving the data.");
}
// Return the view with validation messages
// return View(model);
}
return View(model);
return RedirectToAction("Dashboard", "Home");
}

}
}
62 changes: 62 additions & 0 deletions Controllers/PropertyDashboardController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PropertyInventorySystem.Data;
using PropertyInventorySystem.Models;

namespace PropertyInventorySystem.Controllers
{
public class PropertyDashboardController : Controller
{
private readonly AppDbContext _context;

public PropertyDashboardController(AppDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
try
{
// Fetch sold properties including related Property and Contact data
var soldProperties = await _context.SoldProperties
.Include(sp => sp.Property)
.Include(sp => sp.Contact)
.ToListAsync();

// Map to the ViewModel
var dashboardData = soldProperties.Select(sp => new PropertyDashboardViewModel
{
// Id = sp.Property.Id,s
PropertyName = sp.Property.Name,
AskingPriceEUR = sp.Property.Price, // Assuming this is the asking price
Owner = $"{sp.Contact.FirstName} {sp.Contact.LastName}",
ContactNumber = sp.Contact.PhoneNumber,
Email = sp.Contact.Email,
PropertyAddress = sp.Property.Address,
DateOfPurchase = sp.Property.DateOfRegistration, // Assuming this is the purchase date
SoldAtPriceEUR = sp.AcquisitionPrice, // Assuming SoldProperty has this field
SoldAtPriceUSD = ConvertEURtoUSD(sp.AcquisitionPrice) // Convert using your method
}).ToList();

return View(dashboardData);
}
catch (Exception ex)
{
// Log the exception details
// Consider using a logging framework or service
Console.WriteLine(ex.ToString());

// Optionally, return a custom error view
// return View("Error");

// Or return a generic error message to the user
return Content("An error occurred while processing your request. Please try again later.");
}
}
private decimal ConvertEURtoUSD(decimal amountEUR)
{
decimal exchangeRate = 1.10M; // Placeholder exchange rate
return amountEUR * exchangeRate;
}
}
}
Loading

0 comments on commit 83c5f2a

Please sign in to comment.