Files
mygo/internal/repository/session.go
Huxley 901a769ee7 Complete foundational data layer with repository implementation
- Add GORM dependencies for SQLite and PostgreSQL
- Create domain models (User, Session, File) with common errors
- Implement repository interfaces and database layer with migrations
- Update WebApp to bootstrap with database and repositories
- Add comprehensive unit tests for repository methods
- Update config structure to support multiple database drivers
- Extend AGENTS.md with debugging principles and dependency rules
2026-04-28 13:32:33 +08:00

90 lines
2.5 KiB
Go

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
}