session.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package test
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/internal/repository"
  7. "gorm.io/gorm"
  8. )
  9. const (
  10. CreateSessionMethod string = "create_session_0"
  11. SelectSessionMethod string = "select_session_0"
  12. )
  13. // SessionRepository uses gorm.DB for querying the database
  14. type SessionRepository struct {
  15. canQuery bool
  16. failingMethods string
  17. sessions []*models.Session
  18. }
  19. // NewSessionRepository returns pointer to repo along with the db
  20. func NewSessionRepository(canQuery bool, failingMethods ...string) repository.SessionRepository {
  21. return &SessionRepository{canQuery, strings.Join(failingMethods, ","), []*models.Session{}}
  22. }
  23. // CreateSession must take in Key, Data, and ExpiresAt as arguments.
  24. func (repo *SessionRepository) CreateSession(session *models.Session) (*models.Session, error) {
  25. if !repo.canQuery || strings.Contains(repo.failingMethods, CreateSessionMethod) {
  26. return nil, errors.New("Cannot write database")
  27. }
  28. // make sure key doesn't exist
  29. for _, s := range repo.sessions {
  30. if s.Key == session.Key {
  31. return nil, errors.New("Cannot write database")
  32. }
  33. }
  34. sessions := repo.sessions
  35. sessions = append(sessions, session)
  36. repo.sessions = sessions
  37. session.ID = uint(len(repo.sessions))
  38. return session, nil
  39. }
  40. // UpdateSession updates only the Data field using Key as selector.
  41. func (repo *SessionRepository) UpdateSession(session *models.Session) (*models.Session, error) {
  42. if !repo.canQuery {
  43. return nil, errors.New("Cannot write database")
  44. }
  45. var oldSession *models.Session
  46. for _, s := range repo.sessions {
  47. if s.Key == session.Key {
  48. oldSession = s
  49. }
  50. }
  51. if oldSession != nil {
  52. oldSession.Data = session.Data
  53. return oldSession, nil
  54. }
  55. return nil, gorm.ErrRecordNotFound
  56. }
  57. // DeleteSession deletes a session by Key
  58. func (repo *SessionRepository) DeleteSession(session *models.Session) (*models.Session, error) {
  59. if !repo.canQuery {
  60. return nil, errors.New("Cannot write database")
  61. }
  62. if int(session.ID-1) >= len(repo.sessions) || repo.sessions[session.ID-1] == nil {
  63. return nil, gorm.ErrRecordNotFound
  64. }
  65. index := int(session.ID - 1)
  66. repo.sessions[index] = nil
  67. return session, nil
  68. }
  69. // SelectSession returns a session with matching key
  70. func (repo *SessionRepository) SelectSession(session *models.Session) (*models.Session, error) {
  71. if !repo.canQuery || strings.Contains(repo.failingMethods, SelectSessionMethod) {
  72. return nil, errors.New("Cannot write database")
  73. }
  74. for _, s := range repo.sessions {
  75. if s.Key == session.Key {
  76. return s, nil
  77. }
  78. }
  79. return nil, gorm.ErrRecordNotFound
  80. }