implemented rabbitmq

This commit is contained in:
marco committed 2026-08-31 15:27:07 +02:00
1 parent 7384cfcc84
commit bc38afcbf8
5 files changed
+50 -4

No files matched your search

+6 -4
View File
@@ -2,14 +2,16 @@ using Microsoft.AspNetCore.Mvc;
using api.Services; using api.Services;
using api.DTOs; using api.DTOs;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using api.RabbitMQ;
namespace api.Controllers namespace api.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class PersonController(IPersonService personService) : ControllerBase public class PersonController(IPersonService personService, IMessageProducer messagePublisher) : ControllerBase
{ {
private readonly IPersonService _personService = personService; private readonly IPersonService _personService = personService;
private readonly IMessageProducer _messagePublisher = messagePublisher;
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAll() public async Task<IActionResult> GetAll()
@@ -37,9 +39,9 @@ namespace api.Controllers
{ {
try try
{ {
await _personService.AddPersonAsync(personDto); await _personService.AddPersonAsync(personDto);
return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto); _messagePublisher.SendMessage<PersonDto>(personDto);
return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto);
} }
catch (ArgumentException ex) catch (ArgumentException ex)
{ {
+4
View File
@@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore;
using api.Data; using api.Data;
using api.Repositories; using api.Repositories;
using api.Services; using api.Services;
using RabbitMQ.Client;
using api.RabbitMQ;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -17,6 +19,8 @@ builder.Services.AddDbContext<AppDbContext>(options => options.UseNpgsql(builder
builder.Services.AddScoped<IPersonRepository, PersonRepository>(); builder.Services.AddScoped<IPersonRepository, PersonRepository>();
builder.Services.AddScoped<IPersonService, PersonService>(); builder.Services.AddScoped<IPersonService, PersonService>();
builder.Services.AddSingleton<IMessageProducer, RabbitMQProducer>();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
+7
View File
@@ -0,0 +1,7 @@
namespace api.RabbitMQ
{
public interface IMessageProducer
{
void SendMessage<T> (T message);
}
}
+32
View File
@@ -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>(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);
}
}
}
+1
View File
@@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.11" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
<PackageReference Include="RabbitMQ.Client" Version="7.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
</ItemGroup> </ItemGroup>