features/cutover-to-ef #9

Merged
jtom38 merged 27 commits from features/cutover-to-ef into main 2023-06-25 21:15:58 -07:00
2 changed files with 19 additions and 26 deletions
Showing only changes of commit 2c886d22ad - Show all commits

View File

@ -1,6 +1,7 @@
using System.Data; using System.Data;
using Dapper; using Dapper;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Models;
using Npgsql; using Npgsql;
@ -23,37 +24,29 @@ public class IconsTable : IIconsRepository
_connectionString = connstr; _connectionString = connstr;
} }
public void New(IconModel model) public void New(IconEntity model)
{ {
using var context = new DatabaseContext(_connectionString);
model.Id = Guid.NewGuid(); model.Id = Guid.NewGuid();
using var conn = OpenConnection(_connectionString); context.Icons.Add(model);
var q = @"Insert Into icons (id, filename, site, sourceid) values (@Id,@FileName, @Site, @SourceId)"; context.SaveChanges();
conn.Execute(q, model);
} }
public IconModel GetById(Guid id) public IconEntity GetById(Guid id)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Select * From icons where ID = @id Limit 1;"; var res = context.Icons.FirstOrDefault(f => f.Id.Equals(id));
var res = conn.Query<IconModel>(query, new res ??= new IconEntity();
{ return res;
id
});
if (!res.Any()) return new IconModel();
return res.First();
} }
public IconModel GetBySourceId(Guid id) public IconEntity GetBySourceId(Guid id)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Select * From icons where sourceid = @id Limit 1;"; var res = context.Icons.FirstOrDefault(f => f.SourceId.Equals(id));
var res = conn.Query<IconModel>(query, new res ??= new IconEntity();
{ return res;
id
});
if (!res.Any()) return new IconModel();
return res.First();
} }
private IDbConnection OpenConnection(string connectionString) private IDbConnection OpenConnection(string connectionString)

View File

@ -1,11 +1,11 @@
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Entities;
namespace Newsbot.Collector.Domain.Interfaces; namespace Newsbot.Collector.Domain.Interfaces;
public interface IIconsRepository public interface IIconsRepository
{ {
public void New(IconModel model); public void New(IconEntity model);
public IconModel GetById(Guid id); public IconEntity GetById(Guid id);
public IconModel GetBySourceId(Guid id); public IconEntity GetBySourceId(Guid id);
} }