Model refactor, part 2 (#4092)

* 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>
This commit is contained in:
DingDongSoLong4
2023-09-11 04:24:15 +02:00
committed by GitHub
parent cf3301c8bc
commit 24e4719abc
61 changed files with 1514 additions and 1407 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"os"
"strconv"
"time"
"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/pkg/file"
@@ -18,6 +17,7 @@ import (
"github.com/stashapp/stash/pkg/utils"
)
// used to refetch gallery after hooks run
func (r *mutationResolver) getGallery(ctx context.Context, id int) (ret *models.Gallery, err error) {
if err := r.withTxn(ctx, func(ctx context.Context) error {
ret, err = r.repository.Gallery.Find(ctx, id)
@@ -39,42 +39,38 @@ func (r *mutationResolver) GalleryCreate(ctx context.Context, input GalleryCreat
inputMap: getUpdateInputMap(ctx),
}
performerIDs, err := stringslice.StringSliceToIntSlice(input.PerformerIds)
if err != nil {
return nil, fmt.Errorf("converting performer ids: %w", err)
}
tagIDs, err := stringslice.StringSliceToIntSlice(input.TagIds)
if err != nil {
return nil, fmt.Errorf("converting tag ids: %w", err)
}
sceneIDs, err := stringslice.StringSliceToIntSlice(input.SceneIds)
if err != nil {
return nil, fmt.Errorf("converting scene ids: %w", err)
}
// Populate a new gallery from the input
currentTime := time.Now()
newGallery := models.Gallery{
Title: input.Title,
URL: translator.string(input.URL, "url"),
Details: translator.string(input.Details, "details"),
Rating: translator.ratingConversionInt(input.Rating, input.Rating100),
PerformerIDs: models.NewRelatedIDs(performerIDs),
TagIDs: models.NewRelatedIDs(tagIDs),
SceneIDs: models.NewRelatedIDs(sceneIDs),
CreatedAt: currentTime,
UpdatedAt: currentTime,
}
newGallery := models.NewGallery()
newGallery.Date, err = translator.datePtr(input.Date, "date")
newGallery.Title = input.Title
newGallery.URL = translator.string(input.URL)
newGallery.Details = translator.string(input.Details)
newGallery.Rating = translator.ratingConversion(input.Rating, input.Rating100)
var err error
newGallery.Date, err = translator.datePtr(input.Date)
if err != nil {
return nil, fmt.Errorf("converting date: %w", err)
}
newGallery.StudioID, err = translator.intPtrFromString(input.StudioID, "studio_id")
newGallery.StudioID, err = translator.intPtrFromString(input.StudioID)
if err != nil {
return nil, fmt.Errorf("converting studio id: %w", err)
}
newGallery.PerformerIDs, err = translator.relatedIds(input.PerformerIds)
if err != nil {
return nil, fmt.Errorf("converting performer ids: %w", err)
}
newGallery.TagIDs, err = translator.relatedIds(input.TagIds)
if err != nil {
return nil, fmt.Errorf("converting tag ids: %w", err)
}
newGallery.SceneIDs, err = translator.relatedIds(input.SceneIds)
if err != nil {
return nil, fmt.Errorf("converting scene ids: %w", err)
}
// Start the transaction and save the gallery
if err := r.withTxn(ctx, func(ctx context.Context) error {
qb := r.repository.Gallery
@@ -140,6 +136,7 @@ func (r *mutationResolver) GalleriesUpdate(ctx context.Context, input []*models.
}
r.hookExecutor.ExecutePostHooks(ctx, gallery.ID, plugin.GalleryUpdatePost, input, translator.getFields())
gallery, err = r.getGallery(ctx, gallery.ID)
if err != nil {
return nil, err
@@ -154,7 +151,7 @@ func (r *mutationResolver) GalleriesUpdate(ctx context.Context, input []*models.
func (r *mutationResolver) galleryUpdate(ctx context.Context, input models.GalleryUpdateInput, translator changesetTranslator) (*models.Gallery, error) {
galleryID, err := strconv.Atoi(input.ID)
if err != nil {
return nil, err
return nil, fmt.Errorf("converting id: %w", err)
}
qb := r.repository.Gallery
@@ -182,25 +179,24 @@ func (r *mutationResolver) galleryUpdate(ctx context.Context, input models.Galle
updatedGallery.Details = translator.optionalString(input.Details, "details")
updatedGallery.URL = translator.optionalString(input.URL, "url")
updatedGallery.Rating = translator.optionalRatingConversion(input.Rating, input.Rating100)
updatedGallery.Organized = translator.optionalBool(input.Organized, "organized")
updatedGallery.Date, err = translator.optionalDate(input.Date, "date")
if err != nil {
return nil, fmt.Errorf("converting date: %w", err)
}
updatedGallery.Rating = translator.ratingConversionOptional(input.Rating, input.Rating100)
updatedGallery.StudioID, err = translator.optionalIntFromString(input.StudioID, "studio_id")
if err != nil {
return nil, fmt.Errorf("converting studio id: %w", err)
}
updatedGallery.Organized = translator.optionalBool(input.Organized, "organized")
if input.PrimaryFileID != nil {
primaryFileID, err := strconv.Atoi(*input.PrimaryFileID)
if err != nil {
return nil, fmt.Errorf("converting primary file id: %w", err)
}
converted := models.FileID(primaryFileID)
updatedGallery.PrimaryFileID = &converted
updatedGallery.PrimaryFileID, err = translator.fileIDPtrFromString(input.PrimaryFileID)
if err != nil {
return nil, fmt.Errorf("converting primary file id: %w", err)
}
if updatedGallery.PrimaryFileID != nil {
primaryFileID := *updatedGallery.PrimaryFileID
if err := originalGallery.LoadFiles(ctx, r.repository.Gallery); err != nil {
return nil, err
@@ -209,35 +205,27 @@ func (r *mutationResolver) galleryUpdate(ctx context.Context, input models.Galle
// ensure that new primary file is associated with gallery
var f models.File
for _, ff := range originalGallery.Files.List() {
if ff.Base().ID == converted {
if ff.Base().ID == primaryFileID {
f = ff
}
}
if f == nil {
return nil, fmt.Errorf("file with id %d not associated with gallery", converted)
return nil, fmt.Errorf("file with id %d not associated with gallery", primaryFileID)
}
}
if translator.hasField("performer_ids") {
updatedGallery.PerformerIDs, err = translateUpdateIDs(input.PerformerIds, models.RelationshipUpdateModeSet)
if err != nil {
return nil, fmt.Errorf("converting performer ids: %w", err)
}
updatedGallery.PerformerIDs, err = translator.updateIds(input.PerformerIds, "performer_ids")
if err != nil {
return nil, fmt.Errorf("converting performer ids: %w", err)
}
if translator.hasField("tag_ids") {
updatedGallery.TagIDs, err = translateUpdateIDs(input.TagIds, models.RelationshipUpdateModeSet)
if err != nil {
return nil, fmt.Errorf("converting tag ids: %w", err)
}
updatedGallery.TagIDs, err = translator.updateIds(input.TagIds, "tag_ids")
if err != nil {
return nil, fmt.Errorf("converting tag ids: %w", err)
}
if translator.hasField("scene_ids") {
updatedGallery.SceneIDs, err = translateUpdateIDs(input.SceneIds, models.RelationshipUpdateModeSet)
if err != nil {
return nil, fmt.Errorf("converting scene ids: %w", err)
}
updatedGallery.SceneIDs, err = translator.updateIds(input.SceneIds, "scene_ids")
if err != nil {
return nil, fmt.Errorf("converting scene ids: %w", err)
}
// gallery scene is set from the scene only
@@ -253,7 +241,7 @@ func (r *mutationResolver) galleryUpdate(ctx context.Context, input models.Galle
func (r *mutationResolver) BulkGalleryUpdate(ctx context.Context, input BulkGalleryUpdateInput) ([]*models.Gallery, error) {
galleryIDs, err := stringslice.StringSliceToIntSlice(input.Ids)
if err != nil {
return nil, err
return nil, fmt.Errorf("converting ids: %w", err)
}
translator := changesetTranslator{
@@ -265,36 +253,29 @@ func (r *mutationResolver) BulkGalleryUpdate(ctx context.Context, input BulkGall
updatedGallery.Details = translator.optionalString(input.Details, "details")
updatedGallery.URL = translator.optionalString(input.URL, "url")
updatedGallery.Rating = translator.optionalRatingConversion(input.Rating, input.Rating100)
updatedGallery.Organized = translator.optionalBool(input.Organized, "organized")
updatedGallery.Date, err = translator.optionalDate(input.Date, "date")
if err != nil {
return nil, fmt.Errorf("converting date: %w", err)
}
updatedGallery.Rating = translator.ratingConversionOptional(input.Rating, input.Rating100)
updatedGallery.StudioID, err = translator.optionalIntFromString(input.StudioID, "studio_id")
if err != nil {
return nil, fmt.Errorf("converting studio id: %w", err)
}
updatedGallery.Organized = translator.optionalBool(input.Organized, "organized")
if translator.hasField("performer_ids") {
updatedGallery.PerformerIDs, err = translateUpdateIDs(input.PerformerIds.Ids, input.PerformerIds.Mode)
if err != nil {
return nil, fmt.Errorf("converting performer ids: %w", err)
}
updatedGallery.PerformerIDs, err = translator.updateIdsBulk(input.PerformerIds, "performer_ids")
if err != nil {
return nil, fmt.Errorf("converting performer ids: %w", err)
}
if translator.hasField("tag_ids") {
updatedGallery.TagIDs, err = translateUpdateIDs(input.TagIds.Ids, input.TagIds.Mode)
if err != nil {
return nil, fmt.Errorf("converting tag ids: %w", err)
}
updatedGallery.TagIDs, err = translator.updateIdsBulk(input.TagIds, "tag_ids")
if err != nil {
return nil, fmt.Errorf("converting tag ids: %w", err)
}
if translator.hasField("scene_ids") {
updatedGallery.SceneIDs, err = translateUpdateIDs(input.SceneIds.Ids, input.SceneIds.Mode)
if err != nil {
return nil, fmt.Errorf("converting scene ids: %w", err)
}
updatedGallery.SceneIDs, err = translator.updateIdsBulk(input.SceneIds, "scene_ids")
if err != nil {
return nil, fmt.Errorf("converting scene ids: %w", err)
}
ret := []*models.Gallery{}
@@ -336,7 +317,7 @@ func (r *mutationResolver) BulkGalleryUpdate(ctx context.Context, input BulkGall
func (r *mutationResolver) GalleryDestroy(ctx context.Context, input models.GalleryDestroyInput) (bool, error) {
galleryIDs, err := stringslice.StringSliceToIntSlice(input.Ids)
if err != nil {
return false, err
return false, fmt.Errorf("converting ids: %w", err)
}
var galleries []*models.Gallery
@@ -427,12 +408,12 @@ func isStashPath(path string) bool {
func (r *mutationResolver) AddGalleryImages(ctx context.Context, input GalleryAddInput) (bool, error) {
galleryID, err := strconv.Atoi(input.GalleryID)
if err != nil {
return false, err
return false, fmt.Errorf("converting gallery id: %w", err)
}
imageIDs, err := stringslice.StringSliceToIntSlice(input.ImageIds)
if err != nil {
return false, err
return false, fmt.Errorf("converting image ids: %w", err)
}
if err := r.withTxn(ctx, func(ctx context.Context) error {
@@ -457,12 +438,12 @@ func (r *mutationResolver) AddGalleryImages(ctx context.Context, input GalleryAd
func (r *mutationResolver) RemoveGalleryImages(ctx context.Context, input GalleryRemoveInput) (bool, error) {
galleryID, err := strconv.Atoi(input.GalleryID)
if err != nil {
return false, err
return false, fmt.Errorf("converting gallery id: %w", err)
}
imageIDs, err := stringslice.StringSliceToIntSlice(input.ImageIds)
if err != nil {
return false, err
return false, fmt.Errorf("converting image ids: %w", err)
}
if err := r.withTxn(ctx, func(ctx context.Context) error {
@@ -501,14 +482,12 @@ func (r *mutationResolver) GalleryChapterCreate(ctx context.Context, input Galle
return nil, fmt.Errorf("converting gallery id: %w", err)
}
currentTime := time.Now()
newChapter := models.GalleryChapter{
Title: input.Title,
ImageIndex: input.ImageIndex,
GalleryID: galleryID,
CreatedAt: currentTime,
UpdatedAt: currentTime,
}
// Populate a new gallery chapter from the input
newChapter := models.NewGalleryChapter()
newChapter.Title = input.Title
newChapter.ImageIndex = input.ImageIndex
newChapter.GalleryID = galleryID
// Start the transaction and save the gallery chapter
if err := r.withTxn(ctx, func(ctx context.Context) error {
@@ -534,7 +513,7 @@ func (r *mutationResolver) GalleryChapterCreate(ctx context.Context, input Galle
func (r *mutationResolver) GalleryChapterUpdate(ctx context.Context, input GalleryChapterUpdateInput) (*models.GalleryChapter, error) {
chapterID, err := strconv.Atoi(input.ID)
if err != nil {
return nil, err
return nil, fmt.Errorf("converting id: %w", err)
}
translator := changesetTranslator{
@@ -600,7 +579,7 @@ func (r *mutationResolver) GalleryChapterUpdate(ctx context.Context, input Galle
func (r *mutationResolver) GalleryChapterDestroy(ctx context.Context, id string) (bool, error) {
chapterID, err := strconv.Atoi(id)
if err != nil {
return false, err
return false, fmt.Errorf("converting id: %w", err)
}
if err := r.withTxn(ctx, func(ctx context.Context) error {