|
|
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" )
// AddChapterArgs is args for the addChapter mutation
type AddChapterArgs struct { Input *struct { StoryID string Title string Author *string Source string FictionalDate *string } }
// AddChapter resolves the addChapter mutation
func (r *MutationResolver) AddChapter(ctx context.Context, args *AddChapterArgs) (*types.ChapterResolver, error) { input := args.Input
token := auth.TokenFromContext(ctx) if !token.Permitted("member", "chapter.add") { return nil, ErrUnauthorized }
story, err := story.FindID(input.StoryID) if err != nil { return nil, err }
author := token.UserID if input.Author != nil { author = *input.Author
if token.UserID != author && !token.Permitted("chapter.add") { return nil, ErrPermissionDenied } }
fictionalDate := time.Time{} if input.FictionalDate != nil { fictionalDate, err = time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } }
chapter, err := story.AddChapter(input.Title, author, input.Source, time.Now(), fictionalDate) if err != nil { return nil, err }
go change.Submit("Chapter", "add", token.UserID, chapter.ID, map[string]interface{}{ "title": input.Title, "author": author, "fictionalDate": fictionalDate, })
return &types.ChapterResolver{C: chapter}, nil }
|