tiny-url-ex/service/auth/model.go

71 lines
1.5 KiB
Go

package auth
import (
"database/sql"
"time"
)
// tokenModel represents an actual tiny url entry in the database.
type tokenModel struct {
db *sql.DB
token string
role string
expires int64
}
type repository struct {
db *sql.DB
}
// newRepo returns an initialized repository.
func newRepo(db *sql.DB) (*repository, error) {
_, err := db.Exec(`CREATE TABLE if not exists token(
token varchar(128) PRIMARY KEY,
role varchar(300) NOT NULL,
expires INT NOT NULL
);`)
return &repository{db}, err
}
func (repo *repository) NewModel(token, role string, duration time.Duration) *tokenModel {
return &tokenModel{
db: repo.db,
token: token,
role: role,
expires: time.Now().Add(duration).Unix(),
}
}
func (mod *tokenModel) Search() error {
row := mod.db.QueryRow(`SELECT token, role, expires FROM token WHERE token = $1 LIMIT 1;`, mod.token)
receiver := &tokenModel{}
err := row.Scan(&receiver.token, &receiver.role, &receiver.expires)
if err != nil {
return err
}
// delete if expired
if receiver.expires < time.Now().Unix() {
receiver.db = mod.db
receiver.Delete()
return sql.ErrNoRows
}
mod.token = receiver.token
mod.role = receiver.role
mod.expires = receiver.expires
return nil
}
func (mod *tokenModel) Create() error {
_, err := mod.db.Exec(`INSERT INTO token (token, role, expires) VALUES ($1, $2, $3);`, mod.token, mod.role, mod.expires)
return err
}
func (mod *tokenModel) Delete() error {
_, err := mod.db.Exec(`DELETE FROM token WHERE token = $1;`, mod.token)
return err
}