This commit is contained in:
daniele.teti 2013-11-11 00:11:09 +00:00
parent f05e2ea1d7
commit 91d84ae73e
7 changed files with 232 additions and 163 deletions

View File

@ -0,0 +1,108 @@
unit BusinessObjectsU;
interface
uses
ObjectsMappers;
type
TPerson = class
private
FLastName: String;
FDOB: TDate;
FFirstName: String;
FMarried: boolean;
procedure SetDOB(const Value: TDate);
procedure SetFirstName(const Value: String);
procedure SetLastName(const Value: String);
procedure SetMarried(const Value: boolean);
public
property FirstName: String read FFirstName write SetFirstName;
property LastName: String read FLastName write SetLastName;
property DOB: TDate read FDOB write SetDOB;
property Married: boolean read FMarried write SetMarried;
end;
[MapperJSONNaming(JSONNameLowerCase)]
TCustomer = class
private
FName: String;
FAddressLine2: String;
FAddressLine1: String;
FContactFirst: String;
FCity: String;
FContactLast: String;
procedure SetAddressLine1(const Value: String);
procedure SetAddressLine2(const Value: String);
procedure SetCity(const Value: String);
procedure SetContactFirst(const Value: String);
procedure SetContactLast(const Value: String);
procedure SetName(const Value: String);
public
property Name: String read FName write SetName;
[MapperTransient]
property ContactFirst: String read FContactFirst write SetContactFirst;
[MapperTransient]
property ContactLast: String read FContactLast write SetContactLast;
property AddressLine1: String read FAddressLine1 write SetAddressLine1;
property AddressLine2: String read FAddressLine2 write SetAddressLine2;
property City: String read FCity write SetCity;
end;
implementation
{ TPerson }
procedure TPerson.SetDOB(const Value: TDate);
begin
FDOB := Value;
end;
procedure TPerson.SetFirstName(const Value: String);
begin
FFirstName := Value;
end;
procedure TPerson.SetLastName(const Value: String);
begin
FLastName := Value;
end;
procedure TPerson.SetMarried(const Value: boolean);
begin
FMarried := Value;
end;
{ TCustomer }
procedure TCustomer.SetAddressLine1(const Value: String);
begin
FAddressLine1 := Value;
end;
procedure TCustomer.SetAddressLine2(const Value: String);
begin
FAddressLine2 := Value;
end;
procedure TCustomer.SetCity(const Value: String);
begin
FCity := Value;
end;
procedure TCustomer.SetContactFirst(const Value: String);
begin
FContactFirst := Value;
end;
procedure TCustomer.SetContactLast(const Value: String);
begin
FContactLast := Value;
end;
procedure TCustomer.SetName(const Value: String);
begin
FName := Value;
end;
end.

View File

@ -1,47 +0,0 @@
unit BusinessObjectsU;
interface
type
TPerson = class
private
FLastName: String;
FDOB: TDate;
FFirstName: String;
FMarried: boolean;
procedure SetDOB(const Value: TDate);
procedure SetFirstName(const Value: String);
procedure SetLastName(const Value: String);
procedure SetMarried(const Value: boolean);
public
property FirstName: String read FFirstName write SetFirstName;
property LastName: String read FLastName write SetLastName;
property DOB: TDate read FDOB write SetDOB;
property Married: boolean read FMarried write SetMarried;
end;
implementation
{ TPerson }
procedure TPerson.SetDOB(const Value: TDate);
begin
FDOB := Value;
end;
procedure TPerson.SetFirstName(const Value: String);
begin
FFirstName := Value;
end;
procedure TPerson.SetLastName(const Value: String);
begin
FLastName := Value;
end;
procedure TPerson.SetMarried(const Value: boolean);
begin
FMarried := Value;
end;
end.

View File

@ -16,9 +16,9 @@ type
procedure GetCustomers(CTX: TWebContext);
[MVCHTTPMethod([httpGet])]
[MVCPath('/customers')]
[MVCProduces('text/xml')]
procedure GetCustomersXML(CTX: TWebContext);
[MVCPath('/customers/($id)')]
[MVCProduces('application/json')]
procedure GetCustomerByID(CTX: TWebContext);
end;
@ -29,6 +29,25 @@ uses
{ TRoutingSampleController }
procedure TRenderSampleController.GetCustomerByID(CTX: TWebContext);
var
Cust: TCustomer;
begin
if CTX.Request.ParamsAsInteger['id'] = 7 then
Render(404, 'Customer Not Found')
else
begin
Cust := TCustomer.Create;
Cust.Name := 'Daniele Teti Inc.';
Cust.ContactFirst := 'Daniele';
Cust.ContactLast := 'Teti';
Cust.AddressLine1 := 'Rome Street 12';
Cust.AddressLine2 := '00100';
Cust.City := 'ROME';
Render(Cust);
end;
end;
procedure TRenderSampleController.GetCustomers(CTX: TWebContext);
var
wm: TWebModule1;
@ -38,13 +57,4 @@ begin
Render(wm.qryCustomers);
end;
procedure TRenderSampleController.GetCustomersXML(CTX: TWebContext);
var
wm: TWebModule1;
begin
wm := GetCurrentWebModule as TWebModule1;
wm.qryCustomers.Open;
Render(wm.qryCustomers);
end;
end.

