Skip to content

Commit

Permalink
Added more details to the README + uploaded server scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
goldshtn committed Mar 29, 2013
1 parent 69b2661 commit 3fa21d2
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -4,3 +4,12 @@ rentahome
This is a demo application that illustrates use of Windows Azure Mobile Services on four mobile platforms: Android, iOS, Windows Phone 8, and Windows 8.

Rent-a-Home is an application that helps users find apartments for rent and insert new apartments. It can also display apartments on a map. The application supports login (authentication) with Twitter, so apartments can be associated with the user that added them.

Some additional references mentioned during the presentation:

* [Azure Mobile Services component](http://components.xamarin.com/view/azure-mobile-services/) in the Xamarin Store.
* [Parse Pricing Model](https://parse.com/plans).
* [Azure Service Bus Notification Hubs](http://msdn.microsoft.com/en-us/library/windowsazure/jj927170.aspx) -- scalable push notification support, coming soon to Azure Mobile Services as well.
* [Nadlandroid](https://code.google.com/p/nadlandroid/) -- an application illustrating some of the Parse features that are not available in Windows Azure Mobile Services.

Finally, to reproduce this demo with your own mobile service, you'll need two tables: `channels` and `apartment`, with the server scripts described in the server-scripts.md file.
129 changes: 129 additions & 0 deletions server-scripts.md
@@ -0,0 +1,129 @@
Insert script on `channels` table:

```javascript
function insert(item, user, request) {
var channelTable = tables.getTable('channels');
channelTable
.where({ uri: item.uri, type: item.type })
.read({ success: insertChannelIfNotFound });

function insertChannelIfNotFound(existingChannels) {
if (existingChannels.length > 0) {
request.respond(200, existingChannels[0]);
} else {
request.execute();
}
}
}
```

Insert script on `apartment` table:

```javascript
function insert(item, user, request) {
var reqModule = require('request');

item.username = '<Unknown User>';
var identities = user ? user.getIdentities() : null;
if (identities && identities.twitter) {
var userId = user.userId;
var twitterId = userId.substring(userId.indexOf(':') + 1);
var userDetailsUrl = 'https://api.twitter.com/1/users/show.json?user_id=' + twitterId;
reqModule(userDetailsUrl, function (err, resp, body) {
if (err || resp.statusCode !== 200) {
console.error('Error sending data to Twitter API: ' + err + ', response body: ' + body);
request.respond(500, body);
} else {
try {
console.log('Twitter response: ' + body);
var userData = JSON.parse(body);
item.username = userData.name;
continueProcessing();
} catch (ex) {
console.error('Error parsing response from Twitter API: ', ex);
request.respond(500, ex);
}
}
});
} else {
continueProcessing();
}

function continueProcessing() {
var what = escape(item.address);
reqModule('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' + what,
function(error, response, body) {
if (error) {
executeRequestAndSendNotifications();
} else {
var geoResult = JSON.parse(body);
var location = geoResult.results[0].geometry.location;
item.latitude = location.lat;
item.longitude = location.lng;
executeRequestAndSendNotifications();
}
});

function executeRequestAndSendNotifications() {
request.execute({
success: function() {
request.respond();
sendNotifications();
}
});
}

function sendNotifications() {
var channelTable = tables.getTable('channels');
channelTable.read({
success: function(channels) {
channels.forEach(function(channel) {
if (channel.type === "Windows 8") {
push.wns.sendToastText04(channel.uri, {
text1: 'New apartment added',
text2: item.address,
text3: item.bedrooms + ' bedrooms'
}, {
success: function(pushResponse) {
console.log("Sent push notification: ", pushResponse);
}
});
}
if (channel.type === "Windows Phone 8") {
push.mpns.sendToast(channel.uri, 'New apartment added',
'At ' + item.address + ' with ' + item.bedrooms + ' bedrooms',
function (err) {
console.log("Sent push notification: " + JSON.stringify(err));
});
}
if (channel.type === "iOS") {
push.apns.send(channel.uri, {
alert: "New " + item.bedrooms + "-bedrooms apartment added at " + item.address,
badge: 1,
payload: {
address: item.address,
bedrooms: item.bedrooms
}
});
}
if (channel.type === "Android") {
push.gcm.send(channel.uri,
{
bedrooms: item.bedrooms.toString(),
address: item.address
}, {
success: function(response) {
console.log('Push notification sent: ', response);
}, error: function(error) {
console.log('Error sending push notification: ', error);
}
});
}
});
}
});
}
}

}
```

0 comments on commit 3fa21d2

Please sign in to comment.