Permalink
Browse files

Major changes on Admin pages - POST CRUD

  • Loading branch information...
1 parent 956d87b commit f9efba30f45b03c4a81614730881dc508b825c6b @alduro committed Mar 16, 2012
@@ -15,8 +15,8 @@ module.exports = (_app) ->
# Login routes
# render login form
controller.login = (req, res) ->
- User = db.main.model('User')
- res.render 'admin/login', { user: new User(), title: 'Ingresar' }
+ # User = db.main.model('User')
+ res.render 'admin/login', { title: 'Ingresar', user: req.session.user_id }
# login user
controller.session = (req, res) ->
@@ -30,7 +30,8 @@ controller.session = (req, res) ->
loginToken.save( ->
res.cookie('logintoken', loginToken.cookieValue, { expires: new Date(Date.now() + 2 * 604800000), path: '/' })
)
- res.redirect '/'
+ req.flash 'success', 'Successfully logged in!'
+ res.redirect '/admin/posts'
else
req.flash 'error', 'Login failed, try again'
res.redirect '/admin/login'
@@ -63,30 +64,31 @@ controller.createUser = (req, res) ->
user = new User(req.body.user)
userSaveFailed = ->
- req.flash('error', 'Saving user failed')
+ req.flash 'error', 'Saving user failed'
res.render 'users/create', { user: user }
user.save( (err) ->
if (err)
userSaveFailed()
- req.flash('info', 'User has been saved!')
- res.redirect '/'
+ req.flash 'success', 'User has been saved!'
+ res.redirect 'admin/'
)
##########################################################################
# POSTS
#
controller.index = (req, res, next) ->
+ BlogPost = db.main.model('BlogPost')
# expose pusher key
- res.expose
+ res.expose
app_key : req.app.set('pusher_key')
channel : 'blog_post'
events : 'post'
, 'PUSHER'
# render template
- res.render 'home', { posts: db.posts.posts() }
+ res.render 'admin/',{ title:'Posts listing', posts: BlogPost.posts, user: req.session.user_id }
##########################################################################
# NEW POST
View
@@ -40,7 +40,7 @@ module.exports = ->
# Load error routes + pages
- # errors(app)
+ errors(app)
# Load hooks
View
@@ -3,7 +3,8 @@
#
express = require 'express'
-stylus = require 'stylus'
+path = require 'path'
+cons = require 'consolidate'
expose = require 'express-expose'
mongoose = require 'mongoose'
nib = require 'nib'
@@ -33,26 +34,22 @@ module.exports = (app) ->
# }
# Configure expressjs
-
app.configure ->
app.use express.logger('\033[90m:method\033[0m \033[36m:url\033[0m \033[90m:response-time ms\033[0m')
+ # app.register '.html', require('ejs')
+ app.set 'views', __dirname + '/../views'
+ app.set 'view engine', 'ejs'
app.use express.cookieParser()
app.use express.bodyParser()
+ app.use express.methodOverride()
app.use express.errorHandler({dumpException: true, showStack: true})
app.use express.session
secret: '076ee61d63aa10a125ea872411e433b9'
maxAge: new Date(Date.now() + 3600000)
store: new mongoStore { db: db_store }
# app.use app.router
- app.register '.html', require('ejs')
- app.set 'views', __dirname + '/../views'
- app.set 'view engine', 'html'
app.use express.static __dirname + '/../public'
- # .use(stylus.middleware(
- # {
- # src: __dirname + '/../public',
- # compile: compile
- # }))
+ app.dynamicHelpers { messages: require('express-messages-bootstrap') }
# Save reference to database connection
app.configure ->
@@ -8,8 +8,8 @@ module.exports = (app) ->
# Catch all
- # app.all '*', notFound = (req, res, next) ->
- # throw new NotFound
+ app.all '*', notFound = (req, res, next) ->
+ throw new NotFound
# Load 404 page
app.error (err, req, res, next) ->
@@ -38,7 +38,7 @@ controller.loadUser = (req, res, next) ->
else
if (req.session.user_id)
User = db.main.model('User')
- User.findById(req.session.user_id, (err, user) ->
+ User.find({ '_id': req.session.user_id }, (err, user) ->
if (user)
req.currentUser = user
next()
@@ -8,16 +8,16 @@ module.exports = (app) ->
db = app.set('db')
# Load Root
+ app.get '/admin', helpers.loadUser, admin.index
- app.get '/admin/', blog.index
-
- # Load Admin Controller + Routes
+ # User + Session
app.get '/admin/login', admin.login
app.post '/admin/session', admin.session
app.get '/admin/logout', helpers.loadUser, admin.logout
app.get '/admin/user/new', helpers.loadUser, admin.newUser
app.post '/admin/user/create', helpers.loadUser, admin.createUser
+ # Posts
app.get '/admin/posts', helpers.loadUser, admin.index
app.get '/admin/post/new', helpers.loadUser, admin.newPost
app.post '/admin/post/create', helpers.loadUser, admin.createPost
@@ -0,0 +1,23 @@
+require('coffee-script')
+mongoose = require('mongoose')
+controller = {}
+
+module.exports = ->
+ return controller
+
+controller.createUser = ->
+ dblink = 'mongodb://localhost/satio-blog'
+ db = mongoose.createConnection dblink
+
+ User = db.model('User', require('./models/user'))
+
+ user = new User({ email:'aldo@satio.com.ar', name: 'Aldo Nievas'})
+ user.password = 'test'
+
+ user.save(
+ (err) ->
+ if (!err)
+ console.log 'User has been saved'
+ else
+ console.log 'Failed'
+ )
View
@@ -18,10 +18,9 @@
"express" : "2.5.x",
"express-namespace" : "0.0.4",
"express-expose" : "0.2.2",
- "connect-assets" : "2.1.8",
- "stylus" : "0.24.0",
+ "express-messages-bootstrap" : "0.0.8",
"nib" : "0.3.2",
- "bootstrap-stylus" : "0.2.1",
+ // "bootstrap-stylus" : "0.2.1",
"mongodb" : "0.9.x",
"mongoose" : "2.5.x",
"connect-mongodb" : "1.1.3",
View
Binary file not shown.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -3,7 +3,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
- <title>Bootstrap, from Twitter</title>
+ <title>SATIO Web Development</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
Oops, something went wrong. Retry.

0 comments on commit f9efba3

Please sign in to comment.