2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2019-08-21 02:51:30 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/database"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.PerformerCreateInput) (*models.Performer, error) {
|
2019-08-21 02:51:30 +00:00
|
|
|
// generate checksum from performer name rather than image
|
2020-10-21 00:27:16 +00:00
|
|
|
checksum := utils.MD5FromString(input.Name)
|
2019-08-21 02:51:30 +00:00
|
|
|
|
|
|
|
var imageData []byte
|
|
|
|
var err error
|
|
|
|
|
2020-08-11 23:19:27 +00:00
|
|
|
if input.Image != nil {
|
2019-08-24 07:17:25 +00:00
|
|
|
_, imageData, err = utils.ProcessBase64Image(*input.Image)
|
2019-08-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate a new performer from the input
|
|
|
|
currentTime := time.Now()
|
|
|
|
newPerformer := models.Performer{
|
2019-02-14 22:53:32 +00:00
|
|
|
Checksum: checksum,
|
|
|
|
CreatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-10-21 00:27:16 +00:00
|
|
|
newPerformer.Name = sql.NullString{String: input.Name, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.URL != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-03-31 22:36:38 +00:00
|
|
|
if input.Gender != nil {
|
|
|
|
newPerformer.Gender = sql.NullString{String: input.Gender.String(), Valid: true}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.Birthdate != nil {
|
2019-03-05 01:14:52 +00:00
|
|
|
newPerformer.Birthdate = models.SQLiteDate{String: *input.Birthdate, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Ethnicity != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Country != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Country = sql.NullString{String: *input.Country, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.EyeColor != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.EyeColor = sql.NullString{String: *input.EyeColor, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Height != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Height = sql.NullString{String: *input.Height, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Measurements != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Measurements = sql.NullString{String: *input.Measurements, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.FakeTits != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.FakeTits = sql.NullString{String: *input.FakeTits, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.CareerLength != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.CareerLength = sql.NullString{String: *input.CareerLength, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Tattoos != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Tattoos = sql.NullString{String: *input.Tattoos, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Piercings != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Piercings = sql.NullString{String: *input.Piercings, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Aliases != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Aliases = sql.NullString{String: *input.Aliases, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Twitter != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Twitter = sql.NullString{String: *input.Twitter, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Instagram != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Instagram = sql.NullString{String: *input.Instagram, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Favorite != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Favorite = sql.NullBool{Bool: *input.Favorite, Valid: true}
|
2019-02-11 11:06:33 +00:00
|
|
|
} else {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Favorite = sql.NullBool{Bool: false, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the transaction and save the performer
|
|
|
|
tx := database.DB.MustBeginTx(ctx, nil)
|
|
|
|
qb := models.NewPerformerQueryBuilder()
|
2020-10-24 03:31:39 +00:00
|
|
|
jqb := models.NewJoinsQueryBuilder()
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
performer, err := qb.Create(newPerformer, tx)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
// update image table
|
|
|
|
if len(imageData) > 0 {
|
|
|
|
if err := qb.UpdatePerformerImage(performer.ID, imageData, tx); err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 03:31:39 +00:00
|
|
|
// Save the stash_ids
|
|
|
|
if input.StashIds != nil {
|
|
|
|
var stashIDJoins []models.StashID
|
|
|
|
for _, stashID := range input.StashIds {
|
|
|
|
newJoin := models.StashID{
|
|
|
|
StashID: stashID.StashID,
|
|
|
|
Endpoint: stashID.Endpoint,
|
|
|
|
}
|
|
|
|
stashIDJoins = append(stashIDJoins, newJoin)
|
|
|
|
}
|
|
|
|
if err := jqb.UpdatePerformerStashIDs(performer.ID, stashIDJoins, tx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
// Commit
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return performer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) PerformerUpdate(ctx context.Context, input models.PerformerUpdateInput) (*models.Performer, error) {
|
|
|
|
// Populate performer from the input
|
|
|
|
performerID, _ := strconv.Atoi(input.ID)
|
|
|
|
updatedPerformer := models.Performer{
|
2019-02-14 22:53:32 +00:00
|
|
|
ID: performerID,
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-06-22 23:19:19 +00:00
|
|
|
var imageData []byte
|
|
|
|
var err error
|
2020-08-11 23:19:27 +00:00
|
|
|
imageIncluded := wasFieldIncluded(ctx, "image")
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.Image != nil {
|
2020-06-22 23:19:19 +00:00
|
|
|
_, imageData, err = utils.ProcessBase64Image(*input.Image)
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if input.Name != nil {
|
2019-08-22 02:49:09 +00:00
|
|
|
// generate checksum from performer name rather than image
|
|
|
|
checksum := utils.MD5FromString(*input.Name)
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Name = sql.NullString{String: *input.Name, Valid: true}
|
2019-08-22 02:49:09 +00:00
|
|
|
updatedPerformer.Checksum = checksum
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.URL != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-03-31 22:36:38 +00:00
|
|
|
if input.Gender != nil {
|
|
|
|
updatedPerformer.Gender = sql.NullString{String: input.Gender.String(), Valid: true}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.Birthdate != nil {
|
2019-03-05 01:14:52 +00:00
|
|
|
updatedPerformer.Birthdate = models.SQLiteDate{String: *input.Birthdate, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Ethnicity != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Country != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Country = sql.NullString{String: *input.Country, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.EyeColor != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.EyeColor = sql.NullString{String: *input.EyeColor, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Height != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Height = sql.NullString{String: *input.Height, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Measurements != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Measurements = sql.NullString{String: *input.Measurements, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.FakeTits != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.FakeTits = sql.NullString{String: *input.FakeTits, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.CareerLength != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.CareerLength = sql.NullString{String: *input.CareerLength, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Tattoos != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Tattoos = sql.NullString{String: *input.Tattoos, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Piercings != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Piercings = sql.NullString{String: *input.Piercings, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Aliases != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Aliases = sql.NullString{String: *input.Aliases, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Twitter != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Twitter = sql.NullString{String: *input.Twitter, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Instagram != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Instagram = sql.NullString{String: *input.Instagram, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Favorite != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Favorite = sql.NullBool{Bool: *input.Favorite, Valid: true}
|
2019-02-11 11:06:33 +00:00
|
|
|
} else {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedPerformer.Favorite = sql.NullBool{Bool: false, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the transaction and save the performer
|
|
|
|
tx := database.DB.MustBeginTx(ctx, nil)
|
|
|
|
qb := models.NewPerformerQueryBuilder()
|
2020-10-24 03:31:39 +00:00
|
|
|
jqb := models.NewJoinsQueryBuilder()
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
performer, err := qb.Update(updatedPerformer, tx)
|
|
|
|
if err != nil {
|
2020-08-11 23:19:27 +00:00
|
|
|
tx.Rollback()
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
// update image table
|
|
|
|
if len(imageData) > 0 {
|
|
|
|
if err := qb.UpdatePerformerImage(performer.ID, imageData, tx); err != nil {
|
2020-08-11 23:19:27 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if imageIncluded {
|
|
|
|
// must be unsetting
|
|
|
|
if err := qb.DestroyPerformerImage(performer.ID, tx); err != nil {
|
|
|
|
tx.Rollback()
|
2020-06-22 23:19:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 03:31:39 +00:00
|
|
|
// Save the stash_ids
|
|
|
|
if input.StashIds != nil {
|
|
|
|
var stashIDJoins []models.StashID
|
|
|
|
for _, stashID := range input.StashIds {
|
|
|
|
newJoin := models.StashID{
|
|
|
|
StashID: stashID.StashID,
|
|
|
|
Endpoint: stashID.Endpoint,
|
|
|
|
}
|
|
|
|
stashIDJoins = append(stashIDJoins, newJoin)
|
|
|
|
}
|
|
|
|
if err := jqb.UpdatePerformerStashIDs(performerID, stashIDJoins, tx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
// Commit
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return performer, nil
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|
2019-08-14 21:40:51 +00:00
|
|
|
|
|
|
|
func (r *mutationResolver) PerformerDestroy(ctx context.Context, input models.PerformerDestroyInput) (bool, error) {
|
|
|
|
qb := models.NewPerformerQueryBuilder()
|
|
|
|
tx := database.DB.MustBeginTx(ctx, nil)
|
|
|
|
if err := qb.Destroy(input.ID, tx); err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|