Newsbot.Collector/Newsbot.Collector.Database/Repositories/SourcesTable.cs

182 lines
4.7 KiB
C#
Raw Normal View History

using System.Data;
using Dapper;
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 SourcesTable : ISourcesRepository
{
private readonly string _connectionString;
public SourcesTable(string connectionString)
{
_connectionString = connectionString;
}
public SourcesTable(IConfiguration configuration)
{
var connstr = configuration.GetConnectionString("database");
if (connstr is null) connstr = "";
_connectionString = connstr;
}
public SourceEntity New(SourceEntity model)
{
model.Id = Guid.NewGuid();
using var context = new DatabaseContext(_connectionString);
context.Sources.Add(model);
try
{
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} ");
}
return model;
}
public SourceEntity GetById(Guid id)
{
using var context = new DatabaseContext(_connectionString);
var res = context.Sources.FirstOrDefault(f => f.Id.Equals(id));
res ??= new SourceEntity();
return res;
}
public SourceEntity GetById(string id)
{
var uid = Guid.Parse(id);
return GetById(uid);
}
public SourceEntity GetByName(string name)
{
using var context = new DatabaseContext(_connectionString);
var res = context.Sources.FirstOrDefault(f => f.Name.Equals(name));
res ??= new SourceEntity();
return res;
}
public SourceEntity GetByNameAndType(string name, string type)
{
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;
}
public SourceEntity GetByUrl(string url)
{
using var context = new DatabaseContext(_connectionString);
var res = context.Sources
.FirstOrDefault(f => f.Url.Equals(url));
res ??= new SourceEntity();
return res;
}
public List<SourceEntity> List(int page = 0, int count = 100)
{
using var context = new DatabaseContext(_connectionString);
var res = context.Sources
.Skip(page * count)
.Take(count)
.ToList();
return res;
}
public List<SourceEntity> ListBySource(string source, int page = 0, int limit = 25)
{
using var context = new DatabaseContext(_connectionString);
var res = context.Sources
.Where(f => f.Source.Equals(source))
.Skip(page * limit)
.Take(limit)
.ToList();
return res;
}
public List<SourceEntity> ListByType(string type,int page = 0, int limit = 25)
{
using var context = new DatabaseContext(_connectionString);
var res = context.Sources
.Where(f => f.Type.Equals(type))
.Skip(page * limit)
.Take(limit)
.ToList();
return res;
}
public int Disable(Guid id)
{
using var context = new DatabaseContext(_connectionString);
var res = GetById(id);
res.Enabled = false;
context.Sources.Update(res);
try
{
context.SaveChanges();
return 1;
}
catch
{
return 0;
}
}
public int Enable(Guid id)
{
using var context = new DatabaseContext(_connectionString);
var res = GetById(id);
res.Enabled = true;
context.Sources.Update(res);
try
{
context.SaveChanges();
return 1;
}
catch
{
return 0;
}
}
public void Delete(Guid id)
{
using var context = new DatabaseContext(_connectionString);
var res = GetById(id);
context.Sources.Remove(res);
context.SaveChanges();
}
public int UpdateYoutubeId(Guid id, string youtubeId)
{
using var context = new DatabaseContext(_connectionString);
var res = GetById(id);
res.YoutubeId = youtubeId;
context.Sources.Update(res);
try
{
context.SaveChanges();
return 1;
}
catch
{
return 0;
}
}
private IDbConnection OpenConnection(string connectionString)
{
var conn = new NpgsqlConnection(_connectionString);
conn.Open();
return conn;
}
}