Skip to content

Instantly share code, notes, and snippets.

@arthurschreiber
Forked from floriankrueger/buckets.coffee
Created September 24, 2011 19:20
Show Gist options
  • Save arthurschreiber/1239725 to your computer and use it in GitHub Desktop.
Save arthurschreiber/1239725 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::create = (user, name, limit, callback) ->
this.list user, (err, info) =>
return callback(err) if err
# check the bucket name (for already taken)
for bucket in info.buckets
if bucket.name == name
return callback(message: "The name \"#{name}\" is already taken.")
# check the userlimit
if user.limit and (info.totalSize + limit) > user.limit
max = user.limit - info.totalSize
return callback(message: "The desired limit of #{limit} exceeds the total limit of #{user.limit} by #{limit-max}. The maximum limit is #{max}.")
bucket =
name: name
owner: user._id.toHexString()
limit: limit
size: 0
options = safe: true
this.collection.insert bucket, (err, docs) ->
return callback err if err
return callback null, docs[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment