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

132 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;
private DatabaseContext _context;
public DiscordWebhooksTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public DiscordWebhooksTable(DatabaseContext context)
{
_context = context;
}
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;
//}
}