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.

86 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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 LogFilter struct {
  62. UserID string
  63. ProjectGroupIDs []string
  64. ProjectIDs []string
  65. TaskIDs []string
  66. ItemIDs []string
  67. MinTime *time.Time
  68. MaxTime *time.Time
  69. }
  70. type LogRepository interface {
  71. Find(ctx context.Context, id string) (*Log, error)
  72. List(ctx context.Context, filter LogFilter) ([]*Log, error)
  73. Insert(ctx context.Context, log Log) error
  74. Update(ctx context.Context, log Log) error
  75. Delete(ctx context.Context, log Log) error
  76. }