diff --git a/app.js b/app.js index b8663819c9..bf87e283f5 100644 --- a/app.js +++ b/app.js @@ -1,25 +1,28 @@ // # Ghost main app file -/*global require */ +/*global require, __dirname */ (function () { "use strict"; // Module dependencies. var express = require('express'), - fs = require('fs'), admin = require('./core/admin/controllers'), frontend = require('./core/frontend/controllers'), + api = require('./core/shared/api'), flash = require('connect-flash'), Ghost = require('./core/ghost'), I18n = require('./core/lang/i18n'), - helpers = require('./core/frontend/helpers'), - auth, + helpers = require('./core/frontend/helpers'); + + + + var auth, // ## Variables - /** - * Create new Ghost object - * @type {Ghost} - */ + /** + * Create new Ghost object + * @type {Ghost} + */ ghost = new Ghost(); ghost.app().configure('development', function () { @@ -46,11 +49,13 @@ /** * API routes.. - * @todo convert these into a RESTful, public, authenticated API! + * @todo auth should be public auth not user auth */ - ghost.app().post('/api/v0.1/posts/create', auth, admin.posts.create); - ghost.app().post('/api/v0.1/posts/edit', auth, admin.posts.edit); - ghost.app().get('/api/v0.1/posts', auth, admin.posts.index); + ghost.app().get('/api/v0.1/posts', auth, api.requestHandler(api.posts.browse)); + ghost.app().get('/api/v0.1/posts/:id', auth, api.requestHandler(api.posts.read)); + ghost.app().post('/api/v0.1/posts/create', auth, api.requestHandler(api.posts.add)); + ghost.app().put('/api/v0.1/posts/edit', auth, api.requestHandler(api.posts.edit)); + ghost.app()['delete']('/api/v0.1/posts/:id', auth, api.requestHandler(api.posts.destroy)); /** * Admin routes.. @@ -75,6 +80,10 @@ ghost.app().listen(3333, function () { console.log("Express server listening on port " + 3333); - console.log('process: ', process.env); }); +// }, function (e) { +// console.log(e.toString()); +// }).then(null, function (e) { +// console.log(e.stack); +// }); }()); \ No newline at end of file diff --git a/config.js b/config.js index 28875e22f6..1362072b76 100644 --- a/config.js +++ b/config.js @@ -55,6 +55,19 @@ */ config.homepage.posts = 4; + config.database = { + development: { + client: 'sqlite3', + connection: { + filename: './core/shared/data/testdb.db' + } + }, + + staging: {}, + + production: {} + }; + /** * @property {Object} exports */ diff --git a/core/admin/assets/js/editor.js b/core/admin/assets/js/editor.js index 1a56d386e4..31b1ab13e0 100644 --- a/core/admin/assets/js/editor.js +++ b/core/admin/assets/js/editor.js @@ -56,7 +56,7 @@ function save() { var entry = { title: document.getElementById('entry-title').value, - markdown: editor.getValue() + content: editor.getValue() }, urlSegments = window.location.pathname.split('/'); @@ -64,7 +64,7 @@ entry.id = urlSegments[3]; $.ajax({ url: '/api/v0.1/posts/edit', - method: 'POST', + method: 'PUT', data: entry, success: function (data) { console.log('response', data); diff --git a/core/admin/controllers/index.js b/core/admin/controllers/index.js index c5ad0705c7..c8b9decd43 100644 --- a/core/admin/controllers/index.js +++ b/core/admin/controllers/index.js @@ -1,11 +1,12 @@ +/*global require, module */ (function () { "use strict"; var Ghost = require('../../ghost'), _ = require('underscore'), fs = require('fs'), - Showdown = require('showdown'), - converter = new Showdown.converter(), + when = require('when/node/function'), + api = require('../../shared/api'), ghost = new Ghost(), adminNavbar, @@ -60,14 +61,15 @@ }, 'editor': function (req, res) { if (req.params.id !== undefined) { - ghost.dataProvider().posts.findOne({'id': parseInt(req.params.id, 10)}, function (error, post) { - res.render('editor', { - bodyClass: 'editor', - adminNav: setSelected(adminNavbar, 'blog'), - title: post.title, - content: post.content + api.posts.read({id: parseInt(req.params.id, 10)}) + .then(function (post) { + res.render('editor', { + bodyClass: 'editor', + adminNav: setSelected(adminNavbar, 'blog'), + title: post.get('title'), + content: post.get('content') + }); }); - }); } else { res.render('editor', { bodyClass: 'editor', @@ -76,13 +78,14 @@ } }, 'blog': function (req, res) { - ghost.dataProvider().posts.findAll(function (error, posts) { - res.render('blog', { - bodyClass: 'manage', - adminNav: setSelected(adminNavbar, 'blog'), - posts: posts + api.posts.browse() + .then(function (posts) { + res.render('blog', { + bodyClass: 'manage', + adminNav: setSelected(adminNavbar, 'blog'), + posts: posts.toJSON() + }); }); - }); }, 'settings': function (req, res) { res.render('settings', { @@ -119,47 +122,6 @@ res.redirect('/ghost/debug'); }); } - }, - 'posts': { - 'index': function (req, res) { - - }, - 'create': function (req, res) { - var entry = { - title: req.body.title, - content: req.body.markdown, - contentHtml: '', - language: ghost.config().defaultLang, - status: ghost.statuses().draft, - featured: false - }; - - entry.contentHtml = converter.makeHtml(entry.content); - - ghost.dataProvider().posts.add(entry, function (error, post) { - if (!error) { - console.log('added', post); - res.json({id: post.id}); - } else { - res.json(400, {error: post.errors}); - } - }); - }, - 'edit': function (req, res) { - var entry = { - id: parseInt(req.body.id, 10), - title: req.body.title, - content: req.body.markdown, - contentHtml: '' - }; - - entry.contentHtml = converter.makeHtml(entry.content); - - ghost.dataProvider().posts.edit(entry, function (error, post) { - console.log('edited', post); - res.json({id: parseInt(post.id, 10)}); - }); - } } }; diff --git a/core/admin/views/blog.hbs b/core/admin/views/blog.hbs index 39c36da632..f0aa9bbe0e 100644 --- a/core/admin/views/blog.hbs +++ b/core/admin/views/blog.hbs @@ -18,7 +18,7 @@
    {{#each posts}} {{! #if featured class="featured"{{/if}} -
  1. +