From 4f46247d0cf534daa0ac0d757407698555c8b08a Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Fri, 23 Jun 2023 20:09:15 -0700 Subject: [PATCH] articles table was updated for the entity --- .../Repositories/ArticlesTable.cs | 83 ++++++++----------- .../Interfaces/IArticlesRepository.cs | 6 +- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/Newsbot.Collector.Database/Repositories/ArticlesTable.cs b/Newsbot.Collector.Database/Repositories/ArticlesTable.cs index de7c117..d75ecf5 100644 --- a/Newsbot.Collector.Database/Repositories/ArticlesTable.cs +++ b/Newsbot.Collector.Database/Repositories/ArticlesTable.cs @@ -30,61 +30,41 @@ public class ArticlesTable : IArticlesRepository _context = new DatabaseContext(conn); } - public async Task> ListAsync(int page = 0, int count = 25) + public List List(int page = 0, int count = 25) { - await using var context = new DatabaseContext(_connectionString); + using var context = new DatabaseContext(_connectionString); var query = context.Articles + .Skip(page * count) .OrderBy(d => d.PubDate) .Take(25); Console.WriteLine(query.ToQueryString()); - await query.ToListAsync(); - return await query.ToListAsync(); + return query.ToList(); } - - public List List(int page = 0, int count = 25) + + public ArticlesEntity GetById(Guid id) { - using var conn = OpenConnection(_connectionString); - var res = conn.Query(@"select * from Articles - Order By PubDate Desc - Offset @Page - Fetch Next @Count Rows Only", new - { - Page = page * count, - Count = count - }) - .ToList(); + using var context = new DatabaseContext(_connectionString); + var query = context.Articles + .FirstOrDefault(d => d.Id.Equals(id)); + query ??= new ArticlesEntity(); + return query; + } + + public ArticlesEntity GetByUrl(string url) + { + using var context = new DatabaseContext(_connectionString); + var res = context.Articles.FirstOrDefault(d => d.Url!.Equals(url)); + res ??= new ArticlesEntity(); return res; } - public ArticlesModel GetById(Guid ID) + public List ListBySourceId(Guid id, int page, int count) { - using var conn = OpenConnection(_connectionString); - var res = conn.Query("select * from articles where ID = @ID", new { ID }); - if (res.Count() == 0) return new ArticlesModel(); - return res.First(); - } - - public ArticlesModel GetByUrl(string url) - { - using var conn = OpenConnection(_connectionString); - var res = conn.Query("select * from articles where Url = @Url Limit 1", new { Url = url }); - if (res.Count() == 0) return new ArticlesModel(); - return res.First(); - } - - public List ListBySourceId(Guid id, int page, int count) - { - using var conn = OpenConnection(_connectionString); - var query = @"Select * from articles - where sourceid = @sourceid - Offset @page - Fetch next @count rows only"; - return conn.Query(query, new - { - sourceid = id, - page = page * count, - count - }).ToList(); + using var context = new DatabaseContext(_connectionString); + var res = context.Articles + .Skip(page * count) + .Where(d => d.SourceId.Equals(id)); + return res.ToList(); } public ArticlesEntity New(ArticlesEntity model) @@ -126,12 +106,17 @@ public class ArticlesTable : IArticlesRepository public void DeleteAllBySourceId(Guid sourceId) { - using var conn = OpenConnection(_connectionString); - var res = conn.Execute("Delete from articles where sourceid = '@id'", new + using var context = new DatabaseContext(_connectionString); + var res = context.Articles + .Where(d => d.SourceId.Equals(sourceId)) + .ToList(); + + foreach (var item in res) { - sourceId - }); - if (res == 0) throw new Exception($"No records where deleted that linked to SourceId = '{sourceId}'"); + context.Articles.Remove(item); + } + + context.SaveChanges(); } private IDbConnection OpenConnection(string connectionString) diff --git a/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs b/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs index d5b619f..4cabce7 100644 --- a/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs +++ b/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs @@ -6,9 +6,9 @@ namespace Newsbot.Collector.Domain.Interfaces; public interface IArticlesRepository : ITableRepository { List List(int page, int count); - List ListBySourceId(Guid id, int page = 0, int count = 25); - ArticlesModel GetById(Guid ID); - ArticlesModel GetByUrl(string url); + List ListBySourceId(Guid id, int page = 0, int count = 25); + ArticlesEntity GetById(Guid id); + ArticlesEntity GetByUrl(string url); ArticlesEntity New(ArticlesEntity model); void DeleteAllBySourceId(Guid sourceId); } \ No newline at end of file