2023-02-16 22:19:05 -08:00
|
|
|
using System.Data;
|
|
|
|
using Dapper;
|
2023-06-22 08:09:18 -07:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-02-19 21:39:03 -08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-06-22 08:09:18 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-19 21:39:03 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
2023-02-16 22:19:05 -08:00
|
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Database.Repositories;
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public class ArticlesTable : IArticlesRepository
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-04-10 22:59:13 -07:00
|
|
|
private readonly string _connectionString;
|
2023-02-16 22:19:05 -08:00
|
|
|
|
2023-06-22 08:09:18 -07:00
|
|
|
private DatabaseContext _context;
|
|
|
|
|
2023-02-16 22:19:05 -08:00
|
|
|
public ArticlesTable(string connectionString)
|
|
|
|
{
|
|
|
|
_connectionString = connectionString;
|
2023-06-22 08:09:18 -07:00
|
|
|
_context = new DatabaseContext(connectionString);
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public ArticlesTable(IConfiguration configuration)
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-02-19 21:39:03 -08:00
|
|
|
var conn = configuration.GetConnectionString("database");
|
2023-04-10 22:59:13 -07:00
|
|
|
if (conn is null) conn = "";
|
2023-02-19 21:39:03 -08:00
|
|
|
|
|
|
|
_connectionString = conn;
|
2023-06-22 08:09:18 -07:00
|
|
|
_context = new DatabaseContext(conn);
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 20:09:15 -07:00
|
|
|
public List<ArticlesEntity> List(int page = 0, int count = 25)
|
2023-06-22 08:09:18 -07:00
|
|
|
{
|
2023-06-23 20:09:15 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
2023-06-22 08:09:18 -07:00
|
|
|
var query = context.Articles
|
2023-06-23 20:09:15 -07:00
|
|
|
.Skip(page * count)
|
2023-06-22 08:09:18 -07:00
|
|
|
.OrderBy(d => d.PubDate)
|
|
|
|
.Take(25);
|
|
|
|
Console.WriteLine(query.ToQueryString());
|
2023-06-23 20:09:15 -07:00
|
|
|
return query.ToList();
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 20:09:15 -07:00
|
|
|
public ArticlesEntity GetById(Guid id)
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-06-23 20:09:15 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var query = context.Articles
|
|
|
|
.FirstOrDefault(d => d.Id.Equals(id));
|
|
|
|
query ??= new ArticlesEntity();
|
|
|
|
return query;
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 20:09:15 -07:00
|
|
|
public ArticlesEntity GetByUrl(string url)
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-06-23 20:09:15 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Articles.FirstOrDefault(d => d.Url!.Equals(url));
|
|
|
|
res ??= new ArticlesEntity();
|
|
|
|
return res;
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 20:09:15 -07:00
|
|
|
public List<ArticlesEntity> ListBySourceId(Guid id, int page, int count)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-06-23 20:09:15 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Articles
|
|
|
|
.Skip(page * count)
|
|
|
|
.Where(d => d.SourceId.Equals(id));
|
|
|
|
return res.ToList();
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
2023-06-22 08:09:18 -07:00
|
|
|
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)
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
|
|
|
model.ID = Guid.NewGuid();
|
|
|
|
|
|
|
|
using var conn = OpenConnection(_connectionString);
|
2023-04-10 22:59:13 -07:00
|
|
|
var q =
|
2023-04-13 22:13:06 -07:00
|
|
|
"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);";
|
2023-02-19 21:39:03 -08:00
|
|
|
var res = conn.Execute(q, new
|
|
|
|
{
|
2023-03-05 22:33:41 -08:00
|
|
|
id = model.ID,
|
2023-02-19 21:39:03 -08:00
|
|
|
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,
|
2023-04-13 22:13:06 -07:00
|
|
|
authorimage = model.AuthorImage,
|
|
|
|
codeiscommit = model.CodeIsCommit,
|
|
|
|
codeisrelease = model.CodeIsRelease
|
2023-02-19 21:39:03 -08:00
|
|
|
});
|
|
|
|
return model;
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-04-10 22:59:13 -07:00
|
|
|
public void DeleteAllBySourceId(Guid sourceId)
|
|
|
|
{
|
2023-06-23 20:09:15 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Articles
|
|
|
|
.Where(d => d.SourceId.Equals(sourceId))
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
foreach (var item in res)
|
2023-04-10 22:59:13 -07:00
|
|
|
{
|
2023-06-23 20:09:15 -07:00
|
|
|
context.Articles.Remove(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.SaveChanges();
|
2023-04-10 22:59:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private IDbConnection OpenConnection(string connectionString)
|
|
|
|
{
|
|
|
|
var conn = new NpgsqlConnection(_connectionString);
|
|
|
|
conn.Open();
|
|
|
|
return conn;
|
|
|
|
}
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|