52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package scsusers
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
type Metadata struct {
|
|
MetaKey string `db:"meta_key"`
|
|
MetaValue string `db:"meta_value"`
|
|
}
|
|
|
|
func (u *UserData) Get(key string) (string, bool) {
|
|
tmp, ok := u.Meta[key]
|
|
return tmp.Value, ok
|
|
}
|
|
|
|
func (u *UserData) Delete(key string) {
|
|
tmp, ok := u.Meta[key]
|
|
if ok {
|
|
_, err := c.db.Query(fmt.Sprintf("delete from %s_meta where id=?", c.TablePrefix), tmp.ID)
|
|
if err != nil {
|
|
log.Printf("scsauth: set: delete: %s", err.Error())
|
|
}
|
|
delete(u.Meta, key)
|
|
}
|
|
}
|
|
|
|
func (u *UserData) Set(key, value string) error {
|
|
tmp, ok := u.Meta[key]
|
|
if ok {
|
|
_, err := c.db.Query(fmt.Sprintf("delete from %s_meta where id=?", c.TablePrefix), tmp.ID)
|
|
if err != nil {
|
|
log.Printf("scsauth: set: delete: %s", err.Error())
|
|
return err
|
|
}
|
|
}
|
|
var insertid int64
|
|
err := c.db.Get(&insertid, fmt.Sprintf("insert into %s_meta (user, meta_key, meta_value) VALUES (?,?,?) returning id", c.TablePrefix), u.UserID, key, value)
|
|
if err != nil {
|
|
log.Printf("scsauth: set: insert: %s", err.Error())
|
|
return err
|
|
}
|
|
var m metadata
|
|
m.Key = key
|
|
m.Value = value
|
|
m.ID = insertid
|
|
u.Meta[key] = m
|
|
|
|
return nil
|
|
}
|