Newsbot.Collector/Newsbot.Collector.Database/Repositories/IconsTable.cs

58 lines
1.5 KiB
C#

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 IconsTable : IIconsRepository
{
//private readonly string _connectionString;
private DatabaseContext _context;
public IconsTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public IconsTable(DatabaseContext context)
{
_context = context;
}
public void New(IconEntity model)
{
//using var context = new DatabaseContext(_connectionString);
model.Id = Guid.NewGuid();
_context.Icons.Add(model);
_context.SaveChanges();
}
public IconEntity GetById(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.Icons.FirstOrDefault(f => f.Id.Equals(id));
res ??= new IconEntity();
return res;
}
public IconEntity GetBySourceId(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.Icons.FirstOrDefault(f => f.SourceId.Equals(id));
res ??= new IconEntity();
return res;
}
//private IDbConnection OpenConnection(string connectionString)
//{
// var conn = new NpgsqlConnection(_connectionString);
// conn.Open();
// return conn;
//}
}