Добавил repository для модели Person

parent 3de5d1f9
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using MyCities.Repositories;
namespace MyCities.Controllers namespace MyCities.Controllers
{ {
public class PersonController : Controller public class PersonController : Controller
{ {
private IPersonRepository _repository;
public PersonController(IPersonRepository repository)
{
_repository = repository;
}
// GET // GET
public IActionResult Index() public IActionResult Index()
{ {
return View(); return View(_repository.Persons);
} }
} }
} }
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Repositories
{
public interface IPersonRepository
{
List<Person> Persons { get; }
void Add(Person person);
}
}
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Repositories
{
public class InMemoryPersonRepository : IPersonRepository
{
private List<Person> _persons = new List<Person>();
public List<Person> Persons => _persons;
public void Add(Person person)
{
_persons.Add(person);
}
}
}
...@@ -34,6 +34,7 @@ namespace MyCities ...@@ -34,6 +34,7 @@ namespace MyCities
}); });
services.AddSingleton<ICityRepository, InMemoryCityRepository>(); services.AddSingleton<ICityRepository, InMemoryCityRepository>();
services.AddSingleton<IPersonRepository, InMemoryPersonRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} }
......
@model IEnumerable<MyCities.Models.City> @model IEnumerable<MyCities.Models.Person>
@{ Layout = "_Layout";} @{ Layout = "_Layout";}
<table class="table small table-bordered"> <table class="table small table-bordered">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>FullName</th>
<th>Counry</th> <th>NickName</th>
<th>Population</th> <th>Avatar</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach (var city in Model) @foreach (var person in Model)
{ {
<tr> <tr>
<td>@city.Name</td> <td>@person.FullName</td>
<td>@city.Country</td> <td>@person.NickName</td>
<td>@city.Population.ToString("#,###")</td> <td><img src="@person.AvatarImagePath" width="250" height="250"/></td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>
<a href="/Home/Create" class="btn btn-primary">Create city</a> <a href="/Person/Create" class="btn btn-primary">Create person</a>
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