Commit 2206608a authored by Steve Smith's avatar Steve Smith

parent f2cb9398
using CleanArchitecture.Web.ApiModels;
using CleanArchitecture.Web.Filters;
using CleanArchitecture.Web.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace CleanArchitecture.Web.Api
{
[Route("api/[controller]")]
[ValidateModel]
public class ToDoItems2Controller : Controller
{
private readonly IToDoAppService _appService;
public ToDoItems2Controller(IToDoAppService appService)
{
_appService = appService;
}
[HttpGet]
public IActionResult List()
{
var items = _appService.List();
return Ok(items);
}
[HttpGet("{id:int}")]
public IActionResult GetById(int id)
{
var item = _appService.GetById(id);
return Ok(item);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] ToDoItemDTO item)
{
var createdItem = _appService.CreateItem(item);
return Ok(createdItem);
}
[HttpPost("{itemId}")]
public async Task<IActionResult> MarkComplete(int itemId)
{
_appService.MarkComplete(itemId);
return Ok();
}
}
}
using CleanArchitecture.Web.ApiModels;
using CleanArchitecture.Web.Commands;
using CleanArchitecture.Web.Filters;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace CleanArchitecture.Web.Api
{
[Route("api/[controller]")]
[ValidateModel]
public class ToDoItems3Controller : Controller
{
private readonly IMediator _mediator;
public ToDoItems3Controller(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet]
public async Task<IActionResult> List()
{
var items = await _mediator.Send(new ListItemsCommand());
return Ok(items);
}
[HttpGet("{id:int}")]
public async Task<IActionResult> GetById(int id)
{
var command = new GetItemCommand { Id = id };
var item = await _mediator.Send(command);
return Ok(item);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] ToDoItemDTO item)
{
var command = new CreateItemCommand { ItemToCreate = item };
var createdItem = await _mediator.Send(command);
return Ok(createdItem);
}
[HttpPost("{itemId}")]
public async Task<IActionResult> MarkComplete(int itemId)
{
var command = new MarkItemCompleteCommand { Id = itemId };
await _mediator.Send(command);
return Ok();
}
}
}
using System; using CleanArchitecture.Core.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces; using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Web.ApiModels; using CleanArchitecture.Web.ApiModels;
using CleanArchitecture.Web.Filters; using CleanArchitecture.Web.Filters;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
namespace CleanArchitecture.Web.Api namespace CleanArchitecture.Web.Api
{ {
...@@ -21,7 +19,6 @@ namespace CleanArchitecture.Web.Api ...@@ -21,7 +19,6 @@ namespace CleanArchitecture.Web.Api
_todoRepository = todoRepository; _todoRepository = todoRepository;
} }
// GET: api/ToDoItems
[HttpGet] [HttpGet]
public IActionResult List() public IActionResult List()
{ {
...@@ -30,7 +27,6 @@ namespace CleanArchitecture.Web.Api ...@@ -30,7 +27,6 @@ namespace CleanArchitecture.Web.Api
return Ok(items); return Ok(items);
} }
// GET: api/ToDoItems
[HttpGet("{id:int}")] [HttpGet("{id:int}")]
public IActionResult GetById(int id) public IActionResult GetById(int id)
{ {
...@@ -38,7 +34,7 @@ namespace CleanArchitecture.Web.Api ...@@ -38,7 +34,7 @@ namespace CleanArchitecture.Web.Api
return Ok(item); return Ok(item);
} }
// POST: api/ToDoItems [HttpPost]
public async Task<IActionResult> Post([FromBody] ToDoItemDTO item) public async Task<IActionResult> Post([FromBody] ToDoItemDTO item)
{ {
var todoItem = new ToDoItem() var todoItem = new ToDoItem()
...@@ -49,5 +45,15 @@ namespace CleanArchitecture.Web.Api ...@@ -49,5 +45,15 @@ namespace CleanArchitecture.Web.Api
_todoRepository.Add(todoItem); _todoRepository.Add(todoItem);
return Ok(ToDoItemDTO.FromToDoItem(todoItem)); return Ok(ToDoItemDTO.FromToDoItem(todoItem));
} }
[HttpPost("{itemId}")]
public async Task<IActionResult> MarkComplete(int itemId)
{
var item = _todoRepository.GetById(itemId);
item.MarkComplete();
_todoRepository.Update(item);
return Ok();
}
} }
} }
using System; using CleanArchitecture.Core.Entities;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Core.Entities;
namespace CleanArchitecture.Web.ApiModels namespace CleanArchitecture.Web.ApiModels
{ {
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Dapper" Version="1.50.2" /> <PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
......
using CleanArchitecture.Web.ApiModels;
using MediatR;
namespace CleanArchitecture.Web.Commands
{
public class CreateItemCommand : IRequest<ToDoItemDTO>
{
public ToDoItemDTO ItemToCreate { get; set; }
}
}
using CleanArchitecture.Web.ApiModels;
using MediatR;
namespace CleanArchitecture.Web.Commands
{
public class GetItemCommand : IRequest<ToDoItemDTO>
{
public int Id { get; set; }
}
}
using CleanArchitecture.Web.ApiModels;
using MediatR;
using System.Collections.Generic;
namespace CleanArchitecture.Web.Commands
{
public class ListItemsCommand : IRequest<List<ToDoItemDTO>>
{
}
}
using MediatR;
namespace CleanArchitecture.Web.Commands
{
public class MarkItemCompleteCommand : IRequest
{
public int Id { get; set; }
}
}
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Web.ApiModels;
using CleanArchitecture.Web.Commands;
using MediatR;
using System.Collections.Generic;
using System.Linq;
namespace CleanArchitecture.Web.Handlers
{
public class ListItemsHandler : IRequestHandler<ListItemsCommand, List<ToDoItemDTO>>
{
private readonly IRepository<ToDoItem> _todoRepository;
public ListItemsHandler(IRepository<ToDoItem> todoRepository)
{
_todoRepository = todoRepository;
}
public List<ToDoItemDTO> Handle(ListItemsCommand message)
{
return _todoRepository.List()
.Select(ToDoItemDTO.FromToDoItem)
.ToList();
}
}
}
using CleanArchitecture.Web.ApiModels;
using System.Collections.Generic;
namespace CleanArchitecture.Web.Services
{
public interface IToDoAppService
{
ToDoItemDTO GetById(int id);
ToDoItemDTO CreateItem(ToDoItemDTO itemToCreate);
IEnumerable<ToDoItemDTO> List();
void MarkComplete(int itemId);
}
}
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Web.ApiModels;
using System.Collections.Generic;
using System.Linq;
namespace CleanArchitecture.Web.Services
{
public class ToDoAppService : IToDoAppService
{
private readonly IRepository<ToDoItem> _todoRepository;
public ToDoAppService(IRepository<ToDoItem> todoRepository)
{
_todoRepository = todoRepository;
}
public ToDoItemDTO CreateItem(ToDoItemDTO itemToCreate)
{
var todoItem = new ToDoItem()
{
Title = itemToCreate.Title,
Description = itemToCreate.Description
};
_todoRepository.Add(todoItem);
return ToDoItemDTO.FromToDoItem(todoItem);
}
public ToDoItemDTO GetById(int id)
{
return ToDoItemDTO.FromToDoItem(_todoRepository.GetById(id));
}
public IEnumerable<ToDoItemDTO> List()
{
return _todoRepository.List()
.Select(item => ToDoItemDTO.FromToDoItem(item));
}
public void MarkComplete(int itemId)
{
var item = _todoRepository.GetById(itemId);
item.MarkComplete();
_todoRepository.Update(item);
}
}
}
...@@ -9,6 +9,7 @@ using Microsoft.Extensions.Configuration; ...@@ -9,6 +9,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using StructureMap; using StructureMap;
using MediatR;
namespace CleanArchitecture.Web namespace CleanArchitecture.Web
{ {
...@@ -32,6 +33,8 @@ namespace CleanArchitecture.Web ...@@ -32,6 +33,8 @@ namespace CleanArchitecture.Web
services.AddMvc() services.AddMvc()
.AddControllersAsServices(); .AddControllersAsServices();
services.AddMediatR();
var container = new Container(); var container = new Container();
container.Configure(config => container.Configure(config =>
......
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