The Hospitality Survey EMS project is part of the Hospitality Survey App template for Delphi 10.2 Tokyo that Embarcadero has released through their GetIt platform. The Hospitality Survey App consists of four different projects. In this blog post I am going to cover the Hospitality Survey EMS project. It is the backend RAD Server module for the Hospitality Survey Client and the Hospitality Survey Admin to interface with via REST.
RAD Server is a turn-key application foundation for rapidly building and deploying services based applications on Windows and Linux. RAD Server provides automated Delphi and C++ REST/JSON API publishing and management, Enterprise database integration middleware, IoT Edgeware and an array of application services such as User Directory and Authentication services, Push Notifications, Indoor/Outdoor Geolocation and JSON data storage.
The Hospitality Survey EMS end points mainly return the FireDAC JSON format which can be easily loaded in to a RAD Studio client or accessed via standard JSON in a non-RAD Studio environment like AngularJS or Sencha. Here is some sample Javascript code in AngularJS for accessing FireDAC JSON:
// // $scope.getDetail = function(survey_id) { $http.get($scope.getServerURL() + 'survey/details/' + survey_id, $scope.getHeader()). then(function(response) { $scope.currentresult = survey_id; $scope.results = response.data["FDBS"]["Manager"]["TableList"][0]["RowList"]; $scope.SurveyDetail = true; }); }
Endpoints:
GET /survey/ – Download the survey questions in FireDAC JSON format.
GET /survey/results/* – Download the survey results in a paged fashion in FireDAC JSON format. The page number is placed where the * is.
GET /survey/stats/all – Download various stats about the survey results in FireDAC JSON format.
GET /survey/details/* – Download the questions and answers from a specific survey ID in FireDAC JSON format. The survey ID is placed where the * is.
GET /survey/emails/csv – Download a list of all of the email addresses from completed surveys in CSV format.
GET /tenants/ – Download a list of the server Tenants which are active in FireDAC JSON format.
You can easily extend the server with your own end points and custom functionality. Below I will give an overview of how you can do that.
You can also find out more information about how the Resource Suffixes work in the Embarcadero DocWiki. The endpoints are defined like this in code:
// // // SurveyModule.pas [ResourceName('survey')] // procedure Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); // [ResourceSuffix('{query}/*')] procedure GetData(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); // [ResourceSuffix('{query}')] procedure PostData(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); // // if ARequest.Params.Values['query'] = 'results' then if ARequest.Params.Values['query'] = 'stats' then if ARequest.Params.Values['query'] = 'details' then if ARequest.Params.Values['query'] = 'emails' then // // // TenantModule.pas [ResourceName('tenants')] procedure Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
As you can see in the code the ResourceName is the name of the overall endpoint and actually returns the list of questions for the Survey App via a GET call. The GetData function handles the Results, Stats, Details, and Emails requests. While the PostData function handles the submission of completed surveys to the server. You can easily tie into the GetData function by just adding more query parameters.
The variable in the /survey/results/* and /survey/details/* endpoints is accessed via ARequest.Segments object like below:
if ASegments.Count = 3 then // first segment is "survey" second is "results or "details" third segment is the variable begin SurveyId := ASegments.Items[2]; end;
If you would like to edit the questions of your survey in the server database after it has been created you can use the Hospitality Survey Editor.