 # * database
 action "update", ->
 
-    markAppAsBroken = =>
-        @app.state = "broken"
+    manager = new AppManager
+    manager.updateApp @app, (err, result) =>
+
+        return mark_broken @app, err if err
+
+        @app.state = "installed"
+        @app.port = result.drone.port
         @app.save (err) ->
-            if err
-                send_error()
-            else
-                send_error "uninstallation failed"
-
-    manager = new AppManager
-    manager.updateApp @app, (err, result) =>
-        if err
-            markAppAsBroken()
-        else
-            @app.state = "installed"
-            @app.port = result.drone.port
-            @app.save (err) ->
-                if err
-                    send_error()
-                else
-                    manager.resetProxy (err) ->
-                        if err
-                            markAppAsBroken()
-                        else
-                            send
-                                success: true
-                                msg: 'Application succesfuly updated'
+
+            return send_error err if err
+
+            manager.resetProxy (err) ->
+
+                return mark_broken @app, err if err
+
+                send
+                    success: true
+                    msg: 'Application succesfuly updated'
 
 action "start", ->
-
-    markAppAsBroken = =>
-        @app.state = "broken"
-        @app.save (err) ->
-            if err
-                send_error err.message
-            else
-                send_error "starting failed"
-
-
     manager = new AppManager
     manager.start @app, (err, result) =>
-        if err
-            markAppAsBroken()
-        else
-            @app.state = "installed"
-            @app.port = result.drone.port
-            @app.save (err) =>
-                if err
-                    markAppAsBroken()
-                else
-                    manager.resetProxy (err) =>
-                        if err
-                            markAppAsBroken()
-                        else
-                            send
-                                success:true
-                                msg: 'Application running'
-                                app: @app
+
+        return mark_broken @app, err if err
+
+        # require('eyes').inspect result
+
+        @app.state = "installed"
+        @app.port = result.drone.port
+        @app.save (err) =>
+
+            return send_error err if err
+
+            manager.resetProxy (err) =>
+
+                return mark_broken @app, err if err
+
+                send
+                    success: true
+                    msg: 'Application running'
+                    app: @app
 
 action "stop", ->
-
-    markAppAsBroken = =>
-        @app.state = "broken"
-        @app.save (err) ->
-            if err
-                send_error()
-            else
-                send_error "stopping failed"
-
     manager = new AppManager
     manager.stop @app, (err, result) =>
-        if err
-            markAppAsBroken()
-        else
-            @app.state = "stopped"
-            @app.port = 0
-            @app.save (err) =>
-                if err
-                    send_error()
-                else
-                    manager.resetProxy (err) =>
-                        if err
-                            markAppAsBroken()
-                        else
-                            send
-                                success:true
-                                msg: 'Application stopped'
-                                app: @app
+
+        return mark_broken @app, err if err
+
+        @app.state = "stopped"
+        @app.port = 0
+        @app.save (err) =>
+
+            return send_error err if err
+
+            manager.resetProxy (err) =>
+
+                return mark_broken @app, err if err
+
+                send
+                    success:true
+                    msg: 'Application stopped'
+                    app: @app


# Helpers

send_error = (msg, code=500) ->
    if msg
        send error: true, msg: msg, code
    else
        send error: true, msg: "Server error occured", code


# Load application corresponding to slug given in params
before 'load application', ->
    Application.all key: params.slug, (err, apps) =>
        if err
            console.log err
            send_error()
        else if apps is null or apps.length == 0
            send error: 'Application not found', 404
        else
            @app = apps[0]
            next()


## Actions

# Return list of applications available on this cozy instance.
action 'applications', ->
    Application.all (errors, apps) ->
        if errors
            send_error new Error "Retrieve applications failed."
        else
            send rows: apps

