2023-07-18 13:44:14 +02:00
|
|
|
unit ControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework, MVCFramework.Commons, MVCFramework.Serializer.Commons,
|
2023-11-01 23:13:17 +01:00
|
|
|
System.Generics.Collections, Data.DB, JsonDataObjects, System.Rtti;
|
2023-07-18 13:44:14 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TPersonRec = record
|
|
|
|
FirstName, LastName: String;
|
|
|
|
Age: Integer;
|
|
|
|
class function Create: TPersonRec; static;
|
|
|
|
end;
|
|
|
|
|
|
|
|
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;
|
2023-07-20 16:40:39 +02:00
|
|
|
constructor Create(FirstName, LastName: String; Age: Integer);
|
2023-07-18 13:44:14 +02:00
|
|
|
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;
|
2024-06-19 14:25:04 +02:00
|
|
|
[MVCPath('/booleans/($A)/($B)')]
|
|
|
|
function GetOrTruthTable(const A, B: Boolean): Boolean;
|
2024-02-21 14:41:25 +01:00
|
|
|
[MVCPath('/string/($A)/($B)')]
|
|
|
|
function GetConcatAsString(const A, B: String): String;
|
2023-07-18 13:44:14 +02:00
|
|
|
|
|
|
|
{ actions returning records }
|
|
|
|
[MVCPath('/records/single')]
|
|
|
|
function GetSingleRecord: TPersonRec;
|
|
|
|
[MVCPath('/records/multiple')]
|
|
|
|
function GetMultipleRecords: TArray<TPersonRec>;
|
|
|
|
|
|
|
|
{ actions returning objects }
|
|
|
|
[MVCPath('/objects/single')]
|
|
|
|
function GetSingleObject: TPerson;
|
|
|
|
[MVCPath('/objects/multiple')]
|
|
|
|
function GetMultipleObjects: TObjectList<TPerson>;
|
2024-01-20 10:37:34 +01:00
|
|
|
|
|
|
|
{ actions returning json }
|
2023-09-04 17:09:41 +02:00
|
|
|
[MVCPath('/objects/jsonobject')]
|
|
|
|
function GetJSONObject: TJSONObject;
|
|
|
|
[MVCPath('/objects/jsonarray')]
|
|
|
|
function GetJSONArray: TJsonArray;
|
|
|
|
|
2023-07-19 11:10:21 +02:00
|
|
|
{ actions returning datasets }
|
|
|
|
[MVCPath('/datasets/single')]
|
|
|
|
function GetSingleDataSet: TDataSet;
|
|
|
|
[MVCPath('/datasets/multiple')]
|
|
|
|
function GetMultipleDataSet: TEnumerable<TDataSet>;
|
|
|
|
[MVCPath('/datasets/multiple2')]
|
|
|
|
function GetMultipleDataSet2: IMVCObjectDictionary;
|
|
|
|
|
2023-07-18 13:44:14 +02:00
|
|
|
{ customize response headers }
|
|
|
|
[MVCPath('/headers')]
|
|
|
|
function GetWithCustomHeaders: TObjectList<TPerson>;
|
2023-07-20 16:40:39 +02:00
|
|
|
|
2024-06-19 14:25:04 +02:00
|
|
|
{ exceptions }
|
|
|
|
[MVCPath('/exception1')]
|
|
|
|
function GetMVCException: Integer;
|
|
|
|
|
|
|
|
[MVCPath('/exception2')]
|
|
|
|
function GetGeneralException: Integer;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-23 10:03:30 +02:00
|
|
|
{ using IMVCResponse and Response Methods}
|
2023-07-20 16:40:39 +02:00
|
|
|
[MVCPath('/mvcresponse/message')]
|
2023-08-04 13:09:05 +02:00
|
|
|
function GetMVCResponseSimple: IMVCResponse;
|
2023-07-20 16:40:39 +02:00
|
|
|
[MVCPath('/mvcresponse/data')]
|
2023-08-04 13:09:05 +02:00
|
|
|
function GetMVCResponseWithData: IMVCResponse;
|
2023-09-05 09:56:57 +02:00
|
|
|
[MVCPath('/mvcresponse/data/message')]
|
|
|
|
function GetMVCResponseWithDataAndMessage: IMVCResponse;
|
2023-09-04 17:09:41 +02:00
|
|
|
[MVCPath('/mvcresponse/json')]
|
|
|
|
function GetMVCResponseWithJSON: IMVCResponse;
|
2023-07-20 16:40:39 +02:00
|
|
|
[MVCPath('/mvcresponse/list')]
|
2023-08-04 13:09:05 +02:00
|
|
|
function GetMVCResponseWithObjectList: IMVCResponse;
|
|
|
|
[MVCPath('/mvcresponse/dictionary')]
|
|
|
|
function GetMVCResponseWithObjectDictionary: IMVCResponse;
|
2023-09-04 17:09:41 +02:00
|
|
|
[MVCPath('/mvcresponse/message/builder/headers')]
|
|
|
|
function GetMVCResponseSimpleBuilderWithHeaders: IMVCResponse;
|
2024-04-03 16:11:38 +02:00
|
|
|
[MVCPath('/mvcresponse/message/builder/nobody')]
|
|
|
|
function GetMVCResponseNoBody: IMVCResponse;
|
|
|
|
[MVCPath('/mvcresponse/ok')]
|
|
|
|
function GetOKResponse: IMVCResponse;
|
2024-05-23 10:03:30 +02:00
|
|
|
[MVCPath('/mvcresponse/not_found')]
|
|
|
|
function GetNotFound: IMVCResponse;
|
|
|
|
[MVCPath('/mvcresponse/not_modified')]
|
|
|
|
function GetNotModified: IMVCResponse;
|
|
|
|
[MVCPath('/mvcresponse/accepted')]
|
|
|
|
function GetAccepted: IMVCResponse;
|
2023-07-18 13:44:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2023-07-19 11:10:21 +02:00
|
|
|
System.SysUtils, MVCFramework.Logger, System.StrUtils, System.DateUtils,
|
|
|
|
MainDMU, FireDAC.Comp.Client, MVCFramework.FireDAC.Utils;
|
2023-07-18 13:44:14 +02:00
|
|
|
|
|
|
|
{ TMyController }
|
|
|
|
|
2023-10-17 18:08:30 +02:00
|
|
|
function TMyController.GetSingleDataSet: TDataSet;
|
|
|
|
begin
|
|
|
|
var lDM := TdmMain.Create(nil);
|
|
|
|
try
|
|
|
|
lDM.dsPeople.Open;
|
|
|
|
Result := TFDMemTable.CloneFrom(lDM.dsPeople);
|
|
|
|
finally
|
|
|
|
lDM.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-07-19 11:10:21 +02:00
|
|
|
function TMyController.GetMultipleDataSet: TEnumerable<TDataSet>;
|
|
|
|
begin
|
|
|
|
var lDM := TdmMain.Create(nil);
|
|
|
|
try
|
|
|
|
lDM.dsPeople.Open;
|
|
|
|
var lList := TObjectList<TDataSet>.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;
|
|
|
|
|
2024-06-19 14:25:04 +02:00
|
|
|
function TMyController.GetMVCException: Integer;
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create(HTTP_STATUS.NotFound, 'Resource not found');
|
|
|
|
end;
|
2023-07-18 13:44:14 +02:00
|
|
|
|
2024-04-03 16:11:38 +02:00
|
|
|
function TMyController.GetMVCResponseNoBody: IMVCResponse;
|
|
|
|
begin
|
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
|
|
|
.Header('header1', 'Hello World')
|
|
|
|
.Header('header2', 'foo bar')
|
|
|
|
.Build;
|
|
|
|
end;
|
|
|
|
|
2023-08-04 13:09:05 +02:00
|
|
|
function TMyController.GetMVCResponseSimple: IMVCResponse;
|
2023-07-20 16:40:39 +02:00
|
|
|
begin
|
2024-05-23 10:03:30 +02:00
|
|
|
Result := OKResponse('My Message');
|
2023-07-20 16:40:39 +02:00
|
|
|
end;
|
|
|
|
|
2023-09-04 17:09:41 +02:00
|
|
|
function TMyController.GetMVCResponseSimpleBuilderWithHeaders: IMVCResponse;
|
2023-08-29 23:51:08 +02:00
|
|
|
begin
|
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
2023-09-04 17:09:41 +02:00
|
|
|
.Header('header1', 'Hello World')
|
|
|
|
.Header('header2', 'foo bar')
|
|
|
|
.Body('My Message')
|
2023-08-29 23:51:08 +02:00
|
|
|
.Build;
|
|
|
|
end;
|
|
|
|
|
2023-08-04 13:09:05 +02:00
|
|
|
function TMyController.GetMVCResponseWithData: IMVCResponse;
|
2023-08-29 23:51:08 +02:00
|
|
|
begin
|
2024-04-03 16:11:38 +02:00
|
|
|
Result := OKResponse(TPerson.Create('Daniele','Teti', 99));
|
2023-08-29 23:51:08 +02:00
|
|
|
end;
|
|
|
|
|
2023-09-05 09:56:57 +02:00
|
|
|
function TMyController.GetMVCResponseWithDataAndMessage: IMVCResponse;
|
|
|
|
begin
|
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
|
|
|
.Body('This is a message') //<< Message
|
|
|
|
.Body(TPerson.Create('Daniele','Teti', 99)) //<< Data
|
|
|
|
.Body(ObjectDict().Add('person', TPerson.Create('Daniele','Teti', 99)))
|
|
|
|
.Build;
|
|
|
|
end;
|
|
|
|
|
2023-08-04 13:09:05 +02:00
|
|
|
function TMyController.GetMVCResponseWithObjectDictionary: IMVCResponse;
|
|
|
|
begin
|
2023-09-04 17:09:41 +02:00
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
|
|
|
.Body(ObjectDict()
|
2023-08-04 13:09:05 +02:00
|
|
|
.Add('people1', TObjectList<TPerson>.Create([
|
|
|
|
TPerson.Create('Daniele','Teti', 99),
|
|
|
|
TPerson.Create('Peter','Parker', 25),
|
|
|
|
TPerson.Create('Bruce','Banner', 45)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
.Add('people2', TObjectList<TPerson>.Create([
|
|
|
|
TPerson.Create('Daniele','Teti', 99),
|
|
|
|
TPerson.Create('Peter','Parker', 25),
|
|
|
|
TPerson.Create('Bruce','Banner', 45)
|
|
|
|
])
|
|
|
|
)
|
2023-09-04 17:09:41 +02:00
|
|
|
)
|
|
|
|
.Build;
|
2023-08-04 13:09:05 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMyController.GetMVCResponseWithObjectList: IMVCResponse;
|
|
|
|
begin
|
2024-04-03 16:11:38 +02:00
|
|
|
Result := OKResponse(TObjectList<TPerson>.Create([
|
|
|
|
TPerson.Create('Daniele','Teti', 99),
|
|
|
|
TPerson.Create('Peter','Parker', 25),
|
|
|
|
TPerson.Create('Bruce','Banner', 45)
|
|
|
|
]));
|
|
|
|
end;
|
|
|
|
|
2024-05-23 10:03:30 +02:00
|
|
|
function TMyController.GetNotFound: IMVCResponse;
|
|
|
|
begin
|
|
|
|
Result := NotFoundResponse;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMyController.GetNotModified: IMVCResponse;
|
|
|
|
begin
|
|
|
|
Result := NotModifiedResponse;
|
|
|
|
end;
|
|
|
|
|
2024-04-03 16:11:38 +02:00
|
|
|
function TMyController.GetOKResponse: IMVCResponse;
|
|
|
|
begin
|
|
|
|
Result := OKResponse;
|
2023-09-04 17:09:41 +02:00
|
|
|
end;
|
|
|
|
|
2024-06-19 14:25:04 +02:00
|
|
|
function TMyController.GetOrTruthTable(const A, B: Boolean): Boolean;
|
|
|
|
begin
|
|
|
|
Result := A or B;
|
|
|
|
end;
|
|
|
|
|
2023-09-04 17:09:41 +02:00
|
|
|
function TMyController.GetMVCResponseWithJSON: IMVCResponse;
|
|
|
|
begin
|
2024-04-03 16:11:38 +02:00
|
|
|
Result := OKResponse(StrToJSONObject('{"name":"Daniele","surname":"Teti"}'));
|
2023-07-20 16:40:39 +02:00
|
|
|
end;
|
|
|
|
|
2023-07-18 13:44:14 +02:00
|
|
|
function TMyController.GetSingleObject: TPerson;
|
|
|
|
begin
|
2023-07-20 16:40:39 +02:00
|
|
|
Result := TPerson.Create('Daniele', 'Teti', YearsBetween(Date, EncodeDateDay(1979, 1)));
|
2023-07-18 13:44:14 +02:00
|
|
|
end;
|
|
|
|
|
2024-01-20 10:37:34 +01:00
|
|
|
function TMyController.GetMultipleObjects: TObjectList<TPerson>;
|
|
|
|
begin
|
|
|
|
Result := TObjectList<TPerson>.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.GetJSONArray: TJsonArray;
|
|
|
|
begin
|
|
|
|
Result := StrToJSONArray('[1,2,3, {"name":"Daniele","surname":"Teti"}]');
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMyController.GetJSONObject: TJSONObject;
|
|
|
|
begin
|
|
|
|
Result := StrToJSONObject('{"name":"Daniele","surname":"Teti"}');
|
|
|
|
end;
|
|
|
|
|
2023-07-18 13:44:14 +02:00
|
|
|
function TMyController.GetSingleRecord: TPersonRec;
|
|
|
|
begin
|
|
|
|
Result := TPersonRec.Create;
|
|
|
|
end;
|
|
|
|
|
2024-01-20 10:37:34 +01:00
|
|
|
function TMyController.GetMultipleRecords: TArray<TPersonRec>;
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2023-07-18 13:44:14 +02:00
|
|
|
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;
|
|
|
|
|
2024-05-23 10:03:30 +02:00
|
|
|
function TMyController.GetAccepted: IMVCResponse;
|
|
|
|
begin
|
|
|
|
Result := AcceptedResponse('https://www.danieleteti.it');
|
|
|
|
end;
|
|
|
|
|
2024-02-21 14:41:25 +01:00
|
|
|
function TMyController.GetConcatAsString(const A, B: String): String;
|
|
|
|
begin
|
|
|
|
Result := A + B;
|
|
|
|
end;
|
|
|
|
|
2024-06-19 14:25:04 +02:00
|
|
|
function TMyController.GetGeneralException: Integer;
|
|
|
|
begin
|
|
|
|
raise Exception.Create('This is a general exception');
|
|
|
|
end;
|
|
|
|
|
2023-07-18 13:44:14 +02:00
|
|
|
function TMyController.GetWithCustomHeaders: TObjectList<TPerson>;
|
|
|
|
begin
|
|
|
|
Result := TObjectList<TPerson>.Create;
|
|
|
|
|
2023-07-20 16:40:39 +02:00
|
|
|
Result.Add(TPerson.Create('Daniele', 'Teti', YearsBetween(Date, EncodeDateDay(1979, 1))));
|
2023-07-18 13:44:14 +02:00
|
|
|
|
2023-07-20 16:40:39 +02:00
|
|
|
Result.Add(TPerson.Create('Daniele', 'Teti', Result[0].Age + 10));
|
2023-07-18 13:44:14 +02:00
|
|
|
|
2023-07-20 16:40:39 +02:00
|
|
|
Result.Add(TPerson.Create('Daniele', 'Teti', Result[0].Age + 20));
|
2023-07-18 13:44:14 +02:00
|
|
|
|
|
|
|
{ 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;
|
|
|
|
|
2023-07-20 16:40:39 +02:00
|
|
|
{ TPerson }
|
|
|
|
|
|
|
|
constructor TPerson.Create(FirstName, LastName: String; Age: Integer);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fFirstName := FirstName;
|
|
|
|
fLastName := LastName;
|
|
|
|
fAge := Age;
|
|
|
|
end;
|
|
|
|
|
2023-07-18 13:44:14 +02:00
|
|
|
end.
|