expanded on error handing when a record did not exist
This commit is contained in:
parent
578197cb75
commit
dfd77a1253
@ -9,7 +9,7 @@ namespace Newsbot.Collector.Database.Repositories;
|
|||||||
|
|
||||||
public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||||
{
|
{
|
||||||
private string _connectionString;
|
private readonly string _connectionString;
|
||||||
|
|
||||||
public DiscordWebhooksTable(string connectionString)
|
public DiscordWebhooksTable(string connectionString)
|
||||||
{
|
{
|
||||||
@ -22,18 +22,12 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
_connectionString = connstr;
|
_connectionString = connstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IDbConnection OpenConnection(string connectionString)
|
|
||||||
{
|
|
||||||
var conn = new NpgsqlConnection(_connectionString);
|
|
||||||
conn.Open();
|
|
||||||
return conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DiscordWebHookModel New(DiscordWebHookModel model)
|
public DiscordWebHookModel New(DiscordWebHookModel model)
|
||||||
{
|
{
|
||||||
var uid = Guid.NewGuid();
|
var uid = Guid.NewGuid();
|
||||||
using var conn = OpenConnection(_connectionString);
|
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
|
conn.Execute(query, new
|
||||||
{
|
{
|
||||||
id = uid,
|
id = uid,
|
||||||
@ -52,7 +46,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
var query = "Select * from DiscordWebHooks Where ID = @id LIMIT 1;";
|
var query = "Select * from DiscordWebHooks Where ID = @id LIMIT 1;";
|
||||||
return conn.Query<DiscordWebHookModel>(query, new
|
return conn.Query<DiscordWebHookModel>(query, new
|
||||||
{
|
{
|
||||||
id = id
|
id
|
||||||
}).First();
|
}).First();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,10 +54,18 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
{
|
{
|
||||||
using var conn = OpenConnection(_connectionString);
|
using var conn = OpenConnection(_connectionString);
|
||||||
var query = "Select * From DiscordWebHooks Where url = @url;";
|
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)
|
public List<DiscordWebHookModel> List(int page, int count = 25)
|
||||||
@ -73,8 +75,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
Offset @offset Fetch Next @count Rows Only;";
|
Offset @offset Fetch Next @count Rows Only;";
|
||||||
return conn.Query<DiscordWebHookModel>(query, new
|
return conn.Query<DiscordWebHookModel>(query, new
|
||||||
{
|
{
|
||||||
offset = page * count,
|
offset = page * count, count
|
||||||
count = count
|
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,8 +85,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
var query = "Select * From DiscordWebHooks Where Server = @id Limit @limit;";
|
var query = "Select * From DiscordWebHooks Where Server = @id Limit @limit;";
|
||||||
return conn.Query<DiscordWebHookModel>(query, new
|
return conn.Query<DiscordWebHookModel>(query, new
|
||||||
{
|
{
|
||||||
server = server,
|
server, limit
|
||||||
limit = limit
|
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,9 +95,9 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
var query = "SELECT * FROM DiscordWebHooks WHERE Server = @server and Channel = @channel Limit @limit;";
|
var query = "SELECT * FROM DiscordWebHooks WHERE Server = @server and Channel = @channel Limit @limit;";
|
||||||
return conn.Query<DiscordWebHookModel>(query, new
|
return conn.Query<DiscordWebHookModel>(query, new
|
||||||
{
|
{
|
||||||
server = server,
|
server,
|
||||||
channel = channel,
|
channel,
|
||||||
limit = limit
|
limit
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|||||||
var query = "Update discordwebhooks Set Enabled = FALSE where ID = @id;";
|
var query = "Update discordwebhooks Set Enabled = FALSE where ID = @id;";
|
||||||
return conn.Execute(query, new
|
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;";
|
var query = "Update discordwebhooks Set Enabled = TRUE where ID = @id;";
|
||||||
return conn.Execute(query, new
|
return conn.Execute(query, new
|
||||||
{
|
{
|
||||||
id = id
|
id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IDbConnection OpenConnection(string connectionString)
|
||||||
|
{
|
||||||
|
var conn = new NpgsqlConnection(_connectionString);
|
||||||
|
conn.Open();
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ namespace Newsbot.Collector.Database.Repositories;
|
|||||||
|
|
||||||
public class SubscriptionsTable : ISubscriptionRepository
|
public class SubscriptionsTable : ISubscriptionRepository
|
||||||
{
|
{
|
||||||
private string _connectionString;
|
private readonly string _connectionString;
|
||||||
|
|
||||||
public SubscriptionsTable(string connectionString)
|
public SubscriptionsTable(string connectionString)
|
||||||
{
|
{
|
||||||
@ -19,25 +19,17 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
public SubscriptionsTable(IConfiguration configuration)
|
public SubscriptionsTable(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var connstr = configuration.GetConnectionString("database");
|
var connstr = configuration.GetConnectionString("database");
|
||||||
if (connstr is null)
|
if (connstr is null) connstr = "";
|
||||||
{
|
|
||||||
connstr = "";
|
|
||||||
}
|
|
||||||
_connectionString = connstr;
|
_connectionString = connstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IDbConnection OpenConnection(string connectionString)
|
|
||||||
{
|
|
||||||
var conn = new NpgsqlConnection(_connectionString);
|
|
||||||
conn.Open();
|
|
||||||
return conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubscriptionModel New(SubscriptionModel model)
|
public SubscriptionModel New(SubscriptionModel model)
|
||||||
{
|
{
|
||||||
model.ID = Guid.NewGuid();
|
model.ID = Guid.NewGuid();
|
||||||
using var conn = OpenConnection(_connectionString);
|
using var conn = OpenConnection(_connectionString);
|
||||||
var query = "Insert Into subscriptions (ID, DiscordWebHookId, SourceId) Values (@id, @webhookid, @sourceid);";
|
var query = "Insert Into subscriptions (ID, DiscordWebHookId, SourceId) Values (@id, @webhookid, @sourceid);";
|
||||||
|
try
|
||||||
|
{
|
||||||
conn.Execute(query, new
|
conn.Execute(query, new
|
||||||
{
|
{
|
||||||
id = model.ID,
|
id = model.ID,
|
||||||
@ -46,6 +38,11 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
});
|
});
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return new SubscriptionModel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<SubscriptionModel> List(int page = 0, int count = 25)
|
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;";
|
Offset @page Fetch Next @count Rows Only;";
|
||||||
return conn.Query<SubscriptionModel>(query, new
|
return conn.Query<SubscriptionModel>(query, new
|
||||||
{
|
{
|
||||||
page = page * count,
|
page = page * count, count
|
||||||
count = count
|
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +64,7 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
return conn.Query<SubscriptionModel>(query, new
|
return conn.Query<SubscriptionModel>(query, new
|
||||||
{
|
{
|
||||||
page = page * count,
|
page = page * count,
|
||||||
count = count,
|
count,
|
||||||
sourceid = id
|
sourceid = id
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
@ -82,8 +78,8 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
return conn.Query<SubscriptionModel>(query, new
|
return conn.Query<SubscriptionModel>(query, new
|
||||||
{
|
{
|
||||||
page = page * count,
|
page = page * count,
|
||||||
count = count,
|
count,
|
||||||
webhookid = id,
|
webhookid = id
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,12 +89,9 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
var query = @"Select * From subscriptions Where id = @id;";
|
var query = @"Select * From subscriptions Where id = @id;";
|
||||||
var res = conn.Query<SubscriptionModel>(query, new
|
var res = conn.Query<SubscriptionModel>(query, new
|
||||||
{
|
{
|
||||||
id = id,
|
id
|
||||||
});
|
});
|
||||||
if (res.Count() == 0)
|
if (res.Count() == 0) return new SubscriptionModel();
|
||||||
{
|
|
||||||
return new SubscriptionModel();
|
|
||||||
}
|
|
||||||
return res.First();
|
return res.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,12 +104,9 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
var res = conn.Query<SubscriptionModel>(query, new
|
var res = conn.Query<SubscriptionModel>(query, new
|
||||||
{
|
{
|
||||||
webhookid = webhookId,
|
webhookid = webhookId,
|
||||||
sourceid = sourceId,
|
sourceid = sourceId
|
||||||
});
|
});
|
||||||
if (res.Count() == 0)
|
if (res.Count() == 0) return new SubscriptionModel();
|
||||||
{
|
|
||||||
return new SubscriptionModel();
|
|
||||||
}
|
|
||||||
return res.First();
|
return res.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +116,14 @@ public class SubscriptionsTable : ISubscriptionRepository
|
|||||||
var query = "Delete From subscriptions Where id = @id;";
|
var query = "Delete From subscriptions Where id = @id;";
|
||||||
conn.Execute(query, new
|
conn.Execute(query, new
|
||||||
{
|
{
|
||||||
id = id
|
id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IDbConnection OpenConnection(string connectionString)
|
||||||
|
{
|
||||||
|
var conn = new NpgsqlConnection(_connectionString);
|
||||||
|
conn.Open();
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user