CodeLiturgy.Dashboard/BlueWest.Api/Context/ExchangeInterface.cs

94 lines
2.8 KiB
C#
Raw Normal View History

2022-08-19 19:47:35 +03:00
using System;
using System.Threading.Tasks;
using BlueWest.Tools;
2022-09-11 01:22:04 +03:00
using BlueWest.WebApi.Context;
2022-08-22 00:14:50 +03:00
using BlueWest.WebApi.EF;
2022-08-19 19:47:35 +03:00
namespace BlueWest.WebApi.Interfaces
{
2022-08-22 02:51:45 +03:00
/// <summary>
/// Empty constructor
/// </summary>
2022-09-11 01:22:04 +03:00
2022-08-19 19:47:35 +03:00
public struct ExchangeEvent { }
2022-08-20 05:47:32 +03:00
/// <summary>
/// Interface for getting and storing exchange rates data
/// </summary>
///
2022-08-22 02:51:45 +03:00
public sealed class ExchangeInterface : EventListener<ExchangeEvent>, IDisposable, IAsyncDisposable
2022-08-19 19:47:35 +03:00
{
2022-09-10 07:28:41 +03:00
private readonly ApplicationUserDbContext _applicationUserDbContext;
2022-08-19 19:47:35 +03:00
private readonly EventManager _eventManager;
private readonly CountryDbContext _countryDbContext;
private readonly FinanceDbContext _financeDbContext;
private readonly UserDbContext _userDbContext;
2022-08-20 05:47:32 +03:00
#region Initialization
2022-08-22 02:51:45 +03:00
/// <summary>
/// Database Ef context constructor
2022-08-22 02:51:45 +03:00
/// </summary>
/// <param name="countryDbContext">Country context</param>
/// <param name="financeDbContext">Finance context</param>
/// <param name="userDbContext">User context</param>
/// <param name="eventManager">Event manager injection</param>
2022-08-19 19:47:35 +03:00
public ExchangeInterface(
2022-09-10 07:28:41 +03:00
ApplicationUserDbContext applicationUserDbContext,
2022-08-19 19:47:35 +03:00
CountryDbContext countryDbContext,
FinanceDbContext financeDbContext,
UserDbContext userDbContext,
EventManager eventManager)
{
2022-09-10 07:28:41 +03:00
_applicationUserDbContext = applicationUserDbContext;
2022-08-19 19:47:35 +03:00
_countryDbContext = countryDbContext;
_financeDbContext = financeDbContext;
_userDbContext = userDbContext;
_eventManager = eventManager;
Init();
}
2022-08-22 02:51:45 +03:00
/// <summary>
/// Database Ef context constructor
2022-08-22 02:51:45 +03:00
/// </summary>
2022-08-19 19:47:35 +03:00
public ExchangeInterface() { }
private void Init()
{
_eventManager.EventStartListening<ExchangeEvent>(this);
Console.WriteLine($"{nameof(ExchangeInterface)} Just started!");
}
2022-08-20 05:47:32 +03:00
#endregion
2022-08-19 19:47:35 +03:00
2022-08-20 05:47:32 +03:00
/// <summary>
/// On Exchange Event
/// </summary>
/// <param name="eventType"></param>
2022-08-19 19:47:35 +03:00
public void OnEvent(ExchangeEvent eventType)
{
Console.WriteLine($"Service received exchange {nameof(ExchangeEvent)}");
}
/// <summary>
2022-08-22 02:51:45 +03:00
/// Stop Listening for events on dispose
2022-08-19 19:47:35 +03:00
/// </summary>
public void Dispose()
{
_eventManager.EventStopListening<ExchangeEvent>(this);
}
2022-08-22 02:51:45 +03:00
/// <summary>
/// Stop Listening for events on dispose async
/// </summary>
/// <returns></returns>
2022-08-19 19:47:35 +03:00
public ValueTask DisposeAsync()
{
_eventManager.EventStopListening<ExchangeEvent>(this);
return ValueTask.CompletedTask;
}
}
}