Skip to content

Commit

Permalink
Move conversions into changesetTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
DingDongSoLong4 committed Sep 2, 2023
1 parent 7a92143 commit 4bec851
Show file tree
Hide file tree
Showing 14 changed files with 657 additions and 555 deletions.
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) {
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
Loading

0 comments on commit 4bec851

Please sign in to comment.