diff --git a/api/Controllers/PersonController.cs b/api/Controllers/PersonController.cs index 484bc71..e5bdb86 100644 --- a/api/Controllers/PersonController.cs +++ b/api/Controllers/PersonController.cs @@ -2,14 +2,16 @@ using Microsoft.AspNetCore.Mvc; using api.Services; using api.DTOs; using Microsoft.EntityFrameworkCore; +using api.RabbitMQ; namespace api.Controllers { [ApiController] [Route("api/[controller]")] - public class PersonController(IPersonService personService) : ControllerBase + public class PersonController(IPersonService personService, IMessageProducer messagePublisher) : ControllerBase { private readonly IPersonService _personService = personService; + private readonly IMessageProducer _messagePublisher = messagePublisher; [HttpGet] public async Task GetAll() @@ -37,9 +39,9 @@ namespace api.Controllers { try { - await _personService.AddPersonAsync(personDto); - return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto); - + await _personService.AddPersonAsync(personDto); + _messagePublisher.SendMessage(personDto); + return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto); } catch (ArgumentException ex) { diff --git a/api/Program.cs b/api/Program.cs index 2420b4f..49e4c52 100644 --- a/api/Program.cs +++ b/api/Program.cs @@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore; using api.Data; using api.Repositories; using api.Services; +using RabbitMQ.Client; +using api.RabbitMQ; var builder = WebApplication.CreateBuilder(args); @@ -17,6 +19,8 @@ builder.Services.AddDbContext(options => options.UseNpgsql(builder builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddSingleton(); + builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); diff --git a/api/RabbitMQ/IMessageProducer.cs b/api/RabbitMQ/IMessageProducer.cs new file mode 100644 index 0000000..17600f8 --- /dev/null +++ b/api/RabbitMQ/IMessageProducer.cs @@ -0,0 +1,7 @@ +namespace api.RabbitMQ +{ + public interface IMessageProducer + { + void SendMessage (T message); + } +} \ No newline at end of file diff --git a/api/RabbitMQ/RabbitMQProducer.cs b/api/RabbitMQ/RabbitMQProducer.cs new file mode 100644 index 0000000..f09eef3 --- /dev/null +++ b/api/RabbitMQ/RabbitMQProducer.cs @@ -0,0 +1,32 @@ +using api.DTOs; +using RabbitMQ.Client; +using System.Text; +using System.Text.Json; + +namespace api.RabbitMQ; + +public class RabbitMQProducer : IMessageProducer +{ + private IChannel? channel; + private readonly string personQueueName = "persons"; + public async void SendMessage(T message) + { + if (channel == null) + { + ConnectionFactory factory = new ConnectionFactory + { + Uri = new Uri("amqp://guest:guest@localhost:5672") + }; + + IConnection conn = await factory.CreateConnectionAsync(); + channel = await conn.CreateChannelAsync(); + await channel.QueueDeclareAsync(personQueueName); + } + if (message is PersonDto) + { + string jsonString = JsonSerializer.Serialize(message); + byte[] body = Encoding.UTF8.GetBytes(jsonString); + _ = channel.BasicPublishAsync(exchange: "", routingKey: personQueueName, body: body); + } + } +} \ No newline at end of file diff --git a/api/api.csproj b/api/api.csproj index 92990e8..b55c504 100644 --- a/api/api.csproj +++ b/api/api.csproj @@ -11,6 +11,7 @@ +