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.

85 lines
2.4 KiB

  1. package models
  2. import "context"
  3. type ProjectGroup struct {
  4. ID string `json:"id" db:"project_group_id"`
  5. UserID string `json:"-" db:"user_id"`
  6. Name string `json:"name" db:"name"`
  7. Description string `json:"description" db:"description"`
  8. Abbreviation string `json:"abbreviation" db:"abbreviation"`
  9. CategoryNames map[string]string `json:"categoryNames" db:"category_names"`
  10. }
  11. func (group *ProjectGroup) Update(update ProjectGroupUpdate) {
  12. for key, value := range update.SetCategoryNames {
  13. if value == "" {
  14. delete(group.CategoryNames, key)
  15. } else {
  16. group.CategoryNames[key] = value
  17. }
  18. }
  19. if update.Name != nil && *update.Name != "" {
  20. group.Name = *update.Name
  21. }
  22. if update.Abbreviation != nil && *update.Abbreviation != "" {
  23. group.Abbreviation = *update.Abbreviation
  24. }
  25. if update.Description != nil {
  26. group.Description = *update.Description
  27. }
  28. }
  29. type ProjectGroupUpdate struct {
  30. Name *string `json:"name"`
  31. Abbreviation *string `json:"abbreviation"`
  32. Description *string `json:"description"`
  33. SetCategoryNames map[string]string `json:"setCategoryNames"`
  34. }
  35. type ProjectGroupFilter struct {
  36. UserID string `json:"userId"`
  37. }
  38. type ProjectGroupResult struct {
  39. ProjectGroup
  40. Projects []*ProjectResult `json:"projects"`
  41. ProjectCounts map[string]int `json:"projectCounts"`
  42. TaskCounts map[string]int `json:"taskCounts"`
  43. }
  44. func (r *ProjectGroupResult) RecountTasks() {
  45. r.ProjectCounts = make(map[string]int)
  46. r.TaskCounts = make(map[string]int)
  47. r.ProjectCounts["total"] = 0
  48. r.TaskCounts["total"] = 0
  49. for _, project := range r.Projects {
  50. r.ProjectCounts["total"] += 1
  51. if project.StatusTag == nil {
  52. r.ProjectCounts["active"] += 1
  53. } else {
  54. r.ProjectCounts[*project.StatusTag] += 1
  55. }
  56. for _, task := range project.Tasks {
  57. r.TaskCounts["total"] += 1
  58. if task.StatusTag == nil {
  59. r.TaskCounts["active"] += 1
  60. } else {
  61. r.TaskCounts[*task.StatusTag] += 1
  62. }
  63. }
  64. }
  65. }
  66. type ProjectGroupRepository interface {
  67. Find(ctx context.Context, id string) (*ProjectGroup, error)
  68. List(ctx context.Context, filter ProjectGroupFilter) ([]*ProjectGroup, error)
  69. Insert(ctx context.Context, group ProjectGroup) error
  70. Update(ctx context.Context, group ProjectGroup) error
  71. Delete(ctx context.Context, group ProjectGroup) error
  72. }