using CleanArchitecture.Application.Common.Exceptions; using CleanArchitecture.Application.Common.Interfaces; using CleanArchitecture.Domain.Entities; using MediatR; using System.Threading; using System.Threading.Tasks; using CleanArchitecture.WebUI.Controllers; using FluentValidation; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace CleanArchitecture.Application.TodoItems.Commands.UpdateTodoItem { [Authorize] public class TodoItemsController : ApiControllerBase { [HttpPut("/todoItems/{id}")] public async Task<ActionResult> Update(int id, UpdateTodoItemCommand command) { if (id != command.Id) { return BadRequest(); } await Mediator.Send(command); return NoContent(); } } public class UpdateTodoItemCommand : IRequest { public int Id { get; set; } public string Title { get; set; } public bool Done { get; set; } } public class UpdateTodoItemCommandValidator : AbstractValidator<UpdateTodoItemCommand> { public UpdateTodoItemCommandValidator() { RuleFor(v => v.Title) .MaximumLength(200) .NotEmpty(); } } public class UpdateTodoItemCommandHandler : IRequestHandler<UpdateTodoItemCommand> { private readonly IApplicationDbContext _context; public UpdateTodoItemCommandHandler(IApplicationDbContext context) { _context = context; } public async Task<Unit> Handle(UpdateTodoItemCommand request, CancellationToken cancellationToken) { var entity = await _context.TodoItems.FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(TodoItem), request.Id); } entity.Title = request.Title; entity.Done = request.Done; await _context.SaveChangesAsync(cancellationToken); return Unit.Value; } } }