Merge remote-tracking branch 'upstream/master' into middleware_etag

This commit is contained in:
João Antônio Duarte 2020-04-23 19:47:02 -03:00
commit b4272f98f2
4 changed files with 182 additions and 46 deletions

View File

@ -5,6 +5,7 @@ interface
uses uses
mvcframework, mvcframework,
mvcframework.Commons, mvcframework.Commons,
mvcframework.Serializer.Commons,
Controllers.Base; Controllers.Base;
type type
@ -67,7 +68,7 @@ begin
Article := Context.Request.BodyAs<TArticle>; Article := Context.Request.BodyAs<TArticle>;
try try
GetArticlesService.Add(Article); GetArticlesService.Add(Article);
Render201Created('/articles/' + Article.ID.ToString, 'Article Created'); Render201Created('/articles/' + Article.id.ToString, 'Article Created');
finally finally
Article.Free; Article.Free;
end; end;
@ -111,22 +112,27 @@ end;
procedure TArticlesController.GetArticles; procedure TArticlesController.GetArticles;
begin begin
Render<TArticle>(GetArticlesService.GetAll); Render(
ObjectDict().Add('data', GetArticlesService.GetAll)
);
end; end;
procedure TArticlesController.GetArticlesByDescription; procedure TArticlesController.GetArticlesByDescription;
var var
lSearch: String; lSearch: string;
lDict: IMVCObjectDictionary;
begin begin
try try
if random(10) < 2 then
raise EMVCException.Create('ERRORONE!!!');
lSearch := Context.Request.Params['q']; lSearch := Context.Request.Params['q'];
if lSearch = '' then if lSearch = '' then
Render<TArticle>(GetArticlesService.GetAll) begin
lDict := ObjectDict().Add('data', GetArticlesService.GetAll);
end
else else
Render<TArticle>(GetArticlesService.GetArticles(lSearch)); begin
lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(lSearch));
end;
Render(lDict);
except except
on E: EServiceException do on E: EServiceException do
begin begin
@ -152,12 +158,9 @@ begin
end; end;
procedure TArticlesController.GetArticleByID(id: Integer); procedure TArticlesController.GetArticleByID(id: Integer);
var
Article: TArticle;
begin begin
try try
Article := GetArticlesService.GetByID(id); Render(ObjectDict().Add('data', GetArticlesService.GetByID(id)));
Render(Article);
except except
on E: EServiceException do on E: EServiceException do
begin begin

View File

@ -19,7 +19,7 @@ object MainForm: TMainForm
Left = 0 Left = 0
Top = 0 Top = 0
Width = 876 Width = 876
Height = 39 Height = 112
Align = alTop Align = alTop
TabOrder = 0 TabOrder = 0
object DBNavigator1: TDBNavigator object DBNavigator1: TDBNavigator
@ -27,50 +27,87 @@ object MainForm: TMainForm
Left = 585 Left = 585
Top = 4 Top = 4
Width = 287 Width = 287
Height = 31 Height = 40
DataSource = dsrcArticles DataSource = dsrcArticles
Align = alRight Align = alRight
TabOrder = 0 TabOrder = 0
ExplicitHeight = 31
end end
object btnOpen: TButton object btnOpen: TButton
AlignWithMargins = True AlignWithMargins = True
Left = 4 Left = 4
Top = 4 Top = 4
Width = 75 Width = 75
Height = 31 Height = 40
Align = alLeft Align = alLeft
Caption = 'Open' Caption = 'Open'
TabOrder = 1 TabOrder = 1
OnClick = btnOpenClick OnClick = btnOpenClick
ExplicitHeight = 31
end end
object btnRefreshRecord: TButton object btnRefreshRecord: TButton
AlignWithMargins = True AlignWithMargins = True
Left = 166 Left = 166
Top = 4 Top = 4
Width = 107 Width = 107
Height = 31 Height = 40
Align = alLeft Align = alLeft
Caption = 'Refresh Record' Caption = 'Refresh Record'
TabOrder = 2 TabOrder = 2
OnClick = btnRefreshRecordClick OnClick = btnRefreshRecordClick
ExplicitHeight = 35
end end
object Button1: TButton object Button1: TButton
AlignWithMargins = True AlignWithMargins = True
Left = 85 Left = 85
Top = 4 Top = 4
Width = 75 Width = 75
Height = 31 Height = 40
Align = alLeft Align = alLeft
Caption = 'Close' Caption = 'Close'
TabOrder = 3 TabOrder = 3
OnClick = btnCloseClick OnClick = btnCloseClick
ExplicitHeight = 31
end
object Panel2: TPanel
Left = 1
Top = 47
Width = 874
Height = 64
Align = alBottom
BevelOuter = bvNone
TabOrder = 4
ExplicitTop = 48
object Label1: TLabel
Left = 3
Top = 11
Width = 24
Height = 13
Caption = 'Filter'
end
object EditFilter: TEdit
Left = 3
Top = 30
Width = 156
Height = 21
TabOrder = 0
end
object btnFilter: TButton
Left = 165
Top = 19
Width = 107
Height = 40
Caption = 'Filter'
TabOrder = 1
OnClick = btnFilterClick
end
end end
end end
object DBGrid1: TDBGrid object DBGrid1: TDBGrid
Left = 0 Left = 0
Top = 39 Top = 112
Width = 876 Width = 876
Height = 352 Height = 279
Align = alClient Align = alClient
DataSource = dsrcArticles DataSource = dsrcArticles
TabOrder = 1 TabOrder = 1

