Вынес файл Repository.cs в папку Repositories

parent 534ca999
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using MyCities.Models; using MyCities.Models;
using MyCities.Repositories;
namespace MyCities.Controllers namespace MyCities.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
private IRepository _repository; private ICityRepository _cityRepository;
public HomeController(IRepository repository) public HomeController(ICityRepository cityRepository)
{ {
_repository = repository; _cityRepository = cityRepository;
} }
public IActionResult Index() public IActionResult Index()
{ {
return View(_repository.Cities); return View(_cityRepository.Cities);
} }
[HttpGet] [HttpGet]
...@@ -26,7 +27,7 @@ namespace MyCities.Controllers ...@@ -26,7 +27,7 @@ namespace MyCities.Controllers
[HttpPost] [HttpPost]
public IActionResult Create(City city) public IActionResult Create(City city)
{ {
_repository.Add(city); _cityRepository.Add(city);
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
} }
......
using Microsoft.AspNetCore.Mvc;
namespace MyCities.Controllers
{
public class PersonController : Controller
{
// GET
public IActionResult Index()
{
return View();
}
}
}
namespace MyCities.Models
{
public class Person
{
public string FullName { get; set; }
public string NickName { get; set; }
public string AvatarImagePath { get; set; }
}
}
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Repositories
{
public interface ICityRepository
{
List<City> Cities { get; }
void Add(City city);
}
}
using System.Collections.Generic; using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Models namespace MyCities.Repositories
{ {
public interface IRepository public class InMemoryCityRepository : ICityRepository
{
List<City> Cities { get; }
void Add(City city);
}
public class InMemoryRepository : IRepository
{ {
private List<City> _cities = new List<City> private List<City> _cities = new List<City>
{ {
......
...@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc; ...@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using MyCities.Models; using MyCities.Models;
using MyCities.Repositories;
namespace MyCities namespace MyCities
{ {
...@@ -32,7 +33,7 @@ namespace MyCities ...@@ -32,7 +33,7 @@ namespace MyCities
options.MinimumSameSitePolicy = SameSiteMode.None; options.MinimumSameSitePolicy = SameSiteMode.None;
}); });
services.AddSingleton<IRepository, InMemoryRepository>(); services.AddSingleton<ICityRepository, InMemoryCityRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} }
......
@model IEnumerable<MyCities.Models.City>
@{ Layout = "_Layout";}
<table class="table small table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Counry</th>
<th>Population</th>
</tr>
</thead>
<tbody>
@foreach (var city in Model)
{
<tr>
<td>@city.Name</td>
<td>@city.Country</td>
<td>@city.Population.ToString("#,###")</td>
</tr>
}
</tbody>
</table>
<a href="/Home/Create" class="btn btn-primary">Create city</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