feat: Add Lawn Care Tips section and improve Dashboard layout
This commit is contained in:
parent
1f50fedb80
commit
985c944e16
17 changed files with 689 additions and 18 deletions
|
|
@ -10,22 +10,18 @@ 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<IActionResult> 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)
|
||||
|
|
@ -38,6 +34,12 @@ public class HomeController : Controller
|
|||
.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)
|
||||
|
|
@ -48,16 +50,15 @@ public class HomeController : Controller
|
|||
nextPattern = (MowingPattern)nextPatternValue;
|
||||
}
|
||||
|
||||
// Step 2.5: Create the ViewModel and populate it
|
||||
var viewModel = new DashboardViewModel
|
||||
{
|
||||
LastMowDate = lastMowEvent?.EventDate,
|
||||
LastWaterDate = lastWaterDate,
|
||||
LastFertilizeDate = lastFertilizeDate,
|
||||
LastAerationDate = lastAerationDate,
|
||||
NextMowingPattern = nextPattern
|
||||
};
|
||||
|
||||
// Step 2.6: Pass the populated ViewModel to the view
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
|
|
|
|||
157
Controllers/LawnCareTipController.cs
Normal file
157
Controllers/LawnCareTipController.cs
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using turf_tasker.Data;
|
||||
using turf_tasker.Models;
|
||||
|
||||
namespace turf_tasker.Controllers
|
||||
{
|
||||
public class LawnCareTipController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public LawnCareTipController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: LawnCareTip
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return View(await _context.LawnCareTips.ToListAsync());
|
||||
}
|
||||
|
||||
// GET: LawnCareTip/Details/5
|
||||
public async Task<IActionResult> Details(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var lawnCareTip = await _context.LawnCareTips
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if (lawnCareTip == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(lawnCareTip);
|
||||
}
|
||||
|
||||
// GET: LawnCareTip/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: LawnCareTip/Create
|
||||
// To protect from overposting attacks, enable the specific properties you want to bind to.
|
||||
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create([Bind("Id,Title,Category,Content")] LawnCareTip lawnCareTip)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_context.Add(lawnCareTip);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(lawnCareTip);
|
||||
}
|
||||
|
||||
// GET: LawnCareTip/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var lawnCareTip = await _context.LawnCareTips.FindAsync(id);
|
||||
if (lawnCareTip == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return View(lawnCareTip);
|
||||
}
|
||||
|
||||
// POST: LawnCareTip/Edit/5
|
||||
// To protect from overposting attacks, enable the specific properties you want to bind to.
|
||||
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(int id, [Bind("Id,Title,Category,Content")] LawnCareTip lawnCareTip)
|
||||
{
|
||||
if (id != lawnCareTip.Id)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(lawnCareTip);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!LawnCareTipExists(lawnCareTip.Id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(lawnCareTip);
|
||||
}
|
||||
|
||||
// GET: LawnCareTip/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var lawnCareTip = await _context.LawnCareTips
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if (lawnCareTip == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(lawnCareTip);
|
||||
}
|
||||
|
||||
// POST: LawnCareTip/Delete/5
|
||||
[HttpPost, ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
var lawnCareTip = await _context.LawnCareTips.FindAsync(id);
|
||||
if (lawnCareTip != null)
|
||||
{
|
||||
_context.LawnCareTips.Remove(lawnCareTip);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
private bool LawnCareTipExists(int id)
|
||||
{
|
||||
return _context.LawnCareTips.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue