Newsbot.Collector/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs

131 lines
3.7 KiB
C#

using System.Data;
using Dapper;
using Microsoft.Extensions.Configuration;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Entities;
using Npgsql;
namespace Newsbot.Collector.Database.Repositories;
public class DiscordWebhooksTable : IDiscordWebHooksRepository
{
private readonly string _connectionString;
public DiscordWebhooksTable(string connectionString)
{
_connectionString = connectionString;
}
public DiscordWebhooksTable(IConfiguration configuration)
{
var connstr = configuration.GetConnectionString("database") ?? "";
_connectionString = connstr;
}
public DiscordWebhookEntity New(DiscordWebhookEntity model)
{
model.Id = new Guid();
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));
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));
res ??= new DiscordWebhookEntity();
return res;
}
public List<DiscordWebhookEntity> List(int page, int count = 25)
{
using var context = new DatabaseContext(_connectionString);
var res = context.DiscordWebhooks
.Skip(page * count)
.Take(count)
.ToList();
res ??= new List<DiscordWebhookEntity>();
return res;
}
public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
{
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;
}
public List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit = 25)
{
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;
}
public int Disable(Guid id)
{
var res = GetById(id);
using var context = new DatabaseContext(_connectionString);
res.Enabled = true;
context.DiscordWebhooks.Update(res);
try
{
context.SaveChanges();
return 1;
}
catch(Exception ex)
{
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
return 0;
}
}
public int Enable(Guid id)
{
var res = GetById(id);
using var context = new DatabaseContext(_connectionString);
res.Enabled = false;
context.DiscordWebhooks.Update(res);
try
{
context.SaveChanges();
return 1;
}
catch(Exception ex)
{
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
return 0;
}
}
private IDbConnection OpenConnection(string connectionString)
{
var conn = new NpgsqlConnection(_connectionString);
conn.Open();
return conn;
}
}