using DSaC.Models.DTOs; using DSaC.Models.Internal.Queries; using DSaC.Models.Internal.Сommands; using MediatR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace DSaC.Controllers { [Route("api/[controller]")] [ApiController] public class CounterpartiesController : ControllerBase { private readonly IMediator mediator; public CounterpartiesController(IMediator mediator) { this.mediator = mediator; } [HttpGet("")] public async Task GetCounterparties( [FromQuery] int page = 0, [FromQuery] int pageSize = 10, [FromQuery] List? ids = null ) { var request = new GetCounterpartiesQuery { Page = page, PageSize = pageSize, Ids = ids }; var response = await mediator.Send(request); return !response.IsError ? Ok(response.Value) : StatusCode(response.ErrorCode!.Value, response.ErrorText); } [HttpGet("{uuid:guid}")] public async Task GetFullCounterparty([FromRoute] Guid uuid) { var request = new GetCounterpartyQuery { Id = uuid }; var response = await mediator.Send(request); return !response.IsError ? Ok(response.Value) : StatusCode(response.ErrorCode!.Value, response.ErrorText); } [HttpPost("")] public async Task CreateCounterparty([FromBody] CounterpartyBaseDto dto) { var response = await mediator.Send(new CreateCounterpartyCommand() { Model = dto }); return !response.IsError ? Ok(response.Value) : StatusCode(response.ErrorCode!.Value, response.ErrorText); } [HttpPut("{uuid:guid}")] public async Task UpdateRecord([FromRoute] Guid uuid, [FromBody] CounterpartyViewDto dto) { var response = await mediator.Send(new UpdateCounterpartyCommand() { Id=uuid, Model = dto }); return !response.IsError ? Ok(response.Value) : StatusCode(response.ErrorCode!.Value, response.ErrorText); } [HttpDelete("{uuid:guid}")] public async Task DeleteRecord([FromRoute] Guid uuid) { var response = await mediator.Send(new DeleteCounterpartyCommand() { Id = uuid, }); return !response.IsError ? Ok() : StatusCode(response.ErrorCode!.Value, response.ErrorText); } } }