articles table was updated for the entity

This commit is contained in:
James Tombleson 2023-06-23 20:09:15 -07:00
parent 731b2cf9b7
commit 4f46247d0c
2 changed files with 37 additions and 52 deletions

View File

@ -30,61 +30,41 @@ public class ArticlesTable : IArticlesRepository
_context = new DatabaseContext(conn); _context = new DatabaseContext(conn);
} }
public async Task<List<ArticlesEntity>> ListAsync(int page = 0, int count = 25) public List<ArticlesEntity> List(int page = 0, int count = 25)
{ {
await using var context = new DatabaseContext(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = context.Articles var query = context.Articles
.Skip(page * count)
.OrderBy(d => d.PubDate) .OrderBy(d => d.PubDate)
.Take(25); .Take(25);
Console.WriteLine(query.ToQueryString()); Console.WriteLine(query.ToQueryString());
await query.ToListAsync(); return query.ToList();
return await query.ToListAsync();
} }
public List<ArticlesEntity> List(int page = 0, int count = 25) public ArticlesEntity GetById(Guid id)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var res = conn.Query<ArticlesEntity>(@"select * from Articles var query = context.Articles
Order By PubDate Desc .FirstOrDefault(d => d.Id.Equals(id));
Offset @Page query ??= new ArticlesEntity();
Fetch Next @Count Rows Only", new return query;
{ }
Page = page * count,
Count = count public ArticlesEntity GetByUrl(string url)
}) {
.ToList(); using var context = new DatabaseContext(_connectionString);
var res = context.Articles.FirstOrDefault(d => d.Url!.Equals(url));
res ??= new ArticlesEntity();
return res; return res;
} }
public ArticlesModel GetById(Guid ID) public List<ArticlesEntity> ListBySourceId(Guid id, int page, int count)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var res = conn.Query<ArticlesModel>("select * from articles where ID = @ID", new { ID }); var res = context.Articles
if (res.Count() == 0) return new ArticlesModel(); .Skip(page * count)
return res.First(); .Where(d => d.SourceId.Equals(id));
} return res.ToList();
public ArticlesModel GetByUrl(string url)
{
using var conn = OpenConnection(_connectionString);
var res = conn.Query<ArticlesModel>("select * from articles where Url = @Url Limit 1", new { Url = url });
if (res.Count() == 0) return new ArticlesModel();
return res.First();
}
public List<ArticlesModel> 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<ArticlesModel>(query, new
{
sourceid = id,
page = page * count,
count
}).ToList();
} }
public ArticlesEntity New(ArticlesEntity model) public ArticlesEntity New(ArticlesEntity model)
@ -126,12 +106,17 @@ public class ArticlesTable : IArticlesRepository
public void DeleteAllBySourceId(Guid sourceId) public void DeleteAllBySourceId(Guid sourceId)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var res = conn.Execute("Delete from articles where sourceid = '@id'", new var res = context.Articles
.Where(d => d.SourceId.Equals(sourceId))
.ToList();
foreach (var item in res)
{ {
sourceId context.Articles.Remove(item);
}); }
if (res == 0) throw new Exception($"No records where deleted that linked to SourceId = '{sourceId}'");
context.SaveChanges();
} }
private IDbConnection OpenConnection(string connectionString) private IDbConnection OpenConnection(string connectionString)

View File

@ -6,9 +6,9 @@ namespace Newsbot.Collector.Domain.Interfaces;
public interface IArticlesRepository : ITableRepository public interface IArticlesRepository : ITableRepository
{ {
List<ArticlesEntity> List(int page, int count); List<ArticlesEntity> List(int page, int count);
List<ArticlesModel> ListBySourceId(Guid id, int page = 0, int count = 25); List<ArticlesEntity> ListBySourceId(Guid id, int page = 0, int count = 25);
ArticlesModel GetById(Guid ID); ArticlesEntity GetById(Guid id);
ArticlesModel GetByUrl(string url); ArticlesEntity GetByUrl(string url);
ArticlesEntity New(ArticlesEntity model); ArticlesEntity New(ArticlesEntity model);
void DeleteAllBySourceId(Guid sourceId); void DeleteAllBySourceId(Guid sourceId);
} }