1+ use crate :: axum:: AxumState ;
12use crate :: commands:: command_controller:: { Command , CooldownType , MessagePattern } ;
23use crate :: commands:: command_executor_service:: { TriggerId , TwitchUserPermission } ;
34use crate :: commands:: template_service:: StringTemplate ;
45use crate :: db:: ProdDB ;
5- use anyhow:: { Context , Error } ;
6+ use anyhow:: Context ;
67use num_traits:: FromPrimitive ;
7- use sqlx:: { query, query_as, AnyExecutor , Execute , MySql , MySqlConnection , MySqlExecutor , Transaction } ;
8- use std:: ops:: { Deref , DerefMut } ;
98use sqlx:: mysql:: MySqlQueryResult ;
9+ use sqlx:: { query, query_as, MySql , MySqlExecutor , Transaction } ;
10+ use std:: ops:: { Deref , DerefMut } ;
1011
1112pub struct CommandTable {
1213 id : String ,
@@ -195,15 +196,18 @@ async fn get_patterns(prod_db: &ProdDB, d: Vec<CommandTable>) -> anyhow::Result<
195196 Ok ( commands)
196197}
197198
198- pub ( crate ) async fn set_enabled ( prod_db : & ProdDB , trigger_id : & TriggerId , enabled : bool ) -> anyhow:: Result < Option < ( ) > > {
199+ pub ( crate ) async fn set_enabled ( state : AxumState , trigger_id : & TriggerId , enabled : bool ) -> anyhow:: Result < Option < ( ) > > {
200+ let mut transaction = state. prod_db . begin ( ) . await ?;
199201 let affected = query ! ( "UPDATE `sys-chat_trigger-patterns` SET is_enabled = ? WHERE parent_trigger_id = ?" , enabled, trigger_id)
200- . execute ( prod_db . deref ( ) )
202+ . execute ( transaction . deref_mut ( ) )
201203 . await
202204 . context ( "failed to set enabled on command patterns" ) ?
203205 . rows_affected ( ) ;
204206 if affected == 0 {
205207 return Ok ( None )
206208 }
209+ state. command_executor_service . refresh_patterns ( & state. prod_db , trigger_id) ;
210+ transaction. commit ( ) . await ?;
207211 Ok ( Some ( ( ) ) )
208212}
209213
@@ -219,8 +223,25 @@ pub(crate) async fn set_visible(prod_db: &ProdDB, trigger_id: &TriggerId, visibl
219223 Ok ( Some ( ( ) ) )
220224}
221225
222- pub ( crate ) async fn save ( prod_db : & ProdDB , command : & Command ) -> anyhow:: Result < ( ) > {
223- let mut transaction = prod_db. begin ( ) . await ?;
226+ pub ( crate ) enum SaveCommandError {
227+ DbError ( anyhow:: Error ) ,
228+ RegexError ( regex:: Error ) ,
229+ }
230+
231+ pub ( crate ) async fn save ( state : AxumState , command : & Command ) -> Result < ( ) , SaveCommandError > {
232+ let mut transaction = state. prod_db . begin ( ) . await . context ( "failed to start save command transaction" )
233+ . map_err ( |e| SaveCommandError :: DbError ( e) ) ?;
234+ save_command ( & command, & mut transaction) . await
235+ . map_err ( |e| SaveCommandError :: DbError ( e) ) ?;
236+
237+ state. command_executor_service . upsert_command ( & command)
238+ . map_err ( |e| SaveCommandError :: RegexError ( e) ) ?;
239+ transaction. commit ( ) . await . context ( "failed to commit save command transaction" )
240+ . map_err ( |e| SaveCommandError :: DbError ( e) ) ?;
241+ Ok ( ( ) )
242+ }
243+
244+ async fn save_command < ' a > ( command : & Command , transaction : & mut Transaction < ' a , MySql > ) -> anyhow:: Result < ( ) > {
224245 if let Some ( template) = & command. template {
225246 save_template ( transaction. deref_mut ( ) , template) . await ?;
226247 }
@@ -233,14 +254,13 @@ pub(crate) async fn save(prod_db: &ProdDB, command: &Command) -> anyhow::Result<
233254 id = ?, is_auto_generated = ?, description = ?, global_cooldown_amount = ?, global_cooldown_type = ?, permission = ?, user_cooldown_amount = ?, user_cooldown_type = ?, template_id = ?
234255 "# , command. id, command. is_auto_generated, command. description, command. global_cooldown_amount, command. global_cooldown_type, command. permission, command. user_cooldown_amount, command. user_cooldown_type, template_id,
235256 command. id, command. is_auto_generated, command. description, command. global_cooldown_amount, command. global_cooldown_type, command. permission, command. user_cooldown_amount, command. user_cooldown_type, template_id,
236- )
237- . execute ( prod_db. deref ( ) )
257+ ) . execute ( transaction. deref_mut ( ) )
238258 . await
239259 . context ( "failed to set visible on command patterns" ) ?;
240260 Ok ( ( ) )
241261}
242262
243- async fn save_pattern < ' a , E : MySqlExecutor < ' a > > ( prod_db : E , pattern : & MessagePattern , command_id : & str ) -> Result < MySqlQueryResult , Error > {
263+ async fn save_pattern < ' a , E : MySqlExecutor < ' a > > ( prod_db : E , pattern : & MessagePattern , command_id : & str ) -> Result < MySqlQueryResult , anyhow :: Error > {
244264 query ! ( r#"INSERT `sys-chat_trigger-patterns` (pattern, is_enabled, is_regex, is_visible, parent_trigger_id)
245265 VALUE (?, ?, ?, ? , ?) ON DUPLICATE KEY UPDATE
246266 pattern = ?, is_enabled = ?, is_regex = ?, is_visible = ?, parent_trigger_id = ?
@@ -251,23 +271,25 @@ async fn save_pattern<'a, E: MySqlExecutor<'a>>(prod_db: E, pattern: &MessagePat
251271 . context ( "failed to save command template" )
252272}
253273
254- async fn save_template < ' a , E : MySqlExecutor < ' a > > ( prod_db : E , template : & StringTemplate ) -> Result < MySqlQueryResult , Error > {
274+ async fn save_template < ' a , E : MySqlExecutor < ' a > > ( prod_db : E , template : & StringTemplate ) -> Result < MySqlQueryResult , anyhow :: Error > {
255275 query ! ( "INSERT `sys-string_templates` (id, message_color, template) VALUE (?, ?, ?) ON DUPLICATE KEY UPDATE template = ?, message_color = ?" ,
256276 template. id, template. template, template. message_color, template. template, template. message_color)
257277 . execute ( prod_db)
258278 . await
259279 . context ( "failed to save command template" )
260280}
261281
262- pub ( crate ) async fn delete_by_id ( prod_db : & ProdDB , trigger_id : & TriggerId ) -> anyhow:: Result < ( ) > {
263- query ! ( "DELETE FROM `sys-string_templates` WHERE id = (SELECT template_id FROM `sys-chat_trigger-trigger` WHERE id = ?)" , trigger_id)
264- . execute ( prod_db. deref ( ) )
282+ pub ( crate ) async fn delete_by_id ( state : AxumState , trigger_id : & TriggerId ) -> anyhow:: Result < ( ) > {
283+ let pool = state. prod_db . deref ( ) ;
284+ query ! ( "DELETE FROM `sys-string_templates` WHERE id = (SELECT template_id FROM `sys-chat_trigger-trigger` WHERE id = ?)" , trigger_id)
285+ . execute ( pool)
265286 . await
266287 . context ( "failed to delete string template" ) ?;
267288 query ! ( "DELETE FROM `sys-chat_trigger-trigger` WHERE id = ?" , trigger_id)
268- . execute ( prod_db . deref ( ) )
289+ . execute ( pool )
269290 . await
270291 . context ( "failed to delete command trigger" ) ?;
292+ state. command_executor_service . remove_command ( & trigger_id) ;
271293 Ok ( ( ) )
272294}
273295
0 commit comments