implemented rabbitmq
This commit is contained in:
1 parent
7384cfcc84
commit
bc38afcbf8
5 files changed
+48
-2
No files matched your search
@@ -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()
|
||||||
@@ -38,8 +40,8 @@ namespace api.Controllers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _personService.AddPersonAsync(personDto);
|
await _personService.AddPersonAsync(personDto);
|
||||||
|
_messagePublisher.SendMessage<PersonDto>(personDto);
|
||||||
return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto);
|
return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (ArgumentException ex)
|
catch (ArgumentException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace api.RabbitMQ
|
||||||
|
{
|
||||||
|
public interface IMessageProducer
|
||||||
|
{
|
||||||
|
void SendMessage<T> (T message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user