folders.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 saveable 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. }
  73. //
  74. // QUERIES
  75. //
  76. type HasEditPermissionInFoldersQuery struct {
  77. SignedInUser *SignedInUser
  78. Result bool
  79. }
  80. type HasAdminPermissionInFoldersQuery struct {
  81. SignedInUser *SignedInUser
  82. Result bool
  83. }