package repository import ( "context" "errors" "time" "gorm.io/gorm" "github.com/dhao2001/mygo/internal/model" ) // SessionRepository provides access to refresh token sessions. type SessionRepository interface { Create(ctx context.Context, session *model.Session) error FindByID(ctx context.Context, id string) (*model.Session, error) FindByTokenHash(ctx context.Context, tokenHash string) (*model.Session, error) Delete(ctx context.Context, id string) error DeleteByUserID(ctx context.Context, userID string) error DeleteExpired(ctx context.Context) (int64, error) } type sessionRepository struct { db *gorm.DB } // NewSessionRepository creates a SessionRepository backed by GORM. func NewSessionRepository(db *gorm.DB) SessionRepository { return &sessionRepository{db: db} } func (r *sessionRepository) Create(ctx context.Context, session *model.Session) error { result := r.db.WithContext(ctx).Create(session) if result.Error != nil { if isDuplicateKeyError(result.Error) { return model.ErrDuplicate } return result.Error } return nil } func (r *sessionRepository) FindByID(ctx context.Context, id string) (*model.Session, error) { var session model.Session result := r.db.WithContext(ctx).First(&session, "id = ?", id) if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, model.ErrNotFound } if result.Error != nil { return nil, result.Error } return &session, nil } func (r *sessionRepository) FindByTokenHash(ctx context.Context, tokenHash string) (*model.Session, error) { var session model.Session result := r.db.WithContext(ctx).First(&session, "token_hash = ?", tokenHash) if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, model.ErrNotFound } if result.Error != nil { return nil, result.Error } return &session, nil } func (r *sessionRepository) Delete(ctx context.Context, id string) error { result := r.db.WithContext(ctx).Delete(&model.Session{}, "id = ?", id) if result.Error != nil { return result.Error } return nil } func (r *sessionRepository) DeleteByUserID(ctx context.Context, userID string) error { result := r.db.WithContext(ctx).Delete(&model.Session{}, "user_id = ?", userID) if result.Error != nil { return result.Error } return nil } func (r *sessionRepository) DeleteExpired(ctx context.Context) (int64, error) { result := r.db.WithContext(ctx).Delete(&model.Session{}, "expires_at < ?", time.Now()) if result.Error != nil { return 0, result.Error } return result.RowsAffected, nil }