2023-02-19 21:39:03 -08:00
|
|
|
using System.Data;
|
|
|
|
using Dapper;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
2023-06-23 21:55:56 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-19 21:39:03 -08:00
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Database.Repositories;
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-03-13 21:58:33 -07:00
|
|
|
private readonly string _connectionString;
|
2023-02-19 21:39:03 -08:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public DiscordWebhooksTable(string connectionString)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
_connectionString = connectionString;
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public DiscordWebhooksTable(IConfiguration configuration)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var connstr = configuration.GetConnectionString("database") ?? "";
|
2023-02-19 21:39:03 -08:00
|
|
|
_connectionString = connstr;
|
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
public DiscordWebhookEntity New(DiscordWebhookEntity model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
model.Id = new Guid();
|
|
|
|
|
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
context.DiscordWebhooks.Add(model);
|
|
|
|
context.SaveChanges();
|
2023-02-26 09:40:04 -08:00
|
|
|
return model;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
public DiscordWebhookEntity GetById(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id));
|
|
|
|
res ??= new DiscordWebhookEntity();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
public DiscordWebhookEntity GetByUrl(string url)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
|
|
|
|
res ??= new DiscordWebhookEntity();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
public List<DiscordWebhookEntity> List(int page, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordWebhooks
|
|
|
|
.Skip(page * count)
|
|
|
|
.Take(count)
|
|
|
|
.ToList();
|
|
|
|
res ??= new List<DiscordWebhookEntity>();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordWebhooks
|
|
|
|
.Where(d => d.Server.Equals(server))
|
|
|
|
.Take(limit)
|
|
|
|
.ToList();
|
|
|
|
res ??= new List<DiscordWebhookEntity>();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
public List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordWebhooks
|
|
|
|
.Where(s => s.Server.Equals(server))
|
|
|
|
.Where(c => c.Channel.Equals(channel))
|
|
|
|
.Take(limit)
|
|
|
|
.ToList();
|
|
|
|
res ??= new List<DiscordWebhookEntity>();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public int Disable(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
var res = GetById(id);
|
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
|
|
|
|
res.Enabled = true;
|
|
|
|
|
|
|
|
context.DiscordWebhooks.Update(res);
|
|
|
|
try
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
context.SaveChanges();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
catch(Exception ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public int Enable(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
var res = GetById(id);
|
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
|
|
|
|
res.Enabled = false;
|
|
|
|
|
|
|
|
context.DiscordWebhooks.Update(res);
|
|
|
|
try
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 21:55:56 -07:00
|
|
|
context.SaveChanges();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
catch(Exception ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
2023-03-13 21:58:33 -07:00
|
|
|
|
|
|
|
private IDbConnection OpenConnection(string connectionString)
|
|
|
|
{
|
|
|
|
var conn = new NpgsqlConnection(_connectionString);
|
|
|
|
conn.Open();
|
|
|
|
return conn;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|