git_action_config.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "gorm.io/gorm"
  7. )
  8. // GitActionConfigRepository uses gorm.DB for querying the database
  9. type GitActionConfigRepository struct {
  10. canQuery bool
  11. gitActionConfigs []*models.GitActionConfig
  12. }
  13. func NewGitActionConfigRepository(canQuery bool) repository.GitActionConfigRepository {
  14. return &GitActionConfigRepository{
  15. canQuery,
  16. []*models.GitActionConfig{},
  17. }
  18. }
  19. // CreateGitActionConfig creates a new git repo
  20. func (repo *GitActionConfigRepository) CreateGitActionConfig(gac *models.GitActionConfig) (*models.GitActionConfig, error) {
  21. if !repo.canQuery {
  22. return nil, errors.New("Cannot write database")
  23. }
  24. repo.gitActionConfigs = append(repo.gitActionConfigs, gac)
  25. gac.ID = uint(len(repo.gitActionConfigs))
  26. return gac, nil
  27. }
  28. // ReadGitActionConfig gets a git repo specified by a unique id
  29. func (repo *GitActionConfigRepository) ReadGitActionConfig(id uint) (*models.GitActionConfig, error) {
  30. if !repo.canQuery {
  31. return nil, errors.New("Cannot read from database")
  32. }
  33. if int(id-1) >= len(repo.gitActionConfigs) || repo.gitActionConfigs[id-1] == nil {
  34. return nil, gorm.ErrRecordNotFound
  35. }
  36. index := int(id - 1)
  37. return repo.gitActionConfigs[index], nil
  38. }
  39. func (repo *GitActionConfigRepository) UpdateGitActionConfig(gac *models.GitActionConfig) error {
  40. if !repo.canQuery {
  41. return errors.New("Cannot write database")
  42. }
  43. if int(gac.ID-1) >= len(repo.gitActionConfigs) || repo.gitActionConfigs[gac.ID-1] == nil {
  44. return gorm.ErrRecordNotFound
  45. }
  46. index := int(gac.ID - 1)
  47. repo.gitActionConfigs[index] = gac
  48. return nil
  49. }