Commit 00d842fe authored by Clint.Network's avatar Clint.Network

Migrate to Dependency Injection

parent 8d1f3047
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Stratis.Guru.Models;
using Stratis.Guru.Services;
using Stratis.Guru.Settings;
namespace Stratis.Guru
{
public class DatabaseContext
{
private readonly IMongoDatabase _database;
public DatabaseContext(IConfiguration configuration)
{
var client = new MongoClient(configuration.GetConnectionString("DefaultConnection"));
if (client != null)
{
_database = client.GetDatabase("stratis-guru");
}
}
public IMongoCollection<Draw> Draws => _database.GetCollection<Draw>("draws");
public IMongoCollection<Setting> Settings => _database.GetCollection<Setting>("lottery");
}
}
\ No newline at end of file
namespace Stratis.Guru.DependencyInjection
{
public interface IMongoContext
{
}
}
\ No newline at end of file
...@@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Attributes; ...@@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Attributes;
namespace Stratis.Guru.Models namespace Stratis.Guru.Models
{ {
public class LotteryDraw public class Draw
{ {
[BsonId] [BsonId]
public ObjectId Id { get; set; } public ObjectId Id { get; set; }
......
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using Stratis.Guru;
using Stratis.Guru.Services;
namespace Stratis.Guru.Models
{
public class Draws : IDraws
{
private DatabaseContext _databaseContext;
public Draws(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}
public async Task InitDrawAsync(long nextDrawTimestamp)
{
if(!_databaseContext.Draws.Find(x => x.DrawDate.Equals(nextDrawTimestamp)).Any())
{
await _databaseContext.Draws.InsertOneAsync(new Draw()
{
DrawDate = nextDrawTimestamp,
Passed = false
});
}
}
}
}
\ No newline at end of file
using System.Threading.Tasks;
namespace Stratis.Guru.Models
{
public interface IDraws
{
Task InitDrawAsync(long nextDrawTimestamp);
}
}
\ No newline at end of file
using System.Threading.Tasks;
namespace Stratis.Guru.Models
{
public interface ISettings
{
Task InitAsync();
int GetIterator();
}
}
\ No newline at end of file
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
namespace Stratis.Guru.Services namespace Stratis.Guru.Models
{ {
public class LotterySetting public class Setting
{ {
[BsonId] [BsonId]
public ObjectId Id { get; set; } public ObjectId Id { get; set; }
......
using System.Threading.Tasks;
using MongoDB.Driver;
namespace Stratis.Guru.Models
{
public class Settings : ISettings
{
private DatabaseContext _databaseContext;
public Settings(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}
public int GetIterator() => _databaseContext.Settings.Find(x => true).FirstOrDefault().PublicKeyIterator;
public async Task InitAsync()
{
if(!_databaseContext.Settings.Find(x => true).Any())
{
await _databaseContext.Settings.InsertOneAsync(new Setting
{
PublicKeyIterator = 0
});
}
}
}
}
\ No newline at end of file
...@@ -56,6 +56,9 @@ namespace Stratis.Guru ...@@ -56,6 +56,9 @@ namespace Stratis.Guru
services.AddTransient<UpdateHub>(); services.AddTransient<UpdateHub>();
services.AddSingleton<IAsk, Ask>(); services.AddSingleton<IAsk, Ask>();
services.AddTransient<DatabaseContext>();
services.AddSingleton<ISettings, Models.Settings>();
services.AddSingleton<IDraws, Draws>();
services.AddHostedService<UpdateInfosService>(); services.AddHostedService<UpdateInfosService>();
services.AddHostedService<FixerService>(); services.AddHostedService<FixerService>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment