delphimvcframework/samples/basicdemo_server/App1MainControllerU.pas

82 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])]
[MVCProduces(TMVCMediaType.TEXT_HTML)]
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(const par1, par2: integer);
2013-10-29 16:51:16 +01:00
end;
implementation
uses
2017-05-25 16:57:49 +02:00
System.SysUtils,
MVCFramework.Serializer.JSONDataObjects,
JSONDataObjects;
2013-10-29 16:51:16 +01:00
{ TApp1MainController }
procedure TApp1MainController.HelloWorld;
2013-10-29 16:51:16 +01:00
begin
Render('Hello World! It''s ' + TimeToStr(Time) + ' in the DMVCFramework Land!');
2013-10-29 16:51:16 +01:00
end;
procedure TApp1MainController.HelloWorldPost;
2013-10-29 16:51:16 +01:00
var
lJSON: TJSONObject;
2013-10-29 16:51:16 +01:00
begin
lJSON := StrToJSONObject(Context.Request.Body);
2017-03-30 17:00:04 +02:00
try
Log.Info('Hello world called with POST (request is: ' + lJSON.ToJSON(True) + ')', 'basicdemo');
lJSON.S['modified'] := 'from server';
Render(lJSON, false);
2017-03-30 17:00:04 +02:00
finally
lJSON.Free;
2017-03-30 17:00:04 +02:00
end;
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(const par1, par2: integer);
2013-10-29 16:51:16 +01:00
var
lFS: TFormatSettings;
2013-10-29 16:51:16 +01:00
begin
lFS.DecimalSeparator := '.';
Log.Info('Parameter1=%d', [par1], 'basicdemo');
Log.Info('Parameter2=%d', [par2], 'basicdemo');
Render(
StrDict.Add('result', FloatToStr(par1 / par2, lFS))
);
2013-10-29 16:51:16 +01:00
end;
end.