Migration to EF and starting to add Identity #11
@ -11,29 +11,34 @@ namespace Newsbot.Collector.Database.Repositories;
|
||||
|
||||
public class ArticlesTable : IArticlesRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
//private readonly string _connectionString;
|
||||
|
||||
private DatabaseContext _context;
|
||||
|
||||
public ArticlesTable(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
//_connectionString = connectionString;
|
||||
_context = new DatabaseContext(connectionString);
|
||||
}
|
||||
|
||||
public ArticlesTable(IConfiguration configuration)
|
||||
public ArticlesTable(DatabaseContext context)
|
||||
{
|
||||
var conn = configuration.GetConnectionString("database");
|
||||
if (conn is null) conn = "";
|
||||
|
||||
_connectionString = conn;
|
||||
_context = new DatabaseContext(conn);
|
||||
_context = context;
|
||||
}
|
||||
|
||||
//public ArticlesTable(IConfiguration configuration)
|
||||
//{
|
||||
// var conn = configuration.GetConnectionString("database");
|
||||
// if (conn is null) conn = "";
|
||||
//
|
||||
// _context = new DatabaseContext(conn);
|
||||
//}
|
||||
|
||||
public List<ArticlesEntity> List(int page = 0, int count = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var query = context.Articles
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
|
||||
var query = _context.Articles
|
||||
.Skip(page * count)
|
||||
.OrderBy(d => d.PubDate)
|
||||
.Take(25);
|
||||
@ -43,8 +48,8 @@ public class ArticlesTable : IArticlesRepository
|
||||
|
||||
public ArticlesEntity GetById(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var query = context.Articles
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var query = _context.Articles
|
||||
.FirstOrDefault(d => d.Id.Equals(id));
|
||||
query ??= new ArticlesEntity();
|
||||
return query;
|
||||
@ -52,16 +57,16 @@ public class ArticlesTable : IArticlesRepository
|
||||
|
||||
public ArticlesEntity GetByUrl(string url)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Articles.FirstOrDefault(d => d.Url!.Equals(url));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Articles.FirstOrDefault(d => d.Url!.Equals(url));
|
||||
res ??= new ArticlesEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<ArticlesEntity> ListBySourceId(Guid id, int page, int count)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Articles
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Articles
|
||||
.Skip(page * count)
|
||||
.Where(d => d.SourceId.Equals(id));
|
||||
return res.ToList();
|
||||
@ -69,13 +74,14 @@ public class ArticlesTable : IArticlesRepository
|
||||
|
||||
public ArticlesEntity New(ArticlesEntity model)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
model.Id = new Guid();
|
||||
var query = context.Articles.Add(model);
|
||||
context.SaveChanges();
|
||||
var query = _context.Articles.Add(model);
|
||||
_context.SaveChanges();
|
||||
return model;
|
||||
}
|
||||
|
||||
/*
|
||||
public ArticlesModel NewDapper(ArticlesModel model)
|
||||
{
|
||||
model.ID = Guid.NewGuid();
|
||||
@ -103,26 +109,27 @@ public class ArticlesTable : IArticlesRepository
|
||||
});
|
||||
return model;
|
||||
}
|
||||
*/
|
||||
|
||||
public void DeleteAllBySourceId(Guid sourceId)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Articles
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Articles
|
||||
.Where(d => d.SourceId.Equals(sourceId))
|
||||
.ToList();
|
||||
|
||||
foreach (var item in res)
|
||||
{
|
||||
context.Articles.Remove(item);
|
||||
_context.Articles.Remove(item);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
{
|
||||
var conn = new NpgsqlConnection(_connectionString);
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
//private IDbConnection OpenConnection(string connectionString)
|
||||
//{
|
||||
// //var conn = new NpgsqlConnection(_connectionString);
|
||||
// //conn.Open();
|
||||
// //return conn;
|
||||
//}
|
||||
}
|
@ -9,42 +9,49 @@ namespace Newsbot.Collector.Database.Repositories;
|
||||
|
||||
public class DiscordQueueTable : IDiscordQueueRepository
|
||||
{
|
||||
private string _connectionString;
|
||||
//private string _connectionString;
|
||||
private DatabaseContext _context;
|
||||
|
||||
public DiscordQueueTable(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
//_connectionString = connectionString;
|
||||
_context = new DatabaseContext(connectionString);
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
public DiscordQueueTable(DatabaseContext context)
|
||||
{
|
||||
var conn = new NpgsqlConnection(_connectionString);
|
||||
conn.Open();
|
||||
return conn;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
//private IDbConnection OpenConnection(string connectionString)
|
||||
//{
|
||||
// var conn = new NpgsqlConnection(_connectionString);
|
||||
// conn.Open();
|
||||
// return conn;
|
||||
//}
|
||||
|
||||
public void New(DiscordQueueEntity model)
|
||||
{
|
||||
model.Id = new Guid();
|
||||
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordQueue.Add(model);
|
||||
context.SaveChanges();
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordQueue.Add(model);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void Delete(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordQueue.FirstOrDefault(d => d.Id.Equals(id));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordQueue.FirstOrDefault(d => d.Id.Equals(id));
|
||||
res ??= new DiscordQueueEntity();
|
||||
context.DiscordQueue.Remove(res);
|
||||
context.SaveChanges();
|
||||
_context.DiscordQueue.Remove(res);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public List<DiscordQueueEntity> List(int limit = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordQueue.Take(limit).ToList();
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordQueue.Take(limit).ToList();
|
||||
return res;
|
||||
}
|
||||
}
|
@ -9,49 +9,50 @@ namespace Newsbot.Collector.Database.Repositories;
|
||||
|
||||
public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
//private readonly string _connectionString;
|
||||
private DatabaseContext _context;
|
||||
|
||||
public DiscordWebhooksTable(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
//_connectionString = connectionString;
|
||||
_context = new DatabaseContext(connectionString);
|
||||
}
|
||||
|
||||
public DiscordWebhooksTable(IConfiguration configuration)
|
||||
public DiscordWebhooksTable(DatabaseContext context)
|
||||
{
|
||||
var connstr = configuration.GetConnectionString("database") ?? "";
|
||||
_connectionString = connstr;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public DiscordWebhookEntity New(DiscordWebhookEntity model)
|
||||
{
|
||||
model.Id = new Guid();
|
||||
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
context.DiscordWebhooks.Add(model);
|
||||
context.SaveChanges();
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
_context.DiscordWebhooks.Add(model);
|
||||
_context.SaveChanges();
|
||||
return model;
|
||||
}
|
||||
|
||||
public DiscordWebhookEntity GetById(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id));
|
||||
res ??= new DiscordWebhookEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public DiscordWebhookEntity GetByUrl(string url)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
|
||||
res ??= new DiscordWebhookEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<DiscordWebhookEntity> List(int page, int count = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordWebhooks
|
||||
.Skip(page * count)
|
||||
.Take(count)
|
||||
.ToList();
|
||||
@ -61,8 +62,8 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
|
||||
public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordWebhooks
|
||||
.Where(d => d.Server.Equals(server))
|
||||
.Take(limit)
|
||||
.ToList();
|
||||
@ -72,8 +73,8 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
|
||||
public List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.DiscordWebhooks
|
||||
.Where(s => s.Server.Equals(server))
|
||||
.Where(c => c.Channel.Equals(channel))
|
||||
.Take(limit)
|
||||
@ -85,14 +86,14 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
public int Disable(Guid id)
|
||||
{
|
||||
var res = GetById(id);
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
|
||||
res.Enabled = true;
|
||||
|
||||
context.DiscordWebhooks.Update(res);
|
||||
_context.DiscordWebhooks.Update(res);
|
||||
try
|
||||
{
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch(Exception ex)
|
||||
@ -105,14 +106,14 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
public int Enable(Guid id)
|
||||
{
|
||||
var res = GetById(id);
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
|
||||
res.Enabled = false;
|
||||
|
||||
context.DiscordWebhooks.Update(res);
|
||||
_context.DiscordWebhooks.Update(res);
|
||||
try
|
||||
{
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch(Exception ex)
|
||||
@ -122,10 +123,10 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
}
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
{
|
||||
var conn = new NpgsqlConnection(_connectionString);
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
//private IDbConnection OpenConnection(string connectionString)
|
||||
//{
|
||||
// var conn = new NpgsqlConnection(_connectionString);
|
||||
// conn.Open();
|
||||
// return conn;
|
||||
//}
|
||||
}
|
@ -10,49 +10,49 @@ namespace Newsbot.Collector.Database.Repositories;
|
||||
|
||||
public class IconsTable : IIconsRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
//private readonly string _connectionString;
|
||||
private DatabaseContext _context;
|
||||
|
||||
public IconsTable(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
//_connectionString = connectionString;
|
||||
_context = new DatabaseContext(connectionString);
|
||||
}
|
||||
|
||||
public IconsTable(IConfiguration configuration)
|
||||
public IconsTable(DatabaseContext context)
|
||||
{
|
||||
var connstr = configuration.GetConnectionString("database");
|
||||
if (connstr is null) connstr = "";
|
||||
_connectionString = connstr;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void New(IconEntity model)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
model.Id = Guid.NewGuid();
|
||||
|
||||
context.Icons.Add(model);
|
||||
context.SaveChanges();
|
||||
_context.Icons.Add(model);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public IconEntity GetById(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Icons.FirstOrDefault(f => f.Id.Equals(id));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Icons.FirstOrDefault(f => f.Id.Equals(id));
|
||||
res ??= new IconEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public IconEntity GetBySourceId(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Icons.FirstOrDefault(f => f.SourceId.Equals(id));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Icons.FirstOrDefault(f => f.SourceId.Equals(id));
|
||||
res ??= new IconEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
{
|
||||
var conn = new NpgsqlConnection(_connectionString);
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
//private IDbConnection OpenConnection(string connectionString)
|
||||
//{
|
||||
// var conn = new NpgsqlConnection(_connectionString);
|
||||
// conn.Open();
|
||||
// return conn;
|
||||
//}
|
||||
}
|
@ -10,28 +10,28 @@ namespace Newsbot.Collector.Database.Repositories;
|
||||
|
||||
public class SourcesTable : ISourcesRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
//private readonly string _connectionString;
|
||||
private DatabaseContext _context;
|
||||
|
||||
public SourcesTable(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
//_connectionString = connectionString;
|
||||
_context = new DatabaseContext(connectionString);
|
||||
}
|
||||
|
||||
public SourcesTable(IConfiguration configuration)
|
||||
public SourcesTable(DatabaseContext context)
|
||||
{
|
||||
var connstr = configuration.GetConnectionString("database");
|
||||
if (connstr is null) connstr = "";
|
||||
_connectionString = connstr;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public SourceEntity New(SourceEntity model)
|
||||
{
|
||||
model.Id = Guid.NewGuid();
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
context.Sources.Add(model);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
_context.Sources.Add(model);
|
||||
try
|
||||
{
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -43,8 +43,8 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public SourceEntity GetById(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources.FirstOrDefault(f => f.Id.Equals(id));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources.FirstOrDefault(f => f.Id.Equals(id));
|
||||
res ??= new SourceEntity();
|
||||
return res;
|
||||
}
|
||||
@ -57,16 +57,16 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public SourceEntity GetByName(string name)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources.FirstOrDefault(f => f.Name.Equals(name));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources.FirstOrDefault(f => f.Name.Equals(name));
|
||||
res ??= new SourceEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public SourceEntity GetByNameAndType(string name, string type)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources
|
||||
.Where(f => f.Name.Equals(name))
|
||||
.FirstOrDefault(f => f.Type.Equals(type));
|
||||
res ??= new SourceEntity();
|
||||
@ -75,8 +75,8 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public SourceEntity GetByUrl(string url)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources
|
||||
.FirstOrDefault(f => f.Url.Equals(url));
|
||||
res ??= new SourceEntity();
|
||||
return res;
|
||||
@ -84,8 +84,8 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public List<SourceEntity> List(int page = 0, int count = 100)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources
|
||||
.Skip(page * count)
|
||||
.Take(count)
|
||||
.ToList();
|
||||
@ -94,8 +94,8 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public List<SourceEntity> ListBySource(string source, int page = 0, int limit = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources
|
||||
.Where(f => f.Source.Equals(source))
|
||||
.Skip(page * limit)
|
||||
.Take(limit)
|
||||
@ -105,8 +105,8 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public List<SourceEntity> ListByType(string type,int page = 0, int limit = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Sources
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Sources
|
||||
.Where(f => f.Type.Equals(type))
|
||||
.Skip(page * limit)
|
||||
.Take(limit)
|
||||
@ -116,13 +116,13 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public int Disable(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = GetById(id);
|
||||
res.Enabled = false;
|
||||
context.Sources.Update(res);
|
||||
_context.Sources.Update(res);
|
||||
try
|
||||
{
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch
|
||||
@ -133,13 +133,13 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public int Enable(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = GetById(id);
|
||||
res.Enabled = true;
|
||||
context.Sources.Update(res);
|
||||
_context.Sources.Update(res);
|
||||
try
|
||||
{
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch
|
||||
@ -150,21 +150,21 @@ public class SourcesTable : ISourcesRepository
|
||||
|
||||
public void Delete(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = GetById(id);
|
||||
context.Sources.Remove(res);
|
||||
context.SaveChanges();
|
||||
_context.Sources.Remove(res);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public int UpdateYoutubeId(Guid id, string youtubeId)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = GetById(id);
|
||||
res.YoutubeId = youtubeId;
|
||||
context.Sources.Update(res);
|
||||
_context.Sources.Update(res);
|
||||
try
|
||||
{
|
||||
context.SaveChanges();
|
||||
_context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch
|
||||
@ -173,10 +173,10 @@ public class SourcesTable : ISourcesRepository
|
||||
}
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
{
|
||||
var conn = new NpgsqlConnection(_connectionString);
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
//private IDbConnection OpenConnection(string connectionString)
|
||||
//{
|
||||
// var conn = new NpgsqlConnection(_connectionString);
|
||||
// conn.Open();
|
||||
// return conn;
|
||||
//}
|
||||
}
|
@ -10,61 +10,61 @@ namespace Newsbot.Collector.Database.Repositories;
|
||||
|
||||
public class SubscriptionsTable : ISubscriptionRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
//private readonly string _connectionString;
|
||||
private DatabaseContext _context;
|
||||
|
||||
public SubscriptionsTable(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
//_connectionString = connectionString;
|
||||
_context = new DatabaseContext(connectionString);
|
||||
}
|
||||
|
||||
public SubscriptionsTable(IConfiguration configuration)
|
||||
public SubscriptionsTable(DatabaseContext context)
|
||||
{
|
||||
var connstr = configuration.GetConnectionString("database");
|
||||
if (connstr is null) connstr = "";
|
||||
_connectionString = connstr;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public SubscriptionEntity New(SubscriptionEntity model)
|
||||
{
|
||||
model.Id = new Guid();
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
context.Subscriptions.Add(model);
|
||||
context.SaveChanges();
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
_context.Subscriptions.Add(model);
|
||||
_context.SaveChanges();
|
||||
return model;
|
||||
}
|
||||
|
||||
public List<SubscriptionEntity> List(int page = 0, int count = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
return context.Subscriptions.Skip(page * count).Take(count).ToList();
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
return _context.Subscriptions.Skip(page * count).Take(count).ToList();
|
||||
}
|
||||
|
||||
public List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
return context.Subscriptions.Where(f => f.SourceId.Equals(id))
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
return _context.Subscriptions.Where(f => f.SourceId.Equals(id))
|
||||
.Skip(page * count)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
return context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
return _context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
|
||||
}
|
||||
|
||||
public SubscriptionEntity GetById(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Subscriptions
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Subscriptions
|
||||
.FirstOrDefault(f => f.Id.Equals(id));
|
||||
return res ??= new SubscriptionEntity();
|
||||
}
|
||||
|
||||
public SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Subscriptions
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Subscriptions
|
||||
.Where(f => f.DiscordWebHookId.Equals(webhookId))
|
||||
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
|
||||
return res ??= new SubscriptionEntity();
|
||||
@ -72,22 +72,21 @@ public class SubscriptionsTable : ISubscriptionRepository
|
||||
|
||||
public void Delete(Guid id)
|
||||
{
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id));
|
||||
//using var context = new DatabaseContext(_connectionString);
|
||||
var res = _context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id));
|
||||
if (res is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.Subscriptions.Remove(res);
|
||||
context.SaveChanges();
|
||||
|
||||
_context.Subscriptions.Remove(res);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
{
|
||||
var conn = new NpgsqlConnection(_connectionString);
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
//private IDbConnection OpenConnection(string connectionString)
|
||||
//{
|
||||
// var conn = new NpgsqlConnection(_connectionString);
|
||||
// conn.Open();
|
||||
// return conn;
|
||||
//}
|
||||
}
|
Loading…
Reference in New Issue
Block a user