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

50 lines
1.4 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;
public DiscordQueueTable(string connectionString)
{
_connectionString = connectionString;
}
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;
}
}