project.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // CreateProjectRole appends a role to the existing array of roles
  37. func (repo *ProjectRepository) CreateProjectRole(project *models.Project, role *models.Role) (*models.Role, error) {
  38. if !repo.canQuery || strings.Contains(repo.failingMethods, CreateProjectRoleMethod) {
  39. return nil, errors.New("Cannot write database")
  40. }
  41. if int(project.ID-1) >= len(repo.projects) || repo.projects[project.ID-1] == nil {
  42. return nil, gorm.ErrRecordNotFound
  43. }
  44. index := int(project.ID - 1)
  45. oldProject := *repo.projects[index]
  46. repo.projects[index] = project
  47. project.Roles = append(oldProject.Roles, *role)
  48. return role, nil
  49. }
  50. // CreateProjectRole appends a role to the existing array of roles
  51. func (repo *ProjectRepository) UpdateProjectRole(projID uint, role *models.Role) (*models.Role, error) {
  52. if !repo.canQuery {
  53. return nil, errors.New("Cannot read from database")
  54. }
  55. var foundProject *models.Project
  56. // find all roles matching
  57. for _, project := range repo.projects {
  58. if project.ID == projID {
  59. foundProject = project
  60. }
  61. }
  62. if foundProject == nil {
  63. return nil, gorm.ErrRecordNotFound
  64. }
  65. var index int
  66. for i, _role := range foundProject.Roles {
  67. if _role.UserID == role.UserID {
  68. index = i
  69. }
  70. }
  71. if index == 0 {
  72. return nil, gorm.ErrRecordNotFound
  73. }
  74. foundProject.Roles[index] = *role
  75. return role, nil
  76. }
  77. // ReadProject gets a projects specified by a unique id
  78. func (repo *ProjectRepository) ReadProjectRole(userID, projID uint) (*models.Role, error) {
  79. if !repo.canQuery {
  80. return nil, errors.New("Cannot read from database")
  81. }
  82. if int(projID-1) >= len(repo.projects) || repo.projects[projID-1] == nil {
  83. return nil, gorm.ErrRecordNotFound
  84. }
  85. // find role in project roles
  86. index := int(projID - 1)
  87. for _, role := range repo.projects[index].Roles {
  88. if role.UserID == userID {
  89. return &role, nil
  90. }
  91. }
  92. return nil, gorm.ErrRecordNotFound
  93. }
  94. // ReadProject gets a projects specified by a unique id
  95. func (repo *ProjectRepository) ReadProject(id uint) (*models.Project, error) {
  96. if !repo.canQuery || strings.Contains(repo.failingMethods, ReadProjectMethod) {
  97. return nil, errors.New("Cannot read from database")
  98. }
  99. if int(id-1) >= len(repo.projects) || repo.projects[id-1] == nil {
  100. return nil, gorm.ErrRecordNotFound
  101. }
  102. index := int(id - 1)
  103. return repo.projects[index], nil
  104. }
  105. // ListProjectsByUserID lists projects where a user has an associated role
  106. func (repo *ProjectRepository) ListProjectsByUserID(userID uint) ([]*models.Project, error) {
  107. if !repo.canQuery || strings.Contains(repo.failingMethods, ListProjectsByUserIDMethod) {
  108. return nil, errors.New("Cannot read from database")
  109. }
  110. resp := make([]*models.Project, 0)
  111. // find all roles matching
  112. for _, project := range repo.projects {
  113. for _, role := range project.Roles {
  114. if role.UserID == userID {
  115. resp = append(resp, project)
  116. }
  117. }
  118. }
  119. return resp, nil
  120. }
  121. // ListProjectRoles returns a list of roles for the project
  122. func (repo *ProjectRepository) ListProjectRoles(projID uint) ([]models.Role, error) {
  123. if !repo.canQuery {
  124. return nil, errors.New("Cannot read from database")
  125. }
  126. if int(projID-1) >= len(repo.projects) || repo.projects[projID-1] == nil {
  127. return nil, gorm.ErrRecordNotFound
  128. }
  129. index := int(projID - 1)
  130. repo.projects[index] = nil
  131. return repo.projects[index].Roles, nil
  132. }
  133. // DeleteProject removes a project
  134. func (repo *ProjectRepository) DeleteProject(project *models.Project) (*models.Project, error) {
  135. if !repo.canQuery {
  136. return nil, errors.New("Cannot write database")
  137. }
  138. if int(project.ID-1) >= len(repo.projects) || repo.projects[project.ID-1] == nil {
  139. return nil, gorm.ErrRecordNotFound
  140. }
  141. index := int(project.ID - 1)
  142. repo.projects[index] = nil
  143. return project, nil
  144. }
  145. func (repo *ProjectRepository) DeleteProjectRole(projID, userID uint) (*models.Role, error) {
  146. if !repo.canQuery {
  147. return nil, errors.New("Cannot write database")
  148. }
  149. var foundProject *models.Project
  150. // find all roles matching
  151. for _, project := range repo.projects {
  152. if project.ID == projID {
  153. foundProject = project
  154. }
  155. }
  156. if foundProject == nil {
  157. return nil, gorm.ErrRecordNotFound
  158. }
  159. var index int
  160. for i, _role := range foundProject.Roles {
  161. if _role.UserID == userID {
  162. index = i
  163. }
  164. }
  165. if index == 0 {
  166. return nil, gorm.ErrRecordNotFound
  167. }
  168. res := foundProject.Roles[index]
  169. foundProject.Roles = append(foundProject.Roles[:index], foundProject.Roles[index+1:]...)
  170. return &res, nil
  171. }