folders.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package models
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. )
  7. // Typed errors
  8. var (
  9. ErrFolderNotFound = errors.New("Folder not found")
  10. ErrFolderVersionMismatch = errors.New("The folder has been changed by someone else")
  11. ErrFolderTitleEmpty = errors.New("Folder title cannot be empty")
  12. ErrFolderWithSameUIDExists = errors.New("A folder/dashboard with the same uid already exists")
  13. ErrFolderSameNameExists = errors.New("A folder or dashboard in the general folder with the same name already exists")
  14. ErrFolderFailedGenerateUniqueUid = errors.New("Failed to generate unique folder id")
  15. )
  16. type Folder struct {
  17. Id int64
  18. Uid string
  19. Title string
  20. Url string
  21. OrgId int64
  22. Version int
  23. Created time.Time
  24. Updated time.Time
  25. UpdatedBy int64
  26. CreatedBy int64
  27. HasAcl bool
  28. }
  29. // GetDashboardModel turns the command into the savable model
  30. func (cmd *CreateFolderCommand) GetDashboardModel() *Dashboard {
  31. dashFolder := NewDashboardFolder(strings.TrimSpace(cmd.Title))
  32. dashFolder.OrgId = cmd.OrgId
  33. dashFolder.Uid = strings.TrimSpace(cmd.Uid)
  34. dashFolder.Data.Set("uid", cmd.Uid)
  35. userId := cmd.UserId
  36. if userId == 0 {
  37. userId = -1
  38. }
  39. dashFolder.CreatedBy = userId
  40. dashFolder.UpdatedBy = userId
  41. dashFolder.UpdateSlug()
  42. return dashFolder
  43. }
  44. // UpdateDashboardModel updates an existing model from command into model for update
  45. func (cmd *UpdateFolderCommand) UpdateDashboardModel(dashFolder *Dashboard) {
  46. dashFolder.Title = strings.TrimSpace(cmd.Title)
  47. dashFolder.Data.Set("title", cmd.Title)
  48. dashFolder.Uid = dashFolder.Data.MustString("uid")
  49. dashFolder.Data.Set("version", cmd.Version)
  50. dashFolder.Version = cmd.Version
  51. dashFolder.IsFolder = true
  52. userId := cmd.UserId
  53. if userId == 0 {
  54. userId = -1
  55. }
  56. dashFolder.UpdatedBy = userId
  57. dashFolder.UpdateSlug()
  58. }
  59. //
  60. // COMMANDS
  61. //
  62. type CreateFolderCommand struct {
  63. OrgId int64 `json:"-"`
  64. UserId int64 `json:"userId"`
  65. Uid string `json:"uid"`
  66. Title string `json:"title"`
  67. Result *Folder
  68. }
  69. type UpdateFolderCommand struct {
  70. OrgId int64 `json:"-"`
  71. UserId int64 `json:"userId"`
  72. Title string `json:"title"`
  73. Version int `json:"version"`
  74. Overwrite bool `json:"overwrite"`
  75. Result *Folder
  76. }
  77. //
  78. // QUERIES
  79. //
  80. type DashboardFolder struct {
  81. Id int64 `json:"id"`
  82. Title string `json:"title"`
  83. }
  84. type GetFoldersForSignedInUserQuery struct {
  85. OrgId int64
  86. SignedInUser *SignedInUser
  87. Title string
  88. Result []*DashboardFolder
  89. }