Skip to content

Instantly share code, notes, and snippets.

@AndyButland
Created September 26, 2016 09:13
Show Gist options
  • Save AndyButland/91264fe132799a82a627760742b63336 to your computer and use it in GitHub Desktop.
Save AndyButland/91264fe132799a82a627760742b63336 to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
public class TasksController : Controller
{
private readonly IMediator _mediator;
private readonly IMapper _mapper;
private const string NotificationMessageKey = "NotificationMessage";
public TasksController(IMediator mediator, IMapper mapper)
{
_mediator = mediator;
_mapper = mapper;
}
...
public async Task<IActionResult> Edit(
TasksEditViewModelQuery query)
{
var model = await _mediator.SendAsync(query);
if (model.Id == 0)
{
return NotFound();
}
return View(model);
}
...
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateStatus(int id, bool completed, int? categoryId)
{
if (completed)
{
var command = new TaskCompleteCommand
{
Id = id,
};
await _mediator.SendAsync(command);
TempData[NotificationMessageKey] = "Task completed";
}
else
{
var command = new TaskResetCommand
{
Id = id,
};
await _mediator.SendAsync(command);
TempData[NotificationMessageKey] = "Task reset";
}
return RedirectToAction("Index",
categoryId.HasValue
? new { CategoryId = categoryId.Value }
: null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment