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