diff --git a/Requests/CreateTTSUser.cs b/Requests/CreateTTSUser.cs index a4fdf58..f33686f 100644 --- a/Requests/CreateTTSUser.cs +++ b/Requests/CreateTTSUser.cs @@ -27,16 +27,25 @@ namespace HermesSocketServer.Requests if (long.TryParse(data["chatter"].ToString(), out long chatter)) data["chatter"] = chatter; + else + return new RequestResult(false, "Invalid Twitch user id"); + if (data["voice"] is JsonElement v) data["voice"] = v.ToString(); + else + return new RequestResult(false, "Invalid voice id"); + data["user"] = sender; var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; if (check is not bool state || !state) - return new RequestResult(false, null); + return new RequestResult(false, "Voice is disabled on this channel."); string sql = "INSERT INTO \"TtsChatVoice\" (\"userId\", \"chatterId\", \"ttsVoiceId\") VALUES (@user, @chatter, @voice)"; var result = await _database.Execute(sql, data); + if (result == 0) + return new RequestResult(false, "Could not insert the user's voice properly."); + _logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]"); return new RequestResult(result == 1, null); } diff --git a/Requests/CreateTTSVoice.cs b/Requests/CreateTTSVoice.cs index 2768a3d..7e70c61 100644 --- a/Requests/CreateTTSVoice.cs +++ b/Requests/CreateTTSVoice.cs @@ -1,6 +1,6 @@ using System.Text.Json; -using HermesSocketLibrary.db; using HermesSocketLibrary.Requests; +using HermesSocketServer.Store; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,13 +8,13 @@ namespace HermesSocketServer.Requests public class CreateTTSVoice : IRequest { public string Name => "create_tts_voice"; - private Database _database; + private IStore _voices; private ILogger _logger; private Random _random; - public CreateTTSVoice(Database database, ILogger logger) + public CreateTTSVoice(VoiceStore voices, ILogger logger) { - _database = database; + _voices = voices; _logger = logger; _random = new Random(); } @@ -28,18 +28,17 @@ namespace HermesSocketServer.Requests return new RequestResult(false, null); } - string id = RandomString(25); - data.Add("idd", id); - if (data["voice"] is JsonElement v) data["voice"] = v.ToString(); + else + return new RequestResult(false, "Invalid voice name."); - string sql = "INSERT INTO \"TtsVoice\" (id, name) VALUES (@idd, @voice)"; - var result = await _database.Execute(sql, data); - _logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {data["idd"]}]"); + string id = RandomString(25); - data.Remove("idd"); - return new RequestResult(result == 1, id); + _voices.Set(id, data["voice"].ToString()); + _logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]"); + + return new RequestResult(true, id); } private string RandomString(int length) diff --git a/Requests/DeleteTTSVoice.cs b/Requests/DeleteTTSVoice.cs index 87f7edf..db037bd 100644 --- a/Requests/DeleteTTSVoice.cs +++ b/Requests/DeleteTTSVoice.cs @@ -1,6 +1,6 @@ using System.Text.Json; -using HermesSocketLibrary.db; using HermesSocketLibrary.Requests; +using HermesSocketServer.Store; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,12 +8,12 @@ namespace HermesSocketServer.Requests public class DeleteTTSVoice : IRequest { public string Name => "delete_tts_voice"; - private Database _database; + private IStore _voices; private ILogger _logger; - public DeleteTTSVoice(Database database, ILogger logger) + public DeleteTTSVoice(VoiceStore voices, ILogger logger) { - _database = database; + _voices = voices; _logger = logger; } @@ -28,10 +28,9 @@ namespace HermesSocketServer.Requests if (data["voice"] is JsonElement v) data["voice"] = v.ToString(); - string sql = "DELETE FROM \"TtsVoice\" WHERE id = @voice"; - var result = await _database.Execute(sql, data); + _voices.Remove(data["voice"].ToString()); _logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]"); - return new RequestResult(result == 1, null); + return new RequestResult(true, null); } } } \ No newline at end of file diff --git a/Requests/UpdateTTSVoice.cs b/Requests/UpdateTTSVoice.cs index 4672888..357b979 100644 --- a/Requests/UpdateTTSVoice.cs +++ b/Requests/UpdateTTSVoice.cs @@ -1,6 +1,6 @@ using System.Text.Json; -using HermesSocketLibrary.db; using HermesSocketLibrary.Requests; +using HermesSocketServer.Store; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,12 +8,12 @@ namespace HermesSocketServer.Requests public class UpdateTTSVoice : IRequest { public string Name => "update_tts_voice"; - private Database _database; + private IStore _voices; private ILogger _logger; - public UpdateTTSVoice(Database database, ILogger logger) + public UpdateTTSVoice(VoiceStore voices, ILogger logger) { - _database = database; + _voices = voices; _logger = logger; } @@ -30,10 +30,9 @@ namespace HermesSocketServer.Requests if (data["voiceid"] is JsonElement id) data["voiceid"] = id.ToString(); - string sql = "UPDATE \"TtsVoice\" SET name = @voice WHERE id = @voiceid"; - var result = await _database.Execute(sql, data); + _voices.Set(data["voiceid"].ToString(), data["voice"].ToString()); _logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]"); - return new RequestResult(result == 1, null); + return new RequestResult(true, null); } } } \ No newline at end of file diff --git a/db/Database.cs b/db/Database.cs index 401ffc2..e8a2dc4 100644 --- a/db/Database.cs +++ b/db/Database.cs @@ -79,13 +79,13 @@ namespace HermesSocketLibrary.db } } - public async Task Execute(string sql, Action action) + public async Task Execute(string sql, Action prepare) { using (var connection = await _source.OpenConnectionAsync()) { using (var command = new NpgsqlCommand(sql, connection)) { - action(command); + prepare(command); await command.PrepareAsync(); return await command.ExecuteNonQueryAsync();