unit ControllerU; interface uses MVCFramework, MVCFramework.Commons, MVCFramework.Serializer.Commons, System.Generics.Collections, Data.DB; type [MVCNameCase(ncCamelCase)] TPersonRec = record FirstName, LastName: String; Age: Integer; class function Create: TPersonRec; static; end; [MVCNameCase(ncCamelCase)] TPerson = class private fAge: Integer; fFirstName, fLastName: String; public property FirstName: String read fFirstName write fFirstName; property LastName: String read fLastName write fLastName; property Age: Integer read fAge write fAge; constructor Create(FirstName, LastName: String; Age: Integer); end; [MVCPath('/api')] TMyController = class(TMVCController) public { actions returning a simple type } [MVCPath('/sumsasinteger/($A)/($B)')] function GetSum(const A, B: Integer): Integer; [MVCPath('/sumsasfloat/($A)/($B)')] function GetSumAsFloat(const A, B: Extended): Extended; { actions returning records } [MVCPath('/records/single')] function GetSingleRecord: TPersonRec; [MVCPath('/records/multiple')] function GetMultipleRecords: TArray; { actions returning objects } [MVCPath('/objects/single')] function GetSingleObject: TPerson; [MVCPath('/objects/multiple')] function GetMultipleObjects: TObjectList; { actions returning datasets } [MVCPath('/datasets/single')] function GetSingleDataSet: TDataSet; [MVCPath('/datasets/multiple')] function GetMultipleDataSet: TEnumerable; [MVCPath('/datasets/multiple2')] function GetMultipleDataSet2: IMVCObjectDictionary; { customize response headers } [MVCPath('/headers')] function GetWithCustomHeaders: TObjectList; { using TMVCResponse } [MVCPath('/mvcresponse/message')] function GetMVCResponse: TMVCResponse; [MVCPath('/mvcresponse/data')] function GetMVCResponse2: TMVCResponse; [MVCPath('/mvcresponse/list')] function GetMVCResponse3: TMVCResponse; end; implementation uses System.SysUtils, MVCFramework.Logger, System.StrUtils, System.DateUtils, MainDMU, FireDAC.Comp.Client, MVCFramework.FireDAC.Utils; { TMyController } function TMyController.GetMultipleDataSet: TEnumerable; begin var lDM := TdmMain.Create(nil); try lDM.dsPeople.Open; var lList := TObjectList.Create; lList.Add(TFDMemTable.CloneFrom(lDM.dsPeople)); lList.Add(TFDMemTable.CloneFrom(lDM.dsPeople)); Result := lList; finally lDM.Free; end; end; function TMyController.GetMultipleDataSet2: IMVCObjectDictionary; begin var lDM := TdmMain.Create(nil); try lDM.dsPeople.Open; Result := ObjectDict() .Add('people1', TFDMemTable.CloneFrom(lDM.dsPeople)) .Add('people2', TFDMemTable.CloneFrom(lDM.dsPeople)); finally lDM.Free; end; end; function TMyController.GetMultipleObjects: TObjectList; begin Result := TObjectList.Create; Result.Add(TPerson.Create('Daniele', 'Teti', YearsBetween(Date, EncodeDateDay(1979, 1)))); Result.Add(TPerson.Create('Daniele', 'Teti', Result[0].Age + 10)); Result.Add(TPerson.Create('Daniele', 'Teti', Result[0].Age + 20)); end; function TMyController.GetMultipleRecords: TArray; begin SetLength(Result, 3); Result[0] := TPersonRec.Create; Result[1] := TPersonRec.Create; Result[2] := TPersonRec.Create; Inc(Result[1].Age, 10); Inc(Result[2].Age, 20); end; function TMyController.GetMVCResponse: TMVCResponse; begin Result := TMVCResponse.Create(HTTP_STATUS.OK, 'My Reason String', 'My Message'); end; function TMyController.GetMVCResponse2: TMVCResponse; begin Result := TMVCResponse.Create(HTTP_STATUS.OK, 'My Reason String', TPerson.Create('Daniele','Teti', 99)); end; function TMyController.GetMVCResponse3: TMVCResponse; begin Result := TMVCResponse.Create(HTTP_STATUS.OK, 'My Reason String', TObjectList.Create([ TPerson.Create('Daniele','Teti', 99), TPerson.Create('Peter','Parker', 25), TPerson.Create('Bruce','Banner', 45) ]) ); end; function TMyController.GetSingleDataSet: TDataSet; begin var lDM := TdmMain.Create(nil); try lDM.dsPeople.Open; Result := TFDMemTable.CloneFrom(lDM.dsPeople); finally lDM.Free; end; end; function TMyController.GetSingleObject: TPerson; begin Result := TPerson.Create('Daniele', 'Teti', YearsBetween(Date, EncodeDateDay(1979, 1))); end; function TMyController.GetSingleRecord: TPersonRec; begin Result := TPersonRec.Create; end; function TMyController.GetSum(const A, B: Integer): Integer; begin Result := A + B; end; function TMyController.GetSumAsFloat(const A, B: Extended): Extended; begin Result := A + B; end; function TMyController.GetWithCustomHeaders: TObjectList; begin Result := TObjectList.Create; Result.Add(TPerson.Create('Daniele', 'Teti', YearsBetween(Date, EncodeDateDay(1979, 1)))); Result.Add(TPerson.Create('Daniele', 'Teti', Result[0].Age + 10)); Result.Add(TPerson.Create('Daniele', 'Teti', Result[0].Age + 20)); { customize headers } Context.Response.StatusCode := HTTP_STATUS.PartialContent; Context.Response.ContentType := TMVCMediaType.APPLICATION_JSON; Context.Response.SetCustomHeader('X-MYHEADER', 'HELLO WORLD'); end; { TPersonRec } class function TPersonRec.Create: TPersonRec; begin Result.FirstName := 'Daniele'; Result.LastName := 'Teti'; Result.Age := YearsBetween(Date, EncodeDateDay(1979, 1)); end; { TPerson } constructor TPerson.Create(FirstName, LastName: String; Age: Integer); begin inherited Create; fFirstName := FirstName; fLastName := LastName; fAge := Age; end; end.