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.

207 lines
5.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/Masterminds/squirrel"
  6. "github.com/gissleh/stufflog/internal/slerrors"
  7. "github.com/gissleh/stufflog/models"
  8. "github.com/jmoiron/sqlx"
  9. "github.com/lib/pq"
  10. "time"
  11. )
  12. type projectRepository struct {
  13. db *sqlx.DB
  14. }
  15. func (r *projectRepository) Find(ctx context.Context, id string) (*models.Project, error) {
  16. res := projectDBO{}
  17. err := r.db.GetContext(ctx, &res, "SELECT * FROM project WHERE project_id=$1", id)
  18. if err != nil {
  19. if err == sql.ErrNoRows {
  20. return nil, slerrors.NotFound("Project")
  21. }
  22. return nil, err
  23. }
  24. return res.ToProject(), nil
  25. }
  26. func (r *projectRepository) List(ctx context.Context, filter models.ProjectFilter) ([]*models.Project, error) {
  27. sq := squirrel.Select("*").From("project").PlaceholderFormat(squirrel.Dollar)
  28. sq = sq.Where(squirrel.Eq{"user_id": filter.UserID})
  29. if filter.IDs != nil {
  30. sq = sq.Where(squirrel.Eq{"project_id": filter.IDs})
  31. }
  32. if filter.Active != nil {
  33. if filter.IncludeSemiActive {
  34. sq = sq.Where(squirrel.Or{
  35. squirrel.Eq{"active": *filter.Active},
  36. squirrel.Eq{"status_tag": []string{
  37. "to do", "background", "on hold", "progress",
  38. }},
  39. })
  40. } else {
  41. sq = sq.Where(squirrel.Eq{"active": *filter.Active})
  42. }
  43. }
  44. if filter.Expiring {
  45. sq = sq.Where("end_time IS NOT NULL")
  46. }
  47. if filter.Favorite != nil {
  48. sq = sq.Where(squirrel.Eq{"favorite": *filter.Favorite})
  49. }
  50. if filter.Ungrouped == true {
  51. sq = sq.Where("project_group_id is NULL")
  52. } else if filter.ProjectGroupIDs != nil {
  53. sq = sq.Where(squirrel.Eq{"project_group_id": filter.ProjectGroupIDs})
  54. }
  55. sq = sq.OrderBy("active", "created_time DESC")
  56. query, args, err := sq.ToSql()
  57. if err != nil {
  58. return nil, err
  59. }
  60. res := make([]*projectDBO, 0, 8)
  61. err = r.db.SelectContext(ctx, &res, query, args...)
  62. if err != nil {
  63. if err == sql.ErrNoRows {
  64. return []*models.Project{}, nil
  65. }
  66. return nil, err
  67. }
  68. res2 := make([]*models.Project, len(res))
  69. for i, v := range res {
  70. res2[i] = v.ToProject()
  71. }
  72. return res2, nil
  73. }
  74. func (r *projectRepository) Insert(ctx context.Context, project models.Project) error {
  75. if project.Tags == nil {
  76. project.Tags = []string{}
  77. }
  78. _, err := r.db.NamedExecContext(ctx, `
  79. INSERT INTO project(
  80. project_id, user_id, project_group_id, name, description, icon, active, created_time, start_time, end_time, subtract_amount, status_tag, favorite, tags, task_sort_fields
  81. ) VALUES (
  82. :project_id, :user_id, :project_group_id, :name, :description, :icon, :active, :created_time, :start_time, :end_time, :subtract_amount, :status_tag, :favorite, :tags, :task_sort_fields
  83. )
  84. `, toProjectDBO(project))
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func (r *projectRepository) Update(ctx context.Context, project models.Project) error {
  91. if project.Tags == nil {
  92. project.Tags = []string{}
  93. }
  94. _, err := r.db.NamedExecContext(ctx, `
  95. UPDATE project SET
  96. name = :name,
  97. description = :description,
  98. project_group_id = :project_group_id,
  99. icon = :icon,
  100. active = :active,
  101. start_time = :start_time,
  102. end_time = :end_time,
  103. subtract_amount = :subtract_amount,
  104. status_tag = :status_tag,
  105. favorite = :favorite,
  106. tags = :tags,
  107. task_sort_fields = :task_sort_fields
  108. WHERE project_id=:project_id
  109. `, toProjectDBO(project))
  110. if err != nil {
  111. return err
  112. }
  113. return nil
  114. }
  115. func (r *projectRepository) Delete(ctx context.Context, project models.Project) error {
  116. _, err := r.db.ExecContext(ctx, `DELETE FROM project WHERE project_id=$1`, project.ID)
  117. if err != nil {
  118. if err == sql.ErrNoRows {
  119. return slerrors.NotFound("Project")
  120. }
  121. return err
  122. }
  123. _, err = r.db.ExecContext(ctx, `DELETE FROM task_link WHERE project_id=$1`, project.ID)
  124. if err != nil && err != sql.ErrNoRows {
  125. return err
  126. }
  127. return nil
  128. }
  129. type projectDBO struct {
  130. ID string `json:"id" db:"project_id"`
  131. UserID string `json:"-" db:"user_id"`
  132. ProjectGroupID *string `json:"projectGroupID" db:"project_group_id"`
  133. Name string `json:"name" db:"name"`
  134. Description string `json:"description" db:"description"`
  135. Icon string `json:"icon" db:"icon"`
  136. Active bool `json:"active" db:"active"`
  137. CreatedTime time.Time `json:"createdTime" db:"created_time"`
  138. StartTime *time.Time `json:"startTime" db:"start_time"`
  139. EndTime *time.Time `json:"endTime" db:"end_time"`
  140. SubtractAmount int `json:"subtractAmount" db:"subtract_amount"`
  141. StatusTag *string `json:"statusTag" db:"status_tag"`
  142. Favorite bool `json:"favorite" db:"favorite"`
  143. Tags pq.StringArray `json:"tags" db:"tags"`
  144. TaskSortFields pq.StringArray `json:"taskSortFields" db:"task_sort_fields"`
  145. }
  146. func toProjectDBO(project models.Project) *projectDBO {
  147. return &projectDBO{
  148. ID: project.ID,
  149. UserID: project.UserID,
  150. ProjectGroupID: project.GroupID,
  151. Name: project.Name,
  152. Description: project.Description,
  153. Icon: project.Icon,
  154. Active: project.Active,
  155. CreatedTime: project.CreatedTime,
  156. StartTime: project.StartTime,
  157. EndTime: project.EndTime,
  158. SubtractAmount: project.SubtractAmount,
  159. StatusTag: project.StatusTag,
  160. Favorite: project.Favorite,
  161. Tags: project.Tags,
  162. TaskSortFields: project.TaskSortFields,
  163. }
  164. }
  165. func (d *projectDBO) ToProject() *models.Project {
  166. return &models.Project{
  167. ID: d.ID,
  168. UserID: d.UserID,
  169. GroupID: d.ProjectGroupID,
  170. Name: d.Name,
  171. Description: d.Description,
  172. Icon: d.Icon,
  173. Active: d.Active,
  174. CreatedTime: d.CreatedTime,
  175. StartTime: d.StartTime,
  176. EndTime: d.EndTime,
  177. SubtractAmount: d.SubtractAmount,
  178. StatusTag: d.StatusTag,
  179. Favorite: d.Favorite,
  180. Tags: d.Tags,
  181. TaskSortFields: d.TaskSortFields,
  182. }
  183. }