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.

91 lines
2.5 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. "time"
  5. )
  6. type Log struct {
  7. ID string `json:"id" db:"log_id"`
  8. UserID string `json:"-" db:"user_id"`
  9. TaskID string `json:"taskId" db:"task_id"`
  10. ItemID string `json:"itemId" db:"item_id"`
  11. ItemAmount int `json:"itemAmount" db:"item_amount"`
  12. SecondaryItemID *string `json:"secondaryItemId" db:"secondary_item_id"`
  13. SecondaryItemAmount int `json:"secondaryItemAmount" db:"secondary_item_amount"`
  14. LoggedTime time.Time `json:"loggedTime" db:"logged_time"`
  15. Description string `json:"description" db:"description"`
  16. }
  17. func (log *Log) Amount(itemID string) int {
  18. result := 0
  19. if log.ItemID == itemID {
  20. result += log.ItemAmount
  21. }
  22. if log.SecondaryItemID != nil && *log.SecondaryItemID == itemID {
  23. result += log.SecondaryItemAmount
  24. }
  25. return result
  26. }
  27. func (log *Log) Update(update LogUpdate) {
  28. if update.LoggedTime != nil {
  29. log.LoggedTime = update.LoggedTime.UTC()
  30. }
  31. if update.Description != nil {
  32. log.Description = *update.Description
  33. }
  34. if update.ItemAmount != nil {
  35. log.ItemAmount = *update.ItemAmount
  36. }
  37. if update.SecondaryItemID != nil {
  38. log.SecondaryItemID = update.SecondaryItemID
  39. }
  40. if update.ClearSecondaryItem {
  41. log.SecondaryItemID = nil
  42. }
  43. if update.SecondaryItemAmount != nil {
  44. log.SecondaryItemAmount = *update.SecondaryItemAmount
  45. }
  46. }
  47. type LogUpdate struct {
  48. LoggedTime *time.Time `json:"loggedTime"`
  49. Description *string `json:"description"`
  50. ItemAmount *int `json:"itemAmount"`
  51. SecondaryItemID *string `json:"secondaryItemId"`
  52. SecondaryItemAmount *int `json:"secondaryItemAmount"`
  53. ClearSecondaryItem bool `json:"clearSecondaryItemId"`
  54. }
  55. type LogResult struct {
  56. Log
  57. Task *TaskWithProject `json:"task"`
  58. Item *Item `json:"item"`
  59. SecondaryItem *Item `json:"secondaryItem"`
  60. }
  61. type LogWithSecondaryItem struct {
  62. Log
  63. SecondaryItem *Item `json:"secondaryItem,omitempty"`
  64. }
  65. type LogFilter struct {
  66. UserID string
  67. ProjectGroupIDs []string
  68. ProjectIDs []string
  69. TaskIDs []string
  70. ItemIDs []string
  71. MinTime *time.Time
  72. MaxTime *time.Time
  73. }
  74. type LogRepository interface {
  75. Find(ctx context.Context, id string) (*Log, error)
  76. List(ctx context.Context, filter LogFilter) ([]*Log, error)
  77. Insert(ctx context.Context, log Log) error
  78. Update(ctx context.Context, log Log) error
  79. Delete(ctx context.Context, log Log) error
  80. }