project.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // ReadProject gets a projects specified by a unique id
  43. func (repo *ProjectRepository) ReadProject(id uint) (*models.Project, error) {
  44. if !repo.canQuery {
  45. return nil, errors.New("Cannot read from database")
  46. }
  47. if int(id-1) >= len(repo.projects) || repo.projects[id-1] == nil {
  48. return nil, gorm.ErrRecordNotFound
  49. }
  50. index := int(id - 1)
  51. return repo.projects[index], nil
  52. }
  53. // ListProjectsByUserID lists projects where a user has an associated role
  54. func (repo *ProjectRepository) ListProjectsByUserID(userID uint) ([]*models.Project, error) {
  55. if !repo.canQuery {
  56. return nil, errors.New("Cannot read from database")
  57. }
  58. resp := make([]*models.Project, 0)
  59. // find all roles matching
  60. for _, project := range repo.projects {
  61. for _, role := range project.Roles {
  62. if role.UserID == userID {
  63. resp = append(resp, project)
  64. }
  65. }
  66. }
  67. return resp, nil
  68. }
  69. // DeleteProject removes a project
  70. func (repo *ProjectRepository) DeleteProject(project *models.Project) (*models.Project, error) {
  71. if !repo.canQuery {
  72. return nil, errors.New("Cannot write database")
  73. }
  74. if int(project.ID-1) >= len(repo.projects) || repo.projects[project.ID-1] == nil {
  75. return nil, gorm.ErrRecordNotFound
  76. }
  77. index := int(project.ID - 1)
  78. repo.projects[index] = nil
  79. return project, nil
  80. }