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.

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