command.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package rest
  2. type IValidator interface {
  3. Validate(r *Request) *Response
  4. }
  5. type IExecuter interface {
  6. Execute(r *Request) *Response
  7. }
  8. type ICommand interface {
  9. IValidator
  10. IExecuter
  11. }
  12. ///////////////////////////////////////////////
  13. func NewCommand(validator IValidator, executer IExecuter) *Command {
  14. return &Command{
  15. validator,
  16. executer,
  17. }
  18. }
  19. type Command struct {
  20. validator IValidator
  21. executer IExecuter
  22. }
  23. func (s *Command) Validate(r *Request) *Response {
  24. return s.validator.Validate(r)
  25. }
  26. func (s *Command) Execute(r *Request) *Response {
  27. return s.executer.Execute(r)
  28. }
  29. ///////////////////////////////////////////////
  30. func NewValidator(method func(r *Request) *Response) *Validator {
  31. return &Validator{
  32. method,
  33. }
  34. }
  35. type Validator struct {
  36. method func(r *Request) *Response
  37. }
  38. func (s *Validator) Validate(r *Request) *Response {
  39. return s.method(r)
  40. }
  41. ///////////////////////////////////////////////
  42. func NewExecuter(method func(r *Request) *Response) *Executer {
  43. return &Executer{
  44. method,
  45. }
  46. }
  47. type Executer struct {
  48. method func(r *Request) *Response
  49. }
  50. func (s *Executer) Execute(r *Request) *Response {
  51. return s.method(r)
  52. }