features/cutover-to-ef #9

Merged
jtom38 merged 27 commits from features/cutover-to-ef into main 2023-06-25 21:15:58 -07:00
6 changed files with 52 additions and 89 deletions
Showing only changes of commit 49342a8c39 - Show all commits

View File

@ -1,6 +1,7 @@
using System.Data; using System.Data;
using Dapper; using Dapper;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Models;
using Npgsql; using Npgsql;
@ -23,104 +24,64 @@ public class SubscriptionsTable : ISubscriptionRepository
_connectionString = connstr; _connectionString = connstr;
} }
public SubscriptionModel New(SubscriptionModel model) public SubscriptionEntity New(SubscriptionEntity model)
{ {
model.Id = Guid.NewGuid(); model.Id = new Guid();
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = context.Subscriptions.Add(model);
"Insert Into subscriptions (ID, DiscordWebHookId, SourceId, codeAllowCommits, codeAllowReleases) Values (@id, @webhookId, @sourceId, @codeAllowCommits, @codeAllowReleases);"; context.SaveChanges();
try return model;
{
conn.Execute(query, new
{
id = model.Id,
webhookId = model.DiscordWebHookId,
sourceId = model.SourceId,
codeAllowCommits = model.CodeAllowCommits,
codeAllowReleases = model.CodeAllowReleases
});
return model;
}
catch
{
return new SubscriptionModel();
}
} }
public List<SubscriptionModel> List(int page = 0, int count = 25) public List<SubscriptionEntity> List(int page = 0, int count = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = @"Select * From subscriptions return context.Subscriptions.Skip(page * count).Take(count).ToList();
Offset @page Fetch Next @count Rows Only;";
return conn.Query<SubscriptionModel>(query, new
{
page = page * count, count
}).ToList();
} }
public List<SubscriptionModel> ListBySourceID(Guid id, int page = 0, int count = 25) public List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = @"Select * From subscriptions return context.Subscriptions.Where(f => f.SourceId.Equals(id))
Where sourceid = @sourceid .Skip(page * count)
Offset @page Fetch Next @count Rows Only"; .ToList();
return conn.Query<SubscriptionModel>(query, new
{
page = page * count,
count,
sourceid = id
}).ToList();
} }
public List<SubscriptionModel> ListByWebhook(Guid id, int page = 0, int count = 25) public List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = @"Select * From subscriptions return context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
Where discordwebhookid = @webhookid
Offset @page Fetch Next @count Rows Only";
return conn.Query<SubscriptionModel>(query, new
{
page = page * count,
count,
webhookid = id
}).ToList();
} }
public SubscriptionModel GetById(Guid id) public SubscriptionEntity GetById(Guid id)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = @"Select * From subscriptions Where id = @id;"; var res = context.Subscriptions
var res = conn.Query<SubscriptionModel>(query, new .FirstOrDefault(f => f.Id.Equals(id));
{ return res ??= new SubscriptionEntity();
id
});
if (res.Count() == 0) return new SubscriptionModel();
return res.First();
} }
public SubscriptionModel GetByWebhookAndSource(Guid webhookId, Guid sourceId) public SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = @"Select * From subscriptions var res = context.Subscriptions
Where discordwebhookid = @webhookid .Where(f => f.DiscordWebHookId.Equals(webhookId))
and sourceid = @sourceid;"; .FirstOrDefault(f => f.SourceId.Equals(sourceId));
var res = conn.Query<SubscriptionModel>(query, new return res ??= new SubscriptionEntity();
{
webhookid = webhookId,
sourceid = sourceId
});
if (res.Count() == 0) return new SubscriptionModel();
return res.First();
} }
public void Delete(Guid id) public void Delete(Guid id)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Delete From subscriptions Where id = @id;"; var res = context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id));
conn.Execute(query, new if (res is null)
{ {
id return;
}); }
context.Subscriptions.Remove(res);
context.SaveChanges();
} }
private IDbConnection OpenConnection(string connectionString) private IDbConnection OpenConnection(string connectionString)

