package mysqldriver import ( "context" "database/sql" "git.aiterp.net/stufflog/server/internal/xlerrors" "git.aiterp.net/stufflog/server/models" "github.com/jmoiron/sqlx" "time" ) type sessionRepository struct { db *sqlx.DB } func (r *sessionRepository) Find(ctx context.Context, id string) (*models.Session, error) { session := models.Session{} err := r.db.GetContext(ctx, &session, "SELECT * FROM session WHERE session_id=?", id) if err != nil { if err == sql.ErrNoRows { return nil, xlerrors.NotFound("Session") } return nil, err } else if time.Now().After(session.ExpiryTime) { return nil, xlerrors.NotFound("Session") } return &session, nil } func (r *sessionRepository) Save(ctx context.Context, session models.Session) error { _, err := r.db.NamedExecContext(ctx, ` REPLACE INTO session (session_id, user_id, expiry_time) VALUES (:session_id, :user_id, :expiry_time) `, session) return err } func (r *sessionRepository) Delete(ctx context.Context, session models.Session) error { _, err := r.db.ExecContext(ctx, "DELETE FROM session WHERE session_id=?", session.ID) return err } func (r *sessionRepository) DeleteExpired(ctx context.Context) error { _, err := r.db.ExecContext(ctx, "DELETE FROM session WHERE expiry_time