2023-01-06 00:39:21 +01:00
package primary
2022-11-19 23:16:54 +01:00
import (
2023-07-27 17:44:06 +02:00
"blackforestbytes.com/simplecloudnotifier/db"
2022-11-19 23:16:54 +01:00
"blackforestbytes.com/simplecloudnotifier/models"
2022-12-07 23:32:58 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-19 23:16:54 +01:00
)
2024-06-01 13:45:28 +02:00
func ( db * Database ) CreateClient ( ctx db . TxContext , userid models . UserID , ctype models . ClientType , fcmToken string , agentModel string , agentVersion string , name * string ) ( models . Client , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return models . Client { } , err
}
2024-09-15 21:07:46 +02:00
entity := models . Client {
2023-05-28 22:27:38 +02:00
ClientID : models . NewClientID ( ) ,
2022-11-19 23:16:54 +01:00
UserID : userid ,
Type : ctype ,
2023-05-28 23:25:18 +02:00
FCMToken : fcmToken ,
2024-09-15 21:07:46 +02:00
TimestampCreated : models . NowSCNTime ( ) ,
2022-11-19 23:16:54 +01:00
AgentModel : agentModel ,
AgentVersion : agentVersion ,
2024-06-01 13:45:28 +02:00
Name : name ,
2023-05-28 22:27:38 +02:00
}
_ , err = sq . InsertSingle ( ctx , tx , "clients" , entity )
if err != nil {
return models . Client { } , err
}
2024-09-15 21:07:46 +02:00
return entity , nil
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) ClearFCMTokens ( ctx db . TxContext , fcmtoken string ) error {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "DELETE FROM clients WHERE fcm_token = :fcm" , sq . PP { "fcm" : fcmtoken } )
2022-11-19 23:16:54 +01:00
if err != nil {
return err
}
return nil
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) ListClients ( ctx db . TxContext , userid models . UserID ) ( [ ] models . Client , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2024-09-15 21:07:46 +02:00
return sq . QueryAll [ models . Client ] ( ctx , tx , "SELECT * FROM clients WHERE user_id = :uid ORDER BY clients.timestamp_created DESC, clients.client_id ASC" , sq . PP { "uid" : userid } , sq . SModeExtended , sq . Safe )
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) GetClient ( ctx db . TxContext , userid models . UserID , clientid models . ClientID ) ( models . Client , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return models . Client { } , err
}
2024-09-15 21:07:46 +02:00
return sq . QuerySingle [ models . Client ] ( ctx , tx , "SELECT * FROM clients WHERE user_id = :uid AND client_id = :cid LIMIT 1" , sq . PP {
2022-12-07 22:11:44 +01:00
"uid" : userid ,
"cid" : clientid ,
2024-09-15 21:07:46 +02:00
} , sq . SModeExtended , sq . Safe )
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) DeleteClient ( ctx db . TxContext , clientid models . ClientID ) error {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "DELETE FROM clients WHERE client_id = :cid" , sq . PP { "cid" : clientid } )
2022-11-19 23:16:54 +01:00
if err != nil {
return err
}
return nil
}
2022-11-30 12:40:03 +01:00
2023-07-27 17:44:06 +02:00
func ( db * Database ) DeleteClientsByFCM ( ctx db . TxContext , fcmtoken string ) error {
2022-11-30 12:40:03 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "DELETE FROM clients WHERE fcm_token = :fcm" , sq . PP { "fcm" : fcmtoken } )
2022-11-30 12:40:03 +01:00
if err != nil {
return err
}
return nil
}
2023-05-28 23:25:18 +02:00
2023-07-27 17:44:06 +02:00
func ( db * Database ) UpdateClientFCMToken ( ctx db . TxContext , clientid models . ClientID , fcmtoken string ) error {
2023-05-28 23:25:18 +02:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
_ , err = tx . Exec ( ctx , "UPDATE clients SET fcm_token = :vvv WHERE client_id = :cid" , sq . PP {
"vvv" : fcmtoken ,
"cid" : clientid ,
} )
if err != nil {
return err
}
return nil
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) UpdateClientAgentModel ( ctx db . TxContext , clientid models . ClientID , agentModel string ) error {
2023-05-28 23:25:18 +02:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
_ , err = tx . Exec ( ctx , "UPDATE clients SET agent_model = :vvv WHERE client_id = :cid" , sq . PP {
"vvv" : agentModel ,
"cid" : clientid ,
} )
if err != nil {
return err
}
return nil
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) UpdateClientAgentVersion ( ctx db . TxContext , clientid models . ClientID , agentVersion string ) error {
2023-05-28 23:25:18 +02:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
_ , err = tx . Exec ( ctx , "UPDATE clients SET agent_version = :vvv WHERE client_id = :cid" , sq . PP {
"vvv" : agentVersion ,
"cid" : clientid ,
} )
if err != nil {
return err
}
return nil
}
2024-06-01 01:01:58 +02:00
func ( db * Database ) UpdateClientDescriptionName ( ctx db . TxContext , clientid models . ClientID , descriptionName * string ) error {
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2024-06-01 13:45:28 +02:00
_ , err = tx . Exec ( ctx , "UPDATE clients SET name = :vvv WHERE client_id = :cid" , sq . PP {
2024-06-01 01:01:58 +02:00
"vvv" : descriptionName ,
"cid" : clientid ,
} )
if err != nil {
return err
}
return nil
}