delphimvcframework/samples/basicdemo_server/App1MainControllerU.pas

86 lines
1.8 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit App1MainControllerU;
interface
{$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])]
procedure Index;
2013-10-29 16:51:16 +01:00
[MVCPath('/hello')]
[MVCHTTPMethod([httpGET])]
procedure HelloWorld;
2013-10-29 16:51:16 +01:00
[MVCPath('/hello')]
[MVCHTTPMethod([httpPOST])]
procedure HelloWorldPost;
2013-10-29 16:51:16 +01:00
[MVCPath('/div/($par1)/($par2)')]
[MVCHTTPMethod([httpGET])]
procedure RaiseException(par1, par2: String);
2013-10-29 16:51:16 +01:00
end;
implementation
uses
System.SysUtils
{$IFDEF SYSTEMJSON}
, System.JSON
{$ELSE}
, Data.DBXJSON
{$IFEND}
;
2013-10-29 16:51:16 +01:00
{ TApp1MainController }
procedure TApp1MainController.HelloWorld;
2013-10-29 16:51:16 +01:00
begin
Render('Hello World called with GET');
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;
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;
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;
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');
R := StrToInt(par1) / StrToInt(par2);
2013-10-29 16:51:16 +01:00
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
end;
end.