Notes/key.go

70 lines
1.3 KiB
Go
Raw Normal View History

2023-07-01 20:33:26 +02:00
package main
import (
// External
"github.com/jmoiron/sqlx"
)
type Key struct {
ID int
UserID int `db:"user_id"`
Description string
Key string
}
2024-01-05 20:00:02 +01:00
func Keys(userID int) (keys []Key, err error) { // {{{
2023-07-01 20:33:26 +02:00
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
if rows, err = service.Db.Conn.Queryx(`SELECT * FROM crypto_key WHERE user_id=$1`, userID); err != nil {
2023-07-01 20:33:26 +02:00
return
}
defer rows.Close()
keys = []Key{}
for rows.Next() {
key := Key{}
if err = rows.StructScan(&key); err != nil {
return
}
keys = append(keys, key)
}
return
2024-01-05 20:00:02 +01:00
} // }}}
func KeyCreate(userID int, description, keyEncoded string) (key Key, err error) { // {{{
2023-07-12 22:35:38 +02:00
var row *sqlx.Rows
2024-01-05 20:00:02 +01:00
if row, err = service.Db.Conn.Queryx(
2023-07-12 22:35:38 +02:00
`INSERT INTO crypto_key(user_id, description, key)
VALUES($1, $2, $3)
RETURNING *`,
2024-01-05 20:00:02 +01:00
userID,
2023-07-12 22:35:38 +02:00
description,
keyEncoded,
); err != nil {
return
}
defer row.Close()
key = Key{}
for row.Next() {
if err = row.StructScan(&key); err != nil {
return
}
}
return
2024-01-05 20:00:02 +01:00
} // }}}
func KeyCounter() (counter int64, err error) { // {{{
2023-07-12 22:35:38 +02:00
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.Queryx(`SELECT nextval('aes_ccm_counter') AS counter`)
2023-07-12 22:35:38 +02:00
if err != nil {
return
}
defer rows.Close()
rows.Next()
err = rows.Scan(&counter)
return
2024-01-05 20:00:02 +01:00
} // }}}
2023-07-12 22:35:38 +02:00
// vim: foldmethod=marker