package mutations import ( "context" "time" "git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/internal/auth" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/story" ) // StoryEditArgs is args for the addStory mutation type StoryEditArgs struct { Input *struct { ID string Name *string Category *string Author *string Open *bool Listed *bool FictionalDate *string } } // EditStory resolves the editStory mutation func (r *MutationResolver) EditStory(ctx context.Context, args *StoryEditArgs) (*types.StoryResolver, error) { input := args.Input token := auth.TokenFromContext(ctx) if !token.Permitted("member", "story.edit") { return nil, ErrUnauthorized } story, err := story.FindID(input.ID) if err != nil { return nil, err } if story.Author != token.UserID && !token.Permitted("story.edit") { return nil, ErrPermissionDenied } var fictionalDate *time.Time if input.FictionalDate != nil { date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } fictionalDate = &date } err = story.Edit(input.Name, input.Category, input.Listed, input.Open, fictionalDate) if err != nil { return nil, err } go change.Submit("Story", "edit", token.UserID, story.ID, map[string]interface{}{ "name": input.Name, "category": input.Category, "author": input.Author, "open": input.Open, "listed": input.Listed, "fictionalDate": input.FictionalDate, }) return &types.StoryResolver{S: story}, nil }