project.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. CreateProjectMethod string = "create_project_0"
  11. CreateProjectRoleMethod string = "create_project_role_0"
  12. ReadProjectMethod string = "read_project_0"
  13. ListProjectsByUserIDMethod string = "list_projects_by_user_id_0"
  14. )
  15. // ProjectRepository will return errors on queries if canQuery is false
  16. // and only stores a small set of projects in-memory that are indexed by their
  17. // array index + 1
  18. type ProjectRepository struct {
  19. canQuery bool
  20. failingMethods string
  21. projects []*models.Project
  22. }
  23. // NewProjectRepository will return errors if canQuery is false
  24. func NewProjectRepository(canQuery bool, failingMethods ...string) repository.ProjectRepository {
  25. return &ProjectRepository{canQuery, strings.Join(failingMethods, ","), []*models.Project{}}
  26. }
  27. // CreateProject appends a new project to the in-memory projects array
  28. func (repo *ProjectRepository) CreateProject(project *models.Project) (*models.Project, error) {
  29. if !repo.canQuery || strings.Contains(repo.failingMethods, CreateProjectMethod) {
  30. return nil, errors.New("Cannot write database")
  31. }
  32. repo.projects = append(repo.projects, project)
  33. project.ID = uint(len(repo.projects))
  34. return project, nil
  35. }
  36. func (repo *ProjectRepository) UpdateProject(project *models.Project) (*models.Project, error) {
  37. panic("unimplemented")
  38. }
  39. // ReadProject gets a projects specified by a unique id
  40. func (repo *ProjectRepository) ReadProject(id uint) (*models.Project, error) {
  41. if !repo.canQuery || strings.Contains(repo.failingMethods, ReadProjectMethod) {
  42. return nil, errors.New("Cannot read from database")
  43. }
  44. if int(id-1) >= len(repo.projects) || repo.projects[id-1] == nil {
  45. return nil, gorm.ErrRecordNotFound
  46. }
  47. index := int(id - 1)
  48. return repo.projects[index], nil
  49. }
  50. // ListProjectsByUserID lists projects where a user has an associated role
  51. func (repo *ProjectRepository) ListProjectsByUserID(userID uint) ([]*models.Project, error) {
  52. if !repo.canQuery || strings.Contains(repo.failingMethods, ListProjectsByUserIDMethod) {
  53. return nil, errors.New("Cannot read from database")
  54. }
  55. resp := make([]*models.Project, 0)
  56. // find all roles matching
  57. for _, project := range repo.projects {
  58. for _, role := range project.Roles {
  59. if role.UserID == userID {
  60. resp = append(resp, project)
  61. }
  62. }
  63. }
  64. return resp, nil
  65. }
  66. // ListProjectRoles returns a list of roles for the project
  67. func (repo *ProjectRepository) ListLegacyProjectRoles(projID uint) ([]models.Role, error) {
  68. if !repo.canQuery {
  69. return nil, errors.New("Cannot read from database")
  70. }
  71. if int(projID-1) >= len(repo.projects) || repo.projects[projID-1] == nil {
  72. return nil, gorm.ErrRecordNotFound
  73. }
  74. index := int(projID - 1)
  75. repo.projects[index] = nil
  76. return repo.projects[index].Roles, nil
  77. }
  78. // DeleteProject removes a project
  79. func (repo *ProjectRepository) DeleteProject(project *models.Project) (*models.Project, error) {
  80. if !repo.canQuery {
  81. return nil, errors.New("Cannot write database")
  82. }
  83. if int(project.ID-1) >= len(repo.projects) || repo.projects[project.ID-1] == nil {
  84. return nil, gorm.ErrRecordNotFound
  85. }
  86. index := int(project.ID - 1)
  87. repo.projects[index] = nil
  88. return project, nil
  89. }
  90. func (repo *ProjectRepository) DeleteLegacyProjectRole(projID, userID uint) (*models.Role, error) {
  91. if !repo.canQuery {
  92. return nil, errors.New("Cannot write database")
  93. }
  94. var foundProject *models.Project
  95. // find all roles matching
  96. for _, project := range repo.projects {
  97. if project.ID == projID {
  98. foundProject = project
  99. }
  100. }
  101. if foundProject == nil {
  102. return nil, gorm.ErrRecordNotFound
  103. }
  104. var index int
  105. for i, _role := range foundProject.Roles {
  106. if _role.UserID == userID {
  107. index = i
  108. }
  109. }
  110. if index == 0 {
  111. return nil, gorm.ErrRecordNotFound
  112. }
  113. res := foundProject.Roles[index]
  114. foundProject.Roles = append(foundProject.Roles[:index], foundProject.Roles[index+1:]...)
  115. return &res, nil
  116. }