unit Delivery.Web.Services.OrderProtocol;

interface

uses
  System.JSON;

type

{ IOrderProtocol }

  IOrderProtocol = interface
    procedure Send(AOrderJSON: TJSONObject);
  end;

{ Routines }

function CreateOrderProtocol: IOrderProtocol;

implementation

uses
  System.SysUtils,
  StompClient,
  Delivery.Web.Configuration;

type

{ TOrderProtocolService }

  TOrderProtocolService = class(TInterfacedObject, IOrderProtocol)
  private
    FClient: IStompClient;
  public
    procedure Send(AOrderJSON: TJSONObject);
    constructor Create;
  end;

{ TOrderProtocolService }

constructor TOrderProtocolService.Create;
begin
  FClient := StompUtils.StompClient
    .SetHost(MQ_HOST)
    .SetPort(MQ_PORT)
    .SetUserName(MQ_USERNAME)
    .SetPassword(MQ_PASSWORD)
    .Connect();
end;

procedure TOrderProtocolService.Send(AOrderJSON: TJSONObject);
begin
  FClient.Send(Format('/queue/%s', [MQ_QUEUE_NAME]), AOrderJSON.ToJSON());
end;

{ Routines }

function CreateOrderProtocol: IOrderProtocol;
begin
  Result := TOrderProtocolService.Create;
end;

end.
