folders.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. ErrFolderAccessDenied = errors.New("Access denied to folder")
  16. )
  17. type Folder struct {
  18. Id int64
  19. Uid string
  20. Title string
  21. Url string
  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(orgId int64, userId int64) *Dashboard {
  31. dashFolder := NewDashboardFolder(strings.TrimSpace(cmd.Title))
  32. dashFolder.OrgId = orgId
  33. dashFolder.SetUid(strings.TrimSpace(cmd.Uid))
  34. if userId == 0 {
  35. userId = -1
  36. }
  37. dashFolder.CreatedBy = userId
  38. dashFolder.UpdatedBy = userId
  39. dashFolder.UpdateSlug()
  40. return dashFolder
  41. }
  42. // UpdateDashboardModel updates an existing model from command into model for update
  43. func (cmd *UpdateFolderCommand) UpdateDashboardModel(dashFolder *Dashboard, orgId int64, userId int64) {
  44. dashFolder.OrgId = orgId
  45. dashFolder.Title = strings.TrimSpace(cmd.Title)
  46. dashFolder.Data.Set("title", dashFolder.Title)
  47. if cmd.Uid != "" {
  48. dashFolder.SetUid(cmd.Uid)
  49. }
  50. dashFolder.SetVersion(cmd.Version)
  51. dashFolder.IsFolder = true
  52. if userId == 0 {
  53. userId = -1
  54. }
  55. dashFolder.UpdatedBy = userId
  56. dashFolder.UpdateSlug()
  57. }
  58. //
  59. // COMMANDS
  60. //
  61. type CreateFolderCommand struct {
  62. Uid string `json:"uid"`
  63. Title string `json:"title"`
  64. Result *Folder
  65. }
  66. type UpdateFolderCommand struct {
  67. Uid string `json:"uid"`
  68. Title string `json:"title"`
  69. Version int `json:"version"`
  70. Overwrite bool `json:"overwrite"`
  71. Result *Folder
  72. }