GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/internal/auth"
  7. "git.aiterp.net/rpdata/api/model/change"
  8. "git.aiterp.net/rpdata/api/model/story"
  9. )
  10. // EditChapterArgs is args for the editChapter mutation
  11. type EditChapterArgs struct {
  12. Input *struct {
  13. ID string
  14. Title *string
  15. Source *string
  16. FictionalDate *string
  17. }
  18. }
  19. // EditChapter resolves the editChapter mutation
  20. func (r *MutationResolver) EditChapter(ctx context.Context, args *EditChapterArgs) (*types.ChapterResolver, error) {
  21. input := args.Input
  22. token := auth.TokenFromContext(ctx)
  23. if !token.Permitted("member", "chapter.edit") {
  24. return nil, ErrUnauthorized
  25. }
  26. chapter, err := story.FindChapterID(input.ID)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if chapter.Author != token.UserID && !token.Permitted("chapter.edit") {
  31. return nil, ErrPermissionDenied
  32. }
  33. var fictionalDate *time.Time
  34. if input.FictionalDate != nil {
  35. date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate)
  36. if err != nil {
  37. return nil, err
  38. }
  39. fictionalDate = &date
  40. }
  41. err = chapter.Edit(input.Title, input.Source, fictionalDate)
  42. if err != nil {
  43. return nil, err
  44. }
  45. go change.Submit("Chapter", "edit", token.UserID, chapter.ID, map[string]interface{}{
  46. "title": input.Title,
  47. "source": input.Source,
  48. "fictionalDate": fictionalDate,
  49. })
  50. return &types.ChapterResolver{C: chapter}, nil
  51. }