unit Delivery.Web.Controllers.Orders;

interface

uses
  MVCFramework,
  MVCFramework.Commons,
  MVCFramework.Serializer.Commons;

type

  [MVCPath('/api')]
  TOrderController = class(TMVCController)
  public
    [MVCPath]
    [MVCHTTPMethod([httpGET])]
    procedure Index;
  public
    [MVCPath('/order')]
    [MVCHTTPMethod([httpPOST])]
    procedure SubmitOrder;
  end;

implementation

uses
  System.JSON,
  System.SysUtils,
  Delivery.Web.Helpers,
  Delivery.Web.Services.OrderProtocol;

procedure TOrderController.Index;
begin
  ContentType := 'text/html';
  Render('<h1>Order Web API Demo<h1>');
end;

procedure TOrderController.SubmitOrder;
var
  LTimeStamp: string;
begin
  DateTimeToString(LTimeStamp, 'yyyy-mmdd-hhnnss-zzz', Now);
  var LOrder := TJSONObject.Create;
  try
    var LRequest := Context.Request;
    LOrder.AddPair('orderCode', Format('%s-%s', [LRequest.Params['serviceType'], LTimeStamp]));
    LOrder.AddPair('serviceType', LRequest.Params['serviceType']);
    LOrder.AddPair('productType', LRequest.Params['productType']);
    LOrder.AddPair('peopleCount', StrToInt(LRequest.Params['peopleCount']));
    LOrder.AddPair('deliveryAt', LRequest.Params['deliveryDay'] + ' ' + LRequest.Params['deliveryTime']);
    LOrder.AddPair('customerName', LRequest.Params['customerName']);
    LOrder.AddPair('customerPhone', LRequest.Params['customerPhone']);
    LOrder.AddPair('customerMail', LRequest.Params['customerMail']);
    LOrder.AddPair('privacyCheck', LRequest.Params['privacyCheck'] = 'OK_PRIVACY');
    LOrder.AddPair('contactCheck', LRequest.Params['contactCheck'] = 'OK_CONTATTO');
    LOrder.AddPair('currentStatus', 'NEW');
    CreateOrderProtocol.Send(LOrder);
  finally
    LOrder.Free;
  end;
  ContentType := 'text/html';
  Render('<h1>Grazie!<h1> <h2>Il tuo ordine è stato ricevuto.</h2>');
end;

end.
