go-urlsh/internal/db/apikey.go

28 lines
542 B
Go
Raw Normal View History

2023-05-05 15:16:54 +02:00
package db
import (
"codeberg.org/lauralani/go-urlsh/models"
"context"
"log"
)
// IsApiKeyValid checks the database if ApiKey val is valid.
//
// Returns true if it's valid, false if not.
func IsApiKeyValid(val string) bool {
if val == "" {
return false
}
count, err := models.DB.NewSelect().Model((*models.ApiKey)(nil)).Where("key = ?", val).Count(context.Background())
if err != nil {
log.Printf("Error checking apikey validity for key %v\n", val)
return false
}
if count < 1 {
return false
} else {
return true
}
}