stufflog graphql server
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.

129 lines
3.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package resolvers
  2. // This file will be automatically regenerated based on the schema, any resolver implementations
  3. // will be copied through when generating and any unknown code will be moved to the end.
  4. import (
  5. "context"
  6. "errors"
  7. "git.aiterp.net/stufflog/server/graph/graphcore"
  8. "git.aiterp.net/stufflog/server/internal/xlerrors"
  9. "git.aiterp.net/stufflog/server/models"
  10. )
  11. func (r *queryResolver) Issue(ctx context.Context, id string) (*models.Issue, error) {
  12. user := r.Auth.UserFromContext(ctx)
  13. if user == nil {
  14. return nil, xlerrors.PermissionDenied
  15. }
  16. issue, err := r.Database.Issues().Find(ctx, id)
  17. if err != nil {
  18. return nil, err
  19. }
  20. _, err = r.Auth.IssuePermission(ctx, *issue)
  21. if err != nil {
  22. return nil, err
  23. }
  24. return issue, nil
  25. }
  26. func (r *queryResolver) Issues(ctx context.Context, filter *models.IssueFilter) ([]*models.Issue, error) {
  27. user := r.Auth.UserFromContext(ctx)
  28. if user == nil {
  29. return nil, xlerrors.PermissionDenied
  30. }
  31. if filter == nil {
  32. filter = &models.IssueFilter{}
  33. }
  34. issues, err := r.Database.Issues().List(ctx, *filter)
  35. if err != nil {
  36. return nil, err
  37. }
  38. deleteList := make([]int, 0, len(issues))
  39. for i, issue := range issues {
  40. _, err := r.Auth.IssuePermission(ctx, *issue)
  41. if err != nil {
  42. deleteList = append(deleteList, i-len(deleteList))
  43. }
  44. }
  45. for _, index := range deleteList {
  46. issues = append(issues[:index], issues[index+1:]...)
  47. }
  48. return issues, nil
  49. }
  50. func (r *queryResolver) Project(ctx context.Context, id string) (*models.Project, error) {
  51. user := r.Auth.UserFromContext(ctx)
  52. if user == nil {
  53. return nil, xlerrors.PermissionDenied
  54. }
  55. project, err := r.Database.Projects().Find(ctx, id)
  56. if err != nil {
  57. return nil, err
  58. }
  59. _, err = r.Auth.ProjectPermission(ctx, *project)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return project, nil
  64. }
  65. func (r *queryResolver) Projects(ctx context.Context, filter *models.ProjectFilter) ([]*models.Project, error) {
  66. user := r.Auth.UserFromContext(ctx)
  67. if user == nil {
  68. return nil, xlerrors.PermissionDenied
  69. }
  70. skipCheck := false
  71. if filter == nil {
  72. filter = &models.ProjectFilter{}
  73. }
  74. if filter.Permission == nil {
  75. skipCheck = true
  76. filter.Permission = &models.ProjectFilterPermission{
  77. UserID: user.ID,
  78. MinLevel: models.ProjectPermissionLevelObserver,
  79. }
  80. }
  81. projects, err := r.Database.Projects().List(ctx, *filter)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if !skipCheck && len(projects) > 0 {
  86. deleteList := make([]int, 0, 4)
  87. for i, project := range projects {
  88. if _, err := r.Auth.ProjectPermission(ctx, *project); err != nil {
  89. deleteList = append(deleteList, i-len(deleteList))
  90. }
  91. }
  92. for _, di := range deleteList {
  93. projects = append(projects[:di], projects[di:]...)
  94. }
  95. }
  96. return projects, nil
  97. }
  98. func (r *queryResolver) Session(ctx context.Context) (*models.User, error) {
  99. user := r.Auth.UserFromContext(ctx)
  100. if user == nil {
  101. return nil, errors.New("not logged in")
  102. }
  103. return user, nil
  104. }
  105. // Query returns graphcore.QueryResolver implementation.
  106. func (r *Resolver) Query() graphcore.QueryResolver { return &queryResolver{r} }
  107. type queryResolver struct{ *Resolver }