View File

@ -13,7 +13,6 @@ object WebModule1: TWebModule1
'Password=masterkey'
'DriverID=IB')
ConnectedStoredUsage = [auDesignTime]
Connected = True
LoginPrompt = False
Left = 160
Top = 40

View File

@ -8,9 +8,9 @@ uses
IdHTTPWebBrokerBridge,
Web.WebReq,
Web.WebBroker,
WebModuleU in 'WebModuleU.pas' {WebModule1: TWebModule} ,
WebModuleU in 'WebModuleU.pas' {WebModule1: TWebModule},
RenderSampleControllerU in 'RenderSampleControllerU.pas',
BusinessObjectsU in 'BusinessObjectsU.pas';
BusinessObjectsU in '..\commons\BusinessObjectsU.pas';
{$R *.res}

View File

@ -90,7 +90,7 @@
<DesignClass>TWebModule</DesignClass>
</DCCReference>
<DCCReference Include="RenderSampleControllerU.pas"/>
<DCCReference Include="BusinessObjectsU.pas"/>
<DCCReference Include="..\commons\BusinessObjectsU.pas"/>
<BuildConfiguration Include="Release">
<Key>Cfg_2</Key>
<CfgParent>Base</CfgParent>

View File

@ -27,12 +27,7 @@ uses
Generics.Collections,
DBXJSON,
SqlExpr,
DuckListU,
Xml.xmldom,
Xml.XMLIntf,
Xml.XMLDoc
;
DuckListU;
type
Mapper = class
@ -89,10 +84,10 @@ type
AReaderInstanceOwner: boolean = True);
class procedure DataSetToJSONArray(ADataSet: TDataSet; AJSONArray: TJSONArray;
ADataSetInstanceOwner: boolean = True);
// class procedure DataSetRowToXML(ADataSet: TDataSet; Row: IXMLNode;
// ADataSetInstanceOwner: boolean = True);
// class procedure DataSetToXML(ADataSet: TDataSet; XMLDocument: String;
// ADataSetInstanceOwner: boolean = True);
// class procedure DataSetRowToXML(ADataSet: TDataSet; Row: IXMLNode;
// ADataSetInstanceOwner: boolean = True);
// class procedure DataSetToXML(ADataSet: TDataSet; XMLDocument: String;
// ADataSetInstanceOwner: boolean = True);
class function ObjectListToJSONArray<T: class>(AList: TObjectList<T>;
AOwnsInstance: boolean = false): TJSONArray;
class function ObjectListToJSONArrayOfJSONArray<T: class, constructor>(AList: TObjectList<T>)
@ -609,6 +604,10 @@ begin
if DoNotSerializeThis then
Continue;
end;
if HasAttribute<MapperTransientAttribute>(_property) then
Continue;
case _property.PropertyType.TypeKind of
tkInteger, tkInt64:
JSONObject.AddPair(f, TJSONNumber.Create(_property.GetValue(AObject).AsInteger));
@ -1430,39 +1429,39 @@ begin
if ACloseDataSetAfterScroll then
ADataSet.Close;
end;
//class procedure Mapper.DataSetToXML(ADataSet: TDataSet;
//
// class procedure Mapper.DataSetToXML(ADataSet: TDataSet;
// XMLDocument: String; ADataSetInstanceOwner: boolean);
//var
// var
// Xml: IXMLDocument;
// Row: IDOMElement;
//begin
// Row: IXMLNode;
// begin
// DefaultDOMVendor := 'ADOM XML v4';
// Xml := NewXMLDocument();
// Xml.Active := True;
// while not ADataSet.Eof do
// begin
// Row := Xml.DOMDocument.createElement('row');
// Row := Xml.CreateNode('row');
// // Row := Xml.DocumentElement.AddChild('row');
// // DataSetRowToXML(ADataSet, Row, false);
// Xml.DOMDocument.appendChild(Row);
// Xml.ChildNodes.Add(Row);
// break;
// ADataSet.Next;
// end;
// if ADataSetInstanceOwner then
// FreeAndNil(ADataSet);
// Xml.SaveToXML(XMLDocument);
//end;
// end;
//
//class procedure Mapper.DataSetRowToXML(ADataSet: TDataSet;
// class procedure Mapper.DataSetRowToXML(ADataSet: TDataSet;
// Row: IXMLNode; ADataSetInstanceOwner: boolean);
//var
// var
// I: Integer;
// key: string;
// dt: TDateTime;
// tt: TTime;
// Time: TTimeStamp;
// ts: TSQLTimeStamp;
//begin
// begin
// for I := 0 to ADataSet.FieldCount - 1 do
// begin
// key := LowerCase(ADataSet.Fields[I].FieldName);
@ -1520,7 +1519,7 @@ end;
// end;
// if ADataSetInstanceOwner then
// FreeAndNil(ADataSet);
//end;
// end;
class function Mapper.InternalExecuteSQLQuery(AQuery: TSQLQuery; AObject: TObject;
WithResult: boolean): Int64;