Skip to content

Commit

Permalink
Use models.New and models.NewPartial everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
DingDongSoLong4 committed Sep 2, 2023
1 parent f74252b commit 8b33f95
Show file tree
Hide file tree
Showing 35 changed files with 517 additions and 613 deletions.
30 changes: 12 additions & 18 deletions internal/api/resolver_mutation_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"strconv"
"time"

"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/pkg/file"
Expand Down Expand Up @@ -41,15 +40,12 @@ func (r *mutationResolver) GalleryCreate(ctx context.Context, input GalleryCreat
}

// 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.ratingConversion(input.Rating, input.Rating100),
CreatedAt: currentTime,
UpdatedAt: currentTime,
}
newGallery := models.NewGallery()

newGallery.Title = input.Title
newGallery.URL = translator.string(input.URL, "url")
newGallery.Details = translator.string(input.Details, "details")
newGallery.Rating = translator.ratingConversion(input.Rating, input.Rating100)

var err error

Expand Down Expand Up @@ -486,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 {
Expand Down
22 changes: 9 additions & 13 deletions internal/api/resolver_mutation_movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"time"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/plugin"
Expand All @@ -30,18 +29,15 @@ func (r *mutationResolver) MovieCreate(ctx context.Context, input MovieCreateInp
}

// Populate a new movie from the input
currentTime := time.Now()
newMovie := models.Movie{
Name: input.Name,
CreatedAt: currentTime,
UpdatedAt: currentTime,
Aliases: translator.string(input.Aliases, "aliases"),
Duration: input.Duration,
Rating: translator.ratingConversion(input.Rating, input.Rating100),
Director: translator.string(input.Director, "director"),
Synopsis: translator.string(input.Synopsis, "synopsis"),
URL: translator.string(input.URL, "url"),
}
newMovie := models.NewMovie()

newMovie.Name = input.Name
newMovie.Aliases = translator.string(input.Aliases, "aliases")
newMovie.Duration = input.Duration
newMovie.Rating = translator.ratingConversion(input.Rating, input.Rating100)
newMovie.Director = translator.string(input.Director, "director")
newMovie.Synopsis = translator.string(input.Synopsis, "synopsis")
newMovie.URL = translator.string(input.URL, "url")

var err error

Expand Down
54 changes: 25 additions & 29 deletions internal/api/resolver_mutation_performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"time"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/performer"
Expand All @@ -31,34 +30,31 @@ func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.Per
}

// Populate a new performer from the input
currentTime := time.Now()
newPerformer := models.Performer{
Name: input.Name,
Disambiguation: translator.string(input.Disambiguation, "disambiguation"),
URL: translator.string(input.URL, "url"),
Gender: input.Gender,
Ethnicity: translator.string(input.Ethnicity, "ethnicity"),
Country: translator.string(input.Country, "country"),
EyeColor: translator.string(input.EyeColor, "eye_color"),
Measurements: translator.string(input.Measurements, "measurements"),
FakeTits: translator.string(input.FakeTits, "fake_tits"),
PenisLength: input.PenisLength,
Circumcised: input.Circumcised,
CareerLength: translator.string(input.CareerLength, "career_length"),
Tattoos: translator.string(input.Tattoos, "tattoos"),
Piercings: translator.string(input.Piercings, "piercings"),
Twitter: translator.string(input.Twitter, "twitter"),
Instagram: translator.string(input.Instagram, "instagram"),
Favorite: translator.bool(input.Favorite, "favorite"),
Rating: translator.ratingConversion(input.Rating, input.Rating100),
Details: translator.string(input.Details, "details"),
HairColor: translator.string(input.HairColor, "hair_color"),
Weight: input.Weight,
IgnoreAutoTag: translator.bool(input.IgnoreAutoTag, "ignore_auto_tag"),
CreatedAt: currentTime,
UpdatedAt: currentTime,
StashIDs: models.NewRelatedStashIDs(input.StashIds),
}
newPerformer := models.NewPerformer()

