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.

170 lines
3.8 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "git.aiterp.net/stufflog/server/internal/slerrors"
  8. "git.aiterp.net/stufflog/server/models"
  9. sq "github.com/Masterminds/squirrel"
  10. "github.com/jmoiron/sqlx"
  11. "time"
  12. )
  13. var counterKindIssueID = "NextIssueID"
  14. var counterKindIssueSubID = "NextIssueSubID"
  15. type issueRepository struct {
  16. db *sqlx.DB
  17. }
  18. func (r *issueRepository) Find(ctx context.Context, id string) (*models.Issue, error) {
  19. issue := models.Issue{}
  20. err := r.db.GetContext(ctx, &issue, "SELECT * FROM issue WHERE issue_id=?", id)
  21. if err != nil {
  22. if err == sql.ErrNoRows {
  23. return nil, slerrors.NotFound("Issue")
  24. }
  25. return nil, err
  26. }
  27. return &issue, nil
  28. }
  29. func (r *issueRepository) List(ctx context.Context, filter models.IssueFilter) ([]*models.Issue, error) {
  30. q := sq.Select("*").From("issue").OrderBy("updated_time DESC")
  31. if len(filter.IssueIDs) > 0 {
  32. q = q.Where(sq.Eq{"issue_id": filter.IssueIDs})
  33. }
  34. if len(filter.ProjectIDs) > 0 {
  35. q = q.Where(sq.Eq{"project_id": filter.ProjectIDs})
  36. }
  37. if len(filter.OwnerIDs) > 0 {
  38. q = q.Where(sq.Eq{"owner_id": filter.OwnerIDs})
  39. }
  40. if len(filter.AssigneeIDs) > 0 {
  41. q = q.Where(sq.Eq{"assignee_id": filter.AssigneeIDs})
  42. }
  43. if filter.Search != nil && *filter.Search != "" {
  44. q = q.Where("MATCH (name, title, description) AGAINST (?)", *filter.Search)
  45. }
  46. if filter.MinStage != nil {
  47. q = q.Where(sq.GtOrEq{"status_stage": *filter.MinStage})
  48. }
  49. if filter.MaxStage != nil {
  50. q = q.Where(sq.LtOrEq{"status_stage": *filter.MaxStage})
  51. }
  52. if filter.Limit != nil && *filter.Limit > 0 {
  53. q = q.Limit(uint64(*filter.Limit))
  54. }
  55. query, args, err := q.ToSql()
  56. if err != nil {
  57. return nil, err
  58. }
  59. results := make([]*models.Issue, 0, 16)
  60. err = r.db.SelectContext(ctx, &results, query, args...)
  61. if err != nil {
  62. if err == sql.ErrNoRows {
  63. return []*models.Issue{}, nil
  64. }
  65. return nil, err
  66. }
  67. return results, nil
  68. }
  69. func (r *issueRepository) Insert(ctx context.Context, issue models.Issue) (*models.Issue, error) {
  70. if issue.ProjectID == "" {
  71. return nil, errors.New("missing project id")
  72. }
  73. if issue.CreatedTime.IsZero() {
  74. issue.CreatedTime = time.Now().Truncate(time.Second)
  75. issue.UpdatedTime = issue.CreatedTime
  76. }
  77. tx, err := r.db.BeginTxx(ctx, nil)
  78. if err != nil {
  79. return nil, err
  80. }
  81. nextID, err := incCounter(ctx, tx, counterKindIssueID, issue.ProjectID)
  82. if err != nil {
  83. _ = tx.Rollback()
  84. return nil, err
  85. }
  86. issue.ID = fmt.Sprintf("%s-%d", issue.ProjectID, nextID)
  87. _, err = tx.NamedExecContext(ctx, `
  88. INSERT INTO issue (
  89. issue_id, project_id, owner_id, assignee_id,
  90. status_stage, status_name, created_time,
  91. updated_time, due_time, name, title, description
  92. ) VALUES (
  93. :issue_id, :project_id, :owner_id, :assignee_id,
  94. :status_stage, :status_name, :created_time,
  95. :updated_time, :due_time, :name, :title, :description
  96. )
  97. `, issue)
  98. if err != nil {
  99. _ = tx.Rollback()
  100. return nil, err
  101. }
  102. err = tx.Commit()
  103. if err != nil {
  104. _ = tx.Rollback()
  105. return nil, err
  106. }
  107. return &issue, nil
  108. }
  109. func (r *issueRepository) Save(ctx context.Context, issue models.Issue) error {
  110. _, err := r.db.NamedExecContext(ctx, `
  111. UPDATE issue
  112. SET assignee_id=:assignee_id,
  113. status_stage=:status_stage,
  114. status_name=:status_name,
  115. created_time=:created_time,
  116. updated_time=:updated_time,
  117. due_time=:due_time,
  118. name=:name,
  119. title=:title,
  120. description=:description
  121. WHERE issue_id=:issue_id
  122. `, issue)
  123. if err != nil {
  124. return err
  125. }
  126. return nil
  127. }
  128. func (r *issueRepository) Delete(ctx context.Context, issue models.Issue) error {
  129. tx, err := r.db.BeginTxx(ctx, nil)
  130. if err != nil {
  131. return err
  132. }
  133. _, err = tx.ExecContext(ctx, "DELETE FROM issue WHERE issue_id=?", issue.ID)
  134. if err != nil {
  135. _ = tx.Rollback()
  136. return err
  137. }
  138. // TODO: delete from issue_*
  139. err = tx.Commit()
  140. if err != nil {
  141. _ = tx.Rollback()
  142. return err
  143. }
  144. return nil
  145. }