From 1c12dcabc07b6f9d2fa61a8f22bbf9525dd1394a Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Wed, 18 Jan 2023 11:22:02 +0100 Subject: [PATCH] Update items (needs refinement) --- frontend/src/app.svelte | 2 + .../connection/collection/index.svelte | 2 + .../connection/collection/update.svelte | 160 ++++++++++++++++++ frontend/src/reset.css | 7 +- frontend/src/style.css | 17 +- frontend/wailsjs/go/app/App.d.ts | 2 + frontend/wailsjs/go/app/App.js | 4 + internal/app/collection_update.go | 94 ++++++++++ 8 files changed, 281 insertions(+), 7 deletions(-) create mode 100644 frontend/src/organisms/connection/collection/update.svelte create mode 100644 internal/app/collection_update.go diff --git a/frontend/src/app.svelte b/frontend/src/app.svelte index 3f1d28b..6ea4164 100644 --- a/frontend/src/app.svelte +++ b/frontend/src/app.svelte @@ -44,6 +44,8 @@ }); + +
diff --git a/frontend/src/organisms/connection/collection/index.svelte b/frontend/src/organisms/connection/collection/index.svelte index 723ed3d..f4dac9b 100644 --- a/frontend/src/organisms/connection/collection/index.svelte +++ b/frontend/src/organisms/connection/collection/index.svelte @@ -7,6 +7,7 @@ import Insert from './insert.svelte'; import Remove from './remove.svelte'; import Stats from './stats.svelte'; + import Update from './update.svelte'; export let collection; export let hostKey; @@ -45,6 +46,7 @@ {#if tab === 'stats'} {:else if tab === 'find'} {:else if tab === 'insert'} + {:else if tab === 'update'} {:else if tab === 'remove'} {:else if tab === 'indexes'} {/if} diff --git a/frontend/src/organisms/connection/collection/update.svelte b/frontend/src/organisms/connection/collection/update.svelte new file mode 100644 index 0000000..e26c87d --- /dev/null +++ b/frontend/src/organisms/connection/collection/update.svelte @@ -0,0 +1,160 @@ + + +
+ + +
+ + + +
+ + + +
+ {#each form.parameters as param, index} +
+ + + + + + +
+ {/each} +
+ +
+
+ +
+ + + diff --git a/frontend/src/reset.css b/frontend/src/reset.css index c96057d..9a526a9 100644 --- a/frontend/src/reset.css +++ b/frontend/src/reset.css @@ -59,13 +59,12 @@ button { } /* Inputs */ -input { +input, +textarea, +select { font: inherit; } input:not([type="checkbox"]) { min-width: 100px; width: 100%; } -textarea { - font: inherit; -} diff --git a/frontend/src/style.css b/frontend/src/style.css index 9de9e59..722fef8 100644 --- a/frontend/src/style.css +++ b/frontend/src/style.css @@ -72,17 +72,20 @@ p strong { border-bottom-right-radius: 10px; } .field > input, -.field > textarea { +.field > textarea, +.field > select { flex: 1; } +.field > input:focus, .field > textarea:focus, -.field > input:focus { +.field > select:focus { outline: none; border-color: #00008b; box-shadow: 0 0 0 3px rgba(0, 0, 139, 0.2); } .field > input.invalid, -.field > textarea.invalid { +.field > textarea.invalid, +.field > select.invalid { background-color: rgba(255, 80, 80, 0.3); border-color: rgb(255, 80, 80); } @@ -90,6 +93,14 @@ p strong { text-align: center; min-width: 75px; } +.field > select { + appearance: none; + padding: 0.5rem 2rem 0.5rem 0.5rem; + background: #fff; + background-image: url("data:image/svg+xml, %3Csvg xmlns='http://www.w3.org/2000/svg' width='15' height='15' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E"); + background-repeat: no-repeat; + background-position: right 0.5rem center; +} .btn { background-color: #00008b; diff --git a/frontend/wailsjs/go/app/App.d.ts b/frontend/wailsjs/go/app/App.d.ts index 71a6f40..565b178 100755 --- a/frontend/wailsjs/go/app/App.d.ts +++ b/frontend/wailsjs/go/app/App.d.ts @@ -25,3 +25,5 @@ export function OpenConnection(arg1:string):Promise>; export function OpenDatabase(arg1:string,arg2:string):Promise>; export function RemoveItems(arg1:string,arg2:string,arg3:string,arg4:string,arg5:boolean):Promise; + +export function UpdateItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise; diff --git a/frontend/wailsjs/go/app/App.js b/frontend/wailsjs/go/app/App.js index 0951f40..6885d66 100755 --- a/frontend/wailsjs/go/app/App.js +++ b/frontend/wailsjs/go/app/App.js @@ -45,3 +45,7 @@ export function OpenDatabase(arg1, arg2) { export function RemoveItems(arg1, arg2, arg3, arg4, arg5) { return window['go']['app']['App']['RemoveItems'](arg1, arg2, arg3, arg4, arg5); } + +export function UpdateItems(arg1, arg2, arg3, arg4) { + return window['go']['app']['App']['UpdateItems'](arg1, arg2, arg3, arg4); +} diff --git a/internal/app/collection_update.go b/internal/app/collection_update.go new file mode 100644 index 0000000..707eafa --- /dev/null +++ b/internal/app/collection_update.go @@ -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 +}