Newsbot.Collector/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs

92 lines
3.0 KiB
C#

using System.Data;
using Dapper;
using Microsoft.Extensions.Configuration;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models;
using Npgsql;
namespace Newsbot.Collector.Database.Repositories;
public class DiscordNotificationTable : IDiscordNotificationRepository
{
//private readonly string _connectionString;
private DatabaseContext _context;
public DiscordNotificationTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public DiscordNotificationTable(DatabaseContext context)
{
_context = context;
}
public DiscordNotificationEntity New(DiscordNotificationEntity model)
{
model.Id = new Guid();
//using var context = new DatabaseContext(_connectionString);
_context.DiscordNotification.Add(model);
_context.SaveChanges();
return model;
}
public List<DiscordNotificationEntity> List(int page = 0, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
return _context.DiscordNotification.Skip(page * count).Take(count).ToList();
}
public List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
return _context.DiscordNotification.Where(f => f.SourceId.Equals(id))
.Skip(page * count)
.ToList();
}
public List<DiscordNotificationEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
return _context.DiscordNotification.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
}
public DiscordNotificationEntity GetById(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordNotification
.FirstOrDefault(f => f.Id.Equals(id));
return res ??= new DiscordNotificationEntity();
}
public DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordNotification
.Where(f => f.DiscordWebHookId.Equals(webhookId))
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
return res ??= new DiscordNotificationEntity();
}
public void Delete(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordNotification.FirstOrDefault(f => f.Id.Equals(id));
if (res is null)
{
return;
}
_context.DiscordNotification.Remove(res);
_context.SaveChanges();
}
//private IDbConnection OpenConnection(string connectionString)
//{
// var conn = new NpgsqlConnection(_connectionString);
// conn.Open();
// return conn;
//}
}