From 49342a8c39c0ad2756fcff3fc263222f8b94bfd8 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 25 Jun 2023 21:02:26 -0700 Subject: [PATCH] subscriptions updated for ef --- .../Repositories/SubscriptionsTable.cs | 115 ++++++------------ .../Dto/SubscriptionDetailsDto.cs | 2 +- .../Dto/SubscriptionDto.cs | 3 +- .../Interfaces/ISubscriptionsRepository.cs | 13 +- .../Jobs/DiscordNotificationJob.cs | 4 +- .../Jobs/DiscordNotificationJobTest.cs | 4 +- 6 files changed, 52 insertions(+), 89 deletions(-) diff --git a/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs b/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs index 61b30a6..a4c13ad 100644 --- a/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs +++ b/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs @@ -1,6 +1,7 @@ using System.Data; using Dapper; using Microsoft.Extensions.Configuration; +using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Models; using Npgsql; @@ -23,104 +24,64 @@ public class SubscriptionsTable : ISubscriptionRepository _connectionString = connstr; } - public SubscriptionModel New(SubscriptionModel model) + public SubscriptionEntity New(SubscriptionEntity model) { - model.Id = Guid.NewGuid(); - using var conn = OpenConnection(_connectionString); - var query = - "Insert Into subscriptions (ID, DiscordWebHookId, SourceId, codeAllowCommits, codeAllowReleases) Values (@id, @webhookId, @sourceId, @codeAllowCommits, @codeAllowReleases);"; - try - { - 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(); - } + model.Id = new Guid(); + using var context = new DatabaseContext(_connectionString); + context.Subscriptions.Add(model); + context.SaveChanges(); + return model; } - public List List(int page = 0, int count = 25) + public List List(int page = 0, int count = 25) { - using var conn = OpenConnection(_connectionString); - var query = @"Select * From subscriptions - Offset @page Fetch Next @count Rows Only;"; - return conn.Query(query, new - { - page = page * count, count - }).ToList(); + using var context = new DatabaseContext(_connectionString); + return context.Subscriptions.Skip(page * count).Take(count).ToList(); } - public List ListBySourceID(Guid id, int page = 0, int count = 25) + public List ListBySourceId(Guid id, int page = 0, int count = 25) { - using var conn = OpenConnection(_connectionString); - var query = @"Select * From subscriptions - Where sourceid = @sourceid - Offset @page Fetch Next @count Rows Only"; - return conn.Query(query, new - { - page = page * count, - count, - sourceid = id - }).ToList(); + using var context = new DatabaseContext(_connectionString); + return context.Subscriptions.Where(f => f.SourceId.Equals(id)) + .Skip(page * count) + .ToList(); } - public List ListByWebhook(Guid id, int page = 0, int count = 25) + public List ListByWebhook(Guid id, int page = 0, int count = 25) { - using var conn = OpenConnection(_connectionString); - var query = @"Select * From subscriptions - Where discordwebhookid = @webhookid - Offset @page Fetch Next @count Rows Only"; - return conn.Query(query, new - { - page = page * count, - count, - webhookid = id - }).ToList(); + using var context = new DatabaseContext(_connectionString); + return context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList(); } - public SubscriptionModel GetById(Guid id) + public SubscriptionEntity GetById(Guid id) { - using var conn = OpenConnection(_connectionString); - var query = @"Select * From subscriptions Where id = @id;"; - var res = conn.Query(query, new - { - id - }); - if (res.Count() == 0) return new SubscriptionModel(); - return res.First(); + using var context = new DatabaseContext(_connectionString); + var res = context.Subscriptions + .FirstOrDefault(f => f.Id.Equals(id)); + return res ??= new SubscriptionEntity(); } - public SubscriptionModel GetByWebhookAndSource(Guid webhookId, Guid sourceId) + public SubscriptionEntity 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(query, new - { - webhookid = webhookId, - sourceid = sourceId - }); - if (res.Count() == 0) return new SubscriptionModel(); - return res.First(); + 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(); } public void Delete(Guid id) { - using var conn = OpenConnection(_connectionString); - var query = "Delete From subscriptions Where id = @id;"; - conn.Execute(query, new + using var context = new DatabaseContext(_connectionString); + var res = context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id)); + if (res is null) { - id - }); + return; + } + + context.Subscriptions.Remove(res); + context.SaveChanges(); + } private IDbConnection OpenConnection(string connectionString) diff --git a/Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs b/Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs index a73c9cd..90ae0af 100644 --- a/Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs +++ b/Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs @@ -11,7 +11,7 @@ public class SubscriptionDetailsDto public SourceDto? Source { 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) { return new SubscriptionDetailsDto diff --git a/Newsbot.Collector.Domain/Dto/SubscriptionDto.cs b/Newsbot.Collector.Domain/Dto/SubscriptionDto.cs index 1b6f1c2..56b7efb 100644 --- a/Newsbot.Collector.Domain/Dto/SubscriptionDto.cs +++ b/Newsbot.Collector.Domain/Dto/SubscriptionDto.cs @@ -1,3 +1,4 @@ +using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Models; namespace Newsbot.Collector.Domain.Dto; @@ -10,7 +11,7 @@ public class SubscriptionDto public bool CodeAllowReleases { get; set; } public bool CodeAllowCommits { get; set; } - public static SubscriptionDto Convert(SubscriptionModel model) + public static SubscriptionDto Convert(SubscriptionEntity model) { return new SubscriptionDto { diff --git a/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs b/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs index ecfe54b..c955bbf 100644 --- a/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs +++ b/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs @@ -1,17 +1,18 @@ +using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Models; namespace Newsbot.Collector.Domain.Interfaces; public interface ISubscriptionRepository { - SubscriptionModel New(SubscriptionModel model); + SubscriptionEntity New(SubscriptionEntity model); - List List(int page = 0, int count = 25); - List ListBySourceID(Guid id, int page = 0, int count = 25); - List ListByWebhook(Guid id, int page = 0, int count = 25); + List List(int page = 0, int count = 25); + List ListBySourceId(Guid id, int page = 0, int count = 25); + List ListByWebhook(Guid id, int page = 0, int count = 25); - SubscriptionModel GetById(Guid id); - SubscriptionModel GetByWebhookAndSource(Guid webhookId, Guid sourceId); + SubscriptionEntity GetById(Guid id); + SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId); void Delete(Guid id); } \ No newline at end of file diff --git a/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs b/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs index be60738..6ae432e 100644 --- a/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs +++ b/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs @@ -112,7 +112,7 @@ public class DiscordNotificationJob } // Find all the subscriptions for that source - var allSubscriptions = _subs.ListBySourceID(sourceDetails.Id); + var allSubscriptions = _subs.ListBySourceId(sourceDetails.Id); foreach (var sub in allSubscriptions) SendSubscriptionNotification(request.Id, articleDetails, sourceDetails, sourceIcon, sub); @@ -121,7 +121,7 @@ public class DiscordNotificationJob _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 // If the article is a code commit and the subscription does not want them, skip. diff --git a/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs b/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs index 46192b2..a3b3c71 100644 --- a/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs +++ b/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs @@ -1,5 +1,4 @@ using Newsbot.Collector.Domain.Entities; -using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Services.Jobs; using Newsbot.Collector.Services.Notifications.Discord; @@ -79,7 +78,7 @@ public class DiscordNotificationJobTest Id = Guid.NewGuid(), FileName = "https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png" }, - new SubscriptionModel + new SubscriptionEntity { CodeAllowCommits = false, CodeAllowReleases = true @@ -88,6 +87,7 @@ public class DiscordNotificationJobTest } catch (MessageTypeNotRequestedException) { + Console.Write($"Message did not send as expected"); }