2023-02-19 21:39:03 -08:00
|
|
|
using System.Data;
|
|
|
|
using Dapper;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-06-23 23:01:40 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-19 21:39:03 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Database.Repositories;
|
|
|
|
|
|
|
|
public class SourcesTable : ISourcesRepository
|
|
|
|
{
|
2023-03-31 22:49:39 -07:00
|
|
|
private readonly string _connectionString;
|
2023-02-19 21:39:03 -08:00
|
|
|
|
|
|
|
public SourcesTable(string connectionString)
|
|
|
|
{
|
|
|
|
_connectionString = connectionString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SourcesTable(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
var connstr = configuration.GetConnectionString("database");
|
2023-03-31 22:49:39 -07:00
|
|
|
if (connstr is null) connstr = "";
|
2023-02-19 21:39:03 -08:00
|
|
|
_connectionString = connstr;
|
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public SourceEntity New(SourceEntity model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
model.Id = Guid.NewGuid();
|
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
context.Sources.Add(model);
|
|
|
|
try
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
context.SaveChanges();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2023-06-25 20:59:53 -07:00
|
|
|
Console.WriteLine($"Failed to save source to db. {model.ToString()} {ex.Message} ");
|
2023-06-23 23:01:40 -07:00
|
|
|
}
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public SourceEntity GetById(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources.FirstOrDefault(f => f.Id.Equals(id));
|
|
|
|
res ??= new SourceEntity();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public SourceEntity GetById(string id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
var uid = Guid.Parse(id);
|
|
|
|
return GetById(uid);
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public SourceEntity GetByName(string name)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources.FirstOrDefault(f => f.Name.Equals(name));
|
|
|
|
res ??= new SourceEntity();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public SourceEntity GetByNameAndType(string name, string type)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources
|
|
|
|
.Where(f => f.Name.Equals(name))
|
|
|
|
.FirstOrDefault(f => f.Type.Equals(type));
|
|
|
|
res ??= new SourceEntity();
|
|
|
|
return res;
|
2023-03-31 22:49:39 -07:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public SourceEntity GetByUrl(string url)
|
2023-03-31 22:49:39 -07:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources
|
|
|
|
.FirstOrDefault(f => f.Url.Equals(url));
|
|
|
|
res ??= new SourceEntity();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public List<SourceEntity> List(int page = 0, int count = 100)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources
|
|
|
|
.Skip(page * count)
|
|
|
|
.Take(count)
|
|
|
|
.ToList();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public List<SourceEntity> ListBySource(string source, int page = 0, int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources
|
|
|
|
.Where(f => f.Source.Equals(source))
|
|
|
|
.Skip(page * limit)
|
|
|
|
.Take(limit)
|
|
|
|
.ToList();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 23:01:40 -07:00
|
|
|
public List<SourceEntity> ListByType(string type,int page = 0, int limit = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = context.Sources
|
|
|
|
.Where(f => f.Type.Equals(type))
|
|
|
|
.Skip(page * limit)
|
|
|
|
.Take(limit)
|
|
|
|
.ToList();
|
|
|
|
return res;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
2023-03-31 22:49:39 -07:00
|
|
|
|
|
|
|
public int Disable(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = GetById(id);
|
|
|
|
res.Enabled = false;
|
|
|
|
context.Sources.Update(res);
|
|
|
|
try
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
context.SaveChanges();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
2023-03-31 22:49:39 -07:00
|
|
|
|
|
|
|
public int Enable(Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = GetById(id);
|
|
|
|
res.Enabled = true;
|
|
|
|
context.Sources.Update(res);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
context.SaveChanges();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
catch
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2023-03-31 22:49:39 -07:00
|
|
|
}
|
|
|
|
|
2023-04-10 22:59:13 -07:00
|
|
|
public void Delete(Guid id)
|
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = GetById(id);
|
|
|
|
context.Sources.Remove(res);
|
|
|
|
context.SaveChanges();
|
2023-04-10 22:59:13 -07:00
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
public int UpdateYoutubeId(Guid id, string youtubeId)
|
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
using var context = new DatabaseContext(_connectionString);
|
|
|
|
var res = GetById(id);
|
|
|
|
res.YoutubeId = youtubeId;
|
|
|
|
context.Sources.Update(res);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
context.SaveChanges();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
catch
|
2023-03-31 22:49:39 -07:00
|
|
|
{
|
2023-06-23 23:01:40 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
private IDbConnection OpenConnection(string connectionString)
|
|
|
|
{
|
|
|
|
var conn = new NpgsqlConnection(_connectionString);
|
|
|
|
conn.Open();
|
|
|
|
return conn;
|
|
|
|
}
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|