Extendiendo el EMS (Enterprise Mobility Services). Implementando EMS Resource. Código del Server en Delphi XE7.

En el blog anterior les mostraba en video, como extender el EMS, esto esta todo en C++Builder XE7. Para mis amigos de Delphi anexo el código en Delphi. ;-) .

 
unit Unit4;
 
// EMS Resource Module
 
interface
 
uses
  System.SysUtils, System.Classes, System.JSON,
  EMS.Services, EMS.ResourceAPI, EMS.ResourceTypes, FireDAC.Stan.Intf,
  FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf,
  FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys,
  FireDAC.Phys.IB, FireDAC.Phys.IBDef, FireDAC.Stan.Param, FireDAC.DatS,
  FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client;
 
type
  [ResourceName('Countries')]
  TCountriesResource2 = class(TDataModule)
    IbemployeeConnection: TFDConnection;
    CountryTable: TFDQuery;
    qryCountry: TFDQuery;
  published
    procedure Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
    [ResourceSuffix('{item}')]
    procedure GetItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
    procedure Post(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
    [ResourceSuffix('{item}')]
    procedure PutItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
    [ResourceSuffix('{item}')]
    procedure DeleteItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
  end;
 
procedure Register;
 
implementation
 
{%CLASSGROUP 'System.Classes.TPersistent'}
 
{$R *.dfm}
 
procedure TCountriesResource2.Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
  AResponseData : TJSONArray;
  ARow : TJSONObject;
begin
  AResponseData := TJSONArray.Create();
  if not CountryTable.Active then
    CountryTable.Active := True;
  CountryTable.First();
  while(not CountryTable.Eof)do
  begin
    ARow := TJSONObject.Create();
    ARow.AddPair('country', CountryTable.FieldByName('country').AsString);
    ARow.AddPair('country', CountryTable.FieldByName('country').AsString);
    AResponseData.AddElement(ARow);
    CountryTable.Next();
  end;
  AResponse.Body.SetValue(AResponseData, True);
end;
 
procedure TCountriesResource2.GetItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
  LItem: string;
  ARow : TJSONObject;
begin
  LItem := ARequest.Params.Values['item'];
  ARow := TJSONObject.Create();
  // configurar la consulta de busqueda de un registro
  qryCountry.SQL.Text := 'select * from country where country = :country';
  qryCountry.ParamByName('country').AsString := Litem;
  qryCountry.Active := True;
  while( not qryCountry.Eof) do
  begin
    ARow.AddPair('country', qryCountry.FieldByName('country').AsString);
    ARow.AddPair('currency', qryCountry.FieldByName('currency').AsString);
    qryCountry.Next();
  end;
  AResponse.Body.SetValue(ARow, true);
end;
 
procedure TCountriesResource2.Post(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
  AReqObj : TJSONObject;
begin
  AReqObj := ARequest.Body.GetObject();
  // configurar la consulta de insercion
  qryCountry.SQL.Text := 'insert into country values (:country, :currency)';
  qryCountry.ParamByName('country').AsString := AreqObj.Get('country').JsonValue.Value();
  qryCountry.ParamByName('currency').AsString := AreqObj.Get('currency').JsonValue.Value();
  qryCountry.ExecSQL(); // ejecutar la insercion
  AResponse.Body.SetValue(AReqObj, True); // retorno el mismo objeto insertado
end;
 
procedure TCountriesResource2.PutItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
  LItem: string;
  AReqObj : TJSONObject;
begin
  LItem := ARequest.Params.Values['item'];  // Campo Clave
  AReqObj := ARequest.Body.GetObject(); // Obtengo el JSON con los datos a actualizar
  // configurar la consulta de actualizacion
  qryCountry.SQL.Text := 'update country set currency = :currency where country = :country';
  qryCountry.ParamByName('country').AsString := LItem;
  qryCountry.ParamByName('currency').AsString := AReqObj.Get('currency').JsonValue.Value();
  qryCountry.ExecSQL(); // ejecutar la insercion
  AResponse.Body.SetValue(AReqObj, true); // retorno el mismo objeto actualizado
end;
 
procedure TCountriesResource2.DeleteItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
  LItem: string;
  ARow : TJSONObject;
begin
  LItem := ARequest.Params.Values['item'];
  ARow := TJSONObject.Create();
  // configurar la consulta de borrado
  qryCountry.SQL.Text := 'delete from country where country = :country';
  qryCountry.ParamByName('country').AsString = Litem;
  qryCountry.ExecSQL(); // ejecutar la insercion
  AResponse.Body.SetValue(ARow, True); // retorno de objeto vacio
end;
 
procedure Register;
begin
  RegisterResource(TypeInfo(TCountriesResource2));
end;
 
end.
Esta entrada fue publicada en Delphi, Enterprise Mobility Services. Guarda el enlace permanente.

Deja un comentario