UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

140 lines (118 loc) 4.09 kB
var logger = require('../logger'); var api = require('../api'); var auth = require('../auth'); var config = require('../config'); /* callback: (err) */ module.exports.init = function(server, callback) { //routes server.get('/cms/types/list', auth.authenticate, routeContentTypeList); server.get('/cms/types/get', auth.authenticate, routeContentTypeGet); server.post('/cms/types/create', auth.authenticateAdmin, routeContentTypeCreate); server.post('/cms/types/update', auth.authenticateAdmin, routeContentTypeUpdate); server.post('/cms/types/delete', auth.authenticateAdmin, routeContentTypeDelete); //load and build the content types api.contentTypes.loadContentTypes(function(err) { if (err) { if (callback) callback(err); } else { if (callback) callback(null); } }); }; function routeContentTypeList(req, res, next) { try { api.contentTypes.getContentTypes(false, function(err, results) { if (err) res.json({ success: false, message: 'Error retrieving the content types.'}); else { res.json({ success: true, contentTypes: results }); } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the content types.'}); } }; function routeContentTypeGet(req, res, next) { try { var id = req.query.typeId; api.contentTypes.getContentType(id, function(err, type) { if (err) res.json({ success: false, message: 'Error retrieving the content type.'}); else res.json({ success: true, contentType: type }); }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the content type.'}); } }; function routeContentTypeDelete(req, res, next) { try { var id = req.body.typeId; api.contentTypes.delete(id, function(err) { if (err) res.json({ success: false, message: err.message}); else res.json({ success: true, message: 'Content type deleted successfully.' }); }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error deleting the content type.'}); } }; function routeContentTypeCreate(req, res, next) { try { var json = req.body.data; var t = JSON.parse(json); var username = req.user.username; //fix angular hashKey issue fixFields(t); api.contentTypes.createContentType(t, username, function(err, type) { if (err) res.json({ success: false, message: err.message }); else res.json({ success: true, message: 'Content type created successfully.', contentType: type }); }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error creating the content type.'}); } }; function routeContentTypeUpdate(req, res, next) { try { var json = req.body.data; var t = JSON.parse(json); var username = req.user.username; //fix angular hashKey issue fixFields(t); api.contentTypes.updateContentType(t, username, function(err, type) { if (err) res.json({ success: false, message: err.message }); else res.json({ success: true, message: 'Content type updated successfully.', contentType: type }); }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error updating the content type.'}); } }; function fixFields(t) { if (t.fields) { var arr = []; for (var i = 0; i < t.fields.length; i++) { var f = t.fields[i]; arr.push({ name: f.name, label: f.label, type: f.type, source: f.source, help: f.help }); } t.fields = arr; } }