stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.3 KiB

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<?", time.Now())
return err
}