stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.3 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "git.aiterp.net/stufflog/server/internal/slerrors"
  7. "git.aiterp.net/stufflog/server/models"
  8. sq "github.com/Masterminds/squirrel"
  9. "github.com/jmoiron/sqlx"
  10. )
  11. type projectRepository struct {
  12. db *sqlx.DB
  13. }
  14. func (r *projectRepository) Find(ctx context.Context, id string) (*models.Project, error) {
  15. project := models.Project{}
  16. err := r.db.GetContext(ctx, &project, "SELECT * FROM project WHERE project_id=?", id)
  17. if err != nil {
  18. if err == sql.ErrNoRows {
  19. return nil, slerrors.NotFound("Project")
  20. }
  21. return nil, err
  22. }
  23. return &project, nil
  24. }
  25. func (r *projectRepository) List(ctx context.Context, filter models.ProjectFilter) ([]*models.Project, error) {
  26. q := sq.Select("project.*").From("project").OrderBy("project_id")
  27. if len(filter.ProjectIDs) > 0 {
  28. q = q.Where(sq.Eq{"project_id": filter.ProjectIDs})
  29. }
  30. if filter.Search != nil {
  31. q = q.Where("MATCH (name, description) AGAINST (?)", *filter.Search)
  32. }
  33. if filter.Permission != nil && filter.Permission.Valid() {
  34. q = q.LeftJoin("project_permission ON project.project_id = project_permission.project_id AND project_permission.user_id = ?", filter.Permission.UserID)
  35. if filter.Permission.MaxLevel >= filter.Permission.MinLevel {
  36. q = q.Where(sq.And{
  37. sq.GtOrEq{"project_permission.access_level": filter.Permission.MinLevel},
  38. sq.LtOrEq{"project_permission.access_level": filter.Permission.MaxLevel},
  39. })
  40. } else {
  41. q = q.Where(sq.GtOrEq{"project_permission.access_level": filter.Permission.MinLevel})
  42. }
  43. }
  44. query, args, err := q.ToSql()
  45. if err != nil {
  46. return nil, err
  47. }
  48. results := make([]*models.Project, 0, 16)
  49. err = r.db.SelectContext(ctx, &results, query, args...)
  50. if err != nil {
  51. if err == sql.ErrNoRows {
  52. return []*models.Project{}, nil
  53. }
  54. return nil, err
  55. }
  56. return results, nil
  57. }
  58. func (r *projectRepository) Insert(ctx context.Context, project models.Project) (*models.Project, error) {
  59. if !project.ValidKey() {
  60. return nil, errors.New("invalid project id")
  61. }
  62. _, err := r.db.NamedExecContext(ctx, `
  63. INSERT INTO project (project_id, name, description, daily_points)
  64. VALUES (:project_id, :name, :description, :daily_points)
  65. `, project)
  66. return &project, err
  67. }
  68. func (r *projectRepository) Save(ctx context.Context, project models.Project) error {
  69. _, err := r.db.NamedExecContext(ctx, `
  70. UPDATE project
  71. SET name=:name, description=:description, daily_points=:daily_points
  72. WHERE project_id=:project_id
  73. `, project)
  74. return err
  75. }
  76. func (r *projectRepository) ListPermissions(ctx context.Context, project models.Project) ([]*models.ProjectPermission, error) {
  77. permissions := make([]*models.ProjectPermission, 0, 4)
  78. err := r.db.SelectContext(ctx, &permissions, "SELECT * FROM project_permission WHERE project_id=?", project.ID)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return permissions, nil
  83. }
  84. func (r *projectRepository) GetPermission(ctx context.Context, project models.Project, user models.User) (*models.ProjectPermission, error) {
  85. permission := models.ProjectPermission{}
  86. err := r.db.GetContext(ctx, &permission, "SELECT * FROM project_permission WHERE project_id=? AND user_id=?", project.ID, user.ID)
  87. if err != nil {
  88. if err == sql.ErrNoRows {
  89. return &models.ProjectPermission{
  90. ProjectID: project.ID,
  91. UserID: user.ID,
  92. }, nil
  93. }
  94. return nil, err
  95. }
  96. return &permission, nil
  97. }
  98. func (r *projectRepository) GetIssuePermission(ctx context.Context, issue models.Issue, user models.User) (*models.ProjectPermission, error) {
  99. return r.GetPermission(ctx, models.Project{ID: issue.ProjectID}, user)
  100. }
  101. func (r *projectRepository) SetPermission(ctx context.Context, permission models.ProjectPermission) error {
  102. _, err := r.db.NamedExecContext(ctx, `
  103. REPLACE INTO project_permission (project_id, user_id, access_level)
  104. VALUES (:project_id, :user_id, :access_level)
  105. `, permission)
  106. return err
  107. }
  108. func (r *projectRepository) Delete(ctx context.Context, project models.Project) error {
  109. _, err := r.db.ExecContext(ctx, "DELETE FROM project WHERE project_id=?", project.ID)
  110. if err != nil {
  111. return err
  112. }
  113. _, err = r.db.ExecContext(ctx, "DELETE FROM project_permission WHERE project_id=?", project.ID)
  114. if err != nil {
  115. return err
  116. }
  117. //_, err = r.db.ExecContext(ctx, "DELETE FROM project_status WHERE project_id=?", project.ID)
  118. //if err != nil {
  119. // return err
  120. //}
  121. return nil
  122. }