-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from lrodolfol/feat/read_new_user_created
Feat/read new user created
- Loading branch information
Showing
32 changed files
with
649 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...rs/ChurchMonthWork/ConsumerChurchMonthWork/MessageBrocker/Config/RabbitMqConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ConsumerChurchMonthWork.MessageBrocker.Config; | ||
public class RabbitMQConfiguration | ||
{ | ||
public const string ConfigurationSection = "RabbitMQ"; | ||
public string? Host { get; set; } | ||
public string? VirtualHost { get; set; } | ||
public int Port { get; set; } | ||
public string? Username { get; set; } | ||
public string? Password { get; set; } | ||
} | ||
|
79 changes: 79 additions & 0 deletions
79
...hurchMonthWork/ConsumerChurchMonthWork/MessageBrocker/Consumer/NewUserCreatedListerner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using ConsumerChurchMonthWork.Models; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Events; | ||
using System.Text; | ||
using System.Text.Json; | ||
using RabbitMQ.Client.Exceptions; | ||
using System.Security.Cryptography; | ||
using ConsumerChurchMonthWork.Services; | ||
|
||
namespace ConsumerChurchMonthWork.MessageBrocker.Consumer; | ||
public class NewUserCreatedListerner : BackgroundService | ||
{ | ||
private readonly IModel _channel; | ||
private readonly ILogger _logger; | ||
private readonly string _queueName = "user_created"; | ||
public NewUserCreatedListerner(IModel channel, ILogger logger) | ||
{ | ||
_channel = channel; | ||
_logger = logger; | ||
} | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
try | ||
{ | ||
await ExecuteConsume(stoppingToken); | ||
} | ||
catch(Exception ex) when (ex is BrokerUnreachableException || ex is OperationInterruptedException) | ||
{ | ||
_logger.Error("An error with rabbitMq configs occurred before the connection: {0}", ex.Message); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.Error("An error occurred before the connection: {0}", ex.Message); | ||
} | ||
} | ||
|
||
private async Task ExecuteConsume(CancellationToken stoppingToken) | ||
{ | ||
var consumer = new EventingBasicConsumer(_channel); | ||
consumer.Received += OnMessageReceived; | ||
_channel.BasicConsume(_queueName, false, consumer); | ||
|
||
while (!stoppingToken.IsCancellationRequested) | ||
await Task.Delay(1000, stoppingToken); | ||
|
||
_logger.Warning("Dispose connection"); | ||
_channel.Dispose(); | ||
} | ||
|
||
private async void OnMessageReceived(object? sender, BasicDeliverEventArgs eventsArgs) | ||
{ | ||
try | ||
{ | ||
byte[] rawMessage = eventsArgs.Body.ToArray(); | ||
string message = Encoding.UTF8.GetString(rawMessage); | ||
UserCreatedMessageDto? objMessage = JsonSerializer.Deserialize<UserCreatedMessageDto>(message); | ||
|
||
if(objMessage is null || objMessage.EmailAddress is null) | ||
throw new ArgumentNullException(); | ||
|
||
//await new SendEmailNewUser().SendEmailAsync(objMessage); | ||
|
||
_channel.BasicAck(eventsArgs.DeliveryTag, false); | ||
_logger.Information($"Message newUserCreated processed successful - {objMessage.EmailAddress}"); | ||
} | ||
catch(Exception ex) when (ex is JsonException || ex is ArgumentNullException) | ||
{ | ||
_logger.Error("There was an error deserializing the message: {0}", ex.Message); | ||
_channel.BasicNack(eventsArgs.DeliveryTag, false, false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.Error("There was an error processing the message: {0}", ex.Message); | ||
_channel.BasicNack(eventsArgs.DeliveryTag, false, false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
...tions/Consumers/ChurchMonthWork/ConsumerChurchMonthWork/MessageBrocker/IMessageBrocker.cs
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Operations/Consumers/ChurchMonthWork/ConsumerChurchMonthWork/Models/UserCreatedMessageDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
namespace ConsumerChurchMonthWork.Models; | ||
public record UserCreatedMessageDto(short Id, string EmailAddress, DateTime OcurredOn, string Password); |
83 changes: 59 additions & 24 deletions
83
Operations/Consumers/ChurchMonthWork/ConsumerChurchMonthWork/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,76 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Serilog; | ||
using ConsumerChurchMonthWork.MessageBrocker; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Options; | ||
using RabbitMQ.Client; | ||
using ConsumerChurchMonthWork.MessageBrocker.Config; | ||
using ConsumerChurchMonthWork.MessageBrocker.Consumer; | ||
|
||
IConfigurationRoot configuration; | ||
Serilog.ILogger logger; | ||
|
||
ServiceCollection serviceCollection = new ServiceCollection(); | ||
ConfigureServices(serviceCollection); | ||
//ServiceCollection serviceCollection = new ServiceCollection(); | ||
//ConfigureServices(serviceCollection); | ||
|
||
//var churchId = 1; | ||
//var competence = "05-2023"; | ||
//void ConfigureServices(IServiceCollection serviceCollection) | ||
//{ | ||
logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
//IDataBase dataBase = new MysqlDataBase(configuration); | ||
configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json", false) | ||
.Build(); | ||
|
||
//var report = new Report(dataBase, churchId, competence); | ||
//var listMonthleClosing = report.Generate(); | ||
// serviceCollection.AddSingleton<IConfigurationRoot>(configuration); | ||
// serviceCollection.AddSingleton<ILogger>(logger); | ||
// serviceCollection.AddHostedService<NewUserCreatedListerner>(); | ||
//} | ||
|
||
//var jsonObj = JsonSerializer.Serialize(listMonthleClosing.Result); | ||
//var returnList = JsonSerializer.Deserialize<List<Entitie.MonthlyClosing>>(jsonObj); | ||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<IConfigurationRoot>(configuration); | ||
services.AddSingleton<ILogger>(logger); | ||
|
||
services.Configure<RabbitMQConfiguration>(configuration.GetSection("MessageBroker")); | ||
|
||
void ConfigureServices(IServiceCollection serviceCollection) | ||
{ | ||
logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
services.AddSingleton(serviceProvider => | ||
{ | ||
RabbitMQConfiguration rabbitMQConfiguration = | ||
serviceProvider.GetRequiredService<IOptions<RabbitMQConfiguration>>().Value; | ||
|
||
configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json", false) | ||
.Build(); | ||
var factory = new ConnectionFactory | ||
{ | ||
HostName = rabbitMQConfiguration.Host, | ||
UserName = rabbitMQConfiguration.Username, | ||
Password = rabbitMQConfiguration.Password, | ||
Port = rabbitMQConfiguration.Port | ||
}; | ||
|
||
serviceCollection.AddSingleton<IConfigurationRoot>(configuration); | ||
serviceCollection.AddSingleton<ILogger>(logger); | ||
} | ||
IConnection connection = factory.CreateConnection(); | ||
return connection; | ||
}); | ||
|
||
services.AddHostedService(serviceProvider => | ||
{ | ||
var config = serviceProvider.GetRequiredService<IOptions<RabbitMQConfiguration>>(); | ||
IConnection connection = serviceProvider.GetRequiredService<IConnection>(); | ||
|
||
RabbitMq rabbit = new RabbitMq(configuration, logger); | ||
rabbit.StartConsumer(); | ||
return new NewUserCreatedListerner( | ||
connection.CreateModel(), | ||
logger | ||
); | ||
}); | ||
|
||
|
||
|
||
}) | ||
.Build(); | ||
|
||
await host.RunAsync(); | ||
|
||
//RabbitMq rabbit = new RabbitMq(configuration, logger); | ||
//rabbit.StartConsumer(); |
11 changes: 11 additions & 0 deletions
11
Operations/Consumers/ChurchMonthWork/ConsumerChurchMonthWork/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"profiles": { | ||
"ConsumerChurchMonthWork": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"PASSEMAIL": "#Sojesussalva", | ||
"KEYUSERCREATED": "AAECAwQFBgcICQoLDA0ODw==" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...erChurchMonthWork/Repository/IDataBase.cs → ...hWork/Repository/Connections/IDataBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
using Entitie = ConsumerChurchMonthWork.Entitie; | ||
|
||
namespace ConsumerChurchMonthWork.Repository; | ||
namespace ConsumerChurchMonthWork.Repository.Connections; | ||
|
||
public interface IDataBase { | ||
public interface IDataBase | ||
{ | ||
public Task<List<Entitie.MonthlyClosing>> SelectReport(string churchId, string month, string year); | ||
} |
Oops, something went wrong.