delphimvcframework/samples/winecellarclient/MainFormU.pas

125 lines
3.2 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit MainFormU;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
2014-04-01 00:38:12 +02:00
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ComCtrls, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, Vcl.Mask, Vcl.DBCtrls, Vcl.ExtCtrls,
MVCFramework.RESTClient;
2013-10-29 16:51:16 +01:00
type
TForm5 = class(TForm)
Button1: TButton;
2014-04-01 00:38:12 +02:00
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
2013-10-29 16:51:16 +01:00
Memo1: TMemo;
2014-04-01 00:38:12 +02:00
DBGrid1: TDBGrid;
FDMemTable1: TFDMemTable;
DataSource1: TDataSource;
FDMemTable1id: TIntegerField;
FDMemTable1name: TStringField;
FDMemTable1year: TStringField;
FDMemTable1grapes: TStringField;
FDMemTable1country: TStringField;
FDMemTable1region: TStringField;
FDMemTable1description: TMemoField;
TabSheet3: TTabSheet;
Label1: TLabel;
DBEdit1: TDBEdit;
Label2: TLabel;
DBEdit2: TDBEdit;
Label3: TLabel;
DBEdit3: TDBEdit;
Label4: TLabel;
DBEdit4: TDBEdit;
Label5: TLabel;
DBEdit5: TDBEdit;
Label6: TLabel;
DBMemo1: TDBMemo;
DBNavigator1: TDBNavigator;
2013-10-29 16:51:16 +01:00
procedure Button1Click(Sender: TObject);
2014-04-01 00:38:12 +02:00
procedure DBGrid1DblClick(Sender: TObject);
procedure FDMemTable1BeforePost(DataSet: TDataSet);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FDMemTable1BeforeDelete(DataSet: TDataSet);
2013-10-29 16:51:16 +01:00
private
2014-04-01 00:38:12 +02:00
RESTClient: TRESTClient;
Loading: Boolean;
2013-10-29 16:51:16 +01:00
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
2014-04-01 00:38:12 +02:00
uses ObjectsMappers;
2013-10-29 16:51:16 +01:00
procedure TForm5.Button1Click(Sender: TObject);
var
response: IRESTResponse;
begin
2014-04-01 00:38:12 +02:00
response := RESTClient.doGET('/wines', []);
Memo1.Lines.Text := response.BodyAsString;
FDMemTable1.Close;
FDMemTable1.Open;
Loading := True;
FDMemTable1.AppendFromJSONArrayString(response.BodyAsString);
FDMemTable1.First;
Loading := False;
end;
procedure TForm5.DBGrid1DblClick(Sender: TObject);
begin
PageControl1.ActivePageIndex := 1;
end;
procedure TForm5.FDMemTable1BeforeDelete(DataSet: TDataSet);
var
Resp: IRESTResponse;
begin
Resp := RESTClient.DSDelete('/wines', FDMemTable1id.AsString);
if not Resp.ResponseCode in [200] then
raise Exception.Create(Resp.ResponseText);
end;
procedure TForm5.FDMemTable1BeforePost(DataSet: TDataSet);
var
Resp: IRESTResponse;
begin
if Loading then
Exit;
case FDMemTable1.State of
dsEdit:
2014-04-01 19:36:05 +02:00
Resp := RESTClient.DSUpdate('/wines', FDMemTable1,
FDMemTable1id.AsString);
2014-04-01 00:38:12 +02:00
dsInsert:
Resp := RESTClient.DSInsert('/wines', FDMemTable1);
2013-10-29 16:51:16 +01:00
end;
2014-04-01 00:38:12 +02:00
if not Resp.ResponseCode in [200, 201] then
raise Exception.Create(Resp.ResponseText);
end;
procedure TForm5.FormClose(Sender: TObject; var Action: TCloseAction);
begin
RESTClient.free;
end;
procedure TForm5.FormCreate(Sender: TObject);
begin
RESTClient := TRESTClient.Create('localhost', 3000);
2013-10-29 16:51:16 +01:00
end;
end.