delphimvcframework/samples/winecellarserver/WineCellarAppControllerU.pas

121 lines
2.8 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit WineCellarAppControllerU;
interface
uses
MVCFramework,
MVCFramework.Commons,
2013-10-29 16:51:16 +01:00
MainDataModuleUnit;
type
[MVCPath('/api')]
2013-10-29 16:51:16 +01:00
TWineCellarApp = class(TMVCController)
private
FDataModule: TWineCellarDataModule;
2013-10-29 16:51:16 +01:00
protected
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string; var Handled: Boolean); override;
procedure OnAfterAction(Context: TWebContext; const AActionNAme: string); override;
2013-10-29 16:51:16 +01:00
public
[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
System.SysUtils,
WinesBO,
MVCFramework.Logger,
MVCFramework.Serializer.Commons;
2013-10-29 16:51:16 +01:00
procedure TWineCellarApp.FindWines(ctx: TWebContext);
begin
Render(FDataModule.FindWines(ctx.Request.Params['value']));
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarApp.OnAfterAction(Context: TWebContext; const AActionNAme: string);
2013-10-29 16:51:16 +01:00
begin
inherited;
FDataModule.Free;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarApp.OnBeforeAction(Context: TWebContext; const AActionNAme: string; var Handled: Boolean);
2013-10-29 16:51:16 +01:00
begin
inherited;
FDataModule := TWineCellarDataModule.Create(nil);
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarApp.SaveWine(ctx: TWebContext);
var
lWine: TWine;
2013-10-29 16:51:16 +01:00
begin
lWine := ctx.Request.BodyAs<TWine>;
try
FDataModule.AddWine(lWine);
Log.Info('Wine correctly saved', 'WINESERVER');
finally
lWine.Free;
end;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarApp.UpdateWineById(ctx: TWebContext);
var
lWine: TWine;
2013-10-29 16:51:16 +01:00
begin
lWine := ctx.Request.BodyAs<TWine>;
try
FDataModule.UpdateWine(lWine);
Log.Info('Wine correctly updated', 'WINESERVER');
finally
lWine.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:
begin
FDataModule.DeleteWine(StrToInt(ctx.Request.Params['id']));
Log.Info('Wine deleted', 'WINESERVER');
Render(200, 'Wine deleted');
end;
2013-10-29 16:51:16 +01:00
httpGET:
begin
Render(FDataModule.GetWineById(StrToInt(ctx.Request.Params['id'])), False, dstSingleRecord);
end
2013-10-29 16:51:16 +01:00
else
raise Exception.Create('Invalid http method for action');
end;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarApp.WinesList(ctx: TWebContext);
begin
Render(FDataModule.GetAllWines, False);
Log.Info('Getting Wines list', 'WINESERVER');
2013-10-29 16:51:16 +01:00
end;
end.