Moved database logic

To clean up app.coffee