project.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // ProjectRepository will return errors on queries if canQuery is false
  9. // and only stores a small set of projects in-memory that are indexed by their
  10. // array index + 1
  11. type ProjectRepository struct {
  12. canQuery bool
  13. projects []*models.Project
  14. }
  15. // NewProjectRepository will return errors if canQuery is false
  16. func NewProjectRepository(canQuery bool) repository.ProjectRepository {
  17. return &ProjectRepository{canQuery, []*models.Project{}}
  18. }
  19. // CreateProject appends a new project to the in-memory projects array
  20. func (repo *ProjectRepository) CreateProject(project *models.Project) (*models.Project, error) {
  21. if !repo.canQuery {
  22. return nil, errors.New("Cannot write database")
  23. }
  24. repo.projects = append(repo.projects, project)
  25. project.ID = uint(len(repo.projects))
  26. return project, nil
  27. }
  28. // CreateProjectRole appends a role to the existing array of roles
  29. func (repo *ProjectRepository) CreateProjectRole(project *models.Project, role *models.Role) (*models.Role, error) {
  30. if !repo.canQuery {
  31. return nil, errors.New("Cannot write database")
  32. }
  33. if int(project.ID-1) >= len(repo.projects) || repo.projects[project.ID-1] == nil {
  34. return nil, gorm.ErrRecordNotFound
  35. }
  36. index := int(project.ID - 1)
  37. oldProject := *repo.projects[index]
  38. repo.projects[index] = project
  39. project.Roles = append(oldProject.Roles, *role)
  40. return role, nil
  41. }
  42. // CreateProjectRole appends a role to the existing array of roles
  43. func (repo *ProjectRepository) UpdateProjectRole(projID uint, role *models.Role) (*models.Role, error) {
  44. if !repo.canQuery {
  45. return nil, errors.New("Cannot read from database")
  46. }
  47. var foundProject *models.Project
  48. // find all roles matching
  49. for _, project := range repo.projects {
  50. if project.ID == projID {
  51. foundProject = project
  52. }
  53. }
  54. if foundProject == nil {
  55. return nil, gorm.ErrRecordNotFound
  56. }
  57. var index int
  58. for i, _role := range foundProject.Roles {
  59. if _role.UserID == role.UserID {
  60. index = i
  61. }
  62. }
  63. if index == 0 {
  64. return nil, gorm.ErrRecordNotFound
  65. }
  66. foundProject.Roles[index] = *role
  67. return role, nil
  68. }
  69. // ReadProject gets a projects specified by a unique id
  70. func (repo *ProjectRepository) ReadProject(id uint) (*models.Project, error) {
  71. if !repo.canQuery {
  72. return nil, errors.New("Cannot read from database")
  73. }
  74. if int(id-1) >= len(repo.projects) || repo.projects[id-1] == nil {
  75. return nil, gorm.ErrRecordNotFound
  76. }
  77. index := int(id - 1)
  78. return repo.projects[index], nil
  79. }
  80. // ReadProjectRole gets a role specified by a project ID and user ID
  81. func (repo *ProjectRepository) ReadProjectRole(projID, userID uint) (*models.Role, error) {
  82. if !repo.canQuery {
  83. return nil, errors.New("Cannot write database")
  84. }
  85. var foundProject *models.Project
  86. // find all roles matching
  87. for _, project := range repo.projects {
  88. if project.ID == projID {
  89. foundProject = project
  90. }
  91. }
  92. if foundProject == nil {
  93. return nil, gorm.ErrRecordNotFound
  94. }
  95. var index int
  96. for i, _role := range foundProject.Roles {
  97. if _role.UserID == userID {
  98. index = i
  99. }
  100. }
  101. if index == 0 {
  102. return nil, gorm.ErrRecordNotFound
  103. }
  104. res := foundProject.Roles[index]
  105. return &res, nil
  106. }
  107. // ListProjectsByUserID lists projects where a user has an associated role
  108. func (repo *ProjectRepository) ListProjectsByUserID(userID uint) ([]*models.Project, error) {
  109. if !repo.canQuery {
  110. return nil, errors.New("Cannot read from database")
  111. }
  112. resp := make([]*models.Project, 0)
  113. // find all roles matching
  114. for _, project := range repo.projects {
  115. for _, role := range project.Roles {
  116. if role.UserID == userID {
  117. resp = append(resp, project)
  118. }
  119. }
  120. }
  121. return resp, nil
  122. }
  123. // ListProjectRoles returns a list of roles for the project
  124. func (repo *ProjectRepository) ListProjectRoles(projID uint) ([]models.Role, error) {
  125. if !repo.canQuery {
  126. return nil, errors.New("Cannot read from database")
  127. }
  128. if int(projID-1) >= len(repo.projects) || repo.projects[projID-1] == nil {
  129. return nil, gorm.ErrRecordNotFound
  130. }
  131. index := int(projID - 1)
  132. repo.projects[index] = nil
  133. return repo.projects[index].Roles, nil
  134. }
  135. // DeleteProject removes a project
  136. func (repo *ProjectRepository) DeleteProject(project *models.Project) (*models.Project, error) {
  137. if !repo.canQuery {
  138. return nil, errors.New("Cannot write database")
  139. }
  140. if int(project.ID-1) >= len(repo.projects) || repo.projects[project.ID-1] == nil {
  141. return nil, gorm.ErrRecordNotFound
  142. }
  143. index := int(project.ID - 1)
  144. repo.projects[index] = nil
  145. return project, nil
  146. }
  147. func (repo *ProjectRepository) DeleteProjectRole(projID, userID uint) (*models.Role, error) {
  148. if !repo.canQuery {
  149. return nil, errors.New("Cannot write database")
  150. }
  151. var foundProject *models.Project
  152. // find all roles matching
  153. for _, project := range repo.projects {
  154. if project.ID == projID {
  155. foundProject = project
  156. }
  157. }
  158. if foundProject == nil {
  159. return nil, gorm.ErrRecordNotFound
  160. }
  161. var index int
  162. for i, _role := range foundProject.Roles {
  163. if _role.UserID == userID {
  164. index = i
  165. }
  166. }
  167. if index == 0 {
  168. return nil, gorm.ErrRecordNotFound
  169. }
  170. res := foundProject.Roles[index]
  171. foundProject.Roles = append(foundProject.Roles[:index], foundProject.Roles[index+1:]...)
  172. return &res, nil
  173. }