Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package dbModels
import (
"database/sql"
"errors"
"github.com/zachmann/mytoken/internal/db"
)
type PollingCodeStatus struct {
Found bool
Expired bool
}
func (p *PollingCodeStatus) Scan(src interface{}) error {
if src == nil {
p.Found = false
return nil
}
val := src.(int64)
p.Found = true
if val == 0 {
p.Expired = true
}
return nil
}
func CheckPollingCode(pollingCode string) (p PollingCodeStatus, err error) {
if err = db.DB().Get(&p, `SELECT CURRENT_TIMESTAMP() <= expires_at AS valid FROM PollingCodes WHERE polling_code=?`, pollingCode); err != nil {
if errors.Is(err, sql.ErrNoRows) {
err = nil // polling code was not found, but this is fine
return // p.Found is false
}
return
}
return
}