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.

134 lines
3.7 KiB

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. "strings"
  5. "time"
  6. )
  7. type Goal struct {
  8. ID string `json:"id" db:"goal_id"`
  9. UserID string `json:"-" db:"user_id"`
  10. GroupID string `json:"groupId" db:"group_id"`
  11. ItemID *string `json:"itemId" db:"item_id"`
  12. StartTime time.Time `json:"startTime" db:"start_time"`
  13. EndTime time.Time `json:"endTime" db:"end_time"`
  14. Amount int `json:"amount" db:"amount"`
  15. Unweighted bool `json:"unweighted" db:"unweighted"`
  16. Name string `json:"name" db:"name"`
  17. Description string `json:"description" db:"description"`
  18. CompositionMode string `json:"compositionMode" db:"composition_mode"`
  19. TaskFilter *string `json:"taskFilter" db:"task_filter"`
  20. ItemFilter *string `json:"itemFilter" db:"item_filter"`
  21. }
  22. func (goal *Goal) Accepts(item *Item, task *Task) bool {
  23. if item.GroupID != goal.GroupID {
  24. return false
  25. }
  26. if goal.ItemID != nil && item.ID != *goal.ItemID {
  27. return false
  28. }
  29. if goal.TaskFilter != nil && !strings.Contains(strings.ToLower(task.Name), *goal.TaskFilter) {
  30. return false
  31. }
  32. if goal.ItemFilter != nil && !strings.Contains(strings.ToLower(item.Name), *goal.ItemFilter) {
  33. return false
  34. }
  35. return true
  36. }
  37. func (goal *Goal) Update(update GoalUpdate) {
  38. if update.Amount != nil {
  39. goal.Amount = *update.Amount
  40. }
  41. if update.StartTime != nil {
  42. goal.StartTime = *update.StartTime
  43. }
  44. if update.EndTime != nil {
  45. goal.EndTime = *update.EndTime
  46. }
  47. if update.Name != nil {
  48. goal.Name = *update.Name
  49. }
  50. if update.Description != nil {
  51. goal.Description = *update.Description
  52. }
  53. if update.Unweighted != nil {
  54. goal.Unweighted = *update.Unweighted
  55. }
  56. if update.CompositionMode != nil {
  57. goal.CompositionMode = *update.CompositionMode
  58. }
  59. if update.ItemID != nil {
  60. goal.ItemID = update.ItemID
  61. }
  62. if update.ClearItemID {
  63. goal.ItemID = nil
  64. }
  65. if update.TaskFilter != nil {
  66. goal.TaskFilter = update.TaskFilter
  67. }
  68. if update.ClearTaskFilter {
  69. goal.TaskFilter = nil
  70. }
  71. if update.ItemFilter != nil {
  72. goal.ItemFilter = update.ItemFilter
  73. }
  74. if update.ClearItemFilter {
  75. goal.ItemFilter = nil
  76. }
  77. }
  78. type GoalUpdate struct {
  79. StartTime *time.Time `json:"startTime"`
  80. EndTime *time.Time `json:"endTime"`
  81. Amount *int `json:"amount"`
  82. Name *string `json:"name"`
  83. Description *string `json:"description"`
  84. ItemID *string `json:"itemId"`
  85. ClearItemID bool `json:"clearItemID"`
  86. Unweighted *bool `json:"unweighted"`
  87. CompositionMode *string `json:"compositionMode"`
  88. TaskFilter *string `json:"taskFilter"`
  89. ClearTaskFilter bool `json:"clearTaskFilter"`
  90. ItemFilter *string `json:"itemFilter"`
  91. ClearItemFilter bool `json:"clearItemFilter"`
  92. }
  93. type GoalResult struct {
  94. Goal
  95. Group *Group `json:"group"`
  96. Items []*GoalResultItem `json:"items"`
  97. Logs []*GoalResultLog `json:"logs"`
  98. CompletedAmount int `json:"completedAmount"`
  99. }
  100. type GoalResultLog struct {
  101. LogResult
  102. ItemCounted bool `json:"itemCounted"`
  103. SecondaryItemCounted bool `json:"secondaryItemCounted"`
  104. }
  105. type GoalResultItem struct {
  106. Item
  107. CompletedAmount int `json:"completedAmount"`
  108. }
  109. type GoalFilter struct {
  110. UserID string
  111. GroupIDs []string
  112. IncludesTime *time.Time
  113. MinTime *time.Time
  114. MaxTime *time.Time
  115. IDs []string
  116. }
  117. type GoalRepository interface {
  118. Find(ctx context.Context, id string) (*Goal, error)
  119. List(ctx context.Context, filter GoalFilter) ([]*Goal, error)
  120. Insert(ctx context.Context, goal Goal) error
  121. Update(ctx context.Context, goal Goal) error
  122. Delete(ctx context.Context, goal Goal) error
  123. }