2023-02-19 21:39:03 -08:00
|
|
|
using System.Data;
|
|
|
|
using Dapper;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
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-02-26 09:40:04 -08:00
|
|
|
public SubscriptionModel New(SubscriptionModel model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-04-13 22:13:06 -07:00
|
|
|
model.Id = Guid.NewGuid();
|
2023-02-19 21:39:03 -08:00
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-04-13 22:13:06 -07:00
|
|
|
var query =
|
|
|
|
"Insert Into subscriptions (ID, DiscordWebHookId, SourceId, codeAllowCommits, codeAllowReleases) Values (@id, @webhookId, @sourceId, @codeAllowCommits, @codeAllowReleases);";
|
2023-03-13 21:58:33 -07:00
|
|
|
try
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-03-13 21:58:33 -07:00
|
|
|
conn.Execute(query, new
|
|
|
|
{
|
2023-04-13 22:13:06 -07:00
|
|
|
id = model.Id,
|
|
|
|
webhookId = model.DiscordWebHookId,
|
|
|
|
sourceId = model.SourceId,
|
|
|
|
codeAllowCommits = model.CodeAllowCommits,
|
|
|
|
codeAllowReleases = model.CodeAllowReleases
|
2023-03-13 21:58:33 -07:00
|
|
|
});
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return new SubscriptionModel();
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<SubscriptionModel> List(int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-02-26 09:40:04 -08:00
|
|
|
var query = @"Select * From subscriptions
|
|
|
|
Offset @page Fetch Next @count Rows Only;";
|
2023-02-19 21:39:03 -08:00
|
|
|
return conn.Query<SubscriptionModel>(query, new
|
|
|
|
{
|
2023-03-13 21:58:33 -07:00
|
|
|
page = page * count, count
|
2023-02-19 21:39:03 -08:00
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<SubscriptionModel> ListBySourceID(Guid id, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-02-26 09:40:04 -08:00
|
|
|
var query = @"Select * From subscriptions
|
2023-03-05 22:33:41 -08:00
|
|
|
Where sourceid = @sourceid
|
|
|
|
Offset @page Fetch Next @count Rows Only";
|
2023-02-19 21:39:03 -08:00
|
|
|
return conn.Query<SubscriptionModel>(query, new
|
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
page = page * count,
|
2023-03-13 21:58:33 -07:00
|
|
|
count,
|
2023-02-26 09:40:04 -08:00
|
|
|
sourceid = id
|
2023-02-19 21:39:03 -08:00
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<SubscriptionModel> ListByWebhook(Guid id, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-02-26 09:40:04 -08:00
|
|
|
var query = @"Select * From subscriptions
|
2023-03-05 22:33:41 -08:00
|
|
|
Where discordwebhookid = @webhookid
|
|
|
|
Offset @page Fetch Next @count Rows Only";
|
2023-02-19 21:39:03 -08:00
|
|
|
return conn.Query<SubscriptionModel>(query, new
|
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
page = page * count,
|
2023-03-13 21:58:33 -07:00
|
|
|
count,
|
|
|
|
webhookid = id
|
2023-02-19 21:39:03 -08:00
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public SubscriptionModel GetById(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-02-26 09:40:04 -08:00
|
|
|
var query = @"Select * From subscriptions Where id = @id;";
|
|
|
|
var res = conn.Query<SubscriptionModel>(query, new
|
|
|
|
{
|
2023-03-13 21:58:33 -07:00
|
|
|
id
|
2023-02-26 09:40:04 -08:00
|
|
|
});
|
2023-03-13 21:58:33 -07:00
|
|
|
if (res.Count() == 0) return new SubscriptionModel();
|
2023-02-26 09:40:04 -08:00
|
|
|
return res.First();
|
|
|
|
}
|
|
|
|
|
|
|
|
public SubscriptionModel GetByWebhookAndSource(Guid webhookId, Guid sourceId)
|
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = @"Select * From subscriptions
|
|
|
|
Where discordwebhookid = @webhookid
|
|
|
|
and sourceid = @sourceid;";
|
|
|
|
var res = conn.Query<SubscriptionModel>(query, new
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
webhookid = webhookId,
|
2023-03-13 21:58:33 -07:00
|
|
|
sourceid = sourceId
|
2023-02-26 09:40:04 -08:00
|
|
|
});
|
2023-03-13 21:58:33 -07:00
|
|
|
if (res.Count() == 0) return new SubscriptionModel();
|
2023-02-26 09:40:04 -08:00
|
|
|
return res.First();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(Guid id)
|
|
|
|
{
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
|
|
|
var query = "Delete From subscriptions Where id = @id;";
|
2023-02-26 09:40:04 -08:00
|
|
|
conn.Execute(query, new
|
|
|
|
{
|
2023-03-13 21:58:33 -07:00
|
|
|
id
|
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
|
|
|
}
|