79 lines
2.5 KiB
C#
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);
|
||
|
}
|
||
|
|
||
|
}
|