Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model refactor, part 2 #4092

Merged
merged 11 commits into from
Sep 11, 2023
164 changes: 157 additions & 7 deletions internal/api/changeset_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"strings"

"github.com/99designs/gqlgen/graphql"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
)

const updateInputField = "input"
Expand Down Expand Up @@ -151,35 +153,35 @@ func (t changesetTranslator) intPtrFromString(value *string, field string) (*int
return &vv, nil
}

func (t changesetTranslator) ratingConversionInt(legacyValue *int, rating100Value *int) *int {
func (t changesetTranslator) ratingConversion(legacyValue *int, rating100Value *int) *int {
const (
legacyField = "rating"
rating100Field = "rating100"
)

legacyRating := t.optionalInt(legacyValue, legacyField)
if legacyRating.Set && !(legacyRating.Null) {
ret := int(models.Rating5To100(int(legacyRating.Value)))
if legacyRating.Set && !legacyRating.Null {
ret := models.Rating5To100(legacyRating.Value)
return &ret
}

o := t.optionalInt(rating100Value, rating100Field)
if o.Set && !(o.Null) {
if o.Set && !o.Null {
return &o.Value
}

return nil
}

func (t changesetTranslator) ratingConversionOptional(legacyValue *int, rating100Value *int) models.OptionalInt {
func (t changesetTranslator) optionalRatingConversion(legacyValue *int, rating100Value *int) models.OptionalInt {
const (
legacyField = "rating"
rating100Field = "rating100"
)

legacyRating := t.optionalInt(legacyValue, legacyField)
if legacyRating.Set && !(legacyRating.Null) {
legacyRating.Value = int(models.Rating5To100(int(legacyRating.Value)))
if legacyRating.Set && !legacyRating.Null {
legacyRating.Value = models.Rating5To100(legacyRating.Value)
return legacyRating
}
return t.optionalInt(rating100Value, rating100Field)
Expand Down Expand Up @@ -235,3 +237,151 @@ func (t changesetTranslator) optionalFloat64(value *float64, field string) model

return models.NewOptionalFloat64Ptr(value)
}

func (t changesetTranslator) fileIDPtrFromString(value *string, field string) (*models.FileID, error) {
WithoutPants marked this conversation as resolved.
Show resolved Hide resolved
if value == nil || *value == "" {
return nil, nil
}

vv, err := strconv.Atoi(*value)
if err != nil {
return nil, fmt.Errorf("converting %v to int: %w", *value, err)
}

id := models.FileID(vv)
return &id, nil
}

func (t changesetTranslator) fileIDSliceFromStringSlice(value []string, field string) ([]models.FileID, error) {
ints, err := stringslice.StringSliceToIntSlice(value)
if err != nil {
return nil, err
}

fileIDs := make([]models.FileID, len(ints))
for i, v := range ints {
fileIDs[i] = models.FileID(v)
}

return fileIDs, nil
}

func (t changesetTranslator) relatedIds(value []string, field string) (models.RelatedIDs, error) {
ids, err := stringslice.StringSliceToIntSlice(value)
if err != nil {
return models.RelatedIDs{}, err
}

return models.NewRelatedIDs(ids), nil
}

func (t changesetTranslator) updateIds(value []string, field string) (*models.UpdateIDs, error) {
if !t.hasField(field) {
return nil, nil
}

ids, err := stringslice.StringSliceToIntSlice(value)
if err != nil {
return nil, err
}

return &models.UpdateIDs{
IDs: ids,
Mode: models.RelationshipUpdateModeSet,
}, nil
}

func (t changesetTranslator) updateIdsBulk(value *BulkUpdateIds, field string) (*models.UpdateIDs, error) {
if !t.hasField(field) || value == nil {
return nil, nil
}

ids, err := stringslice.StringSliceToIntSlice(value.Ids)
if err != nil {
return nil, fmt.Errorf("converting ids [%v]: %w", value.Ids, err)
}

return &models.UpdateIDs{
IDs: ids,
Mode: value.Mode,
}, nil
}

func (t changesetTranslator) updateStrings(value []string, field string) *models.UpdateStrings {
if !t.hasField(field) {
return nil
}

return &models.UpdateStrings{
Values: value,
Mode: models.RelationshipUpdateModeSet,
}
}

func (t changesetTranslator) updateStringsBulk(value *BulkUpdateStrings, field string) *models.UpdateStrings {
if !t.hasField(field) || value == nil {
return nil
}

return &models.UpdateStrings{
Values: value.Values,
Mode: value.Mode,
}
}

func (t changesetTranslator) updateStashIDs(value []models.StashID, field string) *models.UpdateStashIDs {
if !t.hasField(field) {
return nil
}

return &models.UpdateStashIDs{
StashIDs: value,
Mode: models.RelationshipUpdateModeSet,
}
}

func (t changesetTranslator) relatedMovies(value []models.SceneMovieInput, field string) (models.RelatedMovies, error) {
moviesScenes, err := models.MoviesScenesFromInput(value)
if err != nil {
return models.RelatedMovies{}, err
}

return models.NewRelatedMovies(moviesScenes), nil
}

func (t changesetTranslator) updateMovieIDs(value []models.SceneMovieInput, field string) (*models.UpdateMovieIDs, error) {
if !t.hasField(field) {
return nil, nil
}

moviesScenes, err := models.MoviesScenesFromInput(value)
if err != nil {
return nil, err
}

return &models.UpdateMovieIDs{
Movies: moviesScenes,
Mode: models.RelationshipUpdateModeSet,
}, nil
}

func (t changesetTranslator) updateMovieIDsBulk(value *BulkUpdateIds, field string) (*models.UpdateMovieIDs, error) {
if !t.hasField(field) || value == nil {
return nil, nil
}

ids, err := stringslice.StringSliceToIntSlice(value.Ids)
if err != nil {
return nil, fmt.Errorf("converting ids [%v]: %w", value.Ids, err)
}

movies := make([]models.MoviesScenes, len(value.Ids))
for _, id := range ids {
movies = append(movies, models.MoviesScenes{MovieID: id})
}

return &models.UpdateMovieIDs{
Movies: movies,
Mode: value.Mode,
}, nil
}
10 changes: 0 additions & 10 deletions internal/api/resolver_model_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,6 @@ func (r *sceneResolver) Performers(ctx context.Context, obj *models.Scene) (ret
return ret, firstError(errs)
}

func stashIDsSliceToPtrSlice(v []models.StashID) []*models.StashID {
ret := make([]*models.StashID, len(v))
for i, vv := range v {
c := vv
ret[i] = &c
}

return ret
}

func (r *sceneResolver) StashIds(ctx context.Context, obj *models.Scene) (ret []*models.StashID, err error) {
if err := r.withReadTxn(ctx, func(ctx context.Context) error {
return obj.LoadStashIDs(ctx, r.repository.Scene)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/resolver_mutation_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *mutationResolver) MoveFiles(ctx context.Context, input MoveFilesInput)

fileIDs, err := stringslice.StringSliceToIntSlice(input.Ids)
if err != nil {
return fmt.Errorf("converting file ids: %w", err)
return fmt.Errorf("converting ids: %w", err)
}

switch {
Expand All @@ -35,7 +35,7 @@ func (r *mutationResolver) MoveFiles(ctx context.Context, input MoveFilesInput)

folderID, err := strconv.Atoi(*input.DestinationFolderID)
if err != nil {
return fmt.Errorf("invalid folder id %s: %w", *input.DestinationFolderID, err)
return fmt.Errorf("converting destination folder id: %w", err)
}

folder, err = folderStore.Find(ctx, models.FolderID(folderID))
Expand Down Expand Up @@ -146,7 +146,7 @@ func (r *mutationResolver) validateFileExtensionList(exts []string, oldBasename,
func (r *mutationResolver) DeleteFiles(ctx context.Context, ids []string) (ret bool, err error) {
fileIDs, err := stringslice.StringSliceToIntSlice(ids)
if err != nil {
return false, err
return false, fmt.Errorf("converting ids: %w", err)
}

fileDeleter := file.NewDeleter()
Expand Down
Loading
Loading