moving some commands over to ef

This commit is contained in:
James Tombleson 2023-06-22 08:09:18 -07:00
parent a343fa2d7f
commit 731b2cf9b7

View File

@ -1,6 +1,8 @@
using System.Data; using System.Data;
using Dapper; using Dapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
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;
@ -11,9 +13,12 @@ public class ArticlesTable : IArticlesRepository
{ {
private readonly string _connectionString; private readonly string _connectionString;
private DatabaseContext _context;
public ArticlesTable(string connectionString) public ArticlesTable(string connectionString)
{ {
_connectionString = connectionString; _connectionString = connectionString;
_context = new DatabaseContext(connectionString);
} }
public ArticlesTable(IConfiguration configuration) public ArticlesTable(IConfiguration configuration)
@ -22,12 +27,24 @@ public class ArticlesTable : IArticlesRepository
if (conn is null) conn = ""; if (conn is null) conn = "";
_connectionString = conn; _connectionString = conn;
_context = new DatabaseContext(conn);
} }
public List<ArticlesModel> List(int page = 0, int count = 25) public async Task<List<ArticlesEntity>> ListAsync(int page = 0, int count = 25)
{
await using var context = new DatabaseContext(_connectionString);
var query = context.Articles
.OrderBy(d => d.PubDate)
.Take(25);
Console.WriteLine(query.ToQueryString());
await query.ToListAsync();
return await query.ToListAsync();
}
public List<ArticlesEntity> List(int page = 0, int count = 25)
{ {
using var conn = OpenConnection(_connectionString); using var conn = OpenConnection(_connectionString);
var res = conn.Query<ArticlesModel>(@"select * from articles var res = conn.Query<ArticlesEntity>(@"select * from Articles
Order By PubDate Desc Order By PubDate Desc
Offset @Page Offset @Page
Fetch Next @Count Rows Only", new Fetch Next @Count Rows Only", new
@ -70,7 +87,16 @@ public class ArticlesTable : IArticlesRepository
}).ToList(); }).ToList();
} }
public ArticlesModel New(ArticlesModel model) public ArticlesEntity New(ArticlesEntity model)
{
using var context = new DatabaseContext(_connectionString);
model.Id = new Guid();
var query = context.Articles.Add(model);
context.SaveChanges();
return model;
}
public ArticlesModel NewDapper(ArticlesModel model)
{ {
model.ID = Guid.NewGuid(); model.ID = Guid.NewGuid();