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.

138 lines
3.4 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "git.aiterp.net/stufflog/server/internal/xlerrors"
  8. "git.aiterp.net/stufflog/server/models"
  9. sq "github.com/Masterminds/squirrel"
  10. "github.com/jmoiron/sqlx"
  11. "time"
  12. )
  13. type issueTaskRepository struct {
  14. db *sqlx.DB
  15. }
  16. func (r *issueTaskRepository) Find(ctx context.Context, id string) (*models.IssueTask, error) {
  17. issueTask := models.IssueTask{}
  18. err := r.db.GetContext(ctx, &issueTask, "SELECT * FROM issue_task WHERE issue_task_id=?", id)
  19. if err != nil {
  20. if err == sql.ErrNoRows {
  21. return nil, xlerrors.NotFound("Issue task")
  22. }
  23. return nil, err
  24. }
  25. return &issueTask, nil
  26. }
  27. func (r *issueTaskRepository) List(ctx context.Context, filter models.IssueTaskFilter) ([]*models.IssueTask, error) {
  28. q := sq.Select("*").From("issue_task")
  29. if filter.ActivityIDs != nil {
  30. q = q.Where(sq.Eq{"activity_id": filter.ActivityIDs})
  31. }
  32. if filter.IssueTaskIDs != nil {
  33. q = q.Where(sq.Eq{"issue_id": filter.IssueTaskIDs})
  34. }
  35. if filter.IssueIDs != nil {
  36. q = q.Where(sq.Eq{"issue_id": filter.IssueIDs})
  37. }
  38. if filter.MinStage != nil {
  39. q = q.Where(sq.GtOrEq{"status_stage": *filter.MinStage})
  40. }
  41. if filter.MaxStage != nil {
  42. q = q.Where(sq.LtOrEq{"status_stage": *filter.MaxStage})
  43. }
  44. query, args, err := q.ToSql()
  45. if err != nil {
  46. return nil, err
  47. }
  48. results := make([]*models.IssueTask, 0, 16)
  49. err = r.db.SelectContext(ctx, &results, query, args...)
  50. if err != nil {
  51. if err == sql.ErrNoRows {
  52. return []*models.IssueTask{}, nil
  53. }
  54. return nil, err
  55. }
  56. return results, nil
  57. }
  58. func (r *issueTaskRepository) Insert(ctx context.Context, task models.IssueTask) (*models.IssueTask, error) {
  59. if task.IssueID == "" {
  60. return nil, errors.New("missing issue id")
  61. }
  62. if task.CreatedTime.IsZero() {
  63. task.CreatedTime = time.Now().Truncate(time.Second)
  64. task.UpdatedTime = task.CreatedTime
  65. }
  66. tx, err := r.db.BeginTxx(ctx, nil)
  67. if err != nil {
  68. return nil, err
  69. }
  70. nextID, err := incCounter(ctx, tx, counterKindIssueSubID, task.IssueID)
  71. if err != nil {
  72. _ = tx.Rollback()
  73. return nil, err
  74. }
  75. task.ID = fmt.Sprintf("%s-%d", task.IssueID, nextID)
  76. _, err = tx.NamedExecContext(ctx, `
  77. INSERT INTO issue_task (
  78. issue_task_id, issue_id, activity_id, created_time,
  79. updated_time, due_time, status_stage, status_name,
  80. name, description, estimated_time, estimated_units,
  81. points_multiplier
  82. ) VALUES (
  83. :issue_task_id, :issue_id, :activity_id, :created_time,
  84. :updated_time, :due_time, :status_stage, :status_name,
  85. :name, :description, :estimated_time, :estimated_units,
  86. :points_multiplier
  87. )
  88. `, task)
  89. if err != nil {
  90. _ = tx.Rollback()
  91. return nil, err
  92. }
  93. err = tx.Commit()
  94. if err != nil {
  95. _ = tx.Rollback()
  96. return nil, err
  97. }
  98. return &task, nil
  99. }
  100. func (r *issueTaskRepository) Save(ctx context.Context, task models.IssueTask) error {
  101. _, err := r.db.NamedExecContext(ctx, `
  102. UPDATE issue_task SET
  103. due_time=:due_time,
  104. status_stage=:status_stage,
  105. status_name=:status_name,
  106. name=:name,
  107. description=:description,
  108. estimated_time=:estimated_time,
  109. estimated_units=:estimated_units,
  110. points_multiplier=:points_multiplier
  111. WHERE issue_task_id=:issue_task_id
  112. `, task)
  113. return err
  114. }
  115. func (r *issueTaskRepository) Delete(ctx context.Context, task models.IssueTask) error {
  116. _, err := r.db.ExecContext(ctx, "DELETE FROM issue_task WHERE issue_task_id=? LIMIT 1;", task.ID)
  117. return err
  118. }