The main server, and probably only repository in this org.
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.

62 lines
1.5 KiB

  1. package sqlite
  2. import (
  3. "context"
  4. "git.aiterp.net/lucifer/lucifer/models"
  5. )
  6. // SessionRepository is a sqlite database.
  7. var SessionRepository = &sessionRepository{}
  8. type sessionRepository struct{}
  9. func (r *sessionRepository) FindByID(ctx context.Context, id string) (models.Session, error) {
  10. row := db.QueryRowxContext(ctx, "SELECT * FROM session WHERE id=?", id)
  11. if err := row.Err(); err != nil {
  12. return models.Session{}, err
  13. }
  14. session := models.Session{}
  15. if err := row.StructScan(&session); err != nil {
  16. return models.Session{}, err
  17. }
  18. return session, nil
  19. }
  20. func (r *sessionRepository) Insert(ctx context.Context, session models.Session) error {
  21. _, err := db.NamedExecContext(ctx, "INSERT INTO session (id, user_id, expire_date) VALUES(:id, :user_id, :expire_date)", session)
  22. if err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. func (r *sessionRepository) Update(ctx context.Context, session models.Session) error {
  28. _, err := db.NamedExecContext(ctx, "UPDATE session SET user_id=:user_id AND expire_date=:expire_date", session)
  29. if err != nil {
  30. return err
  31. }
  32. return nil
  33. }
  34. func (r *sessionRepository) Remove(ctx context.Context, session models.Session) error {
  35. _, err := db.NamedExecContext(ctx, "DELETE FROM session WHERE id=:id", session)
  36. if err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. func (r *sessionRepository) Clear(ctx context.Context, user models.User) error {
  42. _, err := db.NamedExecContext(ctx, "DELETE FROM session WHERE user_id=:id", user)
  43. if err != nil {
  44. return err
  45. }
  46. return nil
  47. }