1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 21:17:59 +00:00
rolens/internal/app/views.go

156 lines
3.7 KiB
Go
Raw Normal View History

2023-01-23 19:47:43 +00:00
package app
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type ViewType string
2023-01-29 19:00:15 +00:00
type InputType string
2023-01-23 19:47:43 +00:00
const (
TableView ViewType = "table"
ListView ViewType = "list"
2023-01-29 19:00:15 +00:00
NoInput InputType = "none"
StringInput InputType = "string"
ObjectIdInput InputType = "objectid"
IntegerInput InputType = "int"
LongInput InputType = "long"
Uint64Input InputType = "uint64"
DoubleInput InputType = "double"
DecimalInput InputType = "decimal"
BoolInput InputType = "bool"
DateInput InputType = "date"
2023-01-23 19:47:43 +00:00
)
type ViewColumn struct {
2023-01-29 19:00:15 +00:00
Key string `json:"key"`
Width int64 `json:"width"`
ShowInTable bool `json:"showInTable"`
Mandatory bool `json:"mandatory"`
InputType InputType `json:"inputType"`
2023-01-23 19:47:43 +00:00
}
type View struct {
Name string `json:"name"`
Host string `json:"host"`
Database string `json:"database"`
Collection string `json:"collection"`
Type ViewType `json:"type"`
HideObjectIndicators bool `json:"hideObjectIndicators"`
Columns []ViewColumn `json:"columns"`
}
var BuiltInListView = View{
Name: "List",
Type: ListView,
}
type ViewStore map[string]View
func updateViewStore(newData ViewStore) error {
filePath, err := appDataFilePath("views.json")
if err != nil {
return err
}
jsonData, err := json.MarshalIndent(newData, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, jsonData, os.ModePerm)
return err
}
func (a *App) Views() (ViewStore, error) {
views := make(ViewStore, 0)
filePath, err := appDataFilePath("views.json")
if err != nil {
fmt.Println(err.Error())
return views, err
}
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.
fmt.Println(err.Error())
return views, nil
}
if len(jsonData) > 0 {
err = json.Unmarshal(jsonData, &views)
if err != nil {
fmt.Println(err.Error())
return nil, errors.New("views.json file contains malformatted JSON data")
}
}
views["list"] = BuiltInListView
return views, nil
}
func (a *App) UpdateViewStore(jsonData string) error {
var viewStore ViewStore
err := json.Unmarshal([]byte(jsonData), &viewStore)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Malformed JSON",
})
return errors.New("invalid JSON")
}
err = updateViewStore(viewStore)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not update view store",
})
return errors.New("could not update view store")
}
return nil
}
func (a *App) RemoveView(viewKey string) error {
views, err := a.Views()
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not retrieve views",
})
return errors.New("could not retrieve existing view store")
}
sure, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Confirm",
Message: "Are you sure you want to remove " + views[viewKey].Name + "?",
Buttons: []string{"Yes", "No"},
DefaultButton: "Yes",
CancelButton: "No",
})
if sure != "Yes" {
return errors.New("operation aborted")
}
delete(views, viewKey)
err = updateViewStore(views)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not update view store",
})
return errors.New("could not update view store")
}
return nil
}