gitrepo.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // GitRepoRepository implements repository.GitRepoRepository
  9. type GitRepoRepository struct {
  10. canQuery bool
  11. gitRepos []*models.GitRepo
  12. }
  13. // NewGitRepoRepository will return errors if canQuery is false
  14. func NewGitRepoRepository(canQuery bool) repository.GitRepoRepository {
  15. return &GitRepoRepository{
  16. canQuery,
  17. []*models.GitRepo{},
  18. }
  19. }
  20. // CreateGitRepo creates a new repo client and appends it to the in-memory list
  21. func (repo *GitRepoRepository) CreateGitRepo(gr *models.GitRepo) (*models.GitRepo, error) {
  22. if !repo.canQuery {
  23. return nil, errors.New("Cannot write database")
  24. }
  25. repo.gitRepos = append(repo.gitRepos, gr)
  26. gr.ID = uint(len(repo.gitRepos))
  27. return gr, nil
  28. }
  29. // ReadGitRepo returns a repo client by id
  30. func (repo *GitRepoRepository) ReadGitRepo(id uint) (*models.GitRepo, error) {
  31. if !repo.canQuery {
  32. return nil, errors.New("Cannot read from database")
  33. }
  34. if int(id-1) >= len(repo.gitRepos) || repo.gitRepos[id-1] == nil {
  35. return nil, gorm.ErrRecordNotFound
  36. }
  37. index := int(id - 1)
  38. return repo.gitRepos[index], nil
  39. }
  40. // ListGitReposByProjectID returns a list of repo clients that match a project id
  41. func (repo *GitRepoRepository) ListGitReposByProjectID(projectID uint) ([]*models.GitRepo, error) {
  42. if !repo.canQuery {
  43. return nil, errors.New("Cannot read from database")
  44. }
  45. res := make([]*models.GitRepo, 0)
  46. for _, gr := range repo.gitRepos {
  47. if gr != nil && gr.ProjectID == projectID {
  48. res = append(res, gr)
  49. }
  50. }
  51. return res, nil
  52. }
  53. // UpdateGitRepo modifies an existing GitRepo in the database
  54. func (repo *GitRepoRepository) UpdateGitRepo(
  55. gr *models.GitRepo,
  56. ) (*models.GitRepo, error) {
  57. if !repo.canQuery {
  58. return nil, errors.New("Cannot write database")
  59. }
  60. if int(gr.ID-1) >= len(repo.gitRepos) || repo.gitRepos[gr.ID-1] == nil {
  61. return nil, gorm.ErrRecordNotFound
  62. }
  63. index := int(gr.ID - 1)
  64. repo.gitRepos[index] = gr
  65. return gr, nil
  66. }
  67. // DeleteGitRepo removes a repoistry from the array by setting it to nil
  68. func (repo *GitRepoRepository) DeleteGitRepo(
  69. gr *models.GitRepo,
  70. ) error {
  71. if !repo.canQuery {
  72. return errors.New("Cannot write database")
  73. }
  74. if int(gr.ID-1) >= len(repo.gitRepos) || repo.gitRepos[gr.ID-1] == nil {
  75. return gorm.ErrRecordNotFound
  76. }
  77. index := int(gr.ID - 1)
  78. repo.gitRepos[index] = nil
  79. return nil
  80. }