|
| 1 | +namespace Danom.Examples.Todo; |
| 2 | + |
| 3 | +using Danom; |
| 4 | +using Danom.Mvc; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | + |
| 7 | +public sealed class TodoController( |
| 8 | + TodoStore todo) : DanomController |
| 9 | +{ |
| 10 | + [Route("/")] |
| 11 | + [Route("/todo")] |
| 12 | + public IActionResult Index() |
| 13 | + { |
| 14 | + var result = new ListTodoQueryHandler(todo.GetAll).Handle(); |
| 15 | + return ViewOption(result); |
| 16 | + } |
| 17 | + |
| 18 | + [HttpGet("/todo/create")] |
| 19 | + public IActionResult Create() => |
| 20 | + View(); |
| 21 | + |
| 22 | + [HttpPost("/todo/create")] |
| 23 | + public IActionResult Create(CreateTodoCommand command) |
| 24 | + { |
| 25 | + var result = new CreateTodoCommandHandler(todo.Create).Handle(command); |
| 26 | + return result.Match( |
| 27 | + ok: _ => RedirectToAction(nameof(Index)), |
| 28 | + error: e => ViewResultErrors(e)); |
| 29 | + } |
| 30 | + |
| 31 | + [HttpGet("/todo/complete/{id}")] |
| 32 | + public IActionResult Complete(GetTodoQuery query) |
| 33 | + { |
| 34 | + var result = new GetTodoQueryHandler(todo.Get).Handle(query); |
| 35 | + return ViewOption(result); |
| 36 | + } |
| 37 | + |
| 38 | + [HttpPost("/todo/complete/{id}")] |
| 39 | + public IActionResult Complete(CompleteTodoCommand command) |
| 40 | + { |
| 41 | + var result = new CompleteTodoCommandHandler(todo.Complete).Handle(command); |
| 42 | + return result.Match( |
| 43 | + ok: _ => RedirectToAction(nameof(Index)), |
| 44 | + error: e => ViewResultErrors(e)); |
| 45 | + } |
| 46 | + |
| 47 | + [HttpGet("/todo/delete/{id}")] |
| 48 | + public IActionResult Delete(GetTodoQuery query) |
| 49 | + { |
| 50 | + var result = new GetTodoQueryHandler(todo.Get).Handle(query); |
| 51 | + return ViewOption(result); |
| 52 | + } |
| 53 | + |
| 54 | + [HttpPost("/todo/delete/{id}")] |
| 55 | + public IActionResult Delete(DeleteTodoCommand command) |
| 56 | + { |
| 57 | + var result = new DeleteTodoCommandHandler(todo.Delete).Handle(command); |
| 58 | + return result.Match( |
| 59 | + ok: _ => RedirectToAction(nameof(Index)), |
| 60 | + error: e => ViewResultErrors(e)); |
| 61 | + } |
| 62 | +} |
0 commit comments