View File

@ -24,6 +24,10 @@ type
btnOpen: TButton; btnOpen: TButton;
btnRefreshRecord: TButton; btnRefreshRecord: TButton;
Button1: TButton; Button1: TButton;
Panel2: TPanel;
EditFilter: TEdit;
Label1: TLabel;
btnFilter: TButton;
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure dsArticlesBeforePost(DataSet: TDataSet); procedure dsArticlesBeforePost(DataSet: TDataSet);
procedure dsArticlesBeforeDelete(DataSet: TDataSet); procedure dsArticlesBeforeDelete(DataSet: TDataSet);
@ -34,11 +38,16 @@ type
procedure btnCloseClick(Sender: TObject); procedure btnCloseClick(Sender: TObject);
procedure dsArticlesBeforeRowRequest(DataSet: TFDDataSet); procedure dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
procedure btnRefreshRecordClick(Sender: TObject); procedure btnRefreshRecordClick(Sender: TObject);
procedure btnFilterClick(Sender: TObject);
private private
fFilter: string;
fLoading: Boolean; fLoading: Boolean;
fRESTClient: TRESTClient; fRESTClient: TRESTClient;
{ Private declarations } { Private declarations }
procedure ShowError(const AResponse: IRESTResponse); procedure ShowError(const AResponse: IRESTResponse);
procedure SetFilter(const Value: string);
public
property Filter: string read fFilter write SetFilter;
end; end;
var var
@ -57,8 +66,17 @@ begin
dsArticles.Close; dsArticles.Close;
end; end;
procedure TMainForm.btnFilterClick(Sender: TObject);
begin
dsArticles.Close;
Filter := EditFilter.Text;;
dsArticles.Open;
end;
procedure TMainForm.btnOpenClick(Sender: TObject); procedure TMainForm.btnOpenClick(Sender: TObject);
begin begin
dsArticles.Close;
Filter := '';
dsArticles.Open; dsArticles.Open;
end; end;
@ -71,8 +89,16 @@ procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
var var
Res: IRESTResponse; Res: IRESTResponse;
begin begin
// this a simple sychronous request... if fFilter.IsEmpty then
Res := fRESTClient.doGET('/articles', []); begin
// this a simple sychronous request...
Res := fRESTClient.doGET('/articles', []);
end
else
begin
Res := fRESTClient.doGET('/articles/searches', [], ['q'], [fFilter]);
end;
if Res.HasError then if Res.HasError then
begin begin
ShowError(Res); ShowError(Res);
@ -82,7 +108,7 @@ begin
DataSet.DisableControls; DataSet.DisableControls;
try try
fLoading := true; fLoading := true;
dsArticles.LoadFromJSONArrayString(Res.BodyAsString); dsArticles.LoadJSONArrayFromJSONObjectProperty('data', Res.BodyAsString);
fLoading := false; fLoading := false;
dsArticles.First; dsArticles.First;
finally finally
@ -137,7 +163,7 @@ var
begin begin
Res := fRESTClient.doGET('/articles', [DataSet.FieldByName('id').AsString]); Res := fRESTClient.doGET('/articles', [DataSet.FieldByName('id').AsString]);
fLoading := true; fLoading := true;
DataSet.LoadFromJSONObjectString(Res.BodyAsString); DataSet.LoadJSONObjectFromJSONObjectProperty('data', Res.BodyAsString);
fLoading := false; fLoading := false;
end; end;
@ -151,6 +177,12 @@ begin
fRESTClient := MVCFramework.RESTClient.TRESTClient.Create('localhost', 8080); fRESTClient := MVCFramework.RESTClient.TRESTClient.Create('localhost', 8080);
end; end;
procedure TMainForm.SetFilter(const Value: string);
begin
fFilter := Value;
EditFilter.Text := Value;
end;
procedure TMainForm.ShowError(const AResponse: IRESTResponse); procedure TMainForm.ShowError(const AResponse: IRESTResponse);
begin begin
if AResponse.HasError then if AResponse.HasError then

View File