View File

@ -11,7 +11,7 @@ public class SubscriptionDetailsDto
public SourceDto? Source { get; set; } public SourceDto? Source { get; set; }
public DiscordWebHookDto? DiscordWebHook { get; set; } public DiscordWebHookDto? DiscordWebHook { get; set; }
public static SubscriptionDetailsDto Convert(SubscriptionModel subscription, SourceEntity source, public static SubscriptionDetailsDto Convert(SubscriptionEntity subscription, SourceEntity source,
DiscordWebhookEntity discord) DiscordWebhookEntity discord)
{ {
return new SubscriptionDetailsDto return new SubscriptionDetailsDto

View File

@ -1,3 +1,4 @@
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Dto; namespace Newsbot.Collector.Domain.Dto;
@ -10,7 +11,7 @@ public class SubscriptionDto
public bool CodeAllowReleases { get; set; } public bool CodeAllowReleases { get; set; }
public bool CodeAllowCommits { get; set; } public bool CodeAllowCommits { get; set; }
public static SubscriptionDto Convert(SubscriptionModel model) public static SubscriptionDto Convert(SubscriptionEntity model)
{ {
return new SubscriptionDto return new SubscriptionDto
{ {

View File

@ -1,17 +1,18 @@
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Interfaces; namespace Newsbot.Collector.Domain.Interfaces;
public interface ISubscriptionRepository public interface ISubscriptionRepository
{ {
SubscriptionModel New(SubscriptionModel model); SubscriptionEntity New(SubscriptionEntity model);
List<SubscriptionModel> List(int page = 0, int count = 25); List<SubscriptionEntity> List(int page = 0, int count = 25);
List<SubscriptionModel> ListBySourceID(Guid id, int page = 0, int count = 25); List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25);
List<SubscriptionModel> ListByWebhook(Guid id, int page = 0, int count = 25); List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25);
SubscriptionModel GetById(Guid id); SubscriptionEntity GetById(Guid id);
SubscriptionModel GetByWebhookAndSource(Guid webhookId, Guid sourceId); SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId);
void Delete(Guid id); void Delete(Guid id);
} }

View File

@ -112,7 +112,7 @@ public class DiscordNotificationJob
} }
// Find all the subscriptions for that source // Find all the subscriptions for that source
var allSubscriptions = _subs.ListBySourceID(sourceDetails.Id); var allSubscriptions = _subs.ListBySourceId(sourceDetails.Id);
foreach (var sub in allSubscriptions) foreach (var sub in allSubscriptions)
SendSubscriptionNotification(request.Id, articleDetails, sourceDetails, sourceIcon, sub); SendSubscriptionNotification(request.Id, articleDetails, sourceDetails, sourceIcon, sub);
@ -121,7 +121,7 @@ public class DiscordNotificationJob
_queue.Delete(request.Id); _queue.Delete(request.Id);
} }
public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, SubscriptionModel sub) public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, SubscriptionEntity sub)
{ {
// Check if the subscription code flags // Check if the subscription code flags
// If the article is a code commit and the subscription does not want them, skip. // If the article is a code commit and the subscription does not want them, skip.

View File

@ -1,5 +1,4 @@
using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Models;
using Newsbot.Collector.Services.Jobs; using Newsbot.Collector.Services.Jobs;
using Newsbot.Collector.Services.Notifications.Discord; using Newsbot.Collector.Services.Notifications.Discord;
@ -79,7 +78,7 @@ public class DiscordNotificationJobTest
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
FileName = "https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png" FileName = "https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png"
}, },
new SubscriptionModel new SubscriptionEntity
{ {
CodeAllowCommits = false, CodeAllowCommits = false,
CodeAllowReleases = true CodeAllowReleases = true
@ -88,6 +87,7 @@ public class DiscordNotificationJobTest
} }
catch (MessageTypeNotRequestedException) catch (MessageTypeNotRequestedException)
{ {
Console.Write($"Message did not send as expected"); Console.Write($"Message did not send as expected");
} }