delphimvcframework/samples/winecellarclient_withlivebinding/MainFormU.pas

186 lines
4.6 KiB
ObjectPascal
Raw Normal View History

unit MainFormU;
interface
uses
2020-09-24 21:30:45 +02:00
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Vcl.Grids,
Vcl.DBGrids,
Vcl.ComCtrls,
2015-04-01 17:01:23 +02:00
FireDAC.Stan.Intf,
2020-09-24 21:30:45 +02:00
FireDAC.Stan.Option,
FireDAC.Stan.Param,
FireDAC.Stan.Error,
FireDAC.DatS,
FireDAC.Phys.Intf,
2015-04-01 17:01:23 +02:00
FireDAC.DApt.Intf,
2020-09-24 21:30:45 +02:00
Data.DB,
FireDAC.Comp.DataSet,
FireDAC.Comp.Client,
Vcl.Mask,
Vcl.DBCtrls,
Vcl.ExtCtrls,
MVCFramework.RESTClient.Intf,
MVCFramework.RESTClient,
Data.Bind.GenData,
Vcl.Bind.GenData,
Data.Bind.Components,
2015-04-01 17:01:23 +02:00
Data.Bind.ObjectScope,
2020-09-24 21:30:45 +02:00
System.Rtti,
System.Bindings.Outputs,
Vcl.Bind.Editors,
Data.Bind.EngExt,
Vcl.Bind.DBEngExt,
2015-04-01 17:01:23 +02:00
Vcl.Buttons,
2020-09-24 21:30:45 +02:00
Vcl.Bind.Navigator,
WinesBO,
Generics.Collections,
Data.Bind.Controls;
type
TForm5 = class(TForm)
Button1: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Memo1: TMemo;
TabSheet3: TTabSheet;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
PrototypeBindSource1: TPrototypeBindSource;
ListView1: TListView;
BindingsList1: TBindingsList;
LinkListControlToField1: TLinkListControlToField;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Memo2: TMemo;
LinkControlToField1: TLinkControlToField;
LinkControlToField2: TLinkControlToField;
LinkControlToField3: TLinkControlToField;
LinkControlToField4: TLinkControlToField;
LinkControlToField5: TLinkControlToField;
LinkControlToField6: TLinkControlToField;
BindNavigator1: TBindNavigator;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListView1DblClick(Sender: TObject);
2015-04-01 17:01:23 +02:00
procedure PrototypeBindSource1CreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
procedure BindNavigator1BeforeAction(Sender: TObject; Button: TNavigateButton);
private
2020-09-24 21:30:45 +02:00
RESTClient: IMVCRESTClient;
WinesAdapter: TListBindSourceAdapter<TWine>;
FWines: TObjectList<TWine>;
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
2017-05-09 23:13:51 +02:00
uses
MVCFramework.SystemJSONUtils,
MVCFramework.Serializer.Defaults,
System.JSON,
2017-05-09 23:13:51 +02:00
MVCFramework.Commons;
2017-05-09 23:13:51 +02:00
{$R *.dfm}
2020-09-24 21:30:45 +02:00
procedure TForm5.BindNavigator1BeforeAction(Sender: TObject; Button: TNavigateButton);
var
2020-09-24 21:30:45 +02:00
Resp: IMVCRESTResponse;
begin
Resp := nil;
case Button of
nbDelete:
begin
2020-09-24 21:30:45 +02:00
Resp := RESTClient
.AddPathParam('id',(WinesAdapter.Current as TWine).id.ToString)
.Delete('/wines/{id}');
end;
nbPost:
begin
case WinesAdapter.State of
seEdit:
begin
WinesAdapter.Post;
2020-09-24 21:30:45 +02:00
Resp := RESTClient
.AddPathParam('id', (WinesAdapter.Current as TWine).id.ToString)
.Put('/wines/{id}',
2017-05-09 23:13:51 +02:00
GetDefaultSerializer.SerializeObject(WinesAdapter.Current));
Abort;
end;
seInsert:
begin
WinesAdapter.Post;
2020-09-24 21:30:45 +02:00
Resp := RESTClient.Post('/wines', GetDefaultSerializer.SerializeObject(WinesAdapter.Current));
Abort;
end;
end;
end;
end;
2020-09-24 21:30:45 +02:00
if Assigned(Resp) and (not Resp.StatusCode in [200, 201]) then
raise Exception.Create(Resp.StatusText);
end;
procedure TForm5.Button1Click(Sender: TObject);
var
2020-09-24 21:30:45 +02:00
response: IMVCRESTResponse;
begin
2020-09-24 21:30:45 +02:00
response := RESTClient.Get('/wines');
Memo1.Lines.Text := response.Content;
FWines.Clear;
2020-09-24 21:30:45 +02:00
GetDefaultSerializer.DeserializeCollection(response.Content, FWines, TWine);
WinesAdapter.SetList(FWines, false);
WinesAdapter.Active := True;
end;
procedure TForm5.FormClose(Sender: TObject; var Action: TCloseAction);
begin
2020-09-24 21:30:45 +02:00
RESTClient := nil;
FWines.free;
end;
procedure TForm5.FormCreate(Sender: TObject);
begin
2020-09-24 21:30:45 +02:00
RESTClient := TMVCRESTClient.New.BaseURL('localhost', 3000);
FWines := TObjectList<TWine>.Create(True);
PrototypeBindSource1.Active := True;
end;
procedure TForm5.ListView1DblClick(Sender: TObject);
begin
PageControl1.ActivePageIndex := 1;
end;
2015-04-01 17:01:23 +02:00
procedure TForm5.PrototypeBindSource1CreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
begin
WinesAdapter := TListBindSourceAdapter<TWine>.Create(PrototypeBindSource1);
ABindSourceAdapter := WinesAdapter;
end;
end.