using System.Data; using Dapper; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Models; using Npgsql; namespace Newsbot.Collector.Database.Repositories; public class ArticlesTable : IArticlesRepository { private readonly string _connectionString; private DatabaseContext _context; public ArticlesTable(string connectionString) { _connectionString = connectionString; _context = new DatabaseContext(connectionString); } public ArticlesTable(IConfiguration configuration) { var conn = configuration.GetConnectionString("database"); if (conn is null) conn = ""; _connectionString = conn; _context = new DatabaseContext(conn); } public async Task> 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 List(int page = 0, int count = 25) { using var conn = OpenConnection(_connectionString); var res = conn.Query(@"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("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("select * from articles where Url = @Url Limit 1", new { Url = url }); if (res.Count() == 0) return new ArticlesModel(); return res.First(); } public List 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(query, new { sourceid = id, page = page * count, count }).ToList(); } 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(); using var conn = OpenConnection(_connectionString); var q = "INSERT INTO Articles (id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage, codeiscommit, codeisrelease) Values (@id, @sourceid, @tags, @title, @url, @pubdate, @video, @videoheight, @videowidth, @thumbnail, @description, @authorname, @authorimage, @codeiscommit, @codeisrelease);"; var res = conn.Execute(q, new { id = model.ID, 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, codeiscommit = model.CodeIsCommit, codeisrelease = model.CodeIsRelease }); return model; } public void DeleteAllBySourceId(Guid sourceId) { using var conn = OpenConnection(_connectionString); var res = conn.Execute("Delete from articles where sourceid = '@id'", new { sourceId }); if (res == 0) throw new Exception($"No records where deleted that linked to SourceId = '{sourceId}'"); } private IDbConnection OpenConnection(string connectionString) { var conn = new NpgsqlConnection(_connectionString); conn.Open(); return conn; } }