/* appjet:version 0.1 */
import("storage");
page.setMode("plain");

if (!storage.contacts) {
        storage.contacts = new StorableCollection();;
}

// Utility
function getId() {
    return request.path.substr(request.path.lastIndexOf("/")+1);
}

// Class definition
function Contact(obj) { this.info = this.getObject(obj); }
Contact.prototype.meta = ["id", "Name", "Email"];
Contact.prototype.getObject = function(obj) {    
    var o = {};
    if(obj) this.meta.forEach(function(prop){if(val = obj[prop]) o[prop] = val;});
    return o;
}

// The CRUD
function getContacts() {  
    var contacts = [];
    storage.contacts.forEach(function(o){contacts.push((new Contact(o)).info);});
    print("{length: " + storage.contacts.size() + ", contacts: " + contacts + "}");
}

function getContact() {
    print("{contact: " + (new Contact(getStorable(getId()))).info + "}");    
}

function addContact() {
    storage.contacts.add((new Contact()).getObject(request.params));
}

function deleteContact() {
    storage.contacts.remove(getStorable(getId()));
}

function editContact() {
    deleteContact();
    addContact();
}


// The controller
patternDispatch(
      [/^\/$/, 
        function() {if(request.isGet) showApi();}],
      [/^\/contacts\/trash$/, 
        function() {if(request.isPost) deleteContact();}],
      [/^\/contacts\/obj-.*$/, 
        function() {if(request.isGet) getContact(); else if(request.isPost) editContact();
        }],
      [/^\/contacts$/, 
        function() {if(request.isGet) getContacts(); else if(request.isPost) addContact();
        }],        
        function() {response.setStatusCode(404);});


function showApi() {
    page.body.write("""
        <ul>
        <li><a href="http://contactsapi.appjet.com/">GET 
        http://contactsapi.appjet.com/</a><br />
        <em>Gets this page</em></li>       
        <li><a href="http://contactsapi.appjet.com/contacts">GET 
        http://contactsapi.appjet.com/contacts</a><br />
        <em>Gets list of contacts</em></li>
        <li><a href="http://contactsapi.appjet.com/contacts">POST 
        http://contactsapi.appjet.com/contacts</a><br />
        <em>Posts a new contact to the contacts list, these properties are required:</em><br />
        <code>Name, Email</code></li>       
        <li><a href="http://contactsapi.appjet.com/contacts/{contact-id}">GET
        http://contactsapi.appjet.com/contacts/{contact-id}</a><br />
        <em>Gets contact by its {contact-id}</em></li>       
        <li><a href="http://contactsapi.appjet.com/contacts/{contact-id}">POST
        http://contactsapi.appjet.com/contacts/{contact-id}</a><br />
        <em>Posts changes to the contact, these properties are required:</em><br />
        <code>id, Name, Email</code></li>       
        <li><a href="http://contactsapi.appjet.com/contacts/trash">POST
        http://contactsapi.appjet.com/contacts/trash</a><br />
        <em>Posts contact to the trash</em><br />
        <code>id</code></li>
        </ul>
    """);
}





© Copyright 2007-2009 AppJet Inc.