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.

146 lines
4.0 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
  1. package models
  2. import (
  3. "context"
  4. "sort"
  5. "time"
  6. )
  7. type Project struct {
  8. ID string `json:"id" db:"project_id"`
  9. UserID string `json:"-" db:"user_id"`
  10. GroupID *string `json:"groupId" db:"project_group_id"`
  11. Name string `json:"name" db:"name"`
  12. Description string `json:"description" db:"description"`
  13. Icon string `json:"icon" db:"icon"`
  14. Active bool `json:"active" db:"active"`
  15. CreatedTime time.Time `json:"createdTime" db:"created_time"`
  16. StartTime *time.Time `json:"startTime" db:"start_time"`
  17. EndTime *time.Time `json:"endTime" db:"end_time"`
  18. SubtractAmount int `json:"subtractAmount" db:"subtract_amount"`
  19. StatusTag *string `json:"statusTag" db:"status_tag"`
  20. Favorite bool `json:"favorite" db:"favorite"`
  21. Tags []string `json:"tags" db:"tags"`
  22. TaskSortFields []string `json:"taskSortFields" db:"task_sort_fields"`
  23. }
  24. func (project *Project) Update(update ProjectUpdate) {
  25. if update.GroupID != nil {
  26. if *update.GroupID == "" {
  27. project.GroupID = nil
  28. } else {
  29. project.GroupID = update.GroupID
  30. }
  31. }
  32. if update.Name != nil {
  33. project.Name = *update.Name
  34. }
  35. if update.Description != nil {
  36. project.Description = *update.Description
  37. }
  38. if update.Icon != nil {
  39. project.Icon = *update.Icon
  40. }
  41. if update.Active != nil {
  42. project.Active = *update.Active
  43. }
  44. if update.StartTime != nil {
  45. startTimeCopy := update.StartTime.UTC()
  46. project.StartTime = &startTimeCopy
  47. }
  48. if update.ClearStartTime {
  49. project.StartTime = nil
  50. }
  51. if update.EndTime != nil {
  52. endTimeCopy := update.EndTime.UTC()
  53. project.EndTime = &endTimeCopy
  54. }
  55. if update.ClearEndTime {
  56. project.EndTime = nil
  57. }
  58. if update.SubtractAmount != nil {
  59. project.SubtractAmount = *update.SubtractAmount
  60. if project.SubtractAmount < 0 {
  61. project.SubtractAmount = 0
  62. }
  63. }
  64. if update.StatusTag != nil {
  65. project.StatusTag = update.StatusTag
  66. }
  67. if update.ClearStatusTag {
  68. project.StatusTag = nil
  69. }
  70. if update.Favorite != nil {
  71. project.Favorite = *update.Favorite
  72. }
  73. if update.SetTags != nil {
  74. project.Tags = update.SetTags
  75. }
  76. if update.TaskSortFields != nil {
  77. project.TaskSortFields = append(update.TaskSortFields[:0:0], update.TaskSortFields...)
  78. }
  79. if project.StatusTag != nil && project.Active {
  80. project.StatusTag = nil
  81. }
  82. }
  83. type ProjectUpdate struct {
  84. GroupID *string `json:"groupId"`
  85. Name *string `json:"name"`
  86. Description *string `json:"description"`
  87. Icon *string `json:"icon"`
  88. Active *bool `json:"active"`
  89. StartTime *time.Time `json:"startTime"`
  90. ClearStartTime bool `json:"clearStartTime"`
  91. EndTime *time.Time `json:"endTime"`
  92. ClearEndTime bool `json:"clearEndTime"`
  93. SubtractAmount *int `json:"subtractAmount"`
  94. StatusTag *string `json:"statusTag"`
  95. ClearStatusTag bool `json:"clearStatusTag"`
  96. SetTags []string `json:"setTags"`
  97. Favorite *bool `json:"favorite"`
  98. TaskSortFields []string `json:"taskSortFields"`
  99. }
  100. type ProjectResult struct {
  101. Project
  102. Tasks []*TaskResult `json:"tasks"`
  103. Subtractions []ProjectSubtraction `json:"subtractions"`
  104. }
  105. func (result *ProjectResult) SortTasks() {
  106. sorter := TaskSorter{
  107. Data: result.Tasks,
  108. Fields: result.Project.TaskSortFields,
  109. }
  110. if len(sorter.Fields) == 0 {
  111. sorter.Fields = []string{"status"}
  112. }
  113. sort.Sort(sorter)
  114. }
  115. type ProjectSubtraction struct {
  116. Item Item `json:"item"`
  117. Amount int `json:"amount"`
  118. }
  119. type ProjectFilter struct {
  120. UserID string
  121. ProjectGroupIDs []string
  122. Active *bool
  123. Favorite *bool
  124. Expiring bool
  125. IncludeSemiActive bool
  126. IDs []string
  127. Ungrouped bool
  128. }
  129. type ProjectRepository interface {
  130. Find(ctx context.Context, id string) (*Project, error)
  131. List(ctx context.Context, filter ProjectFilter) ([]*Project, error)
  132. Insert(ctx context.Context, project Project) error
  133. Update(ctx context.Context, project Project) error
  134. Delete(ctx context.Context, project Project) error
  135. }