2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-25 00:56:34 +00:00
|
|
|
"time"
|
2020-06-21 11:43:57 +00:00
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/image"
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
func (r *galleryResolver) Path(ctx context.Context, obj *models.Gallery) (*string, error) {
|
|
|
|
if obj.Path.Valid {
|
|
|
|
return &obj.Path.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
func (r *galleryResolver) Title(ctx context.Context, obj *models.Gallery) (*string, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
if obj.Title.Valid {
|
|
|
|
return &obj.Title.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *galleryResolver) Images(ctx context.Context, obj *models.Gallery) (ret []*models.Image, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
ret, err = repo.Image().FindByGalleryID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *galleryResolver) Cover(ctx context.Context, obj *models.Gallery) (ret *models.Image, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
imgs, err := repo.Image().FindByGalleryID(obj.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
if len(imgs) > 0 {
|
|
|
|
ret = imgs[0]
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
for _, img := range imgs {
|
|
|
|
if image.IsCover(img) {
|
|
|
|
ret = img
|
|
|
|
break
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
func (r *galleryResolver) Date(ctx context.Context, obj *models.Gallery) (*string, error) {
|
|
|
|
if obj.Date.Valid {
|
|
|
|
result := utils.GetYMDFromDatabaseDate(obj.Date.String)
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *galleryResolver) URL(ctx context.Context, obj *models.Gallery) (*string, error) {
|
|
|
|
if obj.URL.Valid {
|
|
|
|
return &obj.URL.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *galleryResolver) Details(ctx context.Context, obj *models.Gallery) (*string, error) {
|
|
|
|
if obj.Details.Valid {
|
|
|
|
return &obj.Details.String, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *galleryResolver) Rating(ctx context.Context, obj *models.Gallery) (*int, error) {
|
|
|
|
if obj.Rating.Valid {
|
|
|
|
rating := int(obj.Rating.Int64)
|
|
|
|
return &rating, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-06-21 11:43:57 +00:00
|
|
|
|
2021-02-01 20:56:54 +00:00
|
|
|
func (r *galleryResolver) Scenes(ctx context.Context, obj *models.Gallery) (ret []*models.Scene, err error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
2021-02-01 20:56:54 +00:00
|
|
|
ret, err = repo.Scene().FindByGalleryID(obj.ID)
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-06-21 11:43:57 +00:00
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *galleryResolver) Studio(ctx context.Context, obj *models.Gallery) (ret *models.Studio, err error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
if !obj.StudioID.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
ret, err = repo.Studio().Find(int(obj.StudioID.Int64))
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *galleryResolver) Tags(ctx context.Context, obj *models.Gallery) (ret []*models.Tag, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
ret, err = repo.Tag().FindByGalleryID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *galleryResolver) Performers(ctx context.Context, obj *models.Gallery) (ret []*models.Performer, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
ret, err = repo.Performer().FindByGalleryID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2020-11-15 03:40:47 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *galleryResolver) ImageCount(ctx context.Context, obj *models.Gallery) (ret int, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
ret, err = repo.Image().CountByGalleryID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-11-15 03:40:47 +00:00
|
|
|
}
|
2021-05-25 00:56:34 +00:00
|
|
|
|
|
|
|
func (r *galleryResolver) CreatedAt(ctx context.Context, obj *models.Gallery) (*time.Time, error) {
|
|
|
|
return &obj.CreatedAt.Timestamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *galleryResolver) UpdatedAt(ctx context.Context, obj *models.Gallery) (*time.Time, error) {
|
|
|
|
return &obj.UpdatedAt.Timestamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *galleryResolver) FileModTime(ctx context.Context, obj *models.Gallery) (*time.Time, error) {
|
|
|
|
return &obj.FileModTime.Timestamp, nil
|
|
|
|
}
|