2023-02-19 21:39:03 -08:00
|
|
|
using System.Data;
|
|
|
|
using Dapper;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-06-25 21:02:26 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
2023-02-19 21:39:03 -08:00
|
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Database.Repositories;
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public class SubscriptionsTable : ISubscriptionRepository
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-03-13 21:58:33 -07:00
|
|
|
private readonly string _connectionString;
|
2023-02-19 21:39:03 -08:00
|
|
|
|
|
|
|
public SubscriptionsTable(string connectionString)
|
|
|
|
{
|
|
|
|
_connectionString = connectionString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SubscriptionsTable(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
var connstr = configuration.GetConnectionString("database");
|
2023-03-13 21:58:33 -07:00
|
|
|
if (connstr is null) connstr = "";
|
2023-02-19 21:39:03 -08:00
|
|
|
_connectionString = connstr;
|
|
|
|
}
|
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
public SubscriptionEntity New(SubscriptionEntity model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
model.Id = new Guid();
|
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
context.Subscriptions.Add(model);
|
|
|
|
context.SaveChanges();
|
|
|
|
return model;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
public List<SubscriptionEntity> List(int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
return context.Subscriptions.Skip(page * count).Take(count).ToList();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
public List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
return context.Subscriptions.Where(f => f.SourceId.Equals(id))
|
|
|
|
.Skip(page * count)
|
|
|
|
.ToList();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
public List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
return context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
public SubscriptionEntity GetById(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Subscriptions
|
|
|
|
.FirstOrDefault(f => f.Id.Equals(id));
|
|
|
|
return res ??= new SubscriptionEntity();
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
public SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Subscriptions
|
|
|
|
.Where(f => f.DiscordWebHookId.Equals(webhookId))
|
|
|
|
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
|
|
|
|
return res ??= new SubscriptionEntity();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(Guid id)
|
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id));
|
|
|
|
if (res is null)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Subscriptions.Remove(res);
|
|
|
|
context.SaveChanges();
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
2023-03-13 21:58:33 -07:00
|
|
|
|
|
|
|
private IDbConnection OpenConnection(string connectionString)
|
|
|
|
{
|
|
|
|
var conn = new NpgsqlConnection(_connectionString);
|
|
|
|
conn.Open();
|
|
|
|
return conn;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|