turf-tasker/Controllers/HomeController.cs

75 lines
No EOL
2.5 KiB
C#

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;
public HomeController(ApplicationDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var lastMowEvent = await _context.LawnCareEvents
.Where(e => e.EventType == LawnCareEventType.Mowing)
.OrderByDescending(e => e.EventDate)
.FirstOrDefaultAsync();
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();
var lastAerationDate = await _context.LawnCareEvents
.Where(e => e.EventType == LawnCareEventType.Aeration)
.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;
}
var viewModel = new DashboardViewModel
{
LastMowDate = lastMowEvent?.EventDate,
LastWaterDate = lastWaterDate,
LastFertilizeDate = lastFertilizeDate,
LastAerationDate = lastAerationDate,
NextMowingPattern = nextPattern
};
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 });
}
}