2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
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
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *mutationResolver) StudioCreate(ctx context.Context, input models.StudioCreateInput) (*models.Studio, error) {
|
|
|
|
// Process the base 64 encoded image string
|
|
|
|
checksum, imageData, err := utils.ProcessBase64Image(input.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate a new studio from the input
|
|
|
|
currentTime := time.Now()
|
|
|
|
newStudio := models.Studio{
|
2019-02-14 22:53:32 +00:00
|
|
|
Image: imageData,
|
|
|
|
Checksum: checksum,
|
|
|
|
Name: sql.NullString{String: input.Name, Valid: true},
|
|
|
|
CreatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.URL != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newStudio.URL = sql.NullString{String: *input.URL, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the transaction and save the studio
|
|
|
|
tx := database.DB.MustBeginTx(ctx, nil)
|
|
|
|
qb := models.NewStudioQueryBuilder()
|
|
|
|
studio, err := qb.Create(newStudio, tx)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return studio, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) StudioUpdate(ctx context.Context, input models.StudioUpdateInput) (*models.Studio, error) {
|
|
|
|
// Populate studio from the input
|
|
|
|
studioID, _ := strconv.Atoi(input.ID)
|
|
|
|
updatedStudio := models.Studio{
|
2019-02-14 22:53:32 +00:00
|
|
|
ID: studioID,
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Image != nil {
|
|
|
|
checksum, imageData, err := utils.ProcessBase64Image(*input.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
updatedStudio.Image = imageData
|
|
|
|
updatedStudio.Checksum = checksum
|
|
|
|
}
|
|
|
|
if input.Name != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
updatedStudio.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
|
|
|
updatedStudio.URL = sql.NullString{String: *input.URL, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the transaction and save the studio
|
|
|
|
tx := database.DB.MustBeginTx(ctx, nil)
|
|
|
|
qb := models.NewStudioQueryBuilder()
|
|
|
|
studio, err := qb.Update(updatedStudio, tx)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return studio, nil
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|