2020-06-15 11:34:39 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func ValidateModifyStudio(studio models.StudioPartial, qb models.StudioReader) error {
|
2020-06-15 11:34:39 +00:00
|
|
|
if studio.ParentID == nil || !studio.ParentID.Valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure there is no cyclic dependency
|
|
|
|
thisID := studio.ID
|
|
|
|
|
|
|
|
currentParentID := *studio.ParentID
|
|
|
|
|
|
|
|
for currentParentID.Valid {
|
|
|
|
if currentParentID.Int64 == int64(thisID) {
|
|
|
|
return errors.New("studio cannot be an ancestor of itself")
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
currentStudio, err := qb.Find(int(currentParentID.Int64))
|
2020-06-15 11:34:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error finding parent studio: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
currentParentID = currentStudio.ParentID
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|