1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2024-11-24 18:26:56 +01:00
rolens/internal/app/collection_update.go

92 lines
2.4 KiB
Go
Raw Normal View History

2023-01-18 11:22:02 +01:00
package app
import (
"encoding/json"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
)
func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 {
var form struct {
Upsert bool `json:"upsert"`
Many bool `json:"many"`
Query string `json:"query"`
Parameters []struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"parameters"`
}
err := json.Unmarshal([]byte(formJson), &form)
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.LogErrorf(a.ctx, "Could not parse update form: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Malformed JSON",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-01-18 11:22:02 +01:00
return 0
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return 0
}
defer close()
var query bson.M
update := bson.M{}
err = bson.UnmarshalExtJSON([]byte(form.Query), true, &query)
2023-01-18 11:22:02 +01:00
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.LogWarningf(a.ctx, "Invalid update query %v: %s", form.Query, err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Invalid update query",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-01-18 11:22:02 +01:00
return 0
}
for _, param := range form.Parameters {
var unmarshalled bson.M
err = json.Unmarshal([]byte(param.Value), &unmarshalled)
if err == nil {
2023-01-18 11:22:02 +01:00
update[param.Type] = unmarshalled
} else {
2023-06-24 20:27:48 +02:00
runtime.LogWarningf(a.ctx, "Invalid update parameter value %v: %s", param.Value, err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Invalid update query",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-01-18 11:22:02 +01:00
return 0
}
}
var result *mongo.UpdateResult
options := mongoOptions.Update().SetUpsert(form.Upsert)
if form.Many {
result, err = client.Database(dbKey).Collection(collKey).UpdateMany(ctx, query, update, options)
} else {
result, err = client.Database(dbKey).Collection(collKey).UpdateOne(ctx, query, update, options)
}
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.LogWarningf(a.ctx, "Encountered an error while performing update: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error performing update query",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-01-18 11:22:02 +01:00
return 0
}
return result.ModifiedCount
}