delphimvcframework/samples/winecellar/MainDataModuleUnit.pas

101 lines
2.7 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit MainDataModuleUnit;
interface
uses System.SysUtils,
System.Classes,
Data.DBXFirebird,
Data.DB,
Data.SqlExpr,
Data.DBXJSON, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf,
FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Comp.Client, FireDAC.Stan.Param,
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Phys.IBBase, FireDAC.Phys.FB;
2013-10-29 16:51:16 +01:00
type
TWineCellarDataModule = class(TDataModule)
Connection: TFDConnection;
qryWines: TFDQuery;
updWines: TFDUpdateSQL;
FDPhysFBDriverLink1: TFDPhysFBDriverLink;
procedure ConnectionBeforeConnect(Sender: TObject);
2013-10-29 16:51:16 +01:00
public
function GetWineById(id: Integer): TJSONObject;
function FindWines(Search: string): TJSONArray;
function AddWine(AWine: TJSONObject): TJSONObject;
2013-10-29 16:51:16 +01:00
function UpdateWine(Wine: TJSONObject): TJSONObject;
function DeleteWine(id: Integer): TJSONObject;
end;
implementation
{$R *.dfm}
2014-02-18 17:55:02 +01:00
2013-10-29 16:51:16 +01:00
uses System.StrUtils,
Data.DBXCommon,
ObjectsMappers,
WinesBO;
{ TCellarSM }
function TWineCellarDataModule.AddWine(AWine: TJSONObject): TJSONObject;
2013-10-29 16:51:16 +01:00
var
Wine: TWine;
2013-10-29 16:51:16 +01:00
begin
Wine := Mapper.JSONObjectToObject<TWine>(AWine);
2013-10-29 16:51:16 +01:00
try
Mapper.ObjectToFDParameters(
updWines.Commands[arInsert].Params,
Wine,
'NEW_');
updWines.Commands[arInsert].Execute;
2013-10-29 16:51:16 +01:00
finally
Wine.Free;
2013-10-29 16:51:16 +01:00
end;
end;
function TWineCellarDataModule.DeleteWine(id: Integer): TJSONObject;
2013-10-29 16:51:16 +01:00
begin
updWines.Commands[arDelete].ParamByName('OLD_ID').AsInteger := id;
updWines.Commands[arDelete].Execute;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarDataModule.ConnectionBeforeConnect(Sender: TObject);
2013-10-29 16:51:16 +01:00
begin
Connection.Params.Values['Database'] := ExtractFilePath(ParamStr(0)
) + '..\..\WINES.FDB';
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.FindWines(Search: string): TJSONArray;
begin
if Search.IsEmpty then
qryWines.Open('SELECT * FROM wine')
else
qryWines.Open('SELECT * FROM wine where NAME CONTAINING ?', [Search]);
Result := qryWines.AsJSONArray;
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.GetWineById(id: Integer): TJSONObject;
begin
qryWines.Open('SELECT * FROM wine where id = ?', [id]);
Result := qryWines.AsJSONObject;
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.UpdateWine(Wine: TJSONObject): TJSONObject;
var
2014-02-18 17:55:02 +01:00
w: TWine;
2013-10-29 16:51:16 +01:00
begin
w := Mapper.JSONObjectToObject<TWine>(Wine);
try
Mapper.ObjectToFDParameters(updWines.Commands[arUpdate].Params, w, 'NEW_');
updWines.Commands[arUpdate].Params.ParamByName('OLD_ID').AsInteger := w.id;
updWines.Commands[arUpdate].Execute;
2013-10-29 16:51:16 +01:00
finally
w.Free;
2013-10-29 16:51:16 +01:00
end;
Result := TJSONObject.Create;
end;
end.