2023-01-14 19:47:29 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2023-02-20 20:04:01 +00:00
|
|
|
"github.com/ncruces/zenity"
|
2023-01-14 19:47:29 +00:00
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
2023-02-15 16:00:53 +00:00
|
|
|
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 {
|
2023-01-24 19:55:53 +00:00
|
|
|
Total int64 `json:"total"`
|
|
|
|
Results []string `json:"results"`
|
2023-01-14 19:47:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 14:41:53 +00:00
|
|
|
func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult {
|
2023-02-15 16:00:53 +00:00
|
|
|
var out QueryResult
|
|
|
|
var form Query
|
2023-01-14 19:47:29 +00:00
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(formJson), &form)
|
|
|
|
if err != nil {
|
2023-02-11 20:57:52 +00:00
|
|
|
runtime.LogError(a.ctx, "Could not parse find form:")
|
|
|
|
runtime.LogError(a.ctx, err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Could not parse form"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
client, ctx, close, err := a.connectToHost(hostKey)
|
|
|
|
if err != nil {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
defer close()
|
|
|
|
var query bson.M
|
|
|
|
var projection bson.M
|
|
|
|
var sort bson.M
|
|
|
|
|
2023-01-24 10:44:43 +00:00
|
|
|
err = bson.UnmarshalExtJSON([]byte(form.Query), true, &query)
|
2023-01-14 19:47:29 +00:00
|
|
|
if err != nil {
|
2023-06-02 20:33:43 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Invalid find query: %s", err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Invalid query"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(form.Fields), &projection)
|
|
|
|
if err != nil {
|
2023-06-02 20:33:43 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Invalid find projection: %s", err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Invalid projection"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(form.Sort), &sort)
|
|
|
|
if err != nil {
|
2023-06-02 20:33:43 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Invalid find sort: %s", err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Invalid sort"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := mongoOptions.FindOptions{
|
|
|
|
Limit: &form.Limit,
|
|
|
|
Projection: projection,
|
|
|
|
Skip: &form.Skip,
|
|
|
|
Sort: sort,
|
|
|
|
}
|
|
|
|
|
|
|
|
total, err := client.Database(dbKey).Collection(collKey).CountDocuments(ctx, query, nil)
|
|
|
|
if err != nil {
|
2023-06-02 20:33:43 +00:00
|
|
|
runtime.LogWarningf(a.ctx, "Encountered an error while counting documents: %s", err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Error while counting docs"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, query, &opt)
|
|
|
|
if err != nil {
|
2023-06-02 20:33:43 +00:00
|
|
|
runtime.LogWarningf(a.ctx, "Encountered an error while performing query: %s", err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Error while querying"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cur.Close(ctx)
|
|
|
|
var results []bson.M
|
|
|
|
err = cur.All(ctx, &results)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-06-02 20:33:43 +00:00
|
|
|
runtime.LogWarningf(a.ctx, "Encountered an error while performing query: %s", err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Error while querying"), zenity.ErrorIcon)
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Total = total
|
2023-06-02 20:33:43 +00:00
|
|
|
out.Results = make([]string, 0)
|
|
|
|
|
2023-01-24 19:55:53 +00:00
|
|
|
for _, r := range results {
|
|
|
|
marshalled, err := bson.MarshalExtJSON(r, true, true)
|
|
|
|
if err != nil {
|
2023-02-11 20:57:52 +00:00
|
|
|
runtime.LogError(a.ctx, "Failed to marshal find BSON:")
|
|
|
|
runtime.LogError(a.ctx, err.Error())
|
2023-02-21 16:27:44 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Failed to marshal JSON"), zenity.ErrorIcon)
|
2023-01-24 19:55:53 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
out.Results = append(out.Results, string(marshalled))
|
|
|
|
}
|
|
|
|
|
2023-01-14 19:47:29 +00:00
|
|
|
return out
|
|
|
|
}
|
2023-06-02 20:33:43 +00:00
|
|
|
|
|
|
|
func (a *App) UpdateFoundDocument(hostKey, dbKey, collKey, idJson, newDocJson string) bool {
|
|
|
|
var id bson.M
|
|
|
|
if err := bson.UnmarshalExtJSON([]byte(idJson), true, &id); err != nil {
|
|
|
|
runtime.LogWarningf(a.ctx, "Could not parse find/update query: %s", err.Error())
|
|
|
|
zenity.Error(err.Error(), zenity.Title("Couldn't parse update query"), zenity.ErrorIcon)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var newDoc bson.M
|
|
|
|
if err := bson.UnmarshalExtJSON([]byte(newDocJson), true, &newDoc); err != nil {
|
|
|
|
runtime.LogWarningf(a.ctx, "Could not parse new find/update document: %s", err.Error())
|
|
|
|
zenity.Error(err.Error(), zenity.Title("Couldn't parse document"), zenity.ErrorIcon)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
client, ctx, close, err := a.connectToHost(hostKey)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
if _, err := client.Database(dbKey).Collection(collKey).UpdateOne(ctx, id, bson.M{"$set": newDoc}); err != nil {
|
|
|
|
runtime.LogInfof(a.ctx, "Error while performing find/update: %s", err.Error())
|
|
|
|
zenity.Error(err.Error(), zenity.Title("Unable to perform update"), zenity.ErrorIcon)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|