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.

64 lines
1.6 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graph2/graphcore"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. // Queries
  9. func (r *queryResolver) Comment(ctx context.Context, id string) (*models.Comment, error) {
  10. return r.s.Stories.FindComment(ctx, id)
  11. }
  12. // Mutations
  13. func (r *mutationResolver) AddComment(ctx context.Context, input graphcore.CommentAddInput) (*models.Comment, error) {
  14. chapter, err := r.s.Stories.FindChapter(ctx, input.ChapterID)
  15. if err != nil {
  16. return nil, err
  17. }
  18. subject := ""
  19. if input.Subject != nil {
  20. subject = *input.Subject
  21. }
  22. fictionalDate := time.Time{}
  23. if input.FictionalDate != nil {
  24. fictionalDate = *input.FictionalDate
  25. }
  26. return r.s.Stories.CreateComment(ctx, *chapter, subject, "", input.Source, input.CharacterName, input.CharacterID, time.Now(), fictionalDate)
  27. }
  28. func (r *mutationResolver) EditComment(ctx context.Context, input graphcore.CommentEditInput) (*models.Comment, error) {
  29. comment, err := r.s.Stories.FindComment(ctx, input.CommentID)
  30. if err != nil {
  31. return nil, err
  32. }
  33. fictionalDate := input.FictionalDate
  34. if input.ClearFictionalDate != nil && *input.ClearFictionalDate {
  35. fictionalDate = &time.Time{}
  36. }
  37. return r.s.Stories.EditComment(ctx, comment, input.Source, input.CharacterName, input.CharacterID, input.Subject, fictionalDate)
  38. }
  39. func (r *mutationResolver) RemoveComment(ctx context.Context, input graphcore.CommentRemoveInput) (*models.Comment, error) {
  40. comment, err := r.s.Stories.FindComment(ctx, input.CommentID)
  41. if err != nil {
  42. return nil, err
  43. }
  44. err = r.s.Stories.RemoveComment(ctx, comment)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return comment, nil
  49. }