newPerformer.Name = input.Name
newPerformer.Disambiguation = translator.string(input.Disambiguation, "disambiguation")
newPerformer.URL = translator.string(input.URL, "url")
newPerformer.Gender = input.Gender
newPerformer.Ethnicity = translator.string(input.Ethnicity, "ethnicity")
newPerformer.Country = translator.string(input.Country, "country")
newPerformer.EyeColor = translator.string(input.EyeColor, "eye_color")
newPerformer.Measurements = translator.string(input.Measurements, "measurements")
newPerformer.FakeTits = translator.string(input.FakeTits, "fake_tits")
newPerformer.PenisLength = input.PenisLength
newPerformer.Circumcised = input.Circumcised
newPerformer.CareerLength = translator.string(input.CareerLength, "career_length")
newPerformer.Tattoos = translator.string(input.Tattoos, "tattoos")
newPerformer.Piercings = translator.string(input.Piercings, "piercings")
newPerformer.Twitter = translator.string(input.Twitter, "twitter")
newPerformer.Instagram = translator.string(input.Instagram, "instagram")
newPerformer.Favorite = translator.bool(input.Favorite, "favorite")
newPerformer.Rating = translator.ratingConversion(input.Rating, input.Rating100)
newPerformer.Details = translator.string(input.Details, "details")
newPerformer.HairColor = translator.string(input.HairColor, "hair_color")
newPerformer.Weight = input.Weight
newPerformer.IgnoreAutoTag = translator.bool(input.IgnoreAutoTag, "ignore_auto_tag")
newPerformer.StashIDs = models.NewRelatedStashIDs(input.StashIds)

var err error

Expand Down
35 changes: 16 additions & 19 deletions internal/api/resolver_mutation_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"strconv"
"time"

"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/pkg/file"
Expand Down Expand Up @@ -40,15 +39,15 @@ func (r *mutationResolver) SceneCreate(ctx context.Context, input models.SceneCr
}

// Populate a new scene from the input
newScene := models.Scene{
Title: translator.string(input.Title, "title"),
Code: translator.string(input.Code, "code"),
Details: translator.string(input.Details, "details"),
Director: translator.string(input.Director, "director"),
Rating: translator.ratingConversion(input.Rating, input.Rating100),
Organized: translator.bool(input.Organized, "organized"),
StashIDs: models.NewRelatedStashIDs(input.StashIds),
}
newScene := models.NewScene()

newScene.Title = translator.string(input.Title, "title")
newScene.Code = translator.string(input.Code, "code")
newScene.Details = translator.string(input.Details, "details")
newScene.Director = translator.string(input.Director, "director")
newScene.Rating = translator.ratingConversion(input.Rating, input.Rating100)
newScene.Organized = translator.bool(input.Organized, "organized")
newScene.StashIDs = models.NewRelatedStashIDs(input.StashIds)

