package Database
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
func (s Session) IsExpired() bool {
|
|
return s.Expiry.Before(time.Now())
|
|
}
|
|
|
|
type Session struct {
|
|
Base
|
|
UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;"`
|
|
User User
|
|
Expiry time.Time
|
|
}
|
|
|
|
// GetSessionByID Gets session
|
|
func GetSessionByID(id string) (Session, error) {
|
|
var (
|
|
session Session
|
|
err error
|
|
)
|
|
|
|
err = DB.Preload(clause.Associations).
|
|
First(&session, "id = ?", id).
|
|
Error
|
|
|
|
return session, err
|
|
}
|
|
|
|
// CreateSession creates session
|
|
func (session *Session) CreateSession() error {
|
|
var (
|
|
err error
|
|
)
|
|
|
|
err = DB.Create(session).Error
|
|
|
|
return err
|
|
}
|
|
|
|
// DeleteSession deletes session
|
|
func (session *Session) DeleteSession() error {
|
|
return DB.Delete(session).Error
|
|
}
|
|
|
|
// DeleteSessionByID deletes session
|
|
func DeleteSessionByID(id string) error {
|
|
return DB.Delete(
|
|
&Session{},
|
|
"id = ?",
|
|
id,
|
|
).Error
|
|
}
|