Make application model callback based

This commit is contained in:
Juan Cruz Viotti 2014-11-26 13:54:05 -04:00
parent 9529e6c335
commit 213cab0480
2 changed files with 37 additions and 25 deletions

View File

@ -21,14 +21,13 @@ exports.create = authHooks.failIfNotLoggedIn (name, program) ->
# Maybe we should break or handle better?
slugifiedType = resin.device.getDeviceSlug(type)
resin.models.application.create(name, slugifiedType).then ->
return callback()
.catch(callback)
resin.models.application.create(name, slugifiedType, callback)
], resin.errors.handle
exports.list = authHooks.failIfNotLoggedIn ->
resin.models.application.getAll().then (applications) ->
resin.models.application.getAll (error, applications) ->
resin.errors.handle(error) if error?
resin.log.out resin.ui.widgets.table.horizontal applications, (application) ->
application.device_type = resin.device.getDisplayName(application.device_type)
@ -39,10 +38,9 @@ exports.list = authHooks.failIfNotLoggedIn ->
return application
, [ 'ID', 'Name', 'Device Type', 'Online Devices', 'All Devices' ]
.catch(resin.errors.handle)
exports.info = authHooks.failIfNotLoggedIn (id) ->
resin.models.application.get(id).then (application) ->
resin.models.application.get id, (error, application) ->
resin.errors.handle(error) if error?
resin.log.out resin.ui.widgets.table.vertical application, (application) ->
application.device_type = resin.device.getDisplayName(application.device_type)
@ -50,16 +48,12 @@ exports.info = authHooks.failIfNotLoggedIn (id) ->
return application
, [ 'ID', 'Name', 'Device Type', 'Git Repository', 'Commit' ]
.catch(resin.errors.handle)
exports.restart = authHooks.failIfNotLoggedIn (id) ->
# TODO: Move this URL to config
resin.server.post("/application/#{id}/restart", resin.errors.handle)
resin.models.application.restart id, (error) ->
resin.errors.handle(error) if error?
exports.remove = authHooks.failIfNotLoggedIn (id, program) ->
resin.ui.patterns.remove 'application', program.parent.yes, (callback) ->
resin.models.application.remove(id).then ->
return callback()
.catch(callback)
resin.models.application.remove(id, callback)
, resin.errors.handle

View File

@ -1,9 +1,9 @@
_ = require('lodash')
Promise = require('bluebird')
canvas = require('./_canvas')
errors = require('../errors/errors')
server = require('../server/server')
exports.getAll = ->
exports.getAll = (callback) ->
return canvas.get
resource: 'application'
options:
@ -11,22 +11,28 @@ exports.getAll = ->
expand: 'device'
.then (applications) ->
if _.isEmpty(applications)
return Promise.reject(new errors.NotAny('applications'))
return callback(new errors.NotAny('applications'))
return applications
return callback(null, applications)
exports.get = (id) ->
.catch (error) ->
return callback(error)
exports.get = (id, callback) ->
return canvas.get
resource: 'application'
id: id
.then (application) ->
if not application?
return Promise.reject(new errors.NotFound("application #{id}"))
return callback(new errors.NotFound("application #{id}"))
return application
return callback(null, application)
exports.create = (name, deviceType) ->
.catch (error) ->
return callback(error)
exports.create = (name, deviceType, callback) ->
return canvas.post
resource: 'application'
data:
@ -37,11 +43,23 @@ exports.create = (name, deviceType) ->
id = res?.id
if not id?
return Promise.reject(new errors.NotFound('created application id'))
return callback(new errors.NotFound('created application id'))
return id
return callback(null, id)
exports.remove = (id) ->
.catch (error) ->
return callback(error)
exports.remove = (id, callback) ->
return canvas.delete
resource: 'application'
id: id
.then ->
return callback()
.catch (error) ->
return callback(error)
exports.restart = (id, callback) ->
# TODO: Move this URL to config
server.post("/application/#{id}/restart", callback)