Newsbot.Collector/Newsbot.Collector.Database/Repositories/ArticlesTable.cs
James Tombleson 9f3a6323a6
Features/html meta extractor (#4)
* gave api access to the db project

* added db models

* working on rss extraction and meta extraction

* test project to debug rsswatcherjob

* added new configs for the project

* new interface to define collectors

* basic rss extraction and article details are now exposed

* tests updated for rss pull

* starting to get dapper working.  Query works but insert seems to have a value issue

* removed dapper from services

* added some basic tests for db calls
2023-02-16 22:19:05 -08:00

79 lines
2.5 KiB
C#

using System.Data;
using Dapper;
using Newsbot.Collector.Domain.Models;
using Npgsql;
namespace Newsbot.Collector.Database.Repositories;
public class ArticlesTable
{
private string _connectionString;
public ArticlesTable(string connectionString)
{
_connectionString = connectionString;
}
public static IDbConnection OpenConnection(string connectionString)
{
var cs = "Host=localhost;Username=postgres;Password=postgres;Database=postgres;sslmode=disable";
var conn = new NpgsqlConnection(cs);
conn.Open();
return conn;
}
public List<ArticlesModel> List(int Page = 0, int Count = 25)
{
using var conn = OpenConnection(_connectionString);
var res = conn.Query<ArticlesModel>(@"select * from articles
Order By PubDate Desc
Offset @Page
Fetch Next @Count Rows Only", new { Page = Page * Count, Count = Count }).ToList();
return res;
}
public ArticlesModel GetById(Guid ID)
{
using var conn = OpenConnection(_connectionString);
var res = conn.Query<ArticlesModel>("select * from articles where ID = @ID", new { ID = ID });
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 });
return res.First();
}
public void New(ArticlesModel model)
{
model.ID = Guid.NewGuid();
using var conn = OpenConnection(_connectionString);
var q = @"INSERT INTO Articles
(ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage)
Values
(@Id, @SourceId, @Tags, @Title, @Url, @PubDate, @Video, @VideoHeight, @VideoWidth, @Thumbnail, @Description, @AuthorName, @AuthorImage);
";
var res = conn.Execute(q, model);
//new{
// Id = Guid.NewGuid(),
// SourceId = model.SourceID,
// Tags = model.Tags,
// Title = model.Title,
// Url = model.URL,
// PubDate = model.PubDate,
// Video = model.Video,
// VideoHeight = model.VideoHeight,
// VideoWidth = model.VideoWidth,
// Thumbnail = model.Thumbnail,
// Description = model.Description,
// AuthorName = model.AuthorName,
// AuthorImage = model.AuthorImage
//});
Console.WriteLine(res);
}
}