expanded on error handing when a record did not exist

This commit is contained in:
James Tombleson 2023-03-13 21:58:33 -07:00
parent 578197cb75
commit dfd77a1253
2 changed files with 60 additions and 56 deletions

View File

@ -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<DiscordWebHookModel>(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<DiscordWebHookModel>(query, new
try
{
url = url
});
var res = conn.QueryFirst<DiscordWebHookModel>(query, new
{
url
});
return res;
}
catch
{
return new DiscordWebHookModel();
}
}
public List<DiscordWebHookModel> List(int page, int count = 25)
@ -73,8 +75,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
Offset @offset Fetch Next @count Rows Only;";
return conn.Query<DiscordWebHookModel>(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<DiscordWebHookModel>(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<DiscordWebHookModel>(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;
}
}

View File

@ -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<SubscriptionModel> 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<SubscriptionModel>(query, new
{
page = page * count,
count = count
page = page * count, count
}).ToList();
}
@ -68,7 +64,7 @@ public class SubscriptionsTable : ISubscriptionRepository
return conn.Query<SubscriptionModel>(query, new
{
page = page * count,
count = count,
count,
sourceid = id
}).ToList();
}
@ -82,8 +78,8 @@ public class SubscriptionsTable : ISubscriptionRepository
return conn.Query<SubscriptionModel>(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<SubscriptionModel>(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<SubscriptionModel>(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;
}
}