2020-03-10 03:28:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-25 00:56:34 +00:00
|
|
|
"time"
|
2020-03-10 03:28:15 +00:00
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/api/urlbuilders"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2020-03-20 21:21:49 +00:00
|
|
|
func (r *movieResolver) Name(ctx context.Context, obj *models.Movie) (string, error) {
|
2020-03-10 03:28:15 +00:00
|
|
|
if obj.Name.Valid {
|
2020-03-20 21:21:49 +00:00
|
|
|
return obj.Name.String, nil
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
2020-03-20 21:21:49 +00:00
|
|
|
return "", nil
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) URL(ctx context.Context, obj *models.Movie) (*string, error) {
|
|
|
|
if obj.URL.Valid {
|
|
|
|
return &obj.URL.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) Aliases(ctx context.Context, obj *models.Movie) (*string, error) {
|
|
|
|
if obj.Aliases.Valid {
|
|
|
|
return &obj.Aliases.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-22 01:22:14 +00:00
|
|
|
func (r *movieResolver) Duration(ctx context.Context, obj *models.Movie) (*int, error) {
|
2020-03-10 03:28:15 +00:00
|
|
|
if obj.Duration.Valid {
|
2020-04-22 01:22:14 +00:00
|
|
|
rating := int(obj.Duration.Int64)
|
|
|
|
return &rating, nil
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) Date(ctx context.Context, obj *models.Movie) (*string, error) {
|
|
|
|
if obj.Date.Valid {
|
|
|
|
result := utils.GetYMDFromDatabaseDate(obj.Date.String)
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-22 01:22:14 +00:00
|
|
|
func (r *movieResolver) Rating(ctx context.Context, obj *models.Movie) (*int, error) {
|
2020-03-10 03:28:15 +00:00
|
|
|
if obj.Rating.Valid {
|
2020-04-22 01:22:14 +00:00
|
|
|
rating := int(obj.Rating.Int64)
|
|
|
|
return &rating, nil
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *movieResolver) Studio(ctx context.Context, obj *models.Movie) (ret *models.Studio, err error) {
|
2020-04-22 01:22:14 +00:00
|
|
|
if obj.StudioID.Valid {
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Studio().Find(int(obj.StudioID.Int64))
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-04-22 01:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 03:28:15 +00:00
|
|
|
func (r *movieResolver) Director(ctx context.Context, obj *models.Movie) (*string, error) {
|
|
|
|
if obj.Director.Valid {
|
|
|
|
return &obj.Director.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) Synopsis(ctx context.Context, obj *models.Movie) (*string, error) {
|
|
|
|
if obj.Synopsis.Valid {
|
|
|
|
return &obj.Synopsis.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) FrontImagePath(ctx context.Context, obj *models.Movie) (*string, error) {
|
|
|
|
baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
|
2021-03-13 00:49:20 +00:00
|
|
|
frontimagePath := urlbuilders.NewMovieURLBuilder(baseURL, obj).GetMovieFrontImageURL()
|
2020-03-10 03:28:15 +00:00
|
|
|
return &frontimagePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) BackImagePath(ctx context.Context, obj *models.Movie) (*string, error) {
|
2021-03-31 03:54:58 +00:00
|
|
|
// don't return any thing if there is no back image
|
|
|
|
var img []byte
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
img, err = repo.Movie().GetBackImage(obj.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if img == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 03:28:15 +00:00
|
|
|
baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
|
2021-03-13 00:49:20 +00:00
|
|
|
backimagePath := urlbuilders.NewMovieURLBuilder(baseURL, obj).GetMovieBackImageURL()
|
2020-03-10 03:28:15 +00:00
|
|
|
return &backimagePath, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *movieResolver) SceneCount(ctx context.Context, obj *models.Movie) (ret *int, err error) {
|
|
|
|
var res int
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
res, err = repo.Scene().CountByMovieID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-10 03:28:15 +00:00
|
|
|
return &res, err
|
2020-03-20 21:21:49 +00:00
|
|
|
}
|
2021-05-25 00:56:34 +00:00
|
|
|
|
|
|
|
func (r *movieResolver) CreatedAt(ctx context.Context, obj *models.Movie) (*time.Time, error) {
|
|
|
|
return &obj.CreatedAt.Timestamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *movieResolver) UpdatedAt(ctx context.Context, obj *models.Movie) (*time.Time, error) {
|
|
|
|
return &obj.UpdatedAt.Timestamp, nil
|
|
|
|
}
|