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.

206 lines
5.1 KiB

  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/models"
  6. "git.aiterp.net/rpdata/api/models/changekeys"
  7. "git.aiterp.net/rpdata/api/repositories"
  8. "git.aiterp.net/rpdata/api/services/loaders"
  9. "sort"
  10. "strings"
  11. )
  12. type CharacterService struct {
  13. characters repositories.CharacterRepository
  14. loader *loaders.CharacterLoader
  15. changeService *ChangeService
  16. authService *AuthService
  17. }
  18. // Find uses the loader to find the character by the ID.
  19. func (s *CharacterService) Find(ctx context.Context, id string) (*models.Character, error) {
  20. return s.loader.Load(id)
  21. }
  22. // Find uses the loader to find the character by the ID.
  23. func (s *CharacterService) FindNick(ctx context.Context, nick string) (*models.Character, error) {
  24. return s.characters.FindNick(ctx, nick)
  25. }
  26. // List lists the characters. If the only filter active is `IDs`, the loader is used to batch together requests.
  27. func (s *CharacterService) List(ctx context.Context, filter models.CharacterFilter) ([]*models.Character, error) {
  28. if len(filter.IDs) > 0 && len(filter.Names) == 0 && len(filter.Nicks) == 0 && filter.Author == nil && filter.Search == nil {
  29. characters, errs := s.loader.LoadAll(filter.IDs)
  30. if len(characters) == 0 && len(errs) > 0 {
  31. if errs[0] == repositories.ErrNotFound {
  32. return []*models.Character{}, nil
  33. } else {
  34. return nil, errs[0]
  35. }
  36. }
  37. if err := ctx.Err(); err != nil {
  38. return nil, err
  39. }
  40. var badIndices []int
  41. for i, character := range characters {
  42. if character == nil {
  43. badIndices = append(badIndices, i-len(badIndices))
  44. }
  45. }
  46. for _, index := range badIndices {
  47. characters = append(characters[:index], characters[index+1:]...)
  48. }
  49. sort.Slice(characters, func(i, j int) bool {
  50. return strings.Compare(characters[i].ID, characters[j].ID) < 0
  51. })
  52. return characters, nil
  53. }
  54. return s.characters.List(ctx, filter)
  55. }
  56. func (s *CharacterService) Create(ctx context.Context, nick, name, shortName, author, description string) (*models.Character, error) {
  57. token := s.authService.TokenFromContext(ctx)
  58. if token == nil {
  59. return nil, ErrUnauthenticated
  60. }
  61. if name == "" {
  62. return nil, errors.New("Name cannot be empty")
  63. }
  64. if author == "" {
  65. author = token.UserID
  66. }
  67. if shortName == "" {
  68. split := strings.SplitN(name, " ", 2)
  69. shortName = split[0]
  70. }
  71. character := &models.Character{
  72. Name: name,
  73. ShortName: shortName,
  74. Author: author,
  75. Nicks: []string{nick},
  76. Description: description,
  77. }
  78. err := s.authService.CheckPermission(ctx, "add", character)
  79. if err != nil {
  80. return nil, err
  81. }
  82. character, err = s.characters.Insert(ctx, *character)
  83. if err != nil {
  84. return nil, err
  85. }
  86. s.changeService.Submit(ctx, "Character", "add", true, changekeys.Listed(character), character)
  87. return character, nil
  88. }
  89. func (s *CharacterService) Update(ctx context.Context, id string, name, shortName, description *string) (*models.Character, error) {
  90. character, err := s.characters.Find(ctx, id)
  91. if err != nil {
  92. return nil, err
  93. }
  94. err = s.authService.CheckPermission(ctx, "edit", character)
  95. if err != nil {
  96. return nil, err
  97. }
  98. character, err = s.characters.Update(ctx, *character, models.CharacterUpdate{
  99. Name: name,
  100. ShortName: shortName,
  101. Description: description,
  102. })
  103. if err != nil {
  104. return nil, err
  105. }
  106. s.loader.Clear(character.ID)
  107. s.loader.Prime(character.ID, character)
  108. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  109. return character, nil
  110. }
  111. func (s *CharacterService) AddNick(ctx context.Context, id string, nick string) (*models.Character, error) {
  112. character, err := s.characters.Find(ctx, id)
  113. if err != nil {
  114. return nil, err
  115. }
  116. err = s.authService.CheckPermission(ctx, "edit", character)
  117. if err != nil {
  118. return nil, err
  119. }
  120. character, err = s.characters.AddNick(ctx, *character, nick)
  121. if err != nil {
  122. return nil, err
  123. }
  124. s.loader.Clear(character.ID)
  125. s.loader.Prime(character.ID, character)
  126. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  127. return character, nil
  128. }
  129. func (s *CharacterService) RemoveNick(ctx context.Context, id string, nick string) (*models.Character, error) {
  130. character, err := s.characters.Find(ctx, id)
  131. if err != nil {
  132. return nil, err
  133. }
  134. err = s.authService.CheckPermission(ctx, "edit", character)
  135. if err != nil {
  136. return nil, err
  137. }
  138. character, err = s.characters.RemoveNick(ctx, *character, nick)
  139. if err != nil {
  140. return nil, err
  141. }
  142. s.loader.Clear(character.ID)
  143. s.loader.Prime(character.ID, character)
  144. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  145. return character, nil
  146. }
  147. func (s *CharacterService) Delete(ctx context.Context, id string) (*models.Character, error) {
  148. character, err := s.characters.Find(ctx, id)
  149. if err != nil {
  150. return nil, err
  151. }
  152. err = s.authService.CheckPermission(ctx, "edit", character)
  153. if err != nil {
  154. return nil, err
  155. }
  156. err = s.characters.Delete(ctx, *character)
  157. if err != nil {
  158. return nil, err
  159. }
  160. s.loader.Clear(character.ID)
  161. s.changeService.Submit(ctx, "Character", "remove", true, changekeys.Listed(character), character)
  162. return character, nil
  163. }