|
|
package mutations
import ( "context" "time"
"git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/story" )
// StoryAddArgs is args for the addStory mutation
type StoryAddArgs struct { Input *struct { Name string Category string Author *string Open *bool Listed *bool FictionalDate *string Tags *[]struct { Kind string Name string } } }
// AddStory resolves the addStory mutation
func (r *MutationResolver) AddStory(ctx context.Context, args *StoryAddArgs) (*types.StoryResolver, error) { input := args.Input
user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.add") { return nil, ErrUnauthorized }
author := user.ID if input.Author != nil { author = *input.Author
if user.ID != author && !user.Permitted("story.add") { return nil, ErrPermissionDenied } }
listed := (input.Listed != nil && *input.Listed == true)
open := (input.Open != nil && *input.Open == true)
tags := make([]story.Tag, 0, 8) if input.Tags != nil { for _, tagInput := range *input.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, }) } }
fictionalDate := time.Time{} if input.FictionalDate != nil { date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err }
fictionalDate = date }
story, err := story.New(input.Name, author, input.Category, listed, open, tags, time.Now(), fictionalDate) if err != nil { return nil, err }
go change.Submit("Story", "add", user.ID, story.ID, map[string]interface{}{ "name": input.Name, "category": input.Category, "author": input.Author, })
return &types.StoryResolver{S: story}, nil }
|