Add metadata handling

This commit is contained in:
scs 2019-07-23 23:34:54 +00:00
parent c7003f5c80
commit 1908474e2d
1 changed files with 55 additions and 0 deletions

55
main.go
View File

@ -191,6 +191,61 @@ func Bump(username string) {
}
}
type Metadata struct {
MetaKey string `db:meta_key`
MetaValue string `db:meta_value`
}
func GetAllMeta(username string) (map[string]string) {
meta:=make(map[string]string)
q:=fmt.Sprintf(`select meta_key, meta_value
from %s_user_metadata where
user_id=(select userid from %s_auth where username=?)`,
c.TablePrefix, c.TablePrefix)
rows,err:=c.db.Queryx(q, username)
if err != nil {
log.Printf("scsusers.GetAllMeta: %s: %s\n", username, err.Error())
return meta
}
var m Metadata
for rows.Next() {
err=rows.StructScan(&m)
if err != nil {
log.Printf("scsusers.GetAllMeta: StructScan: %s\n", username, err.Error())
return meta
}
meta[m.MetaKey]=m.MetaValue
}
return meta
}
func GetMeta(username string, metakey string) string {
var v string
q:=fmt.Sprintf(`select meta_value from %s_user_metadata where
user_id=(select userid from %s_auth where username=?) AND meta_key=?`, c.TablePrefix, c.TablePrefix)
err:=c.db.Get(&v, q, username, metakey)
if err != nil {
log.Printf("scsusers.GetMeta: %s - %s - %s\n", username, metakey, err.Error())
}
return v
}
func SetMeta(username string, metakey string, metavalue string) {
var err error
if metavalue=="" {
q:=fmt.Sprintf(`delete from %s_user_metadata where user_id=(select userid from %s_auth where username=?) AND meta_key=?`,
c.TablePrefix, c.TablePrefix)
_, err=c.db.Exec(q, username, metakey)
} else {
q:=fmt.Sprintf(`insert into %s_user_metadata (user_id, meta_key, meta_value) VALUES
((select userid from %s_auth where username=?), ?, ?)`, c.TablePrefix, c.TablePrefix)
_,err=c.db.Exec(q, username, metakey, metavalue)
}
if err != nil {
log.Printf("scsusers.SetMeta: %s %s %s %s\n", username, metakey, metavalue, err.Error())
}
}
func GetRoles(username string) []string {
var roles []string
q:=fmt.Sprintf(`select role_name from %s_roles