delphimvcframework/samples/winecellarclient/MainFormU.pas

125 lines
3.3 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,
2020-09-24 21:30:45 +02:00
MVCFramework.RESTClient.Intf, 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);
2020-09-03 19:57:00 +02:00
procedure FormClose(Sender: TObject; var Action: TCloseAction);
2014-04-01 00:38:12 +02:00
procedure FDMemTable1BeforeDelete(DataSet: TDataSet);
2013-10-29 16:51:16 +01:00
private
2020-09-24 21:30:45 +02:00
RESTClient: IMVCRESTClient;
2014-04-01 00:38:12 +02:00
Loading: Boolean;
2013-10-29 16:51:16 +01:00
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
uses
MVCFramework.DataSet.Utils;
2013-10-29 16:51:16 +01:00
{$R *.dfm}
2014-04-01 00:38:12 +02:00
2013-10-29 16:51:16 +01:00
procedure TForm5.Button1Click(Sender: TObject);
var
2020-09-24 21:30:45 +02:00
response: IMVCRESTResponse;
2013-10-29 16:51:16 +01:00
begin
2020-09-24 21:30:45 +02:00
response := RESTClient.Get('/api/wines');
Memo1.Lines.Text := response.Content;
2014-04-01 00:38:12 +02:00
FDMemTable1.Close;
FDMemTable1.Open;
Loading := True;
2020-09-24 21:30:45 +02:00
FDMemTable1.AppendFromJSONArrayString(response.Content);
2014-04-01 00:38:12 +02:00
FDMemTable1.First;
Loading := False;
end;
procedure TForm5.DBGrid1DblClick(Sender: TObject);
begin
PageControl1.ActivePageIndex := 1;
end;
procedure TForm5.FDMemTable1BeforeDelete(DataSet: TDataSet);
var
2020-09-24 21:30:45 +02:00
Resp: IMVCRESTResponse;
2014-04-01 00:38:12 +02:00
begin
Resp := RESTClient.DataSetDelete('/api/wines', FDMemTable1id.AsString);
2020-09-24 21:30:45 +02:00
if not Resp.StatusCode in [200] then
raise Exception.Create(Resp.Content);
2014-04-01 00:38:12 +02:00
end;
procedure TForm5.FDMemTable1BeforePost(DataSet: TDataSet);
var
2020-09-24 21:30:45 +02:00
Resp: IMVCRESTResponse;
2014-04-01 00:38:12 +02:00
begin
if Loading then
Exit;
case FDMemTable1.State of
dsEdit:
2020-09-24 21:30:45 +02:00
Resp := RESTClient.DataSetUpdate('/api/wines', FDMemTable1id.AsString, FDMemTable1);
2014-04-01 00:38:12 +02:00
dsInsert:
Resp := RESTClient.DataSetInsert('/api/wines', FDMemTable1);
2013-10-29 16:51:16 +01:00
end;
2020-09-24 21:30:45 +02:00
if not Resp.StatusCode in [200, 201] then
raise Exception.Create(Resp.Content);
2020-09-03 19:57:00 +02:00
end;
procedure TForm5.FormClose(Sender: TObject; var Action: TCloseAction);
begin
2020-09-24 21:30:45 +02:00
RESTClient := nil;
2014-04-01 00:38:12 +02:00
end;
procedure TForm5.FormCreate(Sender: TObject);
begin
2020-09-24 21:30:45 +02:00
RESTClient := TMVCRESTClient.New.BaseURL('localhost', 3000);
2013-10-29 16:51:16 +01:00
end;
end.