Allow unsetting of rating, studio, gallery

This commit is contained in:
WithoutPants
2019-10-12 22:32:01 +11:00
parent d8b566250e
commit 470c64b840
8 changed files with 78 additions and 30 deletions

View File

@@ -3,10 +3,11 @@ package api
import (
"context"
"database/sql"
"github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/models"
"strconv"
"time"
"github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/models"
)
func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUpdateInput) (*models.Scene, error) {
@@ -29,12 +30,20 @@ func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUp
if input.Date != nil {
updatedScene.Date = models.SQLiteDate{String: *input.Date, Valid: true}
}
if input.Rating != nil {
updatedScene.Rating = sql.NullInt64{Int64: int64(*input.Rating), Valid: true}
} else {
// rating must be nullable
updatedScene.Rating = sql.NullInt64{Valid: false}
}
if input.StudioID != nil {
studioID, _ := strconv.ParseInt(*input.StudioID, 10, 64)
updatedScene.StudioID = sql.NullInt64{Int64: studioID, Valid: true}
} else {
// studio must be nullable
updatedScene.StudioID = sql.NullInt64{Valid: false}
}
// Start the transaction and save the scene marker
@@ -47,6 +56,14 @@ func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUp
return nil, err
}
// Clear the existing gallery value
gqb := models.NewGalleryQueryBuilder()
err = gqb.ClearGalleryId(sceneID, tx)
if err != nil {
_ = tx.Rollback()
return nil, err
}
if input.GalleryID != nil {
// Save the gallery
galleryID, _ := strconv.Atoi(*input.GalleryID)

View File

@@ -2,9 +2,10 @@ package models
import (
"database/sql"
"path/filepath"
"github.com/jmoiron/sqlx"
"github.com/stashapp/stash/pkg/database"
"path/filepath"
)
type GalleryQueryBuilder struct{}
@@ -50,6 +51,24 @@ func (qb *GalleryQueryBuilder) Update(updatedGallery Gallery, tx *sqlx.Tx) (*Gal
return &updatedGallery, nil
}
type GalleryNullSceneID struct {
SceneID sql.NullInt64
}
func (qb *GalleryQueryBuilder) ClearGalleryId(sceneID int, tx *sqlx.Tx) error {
ensureTx(tx)
_, err := tx.NamedExec(
`UPDATE galleries SET scene_id = null WHERE scene_id = :sceneid`,
GalleryNullSceneID{
SceneID: sql.NullInt64{
Int64: int64(sceneID),
Valid: true,
},
},
)
return err
}
func (qb *GalleryQueryBuilder) Find(id int) (*Gallery, error) {
query := "SELECT * FROM galleries WHERE id = ? LIMIT 1"
args := []interface{}{id}

View File

@@ -3,13 +3,14 @@ package models
import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/logger"
"math/rand"
"reflect"
"strconv"
"strings"
"github.com/jmoiron/sqlx"
"github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/logger"
)
var randomSortFloat = rand.Float64()
@@ -266,21 +267,17 @@ func SQLGenKeys(i interface{}) string {
query = append(query, fmt.Sprintf("%s=:%s", key, key))
}
case sql.NullString:
if t.Valid {
query = append(query, fmt.Sprintf("%s=:%s", key, key))
}
// should be nullable
query = append(query, fmt.Sprintf("%s=:%s", key, key))
case sql.NullBool:
if t.Valid {
query = append(query, fmt.Sprintf("%s=:%s", key, key))
}
// should be nullable
query = append(query, fmt.Sprintf("%s=:%s", key, key))
case sql.NullInt64:
if t.Valid {
query = append(query, fmt.Sprintf("%s=:%s", key, key))
}
// should be nullable
query = append(query, fmt.Sprintf("%s=:%s", key, key))
case sql.NullFloat64:
if t.Valid {
query = append(query, fmt.Sprintf("%s=:%s", key, key))
}
// should be nullable
query = append(query, fmt.Sprintf("%s=:%s", key, key))
default:
reflectValue := reflect.ValueOf(t)
kind := reflectValue.Kind()