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-02-19 21:39:03 -08:00
|
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
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
|
|
|
{
|
|
|
|
private string _connectionString;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
var connstr = configuration.GetConnectionString("database");
|
|
|
|
if (connstr is null)
|
|
|
|
{
|
|
|
|
connstr = "";
|
|
|
|
}
|
|
|
|
_connectionString = connstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IDbConnection OpenConnection(string connectionString)
|
|
|
|
{
|
|
|
|
var conn = new NpgsqlConnection(_connectionString);
|
|
|
|
conn.Open();
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public DiscordWebHookModel New(DiscordWebHookModel model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
var uid = Guid.NewGuid();
|
2023-02-19 21:39:03 -08:00
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Insert Into DiscordWebHooks (ID, Url, Server, Channel, Enabled) Values (@id, @url, @server, @channel, @enabled);";
|
|
|
|
conn.Execute(query, new
|
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
id = uid,
|
2023-02-19 21:39:03 -08:00
|
|
|
url = model.Url,
|
|
|
|
server = model.Server,
|
|
|
|
channel = model.Channel,
|
|
|
|
enabled = model.Enabled
|
|
|
|
});
|
2023-02-26 09:40:04 -08:00
|
|
|
model.ID = uid;
|
|
|
|
return model;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public DiscordWebHookModel GetByID(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Select * from DiscordWebHooks Where ID = @id LIMIT 1;";
|
2023-02-26 09:40:04 -08:00
|
|
|
return conn.Query<DiscordWebHookModel>(query, new
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
id = id
|
2023-02-19 21:39:03 -08:00
|
|
|
}).First();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public DiscordWebHookModel GetByUrl(string url)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Select * From DiscordWebHooks Where url = @url;";
|
2023-02-26 09:40:04 -08:00
|
|
|
return conn.QueryFirst<DiscordWebHookModel>(query, new
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
url = url
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<DiscordWebHookModel> List(int page, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-02-26 09:40:04 -08:00
|
|
|
var query = @"Select * From DiscordWebHooks
|
|
|
|
Offset @offset Fetch Next @count Rows Only;";
|
|
|
|
return conn.Query<DiscordWebHookModel>(query, new
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
offset = page * count,
|
|
|
|
count = count
|
2023-02-19 21:39:03 -08:00
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<DiscordWebHookModel> ListByServer(string server, int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Select * From DiscordWebHooks Where Server = @id Limit @limit;";
|
2023-02-26 09:40:04 -08:00
|
|
|
return conn.Query<DiscordWebHookModel>(query, new
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
server = server,
|
|
|
|
limit = limit
|
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<DiscordWebHookModel> ListByServerAndChannel(string server, string channel, int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "SELECT * FROM DiscordWebHooks WHERE Server = @server and Channel = @channel Limit @limit;";
|
2023-02-26 09:40:04 -08:00
|
|
|
return conn.Query<DiscordWebHookModel>(query, new
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
server = server,
|
|
|
|
channel = channel,
|
|
|
|
limit = limit
|
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public int Disable(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Update discordwebhooks Set Enabled = FALSE where ID = @id;";
|
|
|
|
return conn.Execute(query, new
|
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
id = id
|
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
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Update discordwebhooks Set Enabled = TRUE where ID = @id;";
|
|
|
|
return conn.Execute(query, new
|
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
id = id
|
2023-02-19 21:39:03 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|