Skip to content

Instantly share code, notes, and snippets.

@floriankrueger
Created September 24, 2011 18:43
Show Gist options
  • Save floriankrueger/1239688 to your computer and use it in GitHub Desktop.
Save floriankrueger/1239688 to your computer and use it in GitHub Desktop.
if if if
###
Creates a new bucket for a user using the given parameters (if not already above the user's limit)
@param {Object} user the document of the authenticated user
@param {String} name to be used for the bucket
@param {Number} limit to be used for the bucket (in Byte)
@param {Function} callback gets two parameters - error object (if an error occured) and the new bucket document if successful.
@api public
###
Buckets.prototype.create = (user, name, limit, callback) ->
c = true
self = this
this.list user, (err, info) ->
if err
callback err
c = false
# check the bucket name (for already taken)
if c
info.buckets.forEach (bucket) ->
if bucket.name == name
callback
message: "The name \"#{name}\" is already taken."
c = false
# check the userlimit
if c && user.limit != 0
if (info.totalSize + limit) <= user.limit
c = true
else
max = user.limit - info.totalSize
callback
message: "The desired limit of #{limit} exceeds the total limit of #{user.limit} by #{limit-max}. The maximum limit is #{max}."
c = false
if c
bucket =
name: name
owner: user._id.toHexString()
limit: limit
size: 0
options =
safe: true
self.collection.insert bucket, (err,docs) ->
if err
return callback err
else
return callback null, docs[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment