You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

operator.go 1.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package reward
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/services/reward/point"
  5. "errors"
  6. "fmt"
  7. )
  8. var RewardOperatorMap = map[string]RewardOperator{
  9. fmt.Sprint(models.RewardTypePoint): new(point.PointOperator),
  10. }
  11. type RewardOperateContext struct {
  12. SourceType models.RewardSourceType
  13. SourceId string
  14. Remark string
  15. Reward Reward
  16. TargetUserId int64
  17. }
  18. type RewardOperateResponse int
  19. const (
  20. RewardOperateSuccess RewardOperateResponse = iota + 1
  21. RewardOperateBalanceNotEnough
  22. )
  23. func (t RewardOperateResponse) IsSuccess() bool {
  24. return t == RewardOperateSuccess
  25. }
  26. type RewardOperator interface {
  27. IsOperated(ctx RewardOperateContext) bool
  28. IsLimited(ctx RewardOperateContext) bool
  29. Operate(ctx RewardOperateContext) error
  30. }
  31. func Send(ctx RewardOperateContext) error {
  32. operator := GetOperator(ctx.Reward.Type)
  33. if operator == nil {
  34. return errors.New("operator of reward type is not exist")
  35. }
  36. if operator.IsOperated(ctx) {
  37. return nil
  38. }
  39. if operator.IsLimited(ctx) {
  40. return nil
  41. }
  42. if err := operator.Operate(ctx); err != nil {
  43. return err
  44. }
  45. return nil
  46. }
  47. func GetOperator(rewardType string) RewardOperator {
  48. return RewardOperatorMap[rewardType]
  49. }