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 Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace CleanArchitecture.Application.TodoItems.Commands.UpdateTodoItemDetail { [Authorize] public class UpdateTodoItemDetailController : ApiControllerBase { [HttpPut("/todoItems/[action]")] public async Task<ActionResult> UpdateItemDetails(int id, UpdateTodoItemDetailCommand command) { if (id != command.Id) { return BadRequest(); } await Mediator.Send(command); return NoContent(); } } public class UpdateTodoItemDetailCommand : IRequest { public int Id { get; set; } public int ListId { get; set; } public PriorityLevel Priority { get; set; } public string Note { get; set; } } public class UpdateTodoItemDetailCommandHandler : IRequestHandler<UpdateTodoItemDetailCommand> { private readonly IApplicationDbContext _context; public UpdateTodoItemDetailCommandHandler(IApplicationDbContext context) { _context = context; } public async Task<Unit> Handle(UpdateTodoItemDetailCommand request, CancellationToken cancellationToken) { var entity = await _context.TodoItems.FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(TodoItem), request.Id); } entity.ListId = request.ListId; entity.Priority = request.Priority; entity.Note = request.Note; await _context.SaveChangesAsync(cancellationToken); return Unit.Value; } } }