using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using turf_tasker.Data; using turf_tasker.Models; namespace turf_tasker.Controllers; public class HomeController : Controller { private readonly ApplicationDbContext _context; // Step 2.1: Inject the database context public HomeController(ApplicationDbContext context) { _context = context; } // Step 2.2: Make the Index action async public async Task Index() { // Step 2.3: Query for the last mow event to get its date and pattern var lastMowEvent = await _context.LawnCareEvents .Where(e => e.EventType == LawnCareEventType.Mowing) .OrderByDescending(e => e.EventDate) .FirstOrDefaultAsync(); // Step 2.4: Query for the last water and fertilize dates var lastWaterDate = await _context.LawnCareEvents .Where(e => e.EventType == LawnCareEventType.Watering) .OrderByDescending(e => e.EventDate) .Select(e => (DateTime?)e.EventDate) // Select just the date .FirstOrDefaultAsync(); var lastFertilizeDate = await _context.LawnCareEvents .Where(e => e.EventType == LawnCareEventType.Fertilizing) .OrderByDescending(e => e.EventDate) .Select(e => (DateTime?)e.EventDate) .FirstOrDefaultAsync(); // Determine the next mowing pattern MowingPattern? nextPattern = MowingPattern.Vertical; // Default if (lastMowEvent?.MowingPattern != null) { // This logic cycles through the enum values int currentPatternValue = (int)lastMowEvent.MowingPattern; int nextPatternValue = (currentPatternValue + 1) % 4; // 4 patterns total nextPattern = (MowingPattern)nextPatternValue; } // Step 2.5: Create the ViewModel and populate it var viewModel = new DashboardViewModel { LastMowDate = lastMowEvent?.EventDate, LastWaterDate = lastWaterDate, LastFertilizeDate = lastFertilizeDate, NextMowingPattern = nextPattern }; // Step 2.6: Pass the populated ViewModel to the view return View(viewModel); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }