codecombat/server/achievements/achievement_handler.coffee

37 lines
1.4 KiB
CoffeeScript
Raw Normal View History

Achievement = require './Achievement'
Handler = require '../commons/Handler'
class AchievementHandler extends Handler
modelClass: Achievement
2014-05-18 10:04:50 -04:00
# Used to determine which properties requests may edit
editableProperties: ['name', 'query', 'worth', 'collection', 'description', 'userField', 'proportionalTo', 'icon', 'function', 'related', 'difficulty', 'category', 'rewards']
allowedMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
jsonSchema = require '../../app/schemas/models/achievement.coffee'
2014-05-18 10:04:50 -04:00
hasAccess: (req) ->
req.method is 'GET' or req.user?.isAdmin()
2014-08-04 16:21:43 -04:00
get: (req, res) ->
# /db/achievement?related=<ID>
if req.query.related
return @sendForbiddenError(res) if not @hasAccess(req)
2014-08-04 16:21:43 -04:00
Achievement.find {related: req.query.related}, (err, docs) =>
return @sendDatabaseError(res, err) if err
docs = (@formatEntity(req, doc) for doc in docs)
@sendSuccess res, docs
else
super req, res
delete: (req, res, slugOrID) ->
return @sendForbiddenError res unless req.user?.isAdmin()
@getDocumentForIdOrSlug slugOrID, (err, document) => # Check first
return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless document?
document.remove (err, document) =>
return @sendDatabaseError(res, err) if err
@sendNoContent res
module.exports = new AchievementHandler()