Commit 451fe3d8 authored by Scott DePouw's avatar Scott DePouw

Created RazorPage for populating database TODO items (to mirror controller)....

Created RazorPage for populating database TODO items (to mirror controller). Extracted common Populate method to static class.
parent e542ce39
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using System.Linq;
namespace CleanArchitecture.Core
{
public static class DatabasePopulator
{
public static int PopulateDatabase(IRepository<ToDoItem> todoRepository)
{
if (todoRepository.List().Any()) return 0;
todoRepository.Add(new ToDoItem()
{
Title = "Get Sample Working",
Description = "Try to get the sample to build."
});
todoRepository.Add(new ToDoItem()
{
Title = "Review Solution",
Description = "Review the different projects in the solution and how they relate to one another."
});
todoRepository.Add(new ToDoItem()
{
Title = "Run and Review Tests",
Description = "Make sure all the tests run and review what they are doing."
});
return todoRepository.List().Count;
}
}
}
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace CleanArchitecture.Web.Controllers
{
......@@ -22,29 +22,8 @@ namespace CleanArchitecture.Web.Controllers
public IActionResult Populate()
{
int recordsAdded = PopulateDatabase();
int recordsAdded = DatabasePopulator.PopulateDatabase(_todoRepository);
return Ok(recordsAdded);
}
public int PopulateDatabase()
{
if (_todoRepository.List().Any()) return 0;
_todoRepository.Add(new ToDoItem()
{
Title = "Get Sample Working",
Description = "Try to get the sample to build."
});
_todoRepository.Add(new ToDoItem()
{
Title = "Review Solution",
Description = "Review the different projects in the solution and how they relate to one another."
});
_todoRepository.Add(new ToDoItem()
{
Title = "Run and Review Tests",
Description = "Make sure all the tests run and review what they are doing."
});
return _todoRepository.List().Count;
}
}
}
@page
@model CleanArchitecture.Web.Pages.ToDoRazorPage.IndexModel
<h2>To Do Items</h2>
<h3>Razor Page</h3>
<h2>To Do Items (Razor Page)</h2>
<ul>
@foreach (var item in Model.ToDoItems)
{
......
@page
@model CleanArchitecture.Web.Pages.ToDoRazorPage.PopulateModel
<h3>Records Added: @Model.RecordsAdded</h3>
using CleanArchitecture.Core;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace CleanArchitecture.Web.Pages.ToDoRazorPage
{
public class PopulateModel : PageModel
{
private readonly IRepository<ToDoItem> _todoRepository;
public PopulateModel(IRepository<ToDoItem> todoRepository)
{
_todoRepository = todoRepository;
}
public int RecordsAdded { get; set; }
public void OnGet()
{
RecordsAdded = DatabasePopulator.PopulateDatabase(_todoRepository);
}
}
}
......@@ -2,10 +2,10 @@
ViewData["Title"] = "ToDo List";
}
@model IEnumerable<CleanArchitecture.Core.Entities.ToDoItem>
<h2>To Do Items</h2>
<h2>To Do Items (MVC View)</h2>
<ul>
@foreach (var item in Model)
{
<li>@item.Title<br/>@item.Description</li>
<li>@item.Title<br/>@item.Description</li>
}
</ul>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment