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 32 additions and 36 deletions
Showing only changes of commit a6ea89be62 - Show all commits

View File

@ -1,5 +1,6 @@
using System.Data; using System.Data;
using Dapper; using Dapper;
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;
@ -22,33 +23,28 @@ public class DiscordQueueTable : IDiscordQueueRepository
return conn; return conn;
} }
public void New(DiscordQueueModel model) public void New(DiscordQueueEntity model)
{ {
using var conn = OpenConnection(_connectionString); model.Id = new Guid();
var query = "Insert into DiscordQueue(ID, ArticleId) Values (@id, @articleid);";
conn.Execute(query, new using var context = new DatabaseContext(_connectionString);
{ var res = context.DiscordQueue.Add(model);
id = Guid.NewGuid(), context.SaveChanges();
articleid = model.ArticleID
});
} }
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 DiscordQueue Where ID = @id;"; var res = context.DiscordQueue.FirstOrDefault(d => d.Id.Equals(id));
conn.Execute(query, new res ??= new DiscordQueueEntity();
{ context.DiscordQueue.Remove(res);
id = id context.SaveChanges();
});
} }
public List<DiscordQueueModel> List(int limit = 25) public List<DiscordQueueEntity> List(int limit = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Select * from DiscordQueue LIMIT @limit;"; var res = context.DiscordQueue.Take(limit).ToList();
return conn.Query<DiscordQueueModel>(query, new { return res;
limit = limit
}).ToList();
} }
} }

View File

@ -1,10 +1,10 @@
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Entities;
namespace Newsbot.Collector.Domain.Interfaces; namespace Newsbot.Collector.Domain.Interfaces;
public interface IDiscordQueueRepository public interface IDiscordQueueRepository
{ {
void New(DiscordQueueModel model); void New(DiscordQueueEntity model);
void Delete(Guid id); void Delete(Guid id);
List<DiscordQueueModel> List(int limit); List<DiscordQueueEntity> List(int limit);
} }

View File

@ -75,9 +75,9 @@ public class CodeProjectWatcherJob
foreach (var item in items) foreach (var item in items)
{ {
_articles.New(item); _articles.New(item);
_queue.New(new DiscordQueueModel _queue.New(new DiscordQueueEntity()
{ {
ArticleID = item.Id ArticleId = item.Id
}); });
} }
} }

View File

@ -84,11 +84,11 @@ public class DiscordNotificationJob
_logger.Information($"{JobName} - Loop has been completed."); _logger.Information($"{JobName} - Loop has been completed.");
} }
public void ProcessQueueItem(DiscordQueueModel request) public void ProcessQueueItem(DiscordQueueEntity request)
{ {
_logger.Debug($"{JobName} - Processing {request.ID}"); _logger.Debug($"{JobName} - Processing {request.Id}");
// Get all details on the article in the queue // Get all details on the article in the queue
var articleDetails = _article.GetById(request.ArticleID); var articleDetails = _article.GetById(request.ArticleId);
// Get the details of the source // Get the details of the source
var sourceDetails = _sources.GetByID(articleDetails.SourceId); var sourceDetails = _sources.GetByID(articleDetails.SourceId);
@ -96,7 +96,7 @@ public class DiscordNotificationJob
{ {
_logger.Error( _logger.Error(
$"{JobName} - Article ({articleDetails.Id}) was linked to a empty Source ID. Removing from the queue."); $"{JobName} - Article ({articleDetails.Id}) was linked to a empty Source ID. Removing from the queue.");
_queue.Delete(request.ID); _queue.Delete(request.Id);
return; return;
} }
@ -115,10 +115,10 @@ public class DiscordNotificationJob
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);
_logger.Debug("{JobName} - Removing {RequestId} from the queue", JobName, request.ID); _logger.Debug("{JobName} - Removing {RequestId} from the queue", JobName, request.Id);
_queue.Delete(request.ID); _queue.Delete(request.Id);
} }
public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceModel sourceDetails, public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceModel sourceDetails,

View File

@ -131,9 +131,9 @@ public class RssWatcherJob
} }
var p = _articles.New(item); var p = _articles.New(item);
_queue.New(new DiscordQueueModel _queue.New(new DiscordQueueEntity
{ {
ArticleID = p.Id ArticleId = p.Id
}); });
} }
} }

View File

@ -83,9 +83,9 @@ public class YoutubeWatcherJob
{ {
_logger.Debug($"{JobName} - {video.AuthorName} '{video.Title}' was found"); _logger.Debug($"{JobName} - {video.AuthorName} '{video.Title}' was found");
_articles.New(video); _articles.New(video);
_queue.New(new DiscordQueueModel _queue.New(new DiscordQueueEntity
{ {
ArticleID = video.Id ArticleId = video.Id
}); });
} }
} }