Skip to content

Commit

Permalink
/~https://github.com/danieleteti/delphimvcframework/issues/345
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Mar 21, 2020
1 parent 6e19787 commit ef6d609
Show file tree
Hide file tree
Showing 4 changed files with 520 additions and 62 deletions.
38 changes: 20 additions & 18 deletions samples/basicdemo_server/App1MainControllerU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface

{$I dmvcframework.inc}


uses MVCFramework,
MVCFramework.Logger,
MVCFramework.Commons,
Expand All @@ -20,6 +21,7 @@ TApp1MainController = class(TMVCController)

[MVCPath('/hello')]
[MVCHTTPMethod([httpGET])]
[MVCProduces(TMVCMediaType.TEXT_HTML)]
procedure HelloWorld;

[MVCPath('/hello')]
Expand All @@ -28,52 +30,52 @@ TApp1MainController = class(TMVCController)

[MVCPath('/div/($par1)/($par2)')]
[MVCHTTPMethod([httpGET])]
procedure RaiseException(par1, par2: string);
procedure RaiseException(const par1, par2: integer);
end;

implementation

uses
System.SysUtils,
MVCFramework.SystemJSONUtils,
System.JSON;
MVCFramework.Serializer.JSONDataObjects,
JSONDataObjects;

{ TApp1MainController }

procedure TApp1MainController.HelloWorld;
begin
Render('Hello World called with GET');
if Context.Request.ThereIsRequestBody then
Log.Info('Body:' + Context.Request.Body, 'basicdemo');
Render('Hello World! It''s ' + TimeToStr(Time) + ' in the DMVCFramework Land!');
end;

procedure TApp1MainController.HelloWorldPost;
var
JSON: TJSONObject;
lJSON: TJSONObject;
begin
JSON := TSystemJSON.StringAsJSONObject(Context.Request.Body);
lJSON := StrToJSONObject(Context.Request.Body);
try
JSON.AddPair('modified', 'from server');
Render(JSON, false);
Log.Info('Hello world called with POST (request is: ' + lJSON.ToJSON(True) + ')', 'basicdemo');
lJSON.S['modified'] := 'from server';
Render(lJSON, false);
finally
JSON.Free;
lJSON.Free;
end;
Log.Info('Hello world called with POST', 'basicdemo');
end;

procedure TApp1MainController.Index;
begin
Redirect('index.html');
end;

procedure TApp1MainController.RaiseException(par1, par2: string);
procedure TApp1MainController.RaiseException(const par1, par2: integer);
var
R: Extended;
lFS: TFormatSettings;
begin
Log.Info('Parameter1=' + QuotedStr(par1), 'basicdemo');
Log.Info('Parameter2=' + QuotedStr(par2), 'basicdemo');
R := StrToInt(par1) / StrToInt(par2);
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
lFS.DecimalSeparator := '.';
Log.Info('Parameter1=%d', [par1], 'basicdemo');
Log.Info('Parameter2=%d', [par2], 'basicdemo');
Render(
StrDict.Add('result', FloatToStr(par1 / par2, lFS))
);
end;

end.
Loading

0 comments on commit ef6d609

Please sign in to comment.