delphimvcframework/samples/articles_crud_vcl_client/MainFormU.pas

161 lines
4.0 KiB
ObjectPascal
Raw Normal View History

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;
type
TMainForm = class(TForm)
Panel1: TPanel;
DBGrid1: TDBGrid;
dsArticles: TFDMemTable;
dsArticlesid: TIntegerField;
dsArticlescode: TStringField;
dsArticlesdescription: TStringField;
dsArticlesprice: TCurrencyField;
dsrcArticles: TDataSource;
2014-06-30 12:32:43 +02:00
DBNavigator1: TDBNavigator;
btnOpen: TButton;
btnRefreshRecord: TButton;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure dsArticlesBeforePost(DataSet: TDataSet);
procedure dsArticlesBeforeDelete(DataSet: TDataSet);
2014-06-30 12:32:43 +02:00
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure dsArticlesBeforeRefresh(DataSet: TDataSet);
procedure dsArticlesAfterOpen(DataSet: TDataSet);
procedure btnOpenClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
procedure btnRefreshRecordClick(Sender: TObject);
private
2014-06-30 12:32:43 +02:00
FLoading: Boolean;
Clt: TRESTClient;
{ Private declarations }
2014-06-30 12:32:43 +02:00
procedure ShowError(const AResponse: IRESTResponse);
end;
var
MainForm: TMainForm;
implementation
uses
System.UITypes, MVCFramework.DataSet.Utils;
{$R *.dfm}
procedure TMainForm.btnCloseClick(Sender: TObject);
begin
dsArticles.Close;
end;
procedure TMainForm.btnOpenClick(Sender: TObject);
2014-06-30 12:32:43 +02:00
begin
dsArticles.Open;
2014-06-30 12:32:43 +02:00
end;
procedure TMainForm.btnRefreshRecordClick(Sender: TObject);
begin
dsArticles.RefreshRecord;
end;
procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
var
Res: IRESTResponse;
begin
// this a simple sychronous request...
Res := Clt.doGET('/articles', []);
if Res.HasError then
begin
ShowError(Res);
Exit;
end;
DataSet.DisableControls;
try
FLoading := true;
dsArticles.LoadFromJSONArrayString(Res.BodyAsString);
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;
end;
2014-06-30 12:32:43 +02:00
end;
procedure TMainForm.dsArticlesBeforePost(DataSet: TDataSet);
2014-06-30 12:32:43 +02:00
var
Res: IRESTResponse;
begin
if not FLoading then
begin
if dsArticles.State = dsInsert then
Res := Clt.DataSetInsert('/articles', dsArticles)
2014-06-30 12:32:43 +02:00
else
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
DataSet.Refresh;
end;
end;
procedure TMainForm.dsArticlesBeforeRefresh(DataSet: TDataSet);
begin
DataSet.Close;
DataSet.Open;
end;
procedure TMainForm.dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
var
Res: IRESTResponse;
2014-06-30 12:32:43 +02:00
begin
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;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Clt.Free;
end;
procedure TMainForm.FormCreate(Sender: TObject);
2014-06-30 12:32:43 +02:00
begin
Clt := TRESTClient.Create('localhost', 8080);
2014-06-30 12:32:43 +02:00
end;
procedure TMainForm.ShowError(const AResponse: IRESTResponse);
2014-06-30 12:32:43 +02:00
begin
MessageDlg(
2014-06-30 12:32:43 +02:00
AResponse.ResponseCode.ToString + ': ' + AResponse.ResponseText + sLineBreak +
AResponse.BodyAsString,
mtError, [mbOK], 0);
2014-06-30 12:32:43 +02:00
end;
end.