delphimvcframework/samples/basicdemo_server/App1MainControllerU.pas

80 lines
1.7 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])]
2017-05-25 16:57:49 +02:00
procedure RaiseException(par1, par2: string);
2013-10-29 16:51:16 +01:00
end;
implementation
uses
2017-05-25 16:57:49 +02:00
System.SysUtils,
MVCFramework.SystemJSONUtils,
MVCFramework.TypesAliases;
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-05-25 16:57:49 +02:00
JSON := TSystemJSON.StringAsJSONObject(Context.Request.Body);
2017-03-30 17:00:04 +02:00
try
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;
2017-05-25 16:57:49 +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');
R := StrToInt(par1) / StrToInt(par2);
2013-10-29 16:51:16 +01:00
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
end;
end.