70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
// External
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
type Key struct {
|
|
ID int
|
|
UserID int `db:"user_id"`
|
|
Description string
|
|
Key string
|
|
}
|
|
|
|
func Keys(userID int) (keys []Key, err error) { // {{{
|
|
var rows *sqlx.Rows
|
|
if rows, err = service.Db.Conn.Queryx(`SELECT * FROM crypto_key WHERE user_id=$1`, userID); err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
keys = []Key{}
|
|
for rows.Next() {
|
|
key := Key{}
|
|
if err = rows.StructScan(&key); err != nil {
|
|
return
|
|
}
|
|
keys = append(keys, key)
|
|
}
|
|
|
|
return
|
|
} // }}}
|
|
func KeyCreate(userID int, description, keyEncoded string) (key Key, err error) { // {{{
|
|
var row *sqlx.Rows
|
|
if row, err = service.Db.Conn.Queryx(
|
|
`INSERT INTO crypto_key(user_id, description, key)
|
|
VALUES($1, $2, $3)
|
|
RETURNING *`,
|
|
userID,
|
|
description,
|
|
keyEncoded,
|
|
); err != nil {
|
|
return
|
|
}
|
|
defer row.Close()
|
|
|
|
key = Key{}
|
|
for row.Next() {
|
|
if err = row.StructScan(&key); err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
return
|
|
} // }}}
|
|
func KeyCounter() (counter int64, err error) { // {{{
|
|
var rows *sqlx.Rows
|
|
rows, err = service.Db.Conn.Queryx(`SELECT nextval('aes_ccm_counter') AS counter`)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
rows.Next()
|
|
err = rows.Scan(&counter)
|
|
return
|
|
} // }}}
|
|
|
|
// vim: foldmethod=marker
|