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; private DatabaseContext _context; public SourcesTable(string connectionString) { //_connectionString = connectionString; _context = new DatabaseContext(connectionString); } public SourcesTable(DatabaseContext context) { _context = context; } 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) { 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 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 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 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; //} }