From dfd77a125332e00be46111c1d180fba3b9a6ce99 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Mon, 13 Mar 2023 21:58:33 -0700 Subject: [PATCH] expanded on error handing when a record did not exist --- .../Repositories/DiscordWebhooksTable.cs | 51 ++++++++------- .../Repositories/SubscriptionsTable.cs | 65 +++++++++---------- 2 files changed, 60 insertions(+), 56 deletions(-) diff --git a/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs b/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs index 0594d5e..f41517c 100644 --- a/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs +++ b/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs @@ -9,7 +9,7 @@ namespace Newsbot.Collector.Database.Repositories; public class DiscordWebhooksTable : IDiscordWebHooksRepository { - private string _connectionString; + private readonly string _connectionString; public DiscordWebhooksTable(string connectionString) { @@ -22,18 +22,12 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository _connectionString = connstr; } - private IDbConnection OpenConnection(string connectionString) - { - var conn = new NpgsqlConnection(_connectionString); - conn.Open(); - return conn; - } - public DiscordWebHookModel New(DiscordWebHookModel model) { var uid = Guid.NewGuid(); using var conn = OpenConnection(_connectionString); - var query = "Insert Into DiscordWebHooks (ID, Url, Server, Channel, Enabled) Values (@id, @url, @server, @channel, @enabled);"; + var query = + "Insert Into DiscordWebHooks (ID, Url, Server, Channel, Enabled) Values (@id, @url, @server, @channel, @enabled);"; conn.Execute(query, new { id = uid, @@ -52,7 +46,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository var query = "Select * from DiscordWebHooks Where ID = @id LIMIT 1;"; return conn.Query(query, new { - id = id + id }).First(); } @@ -60,10 +54,18 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository { using var conn = OpenConnection(_connectionString); var query = "Select * From DiscordWebHooks Where url = @url;"; - return conn.QueryFirst(query, new + try { - url = url - }); + var res = conn.QueryFirst(query, new + { + url + }); + return res; + } + catch + { + return new DiscordWebHookModel(); + } } public List List(int page, int count = 25) @@ -73,8 +75,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository Offset @offset Fetch Next @count Rows Only;"; return conn.Query(query, new { - offset = page * count, - count = count + offset = page * count, count }).ToList(); } @@ -84,8 +85,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository var query = "Select * From DiscordWebHooks Where Server = @id Limit @limit;"; return conn.Query(query, new { - server = server, - limit = limit + server, limit }).ToList(); } @@ -95,9 +95,9 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository var query = "SELECT * FROM DiscordWebHooks WHERE Server = @server and Channel = @channel Limit @limit;"; return conn.Query(query, new { - server = server, - channel = channel, - limit = limit + server, + channel, + limit }).ToList(); } @@ -107,7 +107,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository var query = "Update discordwebhooks Set Enabled = FALSE where ID = @id;"; return conn.Execute(query, new { - id = id + id }); } @@ -117,7 +117,14 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository var query = "Update discordwebhooks Set Enabled = TRUE where ID = @id;"; return conn.Execute(query, new { - id = id + id }); } + + private IDbConnection OpenConnection(string connectionString) + { + var conn = new NpgsqlConnection(_connectionString); + conn.Open(); + return conn; + } } \ No newline at end of file diff --git a/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs b/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs index 5974677..a492f79 100644 --- a/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs +++ b/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs @@ -9,7 +9,7 @@ namespace Newsbot.Collector.Database.Repositories; public class SubscriptionsTable : ISubscriptionRepository { - private string _connectionString; + private readonly string _connectionString; public SubscriptionsTable(string connectionString) { @@ -19,32 +19,29 @@ public class SubscriptionsTable : ISubscriptionRepository public SubscriptionsTable(IConfiguration configuration) { var connstr = configuration.GetConnectionString("database"); - if (connstr is null) - { - connstr = ""; - } + if (connstr is null) connstr = ""; _connectionString = connstr; } - private IDbConnection OpenConnection(string connectionString) - { - var conn = new NpgsqlConnection(_connectionString); - conn.Open(); - return conn; - } - public SubscriptionModel New(SubscriptionModel model) { model.ID = Guid.NewGuid(); using var conn = OpenConnection(_connectionString); var query = "Insert Into subscriptions (ID, DiscordWebHookId, SourceId) Values (@id, @webhookid, @sourceid);"; - conn.Execute(query, new + try { - id = model.ID, - webhookid = model.DiscordWebHookID, - sourceid = model.SourceID - }); - return model; + conn.Execute(query, new + { + id = model.ID, + webhookid = model.DiscordWebHookID, + sourceid = model.SourceID + }); + return model; + } + catch + { + return new SubscriptionModel(); + } } public List List(int page = 0, int count = 25) @@ -54,8 +51,7 @@ public class SubscriptionsTable : ISubscriptionRepository Offset @page Fetch Next @count Rows Only;"; return conn.Query(query, new { - page = page * count, - count = count + page = page * count, count }).ToList(); } @@ -68,7 +64,7 @@ public class SubscriptionsTable : ISubscriptionRepository return conn.Query(query, new { page = page * count, - count = count, + count, sourceid = id }).ToList(); } @@ -82,8 +78,8 @@ public class SubscriptionsTable : ISubscriptionRepository return conn.Query(query, new { page = page * count, - count = count, - webhookid = id, + count, + webhookid = id }).ToList(); } @@ -93,12 +89,9 @@ public class SubscriptionsTable : ISubscriptionRepository var query = @"Select * From subscriptions Where id = @id;"; var res = conn.Query(query, new { - id = id, + id }); - if (res.Count() == 0) - { - return new SubscriptionModel(); - } + if (res.Count() == 0) return new SubscriptionModel(); return res.First(); } @@ -111,12 +104,9 @@ public class SubscriptionsTable : ISubscriptionRepository var res = conn.Query(query, new { webhookid = webhookId, - sourceid = sourceId, + sourceid = sourceId }); - if (res.Count() == 0) - { - return new SubscriptionModel(); - } + if (res.Count() == 0) return new SubscriptionModel(); return res.First(); } @@ -126,7 +116,14 @@ public class SubscriptionsTable : ISubscriptionRepository var query = "Delete From subscriptions Where id = @id;"; conn.Execute(query, new { - id = id + id }); } + + private IDbConnection OpenConnection(string connectionString) + { + var conn = new NpgsqlConnection(_connectionString); + conn.Open(); + return conn; + } } \ No newline at end of file