mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-07-22 15:18:02 +00:00
Update items (needs refinement)
This commit is contained in:
94
internal/app/collection_update.go
Normal file
94
internal/app/collection_update.go
Normal file
@ -0,0 +1,94 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"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 {
|
||||
fmt.Println(err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.ErrorDialog,
|
||||
Title: "Couldn't parse form",
|
||||
Message: err.Error(),
|
||||
})
|
||||
return 0
|
||||
}
|
||||
|
||||
client, ctx, close, err := a.connectToHost(hostKey)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return 0
|
||||
}
|
||||
|
||||
defer close()
|
||||
var query bson.M
|
||||
update := bson.M{}
|
||||
|
||||
err = json.Unmarshal([]byte(form.Query), &query)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.ErrorDialog,
|
||||
Title: "Invalid query",
|
||||
Message: err.Error(),
|
||||
})
|
||||
return 0
|
||||
}
|
||||
|
||||
for _, param := range form.Parameters {
|
||||
var unmarshalled bson.M
|
||||
if json.Unmarshal([]byte(param.Value), &unmarshalled) == nil {
|
||||
update[param.Type] = unmarshalled
|
||||
} else {
|
||||
fmt.Println(err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.ErrorDialog,
|
||||
Title: "Invalid query",
|
||||
Message: err.Error(),
|
||||
})
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
var result *mongo.UpdateResult
|
||||
options := mongoOptions.Update().SetUpsert(form.Upsert)
|
||||
|
||||
fmt.Println(query, update)
|
||||
|
||||
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 {
|
||||
fmt.Println(err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.ErrorDialog,
|
||||
Title: "Encountered an error while updating items",
|
||||
Message: err.Error(),
|
||||
})
|
||||
return 0
|
||||
}
|
||||
|
||||
return result.ModifiedCount
|
||||
}
|
Reference in New Issue
Block a user