2013-10-29 16:51:16 +01:00
|
|
|
unit WineCellarAppControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework,
|
2017-01-18 21:53:53 +01:00
|
|
|
MVCFramework.Commons,
|
2013-10-29 16:51:16 +01:00
|
|
|
MainDataModuleUnit;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
TWineCellarApp = class(TMVCController)
|
|
|
|
private
|
|
|
|
dm: TWineCellarDataModule;
|
|
|
|
|
|
|
|
protected
|
|
|
|
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
|
|
|
|
var Handled: Boolean);
|
|
|
|
override;
|
|
|
|
procedure OnAfterAction(Context: TWebContext;
|
|
|
|
const AActionNAme: string); override;
|
|
|
|
|
|
|
|
public
|
|
|
|
[MVCPath('/')]
|
2013-11-11 02:05:03 +01:00
|
|
|
[MVCHTTPMethod([httpGET])]
|
2013-10-29 16:51:16 +01:00
|
|
|
procedure Index(ctx: TWebContext);
|
|
|
|
|
|
|
|
[MVCPath('/wines')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure WinesList(ctx: TWebContext);
|
|
|
|
|
|
|
|
[MVCPath('/wines')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
|
|
|
procedure SaveWine(ctx: TWebContext);
|
|
|
|
|
|
|
|
[MVCPath('/wines/search/($value)')]
|
|
|
|
procedure FindWines(ctx: TWebContext);
|
|
|
|
|
|
|
|
[MVCPath('/wines/($id)')]
|
|
|
|
[MVCHTTPMethod([httpGET, httpDELETE])]
|
|
|
|
procedure WineById(ctx: TWebContext);
|
|
|
|
|
|
|
|
[MVCPath('/wines/($id)')]
|
|
|
|
[MVCHTTPMethod([httpPUT])]
|
|
|
|
procedure UpdateWineById(ctx: TWebContext);
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2014-07-08 12:55:39 +02:00
|
|
|
System.SysUtils, WinesBO, MVCFramework.Logger;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
procedure TWineCellarApp.FindWines(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
Render(dm.FindWines(ctx.Request.Params['value']));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.Index(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
Redirect('/index.html');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.OnAfterAction(Context: TWebContext;
|
|
|
|
const AActionNAme: string);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
dm.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.OnBeforeAction(Context: TWebContext;
|
|
|
|
const AActionNAme: string;
|
|
|
|
var Handled: Boolean);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
dm := TWineCellarDataModule.Create(nil);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.SaveWine(ctx: TWebContext);
|
2014-05-22 23:37:13 +02:00
|
|
|
var
|
|
|
|
Wine: TWine;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-05-22 23:37:13 +02:00
|
|
|
Wine := ctx.Request.BodyAs<TWine>;
|
|
|
|
try
|
|
|
|
dm.AddWine(Wine);
|
2016-11-18 00:17:18 +01:00
|
|
|
Log.Info('Wine correctly saved', 'WINESERVER');
|
2014-05-22 23:37:13 +02:00
|
|
|
finally
|
|
|
|
Wine.Free;
|
|
|
|
end;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.UpdateWineById(ctx: TWebContext);
|
2014-05-22 23:37:13 +02:00
|
|
|
var
|
|
|
|
Wine: TWine;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-05-22 23:37:13 +02:00
|
|
|
Wine := ctx.Request.BodyAs<TWine>;
|
|
|
|
try
|
|
|
|
dm.UpdateWine(Wine);
|
2016-11-18 00:17:18 +01:00
|
|
|
Log.Info('Wine correctly updated', 'WINESERVER');
|
2014-05-22 23:37:13 +02:00
|
|
|
finally
|
|
|
|
Wine.Free;
|
|
|
|
end;
|
|
|
|
Render(200, 'Wine updated');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.WineById(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
// different behaviour according to the request http method
|
|
|
|
case ctx.Request.HTTPMethod of
|
|
|
|
httpDELETE:
|
2014-04-01 02:12:34 +02:00
|
|
|
begin
|
|
|
|
dm.DeleteWine(StrToInt(ctx.Request.Params['id']));
|
2016-11-18 00:17:18 +01:00
|
|
|
Log.Info('Wine deleted', 'WINESERVER');
|
2014-04-01 02:12:34 +02:00
|
|
|
Render(200, 'Wine deleted');
|
|
|
|
end;
|
2013-10-29 16:51:16 +01:00
|
|
|
httpGET:
|
2014-04-01 02:12:34 +02:00
|
|
|
begin
|
|
|
|
Render(dm.GetWineById(StrToInt(ctx.Request.Params['id'])));
|
|
|
|
end
|
2013-10-29 16:51:16 +01:00
|
|
|
else
|
|
|
|
raise Exception.Create('Invalid http method for action');
|
|
|
|
end;
|
2014-04-01 02:12:34 +02:00
|
|
|
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWineCellarApp.WinesList(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
Render(dm.FindWines(''));
|
2016-11-18 00:17:18 +01:00
|
|
|
Log.Info('Getting Wines list', 'WINESERVER');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|