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
5 changed files with 87 additions and 86 deletions
Showing only changes of commit 0b68261181 - Show all commits

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newsbot.Collector.Database.Repositories; using Newsbot.Collector.Database.Repositories;
using Newsbot.Collector.Domain.Dto; using Newsbot.Collector.Domain.Dto;
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;
@ -40,12 +41,12 @@ public class DiscordWebHookController : ControllerBase
{ {
var exists = _webhooks.GetByUrl(url); var exists = _webhooks.GetByUrl(url);
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (exists.ID != Guid.Empty) if (exists.Id != Guid.Empty)
{ {
return DiscordWebHookDto.Convert(exists); return DiscordWebHookDto.Convert(exists);
} }
var res = _webhooks.New(new DiscordWebHookModel var res = _webhooks.New(new DiscordWebhookEntity
{ {
Url = url, Url = url,
Server = server, Server = server,
@ -72,7 +73,7 @@ public class DiscordWebHookController : ControllerBase
[HttpGet("{id}")] [HttpGet("{id}")]
public DiscordWebHookDto GetById(Guid id) public DiscordWebHookDto GetById(Guid id)
{ {
var res = _webhooks.GetByID(id); var res = _webhooks.GetById(id);
return DiscordWebHookDto.Convert(res); return DiscordWebHookDto.Convert(res);
} }

View File

@ -2,7 +2,7 @@ using System.Data;
using Dapper; using Dapper;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Entities;
using Npgsql; using Npgsql;
namespace Newsbot.Collector.Database.Repositories; namespace Newsbot.Collector.Database.Repositories;
@ -22,103 +22,104 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
_connectionString = connstr; _connectionString = connstr;
} }
public DiscordWebHookModel New(DiscordWebHookModel model) public DiscordWebhookEntity New(DiscordWebhookEntity model)
{ {
var uid = Guid.NewGuid(); model.Id = new Guid();
using var conn = OpenConnection(_connectionString);
var query = using var context = new DatabaseContext(_connectionString);
"Insert Into DiscordWebHooks (ID, Url, Server, Channel, Enabled) Values (@id, @url, @server, @channel, @enabled);"; context.DiscordWebhooks.Add(model);
conn.Execute(query, new context.SaveChanges();
{
id = uid,
url = model.Url,
server = model.Server,
channel = model.Channel,
enabled = model.Enabled
});
model.ID = uid;
return model; return model;
} }
public DiscordWebHookModel GetByID(Guid id) public DiscordWebhookEntity GetById(Guid id)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Select * from DiscordWebHooks Where ID = @id LIMIT 1;"; var res = context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id));
return conn.Query<DiscordWebHookModel>(query, new res ??= new DiscordWebhookEntity();
{ return res;
id
}).First();
} }
public DiscordWebHookModel GetByUrl(string url) public DiscordWebhookEntity GetByUrl(string url)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Select * From DiscordWebHooks Where url = @url;"; var res = context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
try res ??= new DiscordWebhookEntity();
{ return res;
var res = conn.QueryFirst<DiscordWebHookModel>(query, new
{
url
});
return res;
}
catch
{
return new DiscordWebHookModel();
}
} }
public List<DiscordWebHookModel> List(int page, int count = 25) public List<DiscordWebhookEntity> List(int page, int count = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = @"Select * From DiscordWebHooks var res = context.DiscordWebhooks
Offset @offset Fetch Next @count Rows Only;"; .Skip(page * count)
return conn.Query<DiscordWebHookModel>(query, new .Take(count)
{ .ToList();
offset = page * count, count res ??= new List<DiscordWebhookEntity>();
}).ToList(); return res;
} }
public List<DiscordWebHookModel> ListByServer(string server, int limit = 25) public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "Select * From DiscordWebHooks Where Server = @id Limit @limit;"; var res = context.DiscordWebhooks
return conn.Query<DiscordWebHookModel>(query, new .Where(d => d.Server.Equals(server))
{ .Take(limit)
server, limit .ToList();
}).ToList(); res ??= new List<DiscordWebhookEntity>();
return res;
} }
public List<DiscordWebHookModel> ListByServerAndChannel(string server, string channel, int limit = 25) public List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit = 25)
{ {
using var conn = OpenConnection(_connectionString); using var context = new DatabaseContext(_connectionString);
var query = "SELECT * FROM DiscordWebHooks WHERE Server = @server and Channel = @channel Limit @limit;"; var res = context.DiscordWebhooks
return conn.Query<DiscordWebHookModel>(query, new .Where(s => s.Server.Equals(server))
{ .Where(c => c.Channel.Equals(channel))
server, .Take(limit)
channel, .ToList();
limit res ??= new List<DiscordWebhookEntity>();
}).ToList(); return res;
} }
public int Disable(Guid id) public int Disable(Guid id)
{ {
using var conn = OpenConnection(_connectionString); var res = GetById(id);
var query = "Update discordwebhooks Set Enabled = FALSE where ID = @id;"; using var context = new DatabaseContext(_connectionString);
return conn.Execute(query, new
res.Enabled = true;
context.DiscordWebhooks.Update(res);
try
{ {
id context.SaveChanges();
}); return 1;
}
catch(Exception ex)
{
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
return 0;
}
} }
public int Enable(Guid id) public int Enable(Guid id)
{ {
using var conn = OpenConnection(_connectionString); var res = GetById(id);
var query = "Update discordwebhooks Set Enabled = TRUE where ID = @id;"; using var context = new DatabaseContext(_connectionString);
return conn.Execute(query, new
res.Enabled = false;
context.DiscordWebhooks.Update(res);
try
{ {
id context.SaveChanges();
}); return 1;
}
catch(Exception ex)
{
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
return 0;
}
} }
private IDbConnection OpenConnection(string connectionString) private IDbConnection OpenConnection(string connectionString)