newScene.Date, err = translator.datePtr(input.Date, "date")
if err != nil {
Expand Down Expand Up @@ -622,15 +621,13 @@ func (r *mutationResolver) SceneMarkerCreate(ctx context.Context, input SceneMar
return nil, fmt.Errorf("converting primary tag id: %w", err)
}

currentTime := time.Now()
newMarker := models.SceneMarker{
Title: input.Title,
Seconds: input.Seconds,
PrimaryTagID: primaryTagID,
SceneID: sceneID,
CreatedAt: currentTime,
UpdatedAt: currentTime,
}
// Populate a new scene marker from the input
newMarker := models.NewSceneMarker()

newMarker.Title = input.Title
newMarker.Seconds = input.Seconds
newMarker.PrimaryTagID = primaryTagID
newMarker.SceneID = sceneID

tagIDs, err := stringslice.StringSliceToIntSlice(input.TagIds)
if err != nil {
Expand Down
43 changes: 19 additions & 24 deletions internal/api/resolver_mutation_studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"time"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/plugin"
Expand All @@ -31,18 +30,15 @@ func (r *mutationResolver) StudioCreate(ctx context.Context, input models.Studio
}

// Populate a new studio from the input
currentTime := time.Now()
newStudio := models.Studio{
Name: input.Name,
CreatedAt: currentTime,
UpdatedAt: currentTime,
URL: translator.string(input.URL, "url"),
Rating: translator.ratingConversion(input.Rating, input.Rating100),
Details: translator.string(input.Details, "details"),
IgnoreAutoTag: translator.bool(input.IgnoreAutoTag, "ignore_auto_tag"),
Aliases: models.NewRelatedStrings(input.Aliases),
StashIDs: models.NewRelatedStashIDs(input.StashIds),
}
newStudio := models.NewStudio()

newStudio.Name = input.Name
newStudio.URL = translator.string(input.URL, "url")
newStudio.Rating = translator.ratingConversion(input.Rating, input.Rating100)
newStudio.Details = translator.string(input.Details, "details")
newStudio.IgnoreAutoTag = translator.bool(input.IgnoreAutoTag, "ignore_auto_tag")
newStudio.Aliases = models.NewRelatedStrings(input.Aliases)
newStudio.StashIDs = models.NewRelatedStashIDs(input.StashIds)

var err error

Expand Down Expand Up @@ -102,17 +98,16 @@ func (r *mutationResolver) StudioUpdate(ctx context.Context, input models.Studio
}

// Populate studio from the input
updatedStudio := models.StudioPartial{
ID: studioID,
Name: translator.optionalString(input.Name, "name"),
URL: translator.optionalString(input.URL, "url"),
Details: translator.optionalString(input.Details, "details"),
Rating: translator.optionalRatingConversion(input.Rating, input.Rating100),
IgnoreAutoTag: translator.optionalBool(input.IgnoreAutoTag, "ignore_auto_tag"),
Aliases: translator.updateStrings(input.Aliases, "aliases"),
StashIDs: translator.updateStashIDs(input.StashIds, "stash_ids"),
UpdatedAt: models.NewOptionalTime(time.Now()),
}
updatedStudio := models.NewStudioPartial()

updatedStudio.ID = studioID
updatedStudio.Name = translator.optionalString(input.Name, "name")
updatedStudio.URL = translator.optionalString(input.URL, "url")
updatedStudio.Details = translator.optionalString(input.Details, "details")
updatedStudio.Rating = translator.optionalRatingConversion(input.Rating, input.Rating100)
updatedStudio.IgnoreAutoTag = translator.optionalBool(input.IgnoreAutoTag, "ignore_auto_tag")
updatedStudio.Aliases = translator.updateStrings(input.Aliases, "aliases")
updatedStudio.StashIDs = translator.updateStashIDs(input.StashIds, "stash_ids")

updatedStudio.ParentID, err = translator.optionalIntFromString(input.ParentID, "parent_id")
if err != nil {
Expand Down
14 changes: 5 additions & 9 deletions internal/api/resolver_mutation_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"time"

"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
Expand All @@ -31,14 +30,11 @@ func (r *mutationResolver) TagCreate(ctx context.Context, input TagCreateInput)
}

// Populate a new tag from the input
currentTime := time.Now()
newTag := models.Tag{
Name: input.Name,
CreatedAt: currentTime,
UpdatedAt: currentTime,
Description: translator.string(input.Description, "description"),
IgnoreAutoTag: translator.bool(input.IgnoreAutoTag, "ignore_auto_tag"),
}
newTag := models.NewTag()

newTag.Name = input.Name
newTag.Description = translator.string(input.Description, "description")
newTag.IgnoreAutoTag = translator.bool(input.IgnoreAutoTag, "ignore_auto_tag")

var err error

Expand Down
30 changes: 12 additions & 18 deletions internal/autotag/studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ func addSceneStudio(ctx context.Context, sceneWriter models.SceneUpdater, o *mod
}

// set the studio id
scenePartial := models.ScenePartial{
StudioID: models.NewOptionalInt(studioID),
}
scenePartial := models.NewScenePartial()
scenePartial.StudioID = models.NewOptionalInt(studioID)

if _, err := sceneWriter.UpdatePartial(ctx, o.ID, scenePartial); err != nil {
return false, err
Expand All @@ -35,9 +34,8 @@ func addImageStudio(ctx context.Context, imageWriter models.ImageUpdater, i *mod
}

// set the studio id
imagePartial := models.ImagePartial{
StudioID: models.NewOptionalInt(studioID),
}
imagePartial := models.NewImagePartial()
imagePartial.StudioID = models.NewOptionalInt(studioID)

if _, err := imageWriter.UpdatePartial(ctx, i.ID, imagePartial); err != nil {
return false, err
Expand All @@ -52,9 +50,8 @@ func addGalleryStudio(ctx context.Context, galleryWriter GalleryFinderUpdater, o
}

// set the studio id
galleryPartial := models.GalleryPartial{
StudioID: models.NewOptionalInt(studioID),
}
galleryPartial := models.NewGalleryPartial()
galleryPartial.StudioID = models.NewOptionalInt(studioID)

if _, err := galleryWriter.UpdatePartial(ctx, o.ID, galleryPartial); err != nil {
return false, err
Expand Down Expand Up @@ -93,9 +90,8 @@ func (tagger *Tagger) StudioScenes(ctx context.Context, p *models.Studio, paths
}

// set the studio id
scenePartial := models.ScenePartial{
StudioID: models.NewOptionalInt(p.ID),
}
scenePartial := models.NewScenePartial()
scenePartial.StudioID = models.NewOptionalInt(p.ID)

if err := txn.WithTxn(ctx, tagger.TxnManager, func(ctx context.Context) error {
_, err := rw.UpdatePartial(ctx, o.ID, scenePartial)
Expand Down Expand Up @@ -124,9 +120,8 @@ func (tagger *Tagger) StudioImages(ctx context.Context, p *models.Studio, paths
}

// set the studio id
imagePartial := models.ImagePartial{
StudioID: models.NewOptionalInt(p.ID),
}
imagePartial := models.NewImagePartial()
imagePartial.StudioID = models.NewOptionalInt(p.ID)

if err := txn.WithTxn(ctx, tagger.TxnManager, func(ctx context.Context) error {
_, err := rw.UpdatePartial(ctx, i.ID, imagePartial)
Expand Down Expand Up @@ -155,9 +150,8 @@ func (tagger *Tagger) StudioGalleries(ctx context.Context, p *models.Studio, pat
}

// set the studio id
galleryPartial := models.GalleryPartial{
StudioID: models.NewOptionalInt(p.ID),
}
galleryPartial := models.NewGalleryPartial()
galleryPartial.StudioID = models.NewOptionalInt(p.ID)

if err := txn.WithTxn(ctx, tagger.TxnManager, func(ctx context.Context) error {
_, err := rw.UpdatePartial(ctx, o.ID, galleryPartial)
Expand Down
10 changes: 3 additions & 7 deletions internal/identify/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
Expand Down Expand Up @@ -164,12 +163,9 @@ func (g sceneRelationships) tags(ctx context.Context) ([]int, error) {

tagIDs = intslice.IntAppendUnique(tagIDs, int(tagID))
} else if createMissing {
now := time.Now()
newTag := models.Tag{
Name: t.Name,
CreatedAt: now,
UpdatedAt: now,
}
newTag := models.NewTag()
newTag.Name = t.Name

err := g.tagCreator.Create(ctx, &newTag)
if err != nil {
return nil, fmt.Errorf("error creating tag: %w", err)
Expand Down
Loading

0 comments on commit 8b33f95

Please sign in to comment.