RestApiv2/internal/bd/tokenrepo.go

63 lines
1.3 KiB
Go

package bd
import (
"context"
"fmt"
)
type Tokenrepo struct {
db Bd
}
func (t *Tokenrepo) Create(login string, refreshToken string) error {
sql := fmt.Sprint("INSERT INTO tokens (login, refreshtoken) VALUES($1, $2)")
_, err := t.db.db.Exec(context.Background(), sql, login, refreshToken)
if err != nil {
return err
}
return nil
}
func (t *Tokenrepo) FindByLogin(login string) (*string, error) {
var refreshToken string
sql := fmt.Sprint("Select refreshtoken from tokens WHERE login = $1")
rows, err := t.db.db.Query(context.Background(), sql, login)
if err != nil {
return nil, err
}
for rows.Next() {
err = rows.Scan(&refreshToken)
if err != nil {
return nil, err
}
}
return &refreshToken, nil
}
func (t *Tokenrepo) FindByToken(token string) (*string, error) {
var login string
sql := fmt.Sprint("Select login from tokens WHERE refreshtoken = $1")
rows, err := t.db.db.Query(context.Background(), sql, token)
if err != nil {
return nil, err
}
for rows.Next() {
err = rows.Scan(&login)
if err != nil {
return nil, err
}
}
return &login, nil
}
func (t *Tokenrepo) DeleteByLogin(login string) error {
sql := fmt.Sprint("DELETE FROM tokens WHERE login = $1")
_, err := t.db.db.Exec(context.Background(), sql, login)
if err != nil {
return err
}
return err
}