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

View File

@ -19,7 +19,7 @@ object MainForm: TMainForm
Left = 0
Top = 0
Width = 876
Height = 39
Height = 112
Align = alTop
TabOrder = 0
object DBNavigator1: TDBNavigator
@ -27,50 +27,87 @@ object MainForm: TMainForm
Left = 585
Top = 4
Width = 287
Height = 31
Height = 40
DataSource = dsrcArticles
Align = alRight
TabOrder = 0
ExplicitHeight = 31
end
object btnOpen: TButton
AlignWithMargins = True
Left = 4
Top = 4
Width = 75
Height = 31
Height = 40
Align = alLeft
Caption = 'Open'
TabOrder = 1
OnClick = btnOpenClick
ExplicitHeight = 31
end
object btnRefreshRecord: TButton
AlignWithMargins = True
Left = 166
Top = 4
Width = 107
Height = 31
Height = 40
Align = alLeft
Caption = 'Refresh Record'
TabOrder = 2
OnClick = btnRefreshRecordClick
ExplicitHeight = 35
end
object Button1: TButton
AlignWithMargins = True
Left = 85
Top = 4
Width = 75
Height = 31
Height = 40
Align = alLeft
Caption = 'Close'
TabOrder = 3
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
object DBGrid1: TDBGrid
Left = 0
Top = 39
Top = 112
Width = 876
Height = 352
Height = 279
Align = alClient
DataSource = dsrcArticles
TabOrder = 1

View File

@ -24,6 +24,10 @@ type
btnOpen: TButton;
btnRefreshRecord: TButton;
Button1: TButton;
Panel2: TPanel;
EditFilter: TEdit;
Label1: TLabel;
btnFilter: TButton;
procedure FormCreate(Sender: TObject);
procedure dsArticlesBeforePost(DataSet: TDataSet);
procedure dsArticlesBeforeDelete(DataSet: TDataSet);
@ -34,11 +38,16 @@ type
procedure btnCloseClick(Sender: TObject);
procedure dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
procedure btnRefreshRecordClick(Sender: TObject);
procedure btnFilterClick(Sender: TObject);
private
fFilter: string;
fLoading: Boolean;
fRESTClient: TRESTClient;
{ Private declarations }
procedure ShowError(const AResponse: IRESTResponse);
procedure SetFilter(const Value: string);
public
property Filter: string read fFilter write SetFilter;
end;
var
@ -57,8 +66,17 @@ begin
dsArticles.Close;
end;
procedure TMainForm.btnFilterClick(Sender: TObject);
begin
dsArticles.Close;
Filter := EditFilter.Text;;
dsArticles.Open;
end;
procedure TMainForm.btnOpenClick(Sender: TObject);
begin
dsArticles.Close;
Filter := '';
dsArticles.Open;
end;
@ -71,8 +89,16 @@ procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
var
Res: IRESTResponse;
begin
// this a simple sychronous request...
Res := fRESTClient.doGET('/articles', []);
if fFilter.IsEmpty then
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
begin
ShowError(Res);
@ -82,7 +108,7 @@ begin
DataSet.DisableControls;
try
fLoading := true;
dsArticles.LoadFromJSONArrayString(Res.BodyAsString);
dsArticles.LoadJSONArrayFromJSONObjectProperty('data', Res.BodyAsString);
fLoading := false;
dsArticles.First;
finally
@ -137,7 +163,7 @@ var
begin
Res := fRESTClient.doGET('/articles', [DataSet.FieldByName('id').AsString]);
fLoading := true;
DataSet.LoadFromJSONObjectString(Res.BodyAsString);
DataSet.LoadJSONObjectFromJSONObjectProperty('data', Res.BodyAsString);
fLoading := false;
end;
@ -151,6 +177,12 @@ begin
fRESTClient := MVCFramework.RESTClient.TRESTClient.Create('localhost', 8080);
end;
procedure TMainForm.SetFilter(const Value: string);
begin
fFilter := Value;
EditFilter.Text := Value;
end;
procedure TMainForm.ShowError(const AResponse: IRESTResponse);
begin
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 AIgnoredFields: TArray<string> = [];
const FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: string;
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;
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;
procedure LoadFromJSONObjectString(AJSONObjectString: string); overload;
procedure LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; const aPropertyName: string;
const FieldNameCase: TMVCNameCase = ncLowerCase);
// procedure LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; const aPropertyName: string;
// const FieldNameCase: TMVCNameCase = ncLowerCase);
procedure AppendFromJSONArrayString(AJSONArrayString: string); overload;
procedure AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
@ -163,19 +184,19 @@ begin
end;
procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string;
const aPropertyName: string; const FieldNameCase: TMVCNameCase);
var
lJson: TJSONObject;
begin
lJson := TJSONObject.Create;
try
lJson.FromJSON(AJSONObjectString);
LoadFromJSONArray(lJson.A[aPropertyName], FieldNameCase);
finally
lJson.Free;
end;
end;
// procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string;
// const aPropertyName: string; const FieldNameCase: TMVCNameCase);
// var
// lJson: TJSONObject;
// begin
// lJson := TJSONObject.Create;
// try
// lJson.FromJSON(AJSONObjectString);
// LoadFromJSONArray(lJson.A[aPropertyName], FieldNameCase);
// finally
// lJson.Free;
// end;
// end;
function TDataSetHelper.AsJDOJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): TJDOJsonArray;
var
@ -284,7 +305,7 @@ end;
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; FieldNameCase: TMVCNameCase);
var
lSerializer: IMVCSerializer;
lSerializer: TMVCJsonDataObjectsSerializer;
begin
Self.DisableControls;
try
@ -322,6 +343,44 @@ begin
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);
begin
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), FieldNameCase);
@ -340,10 +399,15 @@ end;
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase);
var
lSerializer: TMVCJsonDataObjectsSerializer;
begin
raise Exception.Create('Not Implemented');
// Mapper.JSONObjectToDataSet(AJSONObject, Self, AIgnoredFields, false,
// AFieldNamePolicy);
lSerializer := TMVCJsonDataObjectsSerializer.Create;
try
lSerializer.JsonObjectToDataSet(JSONObject, Self, TMVCIgnoredList(AIgnoredFields), FieldNameCase);
finally
lSerializer.Free;
end;
end;
procedure TDataSetHelper.LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>;