This repo contains examples and the API used in the Webbprograming course.
- /Examples - Here you find code examples used in the course
- /SPA-templates - Here you find single-page-application (SPA) templates
- /admin - Here you find tools to manipulate booking data
- /booking - Here you find the booking API that your webb application should use
All data is returned as XML.
Call the appropropriate service with the required (and optional) parameters. For example, to create a new customer the following Fetch-call can be used:
try {
const url = "../booking/makecustomer_XML.php";
// Manually create the JSON object to be submitted to our API
const params = {};
const inputs = document.querySelectorAll("#make-customer-form input[type='text']");
for(const input of inputs){
console.log(input)
params[input.name]=input.value;
}
// Make request
const response = await fetch(url,{
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
});
// Act on the response
if(!response.ok){
throw new Error(await response.text());
}
const response_text = await response.text(); // <-- Contains the XML returned byt the API
}catch(err){
console.log("error", err);
}Call the appropropriate service with the required (and optional) parameters. For example, to create a new customer the following Fetch-call can be used:
try {
const url = "../booking/makecustomer_XML.php";
// Manually create the form data to be submitted to our API
const form = new FormData();
const inputs = document.querySelectorAll("#make-customer-form input[type='text']");
for(const input of inputs){
console.log(input)
form.append(input.name,input.value);
}
// Make request
const response = await fetch(url,{
method: "post",
body: form
});
// Act on the response
if(!response.ok){
throw new Error(await response.text());
}
const response_text = await response.text(); // <-- Contains the XML returned byt the API
}catch(err){
console.log("error", err);
}When we have received a response from the API we can parse and use the data to create our dynamic web page. Here is an example of how you can parse the XML:
...
const response_text = await response.text();
// Parse text response as XML document
xml_parser = new DOMParser();
xml_document = xml_parser.parseFromString(response_text, "text/xml");
// Search in your XML document for data that you need (in this example we retreive all 'resource' elements)
const xml_nodes = xml_document.querySelectorAll('resource');
for (const xml_node of xml_nodes) {
console.log(xml_node);
for (const attribute of xml_node.attributes) {
console.log(attribute.name, attribute.value);
}
}
...Call the appropropriate service with the required (and optional) parameters. For example, to create a new customer the following AJAX-call using jQuery can be used:
$.ajax({
type: 'POST',
url: '../booking/makecustomer_XML.php',
data: { ID: escape(customerID),
firstname: escape(firstname),
lastname: escape(lastname),
email: escape(email),
address: escape(address),
auxdata: escape(auxdata),
},
success: ResultCustomern,
error: errormsg
});
}Creates a customer.
This API call has a built-in artifical delay of 3-5s
ID REQUIRED ID of the customer
firstname REQUIRED Customers firstname
lastname REQUIRED Customers lastname
email REQUIRED Customers email
address REQUIRED Customers address
auxdata OPTIONAL Auxillary information about customer\
<created status="OK"/>Reads all information about a customer and updates last visit date to current date and time. Is used for logging in to a customer account.
customerID REQUIRED ID of the customer\
<customers>
<customer id="test" firstname="test" lastname="test" address="test" lastvisit="2012-10-16 12:31:51" email="test" auxdata="None!" />
</customers>Makes a booking and stores it in the database. (Deletes all temporary bookings for the user with that customerID that does not have state=2)
This API call has a built-in artifical delay of 3-5s
type REQUIRED the application to book resource in
resourceID REQUIRED ID of the resource
date REQUIRED Date of the booking. Format: 2012-10-02 (year-month-day)
dateto REQUIRED End Date of the booking if there is one. Format: 2012-10-02 (year-month-day)
customerID REQUIRED ID of the customer
status REQUIRED Temporary or "real" booking. (1 = temporary, 2 = permanent)
position REQUIRED Position of the booking (Integer)
rebate OPTIONAL Rebate, defaults to 0
auxdata OPTIONAL Auxillary data\
<result size='20' bookingcost='100' remaining='4' /> Reads bookings for a resource given the resource ID. If the searchresource parameter is non-empty, uses LIKE to match the resource ID.
type REQUIRED Unique Application type. In this case Hotel_Demo for example login name of student
resourceID OPTIONAL ID of the resource
searchresource OPTIONAL If not empty searches for resource ID using like
date OPTIONAL Date of the booking. Format: 2012-10-02 (year-month-day)\
<bookings>
<booking
application='Hotell_Demo'
customerID='Leiflert'
resourceID='1008'
name='Karl Hotel and Resort'
company='Karls'
location='Exmouth'
date='2001-07-20 00:00:00'
dateto='2001-07-21 00:00:00'
position='26'
status='2'
cost='40'
size='24'
auxdata=''
/>
...
</bookings>Get all bookings made by a certain customer
type REQUIRED Unique Application type. In this case Hotel_Demo for example login name of student
customerID REQUIRED ID of the customer\
<bookings>
<booking
application='Hotell_Demo'
customerID='Haakon'
resourceID='1008'
name='Karl Hotel and Resort'
company='Karls'
location='Exmouth'
date='2001-07-20 00:00:00'
dateto='2001-07-20 00:00:00'
position='13'
cost='280'
category='Hotel'
size='200'
auxdata='None'
/>
...
</bookings>The booking/getresources_XML.php performs a search using a set of search terms. If none is given, that search term is ignored. The terms name, location and company use a logical or if more than one term is given. The full text search works in isolation from the other terms.
type REQUIRED Application type, in the example Hotel_Demo
name OPTIONAL Name of the resource
company OPTIONAL Company that used the resource
location OPTIONAL Location of the resource
fulltext OPTIONAL Tries to find a match from name, company or location\
<resources>
<resource
id='1001'
name='Pilkington Inn'
company='Sunside Hotels'
location='Manchester'
size='15'
cost='350'
category='Hostel'
auxdata='None'
/>
</resource>Shows the availability information for all available dates for a given resourceID. There are 3 different ways to select resourse.
Alt 1 - Search by resource id
type REQUIRED Application type
resid REQUIRED The resource id
Alt 2 - Search by name,location,and company
type REQUIRED Application type
name REQUIRED The resource name
location REQUIRED The resource location
company REQUIRED The resource companty
Alt 3 - Search with one search term for name,location, and company
type REQUIRED Application type
fulltext REQUIRED Full text search much like for resource search, matches any search term.
<avail>
<availability
resourceID='1008'
name='The Laszlo Plaza'
location='Athens'
company='Laszlo Inc'
size='96'
cost='110'
category='1'
date='2001-01-01'
dateto='2001-01-02'
bookingcount='0'
bookingcost='28.99'
bookingclass='1'
remaining='96'
/>
...
</avail>Makes a new resource for the cases when we need the application to create new resources e.g. peer-to-peer hotels.
ID REQUIRED ID of the reresource
name REQUIRED Name of the resource
type REQUIRED Application type in the example Hotel_Demo
company REQUIRED Company of the resource
location REQUIRED Location of the resource
category REQUIRED Category of the resource
cost REQUIRED Cost of the resource
auxdata OPTIONAL Auxillary information about resource\
<created status="OK"/>Deletes a booking, for the cases that a user wants to cancel a booking of a resource.
ID REQUIRED ID of the reresource
date REQUIRED Date of Booking
customerID REQUIRED ID of Customer\
<deleted status="OK"/>