mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-07-18 22:04:05 +00:00
Save queries
This commit is contained in:
@ -4,25 +4,26 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type findResult struct {
|
||||
type Query struct {
|
||||
Fields string `json:"fields"`
|
||||
Limit int64 `json:"limit"`
|
||||
Query string `json:"query"`
|
||||
Skip int64 `json:"skip"`
|
||||
Sort string `json:"sort"`
|
||||
}
|
||||
|
||||
type QueryResult struct {
|
||||
Total int64 `json:"total"`
|
||||
Results []string `json:"results"`
|
||||
}
|
||||
|
||||
func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findResult {
|
||||
var out findResult
|
||||
var form struct {
|
||||
Fields string `json:"fields"`
|
||||
Limit int64 `json:"limit"`
|
||||
Query string `json:"query"`
|
||||
Skip int64 `json:"skip"`
|
||||
Sort string `json:"sort"`
|
||||
}
|
||||
func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) QueryResult {
|
||||
var out QueryResult
|
||||
var form Query
|
||||
|
||||
err := json.Unmarshal([]byte(formJson), &form)
|
||||
if err != nil {
|
||||
|
121
internal/app/collection_find_queries.go
Normal file
121
internal/app/collection_find_queries.go
Normal file
@ -0,0 +1,121 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
type SavedQuery struct {
|
||||
Query
|
||||
Name string `json:"name"`
|
||||
Remarks string `json:"remarks"`
|
||||
HostKey string `json:"hostKey"`
|
||||
DbKey string `json:"dbKey"`
|
||||
CollKey string `json:"collKey"`
|
||||
}
|
||||
|
||||
func updateQueryFile(a *App, newData map[string]SavedQuery) error {
|
||||
filePath := path.Join(a.Env.DataDirectory, "queries.json")
|
||||
jsonData, err := json.MarshalIndent(newData, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(filePath, jsonData, os.ModePerm)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *App) SavedQueries() map[string]SavedQuery {
|
||||
filePath := path.Join(a.Env.DataDirectory, "queries.json")
|
||||
jsonData, err := ioutil.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
// It's ok if the file cannot be opened, for example if it is not accessible.
|
||||
// Therefore no error is returned.
|
||||
runtime.LogInfo(a.ctx, "Could not open queries.json")
|
||||
runtime.LogInfo(a.ctx, err.Error())
|
||||
return make(map[string]SavedQuery, 0)
|
||||
}
|
||||
|
||||
if len(jsonData) == 0 {
|
||||
return make(map[string]SavedQuery, 0)
|
||||
} else {
|
||||
var queries map[string]SavedQuery
|
||||
err = json.Unmarshal(jsonData, &queries)
|
||||
|
||||
if err != nil {
|
||||
runtime.LogInfo(a.ctx, "queries.json file contains malformatted JSON data")
|
||||
runtime.LogInfo(a.ctx, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
return queries
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SaveQuery(jsonData string) string {
|
||||
var query SavedQuery
|
||||
err := json.Unmarshal([]byte(jsonData), &query)
|
||||
if err != nil {
|
||||
runtime.LogError(a.ctx, "Add query: malformed form")
|
||||
runtime.LogError(a.ctx, err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.InfoDialog,
|
||||
Title: "Malformed JSON",
|
||||
})
|
||||
return ""
|
||||
}
|
||||
|
||||
queries := a.SavedQueries()
|
||||
queries[query.Name] = query
|
||||
err = updateQueryFile(a, queries)
|
||||
if err != nil {
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.InfoDialog,
|
||||
Title: "Could not update query list",
|
||||
})
|
||||
return ""
|
||||
}
|
||||
|
||||
return query.Name
|
||||
}
|
||||
|
||||
func (a *App) RemoveQuery(queryName string) {
|
||||
queries := a.SavedQueries()
|
||||
delete(queries, queryName)
|
||||
if err := updateQueryFile(a, queries); err != nil {
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.InfoDialog,
|
||||
Title: "Could not update query list",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) UpdateQueries(jsonData string) bool {
|
||||
var queries map[string]SavedQuery
|
||||
err := json.Unmarshal([]byte(jsonData), &queries)
|
||||
if err != nil {
|
||||
runtime.LogError(a.ctx, "Update queries: malformed form")
|
||||
runtime.LogError(a.ctx, err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.InfoDialog,
|
||||
Title: "Malformed JSON",
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
err = updateQueryFile(a, queries)
|
||||
if err != nil {
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.InfoDialog,
|
||||
Title: "Could not save queries",
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
Reference in New Issue
Block a user