| | | 1 | | using ClientManager.API.Mappers; |
| | | 2 | | using ClientManager.API.Services; |
| | | 3 | | using ClientManager.Shared.DTOs.Requests; |
| | | 4 | | using ClientManager.Shared.DTOs.Responses; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | using RabbitMQ.Client.Exceptions; |
| | | 7 | | |
| | | 8 | | namespace ClientManager.API.Controllers; |
| | | 9 | | |
| | | 10 | | [ApiController] |
| | | 11 | | [Route("api/[controller]")] |
| | 4 | 12 | | public class ClientController(IClientService clientService) : ControllerBase |
| | | 13 | | { |
| | 4 | 14 | | readonly IClientService _clientService = clientService; |
| | | 15 | | |
| | | 16 | | [HttpPost] |
| | | 17 | | public async Task<IActionResult> CreateClient([FromBody] CreateClientRequest createClientRequest) |
| | | 18 | | { |
| | 4 | 19 | | if (createClientRequest is null) |
| | 1 | 20 | | return BadRequest("Client data is required"); |
| | | 21 | | |
| | 3 | 22 | | if (!ModelState.IsValid) |
| | 1 | 23 | | return BadRequest(ModelState); |
| | | 24 | | |
| | | 25 | | try |
| | | 26 | | { |
| | 2 | 27 | | var clientCommand = await _clientService.SendCreateClientMessage(ClientMapper.ToCreateClientCommand(createCl |
| | 1 | 28 | | return AcceptedAtAction(nameof(GetClient), new { id = clientCommand.Id }, ClientMapper.ToResponse(clientComm |
| | | 29 | | } |
| | 1 | 30 | | catch (PublishException ex) |
| | | 31 | | { |
| | 1 | 32 | | var errorResponse = new ErrorResponse |
| | 1 | 33 | | { |
| | 1 | 34 | | Message = "Unexpected server error occurred sending message to broker.", |
| | 1 | 35 | | Details = ex.InnerException?.Message ?? ex.Message |
| | 1 | 36 | | }; |
| | 1 | 37 | | return StatusCode(500, errorResponse); |
| | | 38 | | } |
| | 4 | 39 | | } |
| | | 40 | | |
| | | 41 | | [HttpGet("{id}")] |
| | | 42 | | public async Task<IActionResult> GetClient(Guid id) |
| | | 43 | | { |
| | 0 | 44 | | var client = await _clientService.GetClientById(id); |
| | 0 | 45 | | if (client == null) |
| | 0 | 46 | | return NotFound(); |
| | 0 | 47 | | return Ok(ClientMapper.ToResponse(client)); |
| | 0 | 48 | | } |
| | | 49 | | } |