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);
}
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
.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<ArticlesEntity> List(int page = 0, int count = 25)
public ArticlesEntity GetById(Guid id)
{
using var conn = OpenConnection(_connectionString);
var res = conn.Query<ArticlesEntity>(@"select * from Articles
Order By PubDate Desc
Offset @Page
Fetch Next @Count Rows Only", new
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)
{
Page = page * count,
Count = count
})
.ToList();
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<ArticlesEntity> ListBySourceId(Guid id, int page, int count)
{
using var conn = OpenConnection(_connectionString);
var res = conn.Query<ArticlesModel>("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<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();
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)

View File

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