2014-06-27 16:38:49 +02:00
|
|
|
unit MainFormU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
|
|
|
|
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 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,
|
2014-06-30 12:32:43 +02:00
|
|
|
Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.StdCtrls, MVCFramework.RESTClient,
|
|
|
|
Vcl.DBCtrls;
|
2014-06-27 16:38:49 +02:00
|
|
|
|
|
|
|
type
|
2017-02-04 23:43:06 +01:00
|
|
|
TMainForm = class(TForm)
|
2014-06-27 16:38:49 +02:00
|
|
|
Panel1: TPanel;
|
|
|
|
DBGrid1: TDBGrid;
|
2017-02-04 23:43:06 +01:00
|
|
|
dsArticles: TFDMemTable;
|
|
|
|
dsArticlesid: TIntegerField;
|
|
|
|
dsArticlescode: TStringField;
|
|
|
|
dsArticlesdescription: TStringField;
|
|
|
|
dsArticlesprice: TCurrencyField;
|
|
|
|
dsrcArticles: TDataSource;
|
2014-06-30 12:32:43 +02:00
|
|
|
DBNavigator1: TDBNavigator;
|
2017-02-04 23:43:06 +01:00
|
|
|
btnOpen: TButton;
|
2017-02-06 14:10:04 +01:00
|
|
|
btnRefreshRecord: TButton;
|
|
|
|
Button1: TButton;
|
2014-06-27 16:38:49 +02:00
|
|
|
procedure FormCreate(Sender: TObject);
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure dsArticlesBeforePost(DataSet: TDataSet);
|
|
|
|
procedure dsArticlesBeforeDelete(DataSet: TDataSet);
|
2014-06-30 12:32:43 +02:00
|
|
|
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure dsArticlesBeforeRefresh(DataSet: TDataSet);
|
|
|
|
procedure dsArticlesAfterOpen(DataSet: TDataSet);
|
|
|
|
procedure btnOpenClick(Sender: TObject);
|
|
|
|
procedure btnCloseClick(Sender: TObject);
|
2017-02-06 12:42:17 +01:00
|
|
|
procedure dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
|
2017-02-06 14:10:04 +01:00
|
|
|
procedure btnRefreshRecordClick(Sender: TObject);
|
2014-06-27 16:38:49 +02:00
|
|
|
private
|
2014-06-30 12:32:43 +02:00
|
|
|
FLoading: Boolean;
|
|
|
|
Clt: TRESTClient;
|
2014-06-27 16:38:49 +02:00
|
|
|
{ Private declarations }
|
2014-06-30 12:32:43 +02:00
|
|
|
procedure ShowError(const AResponse: IRESTResponse);
|
2014-06-27 16:38:49 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
2017-02-04 23:43:06 +01:00
|
|
|
MainForm: TMainForm;
|
2014-06-27 16:38:49 +02:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-04-24 00:19:53 +02:00
|
|
|
System.UITypes, MVCFramework.DataSet.Utils;
|
2014-06-27 16:38:49 +02:00
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.btnCloseClick(Sender: TObject);
|
2014-06-27 16:38:49 +02:00
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
dsArticles.Close;
|
2014-06-27 16:38:49 +02:00
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.btnOpenClick(Sender: TObject);
|
2014-06-30 12:32:43 +02:00
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
dsArticles.Open;
|
2014-06-30 12:32:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-02-06 14:10:04 +01:00
|
|
|
procedure TMainForm.btnRefreshRecordClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
dsArticles.RefreshRecord;
|
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
|
2014-06-27 16:38:49 +02:00
|
|
|
var
|
|
|
|
Res: IRESTResponse;
|
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
// this a simple sychronous request...
|
|
|
|
Res := Clt.doGET('/articles', []);
|
2017-04-29 23:56:56 +02:00
|
|
|
if Res.HasError then
|
|
|
|
begin
|
|
|
|
ShowError(Res);
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
DataSet.DisableControls;
|
|
|
|
try
|
|
|
|
FLoading := true;
|
2017-04-24 00:19:53 +02:00
|
|
|
dsArticles.LoadFromJSONArrayString(Res.BodyAsString);
|
2017-02-04 23:43:06 +01:00
|
|
|
FLoading := false;
|
|
|
|
dsArticles.First;
|
|
|
|
finally
|
|
|
|
DataSet.EnableControls;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMainForm.dsArticlesBeforeDelete(DataSet: TDataSet);
|
|
|
|
var
|
|
|
|
Res: IRESTResponse;
|
|
|
|
begin
|
|
|
|
if dsArticles.State = dsBrowse then
|
|
|
|
Res := Clt.DataSetDelete('/articles', dsArticlesid.AsString);
|
|
|
|
if not(Res.ResponseCode in [200]) then
|
2014-06-30 12:32:43 +02:00
|
|
|
begin
|
|
|
|
ShowError(Res);
|
|
|
|
Abort;
|
2017-02-04 23:43:06 +01:00
|
|
|
end;
|
2014-06-30 12:32:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.dsArticlesBeforePost(DataSet: TDataSet);
|
2014-06-30 12:32:43 +02:00
|
|
|
var
|
|
|
|
Res: IRESTResponse;
|
|
|
|
begin
|
|
|
|
if not FLoading then
|
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
if dsArticles.State = dsInsert then
|
|
|
|
Res := Clt.DataSetInsert('/articles', dsArticles)
|
2014-06-30 12:32:43 +02:00
|
|
|
else
|
2017-02-04 23:43:06 +01:00
|
|
|
Res := Clt.DataSetUpdate('/articles', dsArticles, dsArticlesid.AsString);
|
2014-06-30 12:32:43 +02:00
|
|
|
if not(Res.ResponseCode in [200, 201]) then
|
|
|
|
begin
|
|
|
|
ShowError(Res);
|
|
|
|
Abort;
|
|
|
|
end
|
|
|
|
else
|
2017-02-04 23:43:06 +01:00
|
|
|
DataSet.Refresh;
|
2014-06-27 16:38:49 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.dsArticlesBeforeRefresh(DataSet: TDataSet);
|
2017-02-06 12:42:17 +01:00
|
|
|
begin
|
|
|
|
DataSet.Close;
|
|
|
|
DataSet.Open;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMainForm.dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
|
2017-02-06 14:10:04 +01:00
|
|
|
var
|
|
|
|
Res: IRESTResponse;
|
2014-06-30 12:32:43 +02:00
|
|
|
begin
|
2017-02-06 14:10:04 +01:00
|
|
|
Res := Clt.doGET('/articles', [DataSet.FieldByName('id').AsString]);
|
|
|
|
FLoading := true;
|
|
|
|
DataSet.LoadFromJSONObjectString(Res.BodyAsString);
|
|
|
|
FLoading := false;
|
2014-06-30 12:32:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
|
2014-06-27 16:38:49 +02:00
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
Clt.Free;
|
2014-06-27 16:38:49 +02:00
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.FormCreate(Sender: TObject);
|
2014-06-30 12:32:43 +02:00
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
Clt := TRESTClient.Create('localhost', 8080);
|
2014-06-30 12:32:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-02-04 23:43:06 +01:00
|
|
|
procedure TMainForm.ShowError(const AResponse: IRESTResponse);
|
2014-06-30 12:32:43 +02:00
|
|
|
begin
|
2017-02-04 23:43:06 +01:00
|
|
|
MessageDlg(
|
2014-06-30 12:32:43 +02:00
|
|
|
AResponse.ResponseCode.ToString + ': ' + AResponse.ResponseText + sLineBreak +
|
2017-04-24 00:19:53 +02:00
|
|
|
AResponse.BodyAsString,
|
2017-02-04 23:43:06 +01:00
|
|
|
mtError, [mbOK], 0);
|
2014-06-30 12:32:43 +02:00
|
|
|
end;
|
|
|
|
|
2014-06-27 16:38:49 +02:00
|
|
|
end.
|