1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-19 05:27:57 +00:00
rolens/internal/app/database.go

64 lines
1.4 KiB
Go
Raw Normal View History

2023-01-14 19:47:29 +00:00
package app
import (
"fmt"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/bson"
)
func (a *App) OpenDatabase(hostKey, dbKey string) (collections []string) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
fmt.Println(err.Error())
return nil
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not retrieve collection list for " + dbKey,
Message: err.Error(),
})
return nil
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
defer close()
return collections
}
func (a *App) DropDatabase(hostKey, dbKey string) bool {
sure, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Confirm",
Message: "Are you sure you want to drop " + dbKey + "?",
2023-01-14 20:11:35 +00:00
Buttons: []string{"Yes", "No"},
2023-01-14 19:47:29 +00:00
DefaultButton: "Yes",
CancelButton: "No",
})
if sure != "Yes" {
return false
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
fmt.Println(err.Error())
return false
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
err = client.Database(dbKey).Drop(ctx)
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not drop " + dbKey,
Message: err.Error(),
})
return false
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
defer close()
return true
}