|
|
package queries
import ( "context" "time"
"git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/story" )
// StoriesArg is args for stories query
type StoriesArg struct { Filter *struct { Author *string Tags *[]struct { Kind string Name string } EarliestFictionalDate *string LatestFictionalDate *string Category *string Limit *int32 Open *bool Unlisted *bool } }
// Stories resolves the stories query
func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) { var err error
filter := args.Filter
author := "" category := "" tags := make([]story.Tag, 0, 8) earliest := time.Time{} latest := time.Time{} unlisted := false open := (*bool)(nil) limit := 0
if filter != nil { if filter.Author != nil { author = *filter.Author }
if filter.Category != nil { category = *filter.Category }
if filter.Tags != nil { for _, tagInput := range *filter.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, }) } }
if filter.EarliestFictionalDate != nil { earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate) if err != nil { return nil, err } }
if filter.LatestFictionalDate != nil { latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate) if err != nil { return nil, err } }
if filter.Limit != nil { limit = int(*filter.Limit) }
unlisted = filter.Unlisted != nil && *filter.Unlisted == true if unlisted { user := session.FromContext(ctx).User() if user == nil { return nil, ErrUnauthorized }
if author != "" && author != user.ID && !user.Permitted("story.unlisted") { return nil, ErrPermissionDenied }
author = user.ID }
open = filter.Open }
stories, err := story.List(author, category, tags, earliest, latest, unlisted, open, limit) if err != nil { return nil, err }
resolvers := make([]*types.StoryResolver, len(stories)) for i, story := range stories { resolvers[i] = &types.StoryResolver{S: story} }
return resolvers, nil }
|