@ -54,19 +54,40 @@ type
procedure LoadFromJSONObject(const JSONObject: TJSONObject; const FieldNameCase: TMVCNameCase); overload; procedure LoadFromJSONObject(const JSONObject: TJSONObject; const FieldNameCase: TMVCNameCase); overload;
procedure LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string> = []; procedure LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string> = [];
const FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload; const FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: string; procedure LoadFromJSONArray(AJSONArray: string;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload; FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadFromJSONArrayString(AJSONArrayString: string;
FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: TJSONArray; procedure LoadFromJSONArray(AJSONArray: TJSONArray;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadJSONArrayFromJSONObjectProperty(
PropertyName: string;
JSONObject: string;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadJSONArrayFromJSONObjectProperty(
PropertyName: string;
JSONObject: TJSONObject;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadJSONObjectFromJSONObjectProperty(
PropertyName: string;
JSONObject: string;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadJSONObjectFromJSONObjectProperty(
PropertyName: string;
JSONObject: TJSONObject;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadFromJSONArrayString(AJSONArrayString: string;
AIgnoredFields: TArray<string>;
FieldNameCase: TMVCNameCase = ncLowerCase); overload; deprecated;
procedure LoadFromJSONArrayString(AJSONArrayString: string;
FieldNameCase: TMVCNameCase = ncLowerCase); overload; FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadFromJSONObjectString(AJSONObjectString: string); overload; procedure LoadFromJSONObjectString(AJSONObjectString: string); overload;
procedure LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>; procedure LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase = ncLowerCase); overload; const FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; const aPropertyName: string; // procedure LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; const aPropertyName: string;
const FieldNameCase: TMVCNameCase = ncLowerCase); // const FieldNameCase: TMVCNameCase = ncLowerCase);
procedure AppendFromJSONArrayString(AJSONArrayString: string); overload; procedure AppendFromJSONArrayString(AJSONArrayString: string); overload;
procedure AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>; procedure AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload; FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
@ -163,19 +184,19 @@ begin
end; end;
procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; // procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string;
const aPropertyName: string; const FieldNameCase: TMVCNameCase); // const aPropertyName: string; const FieldNameCase: TMVCNameCase);
var // var
lJson: TJSONObject; // lJson: TJSONObject;
begin // begin
lJson := TJSONObject.Create; // lJson := TJSONObject.Create;
try // try
lJson.FromJSON(AJSONObjectString); // lJson.FromJSON(AJSONObjectString);
LoadFromJSONArray(lJson.A[aPropertyName], FieldNameCase); // LoadFromJSONArray(lJson.A[aPropertyName], FieldNameCase);
finally // finally
lJson.Free; // lJson.Free;
end; // end;
end; // end;
function TDataSetHelper.AsJDOJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): TJDOJsonArray; function TDataSetHelper.AsJDOJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): TJDOJsonArray;
var var
@ -284,7 +305,7 @@ end;
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; FieldNameCase: TMVCNameCase); procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; FieldNameCase: TMVCNameCase);
var var
lSerializer: IMVCSerializer; lSerializer: TMVCJsonDataObjectsSerializer;
begin begin
Self.DisableControls; Self.DisableControls;
try try
@ -322,6 +343,44 @@ begin
end; end;
end; end;
procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(PropertyName: string;
JSONObject: TJSONObject; FieldNameCase: TMVCNameCase);
begin
LoadFromJSONArray(JSONObject.A[PropertyName], FieldNameCase);
end;
procedure TDataSetHelper.LoadJSONObjectFromJSONObjectProperty(
PropertyName: string; JSONObject: TJSONObject; FieldNameCase: TMVCNameCase);
begin
LoadFromJSONObject(JSONObject.O[PropertyName], FieldNameCase);
end;
procedure TDataSetHelper.LoadJSONObjectFromJSONObjectProperty(PropertyName,
JSONObject: string; FieldNameCase: TMVCNameCase);
var
lJObj: TJSONObject;
begin
lJObj := StrToJSONObject(JSONObject);
try
LoadJSONObjectFromJSONObjectProperty(PropertyName, lJObj, FieldNameCase);
finally
lJObj.Free;
end;
end;
procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(PropertyName,
JSONObject: string; FieldNameCase: TMVCNameCase);
var
lJson: TJSONObject;
begin
lJson := StrToJSONObject(JSONObject);
try
LoadJSONArrayFromJSONObjectProperty(PropertyName, lJson, FieldNameCase);
finally
lJson.Free;
end;
end;
procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; FieldNameCase: TMVCNameCase); procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; FieldNameCase: TMVCNameCase);
begin begin
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), FieldNameCase); AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), FieldNameCase);
@ -340,10 +399,15 @@ end;
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string>; procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase); const FieldNameCase: TMVCNameCase);
var
lSerializer: TMVCJsonDataObjectsSerializer;
begin begin
raise Exception.Create('Not Implemented'); lSerializer := TMVCJsonDataObjectsSerializer.Create;
// Mapper.JSONObjectToDataSet(AJSONObject, Self, AIgnoredFields, false, try
// AFieldNamePolicy); lSerializer.JsonObjectToDataSet(JSONObject, Self, TMVCIgnoredList(AIgnoredFields), FieldNameCase);
finally
lSerializer.Free;
end;
end; end;
procedure TDataSetHelper.LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>; procedure TDataSetHelper.LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>;