project.go 5.8 KB

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