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.

203 lines
5.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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
  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
  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. WHERE project_id=:project_id
  108. `, toProjectDBO(project))
  109. if err != nil {
  110. return err
  111. }
  112. return nil
  113. }
  114. func (r *projectRepository) Delete(ctx context.Context, project models.Project) error {
  115. _, err := r.db.ExecContext(ctx, `DELETE FROM project WHERE project_id=$1`, project.ID)
  116. if err != nil {
  117. if err == sql.ErrNoRows {
  118. return slerrors.NotFound("Project")
  119. }
  120. return err
  121. }
  122. _, err = r.db.ExecContext(ctx, `DELETE FROM task_link WHERE project_id=$1`, project.ID)
  123. if err != nil && err != sql.ErrNoRows {
  124. return err
  125. }
  126. return nil
  127. }
  128. type projectDBO struct {
  129. ID string `json:"id" db:"project_id"`
  130. UserID string `json:"-" db:"user_id"`
  131. ProjectGroupID *string `json:"projectGroupID" db:"project_group_id"`
  132. Name string `json:"name" db:"name"`
  133. Description string `json:"description" db:"description"`
  134. Icon string `json:"icon" db:"icon"`
  135. Active bool `json:"active" db:"active"`
  136. CreatedTime time.Time `json:"createdTime" db:"created_time"`
  137. StartTime *time.Time `json:"startTime" db:"start_time"`
  138. EndTime *time.Time `json:"endTime" db:"end_time"`
  139. SubtractAmount int `json:"subtractAmount" db:"subtract_amount"`
  140. StatusTag *string `json:"statusTag" db:"status_tag"`
  141. Favorite bool `json:"favorite" db:"favorite"`
  142. Tags pq.StringArray `json:"tags" db:"tags"`
  143. }
  144. func toProjectDBO(project models.Project) *projectDBO {
  145. return &projectDBO{
  146. ID: project.ID,
  147. UserID: project.UserID,
  148. ProjectGroupID: project.GroupID,
  149. Name: project.Name,
  150. Description: project.Description,
  151. Icon: project.Icon,
  152. Active: project.Active,
  153. CreatedTime: project.CreatedTime,
  154. StartTime: project.StartTime,
  155. EndTime: project.EndTime,
  156. SubtractAmount: project.SubtractAmount,
  157. StatusTag: project.StatusTag,
  158. Favorite: project.Favorite,
  159. Tags: project.Tags,
  160. }
  161. }
  162. func (d *projectDBO) ToProject() *models.Project {
  163. return &models.Project{
  164. ID: d.ID,
  165. UserID: d.UserID,
  166. GroupID: d.ProjectGroupID,
  167. Name: d.Name,
  168. Description: d.Description,
  169. Icon: d.Icon,
  170. Active: d.Active,
  171. CreatedTime: d.CreatedTime,
  172. StartTime: d.StartTime,
  173. EndTime: d.EndTime,
  174. SubtractAmount: d.SubtractAmount,
  175. StatusTag: d.StatusTag,
  176. Favorite: d.Favorite,
  177. Tags: d.Tags,
  178. }
  179. }