58 lines
1.5 KiB
C#
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;
|
|
|
|
public IconsTable(string connectionString)
|
|
{
|
|
_connectionString = connectionString;
|
|
}
|
|
|
|
public IconsTable(IConfiguration configuration)
|
|
{
|
|
var connstr = configuration.GetConnectionString("database");
|
|
if (connstr is null) connstr = "";
|
|
_connectionString = connstr;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |