Newsbot.Collector/Newsbot.Collector.Database/Repositories/DiscordQueue.cs

57 lines
1.6 KiB
C#

using System.Data;
using Dapper;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models;
using Npgsql;
namespace Newsbot.Collector.Database.Repositories;
public class DiscordQueueTable : IDiscordQueueRepository
{
//private string _connectionString;
private DatabaseContext _context;
public DiscordQueueTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public DiscordQueueTable(DatabaseContext context)
{
_context = context;
}
//private IDbConnection OpenConnection(string connectionString)
//{
// var conn = new NpgsqlConnection(_connectionString);
// conn.Open();
// return conn;
//}
public void New(DiscordQueueEntity model)
{
model.Id = new Guid();
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordQueue.Add(model);
_context.SaveChanges();
}
public void Delete(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordQueue.FirstOrDefault(d => d.Id.Equals(id));
res ??= new DiscordQueueEntity();
_context.DiscordQueue.Remove(res);
_context.SaveChanges();
}
public List<DiscordQueueEntity> List(int limit = 25)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordQueue.Take(limit).ToList();
return res;
}
}