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.

46 lines
1.3 KiB

  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. "git.aiterp.net/stufflog/server/graph/loaders"
  7. "git.aiterp.net/stufflog/server/graph/graphcore"
  8. "git.aiterp.net/stufflog/server/models"
  9. )
  10. func (r *issueItemResolver) Issue(ctx context.Context, obj *models.IssueItem) (*models.Issue, error) {
  11. return loaders.IssueLoaderFromContext(ctx).Load(obj.IssueID)
  12. }
  13. func (r *issueItemResolver) Item(ctx context.Context, obj *models.IssueItem) (*models.Item, error) {
  14. return r.Database.Items().Find(ctx, obj.ItemID)
  15. }
  16. func (r *issueItemResolver) Remaining(ctx context.Context, obj *models.IssueItem) (int, error) {
  17. if obj.Acquired {
  18. return 0, nil
  19. }
  20. loader := loaders.LogsByIssueLoaderFromContext(ctx)
  21. logs, err := loader.Load(obj.IssueID)
  22. if err != nil {
  23. return 0, err
  24. }
  25. remaining := obj.Quantity
  26. for _, log := range logs {
  27. if item := log.Item(obj.ID); item != nil {
  28. remaining -= item.Amount
  29. }
  30. }
  31. return remaining, nil
  32. }
  33. // IssueItem returns graphcore.IssueItemResolver implementation.
  34. func (r *Resolver) IssueItem() graphcore.IssueItemResolver { return &issueItemResolver{r} }
  35. type issueItemResolver struct{ *Resolver }