2023-11-24 22:45:38 +08:00
package mysql
import (
"context"
"strings"
"github.com/usememos/memos/store"
)
2024-04-13 11:54:37 +08:00
func ( d * DB ) CreateWebhook ( ctx context . Context , create * store . Webhook ) ( * store . Webhook , error ) {
2023-11-24 22:45:38 +08:00
fields := [ ] string { "`name`" , "`url`" , "`creator_id`" }
placeholder := [ ] string { "?" , "?" , "?" }
2024-04-13 12:11:59 +08:00
args := [ ] any { create . Name , create . URL , create . CreatorID }
2023-11-24 22:45:38 +08:00
stmt := "INSERT INTO `webhook` (" + strings . Join ( fields , ", " ) + ") VALUES (" + strings . Join ( placeholder , ", " ) + ")"
result , err := d . db . ExecContext ( ctx , stmt , args ... )
if err != nil {
return nil , err
}
id , err := result . LastInsertId ( )
if err != nil {
return nil , err
}
2024-04-13 11:54:37 +08:00
create . ID = int32 ( id )
return d . GetWebhook ( ctx , & store . FindWebhook { ID : & create . ID } )
2023-11-24 22:45:38 +08:00
}
2024-04-13 11:54:37 +08:00
func ( d * DB ) ListWebhooks ( ctx context . Context , find * store . FindWebhook ) ( [ ] * store . Webhook , error ) {
2023-11-24 22:45:38 +08:00
where , args := [ ] string { "1 = 1" } , [ ] any { }
if find . ID != nil {
where , args = append ( where , "`id` = ?" ) , append ( args , * find . ID )
}
if find . CreatorID != nil {
where , args = append ( where , "`creator_id` = ?" ) , append ( args , * find . CreatorID )
}
2024-01-06 16:55:13 +08:00
rows , err := d . db . QueryContext ( ctx , "SELECT `id`, UNIX_TIMESTAMP(`created_ts`), UNIX_TIMESTAMP(`updated_ts`), `row_status`, `creator_id`, `name`, `url` FROM `webhook` WHERE " + strings . Join ( where , " AND " ) + " ORDER BY `id` DESC" ,
2023-11-24 22:45:38 +08:00
args ... ,
)
if err != nil {
return nil , err
}
defer rows . Close ( )
2024-04-13 11:54:37 +08:00
list := [ ] * store . Webhook { }
2023-11-24 22:45:38 +08:00
for rows . Next ( ) {
2024-04-13 11:54:37 +08:00
webhook := & store . Webhook { }
2023-11-24 22:45:38 +08:00
var rowStatus string
if err := rows . Scan (
2024-04-13 11:54:37 +08:00
& webhook . ID ,
2023-11-24 22:45:38 +08:00
& webhook . CreatedTs ,
& webhook . UpdatedTs ,
& rowStatus ,
2024-04-13 11:54:37 +08:00
& webhook . CreatorID ,
2023-11-24 22:45:38 +08:00
& webhook . Name ,
2024-04-13 12:11:59 +08:00
& webhook . URL ,
2023-11-24 22:45:38 +08:00
) ; err != nil {
return nil , err
}
2024-04-13 11:54:37 +08:00
webhook . RowStatus = store . RowStatus ( rowStatus )
2023-11-24 22:45:38 +08:00
list = append ( list , webhook )
}
if err := rows . Err ( ) ; err != nil {
return nil , err
}
return list , nil
}
2024-04-13 11:54:37 +08:00
func ( d * DB ) GetWebhook ( ctx context . Context , find * store . FindWebhook ) ( * store . Webhook , error ) {
2023-11-24 22:45:38 +08:00
list , err := d . ListWebhooks ( ctx , find )
if err != nil {
return nil , err
}
if len ( list ) == 0 {
return nil , nil
}
return list [ 0 ] , nil
}
2024-04-13 11:54:37 +08:00
func ( d * DB ) UpdateWebhook ( ctx context . Context , update * store . UpdateWebhook ) ( * store . Webhook , error ) {
2023-11-24 22:45:38 +08:00
set , args := [ ] string { } , [ ] any { }
if update . RowStatus != nil {
set , args = append ( set , "`row_status` = ?" ) , append ( args , update . RowStatus . String ( ) )
}
if update . Name != nil {
set , args = append ( set , "`name` = ?" ) , append ( args , * update . Name )
}
if update . URL != nil {
set , args = append ( set , "`url` = ?" ) , append ( args , * update . URL )
}
args = append ( args , update . ID )
stmt := "UPDATE `webhook` SET " + strings . Join ( set , ", " ) + " WHERE `id` = ?"
_ , err := d . db . ExecContext ( ctx , stmt , args ... )
if err != nil {
return nil , err
}
webhook , err := d . GetWebhook ( ctx , & store . FindWebhook { ID : & update . ID } )
if err != nil {
return nil , err
}
return webhook , nil
}
func ( d * DB ) DeleteWebhook ( ctx context . Context , delete * store . DeleteWebhook ) error {
_ , err := d . db . ExecContext ( ctx , "DELETE FROM `webhook` WHERE `id` = ?" , delete . ID )
return err
}