DAS_2024_1/kuzarin_maxim_lab_3/DSaC/Controllers/CounterpartiesController.cs

89 lines
2.7 KiB
C#
Raw Permalink Normal View History

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<IActionResult> GetCounterparties(
[FromQuery] int page = 0,
[FromQuery] int pageSize = 10,
[FromQuery] List<Guid>? 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteRecord([FromRoute] Guid uuid)
{
var response = await mediator.Send(new DeleteCounterpartyCommand()
{
Id = uuid,
});
return !response.IsError ? Ok() : StatusCode(response.ErrorCode!.Value, response.ErrorText);
}
}
}