2013-10-29 16:51:16 +01:00
|
|
|
unit App1MainControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
2017-01-05 12:44:34 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2013-10-29 16:51:16 +01:00
|
|
|
uses MVCFramework,
|
|
|
|
MVCFramework.Logger,
|
2017-03-30 17:00:04 +02:00
|
|
|
MVCFramework.Commons,
|
2013-10-29 16:51:16 +01:00
|
|
|
Web.HTTPApp;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
TApp1MainController = class(TMVCController)
|
|
|
|
public
|
|
|
|
[MVCPath('/')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure Index;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
[MVCPath('/hello')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure HelloWorld;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
[MVCPath('/hello')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure HelloWorldPost;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
[MVCPath('/div/($par1)/($par2)')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure RaiseException(par1, par2: String);
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-01-05 12:44:34 +01:00
|
|
|
System.SysUtils
|
|
|
|
{$IFDEF SYSTEMJSON}
|
|
|
|
, System.JSON
|
|
|
|
{$ELSE}
|
|
|
|
, Data.DBXJSON
|
|
|
|
{$IFEND}
|
|
|
|
;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
{ TApp1MainController }
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TApp1MainController.HelloWorld;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
|
|
|
Render('Hello World called with GET');
|
2016-06-22 17:49:16 +02:00
|
|
|
if Context.Request.ThereIsRequestBody then
|
2016-11-18 14:09:54 +01:00
|
|
|
Log.Info('Body:' + Context.Request.Body, 'basicdemo');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TApp1MainController.HelloWorldPost;
|
2013-10-29 16:51:16 +01:00
|
|
|
var
|
2014-05-11 19:29:20 +02:00
|
|
|
JSON: TJSONObject;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2017-03-30 17:00:04 +02:00
|
|
|
JSON := TJSONObject.ParseJSONValue(Context.Request.Body) as TJSONObject;
|
|
|
|
try
|
|
|
|
if not Assigned(JSON) then
|
|
|
|
raise EMVCException.Create('Invalid JSON');
|
|
|
|
JSON.AddPair('modified', 'from server');
|
|
|
|
Render(JSON, false);
|
|
|
|
finally
|
|
|
|
JSON.Free;
|
|
|
|
end;
|
2016-11-18 14:09:54 +01:00
|
|
|
Log.Info('Hello world called with POST', 'basicdemo');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TApp1MainController.Index;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-05-11 19:29:20 +02:00
|
|
|
Redirect('index.html');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TApp1MainController.RaiseException(par1, par2: String);
|
2013-10-29 16:51:16 +01:00
|
|
|
var
|
|
|
|
R: Extended;
|
|
|
|
begin
|
2016-11-18 14:09:54 +01:00
|
|
|
Log.Info('Parameter1=' + QuotedStr(par1), 'basicdemo');
|
|
|
|
Log.Info('Parameter2=' + QuotedStr(par2), 'basicdemo');
|
2016-06-22 17:49:16 +02:00
|
|
|
R := StrToInt(par1) / StrToInt(par2);
|
2013-10-29 16:51:16 +01:00
|
|
|
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|