2023-02-19 21:39:03 -08:00
|
|
|
using System.Data;
|
|
|
|
using Dapper;
|
2023-06-23 20:19:09 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-19 21:39:03 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:19:09 -07:00
|
|
|
public void New(DiscordQueueEntity model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 20:19:09 -07:00
|
|
|
model.Id = new Guid();
|
|
|
|
|
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordQueue.Add(model);
|
|
|
|
context.SaveChanges();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(Guid id)
|
|
|
|
{
|
2023-06-23 20:19:09 -07:00
|
|
|
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();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 20:19:09 -07:00
|
|
|
public List<DiscordQueueEntity> List(int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 20:19:09 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.DiscordQueue.Take(limit).ToList();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
}
|