Skip to content

Commit

Permalink
Redesigned UI preference backend storage
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarlslund committed Dec 12, 2024
1 parent 67bd5d8 commit 664bd25
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 119 deletions.
17 changes: 5 additions & 12 deletions modules/frontend/html/preferences.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let prefs = {};

function loadprefs() {
$.ajax({
return $.ajax({
url: "/api/preferences",
dataType: "json",
success: function (data) {
Expand Down Expand Up @@ -34,16 +34,6 @@ function onchangepreference(ele) {
} else {
setpref(ele.attr("preference"), ele.val())
}
saveprefs()
}

function saveprefs() {
$.ajax({
method: "POST",
url: "/api/preferences",
dataType: "json",
data: JSON.stringify(prefs),
});
}

function getpref(key, defvalue) {
Expand All @@ -59,7 +49,10 @@ function getpref(key, defvalue) {

function setpref(key, value) {
prefs[key] = value;
saveprefs();
$.ajax({
method: "GET",
url: `/api/preferences/${key}/${value}`,
});
}

$(function () {
Expand Down
64 changes: 64 additions & 0 deletions modules/frontend/preferences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package frontend

import (
"encoding/json"
"maps"

"github.com/gin-gonic/gin"
"github.com/lkarlslund/adalanche/modules/persistence"
)

type Preference struct {
Name string `json:"name"`
Value any `json:"value"`
}

func (p Preference) ID() string {
return p.Name
}

func AddPreferencesEndpoints(ws *WebService) {
// Saved preferences
sb := persistence.GetStorage[Preference]("preferences", false)

preferences := ws.API.Group("preferences")

preferences.GET("", func(c *gin.Context) {
prefs, err := sb.List()
if err != nil {
return
}
prefsmap := maps.Collect[string, any](func(yield func(string, any) bool) {
for _, pref := range prefs {
if !yield(pref.Name, pref.Value) {
break
}
}
})
c.JSON(200, prefsmap)
})
preferences.POST("", func(c *gin.Context) {
var prefsmap = make(map[string]any)
err := c.BindJSON(&prefsmap)
if err != nil {
c.String(500, err.Error())
}
for key, value := range prefsmap {
sb.Put(Preference{Name: key, Value: value})
}
})
preferences.GET(":key", func(c *gin.Context) {
key := c.Param("key")
pref, found := sb.Get(key)
if !found {
return
}
out, _ := json.Marshal(pref.Value)
c.Writer.Write(out)
})
preferences.GET(":key/:value", func(c *gin.Context) {
key := c.Param("key")
value := c.Param("value")
sb.Put(Preference{Name: key, Value: value})
})
}
35 changes: 0 additions & 35 deletions modules/frontend/webservicefuncs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package frontend

import (
"encoding/json"
"fmt"
"reflect"
"slices"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/lkarlslund/adalanche/modules/engine"
"github.com/lkarlslund/adalanche/modules/integrations/activedirectory"
"github.com/lkarlslund/adalanche/modules/query"
"github.com/lkarlslund/adalanche/modules/settings"
"github.com/lkarlslund/adalanche/modules/ui"
"github.com/lkarlslund/adalanche/modules/util"
"github.com/lkarlslund/adalanche/modules/version"
Expand Down Expand Up @@ -173,39 +171,6 @@ func AddUIEndpoints(ws *WebService) {
ws.quit <- true
})
}
func AddPreferencesEndpoints(ws *WebService) {
// Saved preferences
err := settings.Load()
if err != nil {
ui.Warn().Msgf("Problem loading preferences: %v", err)
}
preferences := ws.API.Group("preferences")
preferences.GET("", func(c *gin.Context) {
c.JSON(200, settings.All())
})
preferences.POST("", func(c *gin.Context) {
var prefsmap = make(map[string]any)
err := c.BindJSON(&prefsmap)
if err != nil {
c.String(500, err.Error())
}
for key, value := range prefsmap {
settings.Set(key, value)
}
settings.Save()
})
preferences.GET(":key", func(c *gin.Context) {
key := c.Param("key")
out, _ := json.Marshal(settings.Get(key))
c.Writer.Write(out)
})
preferences.GET(":key/:value", func(c *gin.Context) {
key := c.Param("key")
value := c.Param("value")
settings.Set(key, value)
settings.Save()
})
}
func AddDataEndpoints(ws *WebService) {
api := ws.API
// Returns JSON describing an object located by distinguishedName, sid or guid
Expand Down
19 changes: 10 additions & 9 deletions modules/persistence/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package persistence
import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/lkarlslund/adalanche/modules/cli"
"github.com/spf13/cobra"
"github.com/ugorji/go/codec"
"go.etcd.io/bbolt"
"os"
"path/filepath"
)

var (
Expand Down Expand Up @@ -62,18 +63,18 @@ type Identifiable interface {
type Defaulter interface {
Default()
}
type storage[i Identifiable] struct {
type Store[i Identifiable] struct {
db *bbolt.DB
cache map[string]i
bucketname []byte
}

func GetStorage[i Identifiable](bucketname string, cached bool) storage[i] {
func GetStorage[i Identifiable](bucketname string, cached bool) Store[i] {
db, err := getDB()
if err != nil {
panic(err) // FIXME
}
s := storage[i]{
s := Store[i]{
db: db,
bucketname: []byte(bucketname),
// cache: make(map[string]i),
Expand All @@ -83,7 +84,7 @@ func GetStorage[i Identifiable](bucketname string, cached bool) storage[i] {
}
return s
}
func (s storage[p]) Get(id string) (*p, bool) {
func (s Store[p]) Get(id string) (*p, bool) {
var result p
if s.cache != nil {
if rv, found := s.cache[string(id)]; found {
Expand Down Expand Up @@ -111,7 +112,7 @@ func (s storage[p]) Get(id string) (*p, bool) {
}
return nil, false
}
func (s storage[p]) Put(saveme p) error {
func (s Store[p]) Put(saveme p) error {
var output []byte
enc := codec.NewEncoderBytes(&output, &mh)
err := enc.Encode(saveme)
Expand Down Expand Up @@ -141,7 +142,7 @@ func (s storage[p]) Put(saveme p) error {
}
return nil
}
func (s storage[p]) Delete(id string) error {
func (s Store[p]) Delete(id string) error {
return s.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(s.bucketname))
if b == nil {
Expand All @@ -158,7 +159,7 @@ func (s storage[p]) Delete(id string) error {
return nil
})
}
func (s storage[p]) List() ([]p, error) {
func (s Store[p]) List() ([]p, error) {
var result []p
return result, s.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(s.bucketname))
Expand Down
63 changes: 0 additions & 63 deletions modules/settings/settings.go

This file was deleted.

0 comments on commit 664bd25

Please sign in to comment.