2021-10-18 08:31:17 +00:00
|
|
|
package administrator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) {
|
|
|
|
var query = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
|
|
|
|
2021-10-30 03:24:53 +00:00
|
|
|
c, err := db.Acquire(ctx)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-10-30 03:24:53 +00:00
|
|
|
defer c.Release()
|
2021-10-18 08:31:17 +00:00
|
|
|
|
2021-11-08 09:58:41 +00:00
|
|
|
tx, err := c.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback(ctx)
|
|
|
|
|
2021-10-18 08:31:17 +00:00
|
|
|
sql, args, err := query.
|
|
|
|
Update("administrators").
|
|
|
|
Set("last_used", time.Now().UTC().Format(time.RFC3339)).
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-11-08 09:58:41 +00:00
|
|
|
_, err = tx.Exec(ctx, sql, args...)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err = query.
|
|
|
|
Select("id").
|
|
|
|
From("administrators").
|
|
|
|
Where(squirrel.Eq{"key": key}).
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var id int
|
2021-11-08 09:58:41 +00:00
|
|
|
err = tx.QueryRow(ctx, sql, args...).Scan(&id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit(ctx)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|