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

57 lines
1.6 KiB
C#
Raw Normal View History

using System.Data;
using Dapper;
2023-06-23 20:19:09 -07:00
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;
//}
2023-06-23 20:19:09 -07:00
public void New(DiscordQueueEntity model)
{
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();
}
public void Delete(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordQueue.FirstOrDefault(d => d.Id.Equals(id));
2023-06-23 20:19:09 -07:00
res ??= new DiscordQueueEntity();
_context.DiscordQueue.Remove(res);
_context.SaveChanges();
}
2023-06-23 20:19:09 -07:00
public List<DiscordQueueEntity> List(int limit = 25)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordQueue.Take(limit).ToList();
2023-06-23 20:19:09 -07:00
return res;
}
}