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

58 lines
1.5 KiB
C#
Raw Normal View History

using System.Data;
using Dapper;
using Microsoft.Extensions.Configuration;
2023-06-23 21:56:15 -07:00
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;
}
2023-06-23 21:56:15 -07:00
public void New(IconEntity model)
{
2023-06-23 21:56:15 -07:00
using var context = new DatabaseContext(_connectionString);
model.Id = Guid.NewGuid();
2023-06-23 21:56:15 -07:00
context.Icons.Add(model);
context.SaveChanges();
}
2023-06-23 21:56:15 -07:00
public IconEntity GetById(Guid id)
{
2023-06-23 21:56:15 -07:00
using var context = new DatabaseContext(_connectionString);
var res = context.Icons.FirstOrDefault(f => f.Id.Equals(id));
res ??= new IconEntity();
return res;
}
2023-06-23 21:56:15 -07:00
public IconEntity GetBySourceId(Guid id)
{
2023-06-23 21:56:15 -07:00
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;
}
}