mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Containing Group/Sub-Group relationships (#5105)
* Add UI support for setting containing groups * Show containing groups in group details panel * Move tag hierarchical filter code into separate type * Add depth to scene_count and add sub_group_count * Add sub-groups tab to groups page * Add containing groups to edit groups dialog * Show containing group description in sub-group view * Show group scene number in group scenes view * Add ability to drag move grid cards * Add sub group order option * Add reorder sub-groups interface * Separate page size selector component * Add interfaces to add and remove sub-groups to a group * Separate MultiSet components * Allow setting description while setting containing groups
This commit is contained in:
112
pkg/group/update.go
Normal file
112
pkg/group/update.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/sliceutil"
|
||||
)
|
||||
|
||||
type SubGroupAlreadyInGroupError struct {
|
||||
GroupIDs []int
|
||||
}
|
||||
|
||||
func (e *SubGroupAlreadyInGroupError) Error() string {
|
||||
return fmt.Sprintf("subgroups with IDs %v already in group", e.GroupIDs)
|
||||
}
|
||||
|
||||
type ImageInput struct {
|
||||
Image []byte
|
||||
Set bool
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePartial(ctx context.Context, id int, updatedGroup models.GroupPartial, frontImage ImageInput, backImage ImageInput) (*models.Group, error) {
|
||||
if err := s.validateUpdate(ctx, id, updatedGroup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := s.Repository
|
||||
|
||||
group, err := r.UpdatePartial(ctx, id, updatedGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// update image table
|
||||
if frontImage.Set {
|
||||
if err := r.UpdateFrontImage(ctx, id, frontImage.Image); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if backImage.Set {
|
||||
if err := r.UpdateBackImage(ctx, id, backImage.Image); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (s *Service) AddSubGroups(ctx context.Context, groupID int, subGroups []models.GroupIDDescription, insertIndex *int) error {
|
||||
// get the group
|
||||
existing, err := s.Repository.Find(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ensure it exists
|
||||
if existing == nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
// ensure the subgroups aren't already sub-groups of the group
|
||||
subGroupIDs := sliceutil.Map(subGroups, func(sg models.GroupIDDescription) int {
|
||||
return sg.GroupID
|
||||
})
|
||||
|
||||
existingSubGroupIDs, err := s.Repository.FindSubGroupIDs(ctx, groupID, subGroupIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(existingSubGroupIDs) > 0 {
|
||||
return &SubGroupAlreadyInGroupError{
|
||||
GroupIDs: existingSubGroupIDs,
|
||||
}
|
||||
}
|
||||
|
||||
// validate the hierarchy
|
||||
d := &models.UpdateGroupDescriptions{
|
||||
Groups: subGroups,
|
||||
Mode: models.RelationshipUpdateModeAdd,
|
||||
}
|
||||
if err := s.validateUpdateGroupHierarchy(ctx, existing, nil, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// validate insert index
|
||||
if insertIndex != nil && *insertIndex < 0 {
|
||||
return ErrInvalidInsertIndex
|
||||
}
|
||||
|
||||
// add the subgroups
|
||||
return s.Repository.AddSubGroups(ctx, groupID, subGroups, insertIndex)
|
||||
}
|
||||
|
||||
func (s *Service) RemoveSubGroups(ctx context.Context, groupID int, subGroupIDs []int) error {
|
||||
// get the group
|
||||
existing, err := s.Repository.Find(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ensure it exists
|
||||
if existing == nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
// add the subgroups
|
||||
return s.Repository.RemoveSubGroups(ctx, groupID, subGroupIDs)
|
||||
}
|
||||
Reference in New Issue
Block a user