Skip to content

Commit

Permalink
Model refactor, part 2 (stashapp#4092)
Browse files Browse the repository at this point in the history
* Move conversions into changesetTranslator
* Improve mutation error messages
* Use models.New and models.NewPartial everywhere
* Replace getStashIDsFor functions
* Remove ImageCreateInput
* Remove unused parameters
* Refactor matching functions
---------
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
  • Loading branch information
DingDongSoLong4 authored and halkeye committed Sep 1, 2024
1 parent 19de6a1 commit cb655e6
Show file tree
Hide file tree
Showing 61 changed files with 1,501 additions and 1,394 deletions.
172 changes: 161 additions & 11 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 @@ -91,7 +93,7 @@ func (t changesetTranslator) getFields() []string {
return ret
}

func (t changesetTranslator) string(value *string, field string) string {
func (t changesetTranslator) string(value *string) string {
if value == nil {
return ""
}
Expand Down Expand Up @@ -127,7 +129,7 @@ func (t changesetTranslator) optionalDate(value *string, field string) (models.O
return models.NewOptionalDate(date), nil
}

func (t changesetTranslator) datePtr(value *string, field string) (*models.Date, error) {
func (t changesetTranslator) datePtr(value *string) (*models.Date, error) {
if value == nil || *value == "" {
return nil, nil
}
Expand All @@ -139,7 +141,7 @@ func (t changesetTranslator) datePtr(value *string, field string) (*models.Date,
return &date, nil
}

func (t changesetTranslator) intPtrFromString(value *string, field string) (*int, error) {
func (t changesetTranslator) intPtrFromString(value *string) (*int, error) {
if value == nil || *value == "" {
return nil, nil
}
Expand All @@ -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 @@ -212,7 +214,7 @@ func (t changesetTranslator) optionalIntFromString(value *string, field string)
return models.NewOptionalInt(vv), nil
}

func (t changesetTranslator) bool(value *bool, field string) bool {
func (t changesetTranslator) bool(value *bool) bool {
if value == nil {
return false
}
Expand All @@ -235,3 +237,151 @@ func (t changesetTranslator) optionalFloat64(value *float64, field string) model

return models.NewOptionalFloat64Ptr(value)
}

func (t changesetTranslator) fileIDPtrFromString(value *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) ([]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) (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) (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

0 comments on commit cb655e6

Please sign in to comment.