View File

@ -1,4 +1,4 @@
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Entities;
namespace Newsbot.Collector.Domain.Dto; namespace Newsbot.Collector.Domain.Dto;
@ -10,11 +10,11 @@ public class DiscordWebHookDto
public string? Channel { get; set; } public string? Channel { get; set; }
public bool Enabled { get; set; } public bool Enabled { get; set; }
public static DiscordWebHookDto Convert(DiscordWebHookModel model) public static DiscordWebHookDto Convert(DiscordWebhookEntity model)
{ {
return new DiscordWebHookDto return new DiscordWebHookDto
{ {
ID = model.ID, ID = model.Id,
Url = model.Url, Url = model.Url,
Server = model.Server, Server = model.Server,
Channel = model.Channel, Channel = model.Channel,

View File

@ -1,18 +1,17 @@
using Microsoft.VisualBasic; using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Interfaces; namespace Newsbot.Collector.Domain.Interfaces;
public interface IDiscordWebHooksRepository public interface IDiscordWebHooksRepository
{ {
DiscordWebHookModel New(DiscordWebHookModel model); DiscordWebhookEntity New(DiscordWebhookEntity model);
DiscordWebHookModel GetByID(Guid id); DiscordWebhookEntity GetById(Guid id);
DiscordWebHookModel GetByUrl(string url); DiscordWebhookEntity GetByUrl(string url);
List<DiscordWebHookModel> List(int page, int count = 25); List<DiscordWebhookEntity> List(int page, int count = 25);
List<DiscordWebHookModel> ListByServer(string server, int limit); List<DiscordWebhookEntity> ListByServer(string server, int limit);
List<DiscordWebHookModel> ListByServerAndChannel(string server, string channel, int limit); List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit);
int Disable(Guid id); int Disable(Guid id);
int Enable(Guid id); int Enable(Guid id);

View File

@ -132,7 +132,7 @@ public class DiscordNotificationJob
if (articleDetails.CodeIsRelease && !sub.CodeAllowReleases) throw new MessageTypeNotRequestedException("Message was a code release and was not requested by the subscription"); if (articleDetails.CodeIsRelease && !sub.CodeAllowReleases) throw new MessageTypeNotRequestedException("Message was a code release and was not requested by the subscription");
// find the discord webhooks we need to post to // find the discord webhooks we need to post to
var discordDetails = _webhook.GetByID(sub.DiscordWebHookId); var discordDetails = _webhook.GetById(sub.DiscordWebHookId);
if (discordDetails.Enabled == false) return; if (discordDetails.Enabled == false) return;
var client = new DiscordWebhookClient(discordDetails.Url); var client = new DiscordWebhookClient(discordDetails.Url);