2013-10-29 16:51:16 +01:00
|
|
|
unit MainDataModuleUnit;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
Data.DBXFirebird,
|
|
|
|
Data.DB,
|
2014-05-22 01:06:35 +02:00
|
|
|
Data.SqlExpr
|
|
|
|
{$IFDEF VER270}
|
|
|
|
, System.JSON
|
|
|
|
{$ELSE}
|
|
|
|
, Data.DBXJSON
|
|
|
|
{$ENDIF}
|
|
|
|
, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf,
|
2014-04-01 02:12:34 +02:00
|
|
|
FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Comp.Client, FireDAC.Stan.Param,
|
2014-05-22 23:37:13 +02:00
|
|
|
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Phys.IBBase, FireDAC.Phys.FB,
|
|
|
|
WinesBO;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
TWineCellarDataModule = class(TDataModule)
|
2014-04-01 02:12:34 +02:00
|
|
|
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;
|
2014-05-22 23:37:13 +02:00
|
|
|
function AddWine(AWine: TWine): TJSONObject;
|
|
|
|
procedure UpdateWine(AWine: TWine);
|
2013-10-29 16:51:16 +01:00
|
|
|
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,
|
2014-05-22 23:37:13 +02:00
|
|
|
ObjectsMappers;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
{ TCellarSM }
|
|
|
|
|
2014-05-22 23:37:13 +02:00
|
|
|
function TWineCellarDataModule.AddWine(AWine: TWine): TJSONObject;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-05-22 23:37:13 +02:00
|
|
|
Mapper.ObjectToFDParameters(updWines.Commands[arInsert].Params, AWine, 'NEW_');
|
|
|
|
updWines.Commands[arInsert].Execute;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2014-04-01 02:12:34 +02:00
|
|
|
function TWineCellarDataModule.DeleteWine(id: Integer): TJSONObject;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-04-01 02:12:34 +02:00
|
|
|
updWines.Commands[arDelete].ParamByName('OLD_ID').AsInteger := id;
|
|
|
|
updWines.Commands[arDelete].Execute;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2014-04-01 02:12:34 +02:00
|
|
|
procedure TWineCellarDataModule.ConnectionBeforeConnect(Sender: TObject);
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-04-01 02:12:34 +02:00
|
|
|
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
|
2014-04-01 02:12:34 +02:00
|
|
|
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
|
2014-04-01 02:12:34 +02:00
|
|
|
qryWines.Open('SELECT * FROM wine where id = ?', [id]);
|
|
|
|
Result := qryWines.AsJSONObject;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2014-05-22 23:37:13 +02:00
|
|
|
procedure TWineCellarDataModule.UpdateWine(AWine: TWine);
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-05-22 23:37:13 +02:00
|
|
|
Mapper.ObjectToFDParameters(updWines.Commands[arUpdate].Params, AWine, 'NEW_');
|
|
|
|
updWines.Commands[arUpdate].Params.ParamByName('OLD_ID').AsInteger := AWine.id;
|
|
|
|
updWines.Commands[arUpdate].Execute;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|