unit1.pas 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. BrookAction, person;
  6. type
  7. { TPersonRESTAction }
  8. TPersonRESTAction = class(specialize TBrookGAction<TPerson>)
  9. private
  10. FOpf: TPersonOpf;
  11. public
  12. constructor Create; overload; override;
  13. destructor Destroy; override;
  14. procedure Get; override;
  15. procedure Post; override;
  16. procedure Put; override;
  17. procedure Delete; override;
  18. property Opf: TPersonOpf read FOpf;
  19. end;
  20. implementation
  21. { TPersonRESTAction }
  22. constructor TPersonRESTAction.Create;
  23. begin
  24. inherited Create;
  25. FOpf := TPersonOpf.Create;
  26. end;
  27. destructor TPersonRESTAction.Destroy;
  28. begin
  29. FOpf.Free;
  30. inherited Destroy;
  31. end;
  32. procedure TPersonRESTAction.Get;
  33. var
  34. VPerson: TPerson;
  35. VPersons: TPersonOpf.TEntities;
  36. begin
  37. VPersons := TPersonOpf.TEntities.Create;
  38. try
  39. FOpf.List(VPersons);
  40. for VPerson in VPersons do
  41. Write('Id: %d, Name: %s<br />', [VPerson.Id, VPerson.Name]);
  42. finally
  43. VPersons.Free;
  44. end;
  45. end;
  46. procedure TPersonRESTAction.Post;
  47. begin
  48. Entity.Validate;
  49. FOpf.Add(Entity);
  50. FOpf.Apply;
  51. end;
  52. procedure TPersonRESTAction.Put;
  53. begin
  54. Entity.Validate;
  55. FOpf.Modify(Entity);
  56. FOpf.Apply;
  57. end;
  58. procedure TPersonRESTAction.Delete;
  59. begin
  60. FOpf.Remove(Entity);
  61. FOpf.Apply;
  62. end;
  63. initialization
  64. TPersonRESTAction.Register('*');
  65. end.