2018-01-29 06:51:01 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-02-01 14:00:37 -06:00
|
|
|
"strings"
|
2018-01-29 06:51:01 -06:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Typed errors
|
|
|
|
var (
|
2020-11-05 06:07:06 -06:00
|
|
|
ErrFolderNotFound = errors.New("folder not found")
|
|
|
|
ErrFolderVersionMismatch = errors.New("the folder has been changed by someone else")
|
|
|
|
ErrFolderTitleEmpty = errors.New("folder title cannot be empty")
|
|
|
|
ErrFolderWithSameUIDExists = errors.New("a folder/dashboard with the same uid already exists")
|
|
|
|
ErrFolderSameNameExists = errors.New("a folder or dashboard in the general folder with the same name already exists")
|
|
|
|
ErrFolderFailedGenerateUniqueUid = errors.New("failed to generate unique folder ID")
|
|
|
|
ErrFolderAccessDenied = errors.New("access denied to folder")
|
2018-01-29 06:51:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Folder struct {
|
|
|
|
Id int64
|
2018-02-01 14:00:37 -06:00
|
|
|
Uid string
|
2018-01-29 06:51:01 -06:00
|
|
|
Title string
|
2018-02-01 14:00:37 -06:00
|
|
|
Url string
|
2018-01-29 06:51:01 -06:00
|
|
|
Version int
|
|
|
|
|
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
|
|
|
|
UpdatedBy int64
|
|
|
|
CreatedBy int64
|
|
|
|
HasAcl bool
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:40:14 -05:00
|
|
|
// GetDashboardModel turns the command into the saveable model
|
2018-02-20 06:55:43 -06:00
|
|
|
func (cmd *CreateFolderCommand) GetDashboardModel(orgId int64, userId int64) *Dashboard {
|
2018-02-01 14:00:37 -06:00
|
|
|
dashFolder := NewDashboardFolder(strings.TrimSpace(cmd.Title))
|
2018-02-20 06:55:43 -06:00
|
|
|
dashFolder.OrgId = orgId
|
|
|
|
dashFolder.SetUid(strings.TrimSpace(cmd.Uid))
|
2018-02-01 14:00:37 -06:00
|
|
|
|
|
|
|
if userId == 0 {
|
|
|
|
userId = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
dashFolder.CreatedBy = userId
|
|
|
|
dashFolder.UpdatedBy = userId
|
|
|
|
dashFolder.UpdateSlug()
|
|
|
|
|
|
|
|
return dashFolder
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDashboardModel updates an existing model from command into model for update
|
2018-02-20 06:55:43 -06:00
|
|
|
func (cmd *UpdateFolderCommand) UpdateDashboardModel(dashFolder *Dashboard, orgId int64, userId int64) {
|
|
|
|
dashFolder.OrgId = orgId
|
2018-02-01 14:00:37 -06:00
|
|
|
dashFolder.Title = strings.TrimSpace(cmd.Title)
|
2018-02-20 06:55:43 -06:00
|
|
|
dashFolder.Data.Set("title", dashFolder.Title)
|
|
|
|
|
|
|
|
if cmd.Uid != "" {
|
|
|
|
dashFolder.SetUid(cmd.Uid)
|
|
|
|
}
|
2018-02-01 14:00:37 -06:00
|
|
|
|
2018-02-20 06:55:43 -06:00
|
|
|
dashFolder.SetVersion(cmd.Version)
|
|
|
|
dashFolder.IsFolder = true
|
2018-02-01 14:00:37 -06:00
|
|
|
|
|
|
|
if userId == 0 {
|
|
|
|
userId = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
dashFolder.UpdatedBy = userId
|
|
|
|
dashFolder.UpdateSlug()
|
|
|
|
}
|
|
|
|
|
2018-01-29 06:51:01 -06:00
|
|
|
//
|
|
|
|
// COMMANDS
|
|
|
|
//
|
|
|
|
|
|
|
|
type CreateFolderCommand struct {
|
2018-02-21 04:24:54 -06:00
|
|
|
Uid string `json:"uid"`
|
|
|
|
Title string `json:"title"`
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
Result *Folder
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateFolderCommand struct {
|
2018-02-20 06:55:43 -06:00
|
|
|
Uid string `json:"uid"`
|
2018-02-01 14:00:37 -06:00
|
|
|
Title string `json:"title"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
Overwrite bool `json:"overwrite"`
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
Result *Folder
|
|
|
|
}
|
2018-04-30 08:34:31 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// QUERIES
|
|
|
|
//
|
|
|
|
|
|
|
|
type HasEditPermissionInFoldersQuery struct {
|
|
|
|
SignedInUser *SignedInUser
|
|
|
|
Result bool
|
|
|
|
}
|
2019-08-12 13:03:48 -05:00
|
|
|
|
|
|
|
type HasAdminPermissionInFoldersQuery struct {
|
|
|
|
SignedInUser *SignedInUser
|
|
|
|
Result bool
|
|
|
|
}
|