From 29ea6a42be35f5cabef59370682a0f7153a78efd Mon Sep 17 00:00:00 2001 From: Jonathon Storer Date: Thu, 13 Sep 2012 00:18:18 -0400 Subject: [PATCH] Clean Up closes #4 --- .../admin/dashboard_controller.coffee | 14 +++----- .../admin/events_controller.coffee | 33 ++++++++---------- .../admin/{admin.coffee => index.coffee} | 0 .../admin/sessions_controller.coffee | 27 +++++++-------- app/controllers/homes_controller.coffee | 16 ++++----- config/application.coffee | 1 + config/routes.coffee | 34 ++++++------------- 7 files changed, 49 insertions(+), 76 deletions(-) rename app/controllers/admin/{admin.coffee => index.coffee} (100%) diff --git a/app/controllers/admin/dashboard_controller.coffee b/app/controllers/admin/dashboard_controller.coffee index cfc8de1..5118d06 100644 --- a/app/controllers/admin/dashboard_controller.coffee +++ b/app/controllers/admin/dashboard_controller.coffee @@ -1,10 +1,6 @@ -BaseController = require './../base_controller' - -class DashboardController extends BaseController - show: -> - if @isAdmin() - @response.render 'admin/dashboard/show' +module.exports = + show: (req, res) -> + if req.session.administrator_id + res.render 'admin/dashboard/show' else - @response.redirect '/' - -module.exports = DashboardController + res.redirect '/' diff --git a/app/controllers/admin/events_controller.coffee b/app/controllers/admin/events_controller.coffee index e129cad..d57a6b6 100644 --- a/app/controllers/admin/events_controller.coffee +++ b/app/controllers/admin/events_controller.coffee @@ -1,29 +1,26 @@ -BaseController = require './../base_controller' { mongoose } = require '../../../config/database' Event = mongoose.model('Event') -class EventsController extends BaseController - index: -> - Event.find({}).exec (error, events) => - @response.render 'admin/events/index', +module.exports = + index: (req, res) -> + Event.find({}).exec (error, events) -> + res.render 'admin/events/index', events: events - new: -> - @response.render 'admin/events/new', + new: (req, res) -> + res.render 'admin/events/new', event: new Event - create: -> - newEvent = new Event @request.body.event - newEvent.save (error, event) => + create: (req, res) -> + newEvent = new Event req.body.event + newEvent.save (error, event) -> throw error if error - @request.session.messages = + req.session.messages = notice: "#{event.name} created" - @response.redirect "admin/events/#{event._id}" + res.redirect "admin/events/#{event._id}" - show: -> - Event.findOne @request.params.id, (error, event) => - @response.render 'admin/events/show', + show: (req, res) -> + Event.findOne req.params.id, (error, event) -> + res.render 'admin/events/show', event: event - messages: @request.session.messages - -module.exports = EventsController + messages: req.session.messages diff --git a/app/controllers/admin/admin.coffee b/app/controllers/admin/index.coffee similarity index 100% rename from app/controllers/admin/admin.coffee rename to app/controllers/admin/index.coffee diff --git a/app/controllers/admin/sessions_controller.coffee b/app/controllers/admin/sessions_controller.coffee index e484c67..9de9343 100644 --- a/app/controllers/admin/sessions_controller.coffee +++ b/app/controllers/admin/sessions_controller.coffee @@ -1,23 +1,20 @@ -BaseController = require './../base_controller' { mongoose } = require '../../../config/database' Administrator = mongoose.model('Administrator') -class SessionsController extends BaseController - new: -> - @response.render 'admin/sessions/new' +module.exports = + new: (req, res) -> + res.render 'admin/sessions/new' - create: -> + create: (req, res) -> params = - username: @request.body.admin.username - password: @request.body.admin.password + username: req.body.admin.username + password: req.body.admin.password - Administrator.findOne params, (error, administrator) => + Administrator.findOne params, (error, administrator) -> throw error if error - @request.session.administrator_id = administrator._id - @response.redirect '/' + req.session.administrator_id = administrator._id + res.redirect '/' - destroy: -> - @request.session.administrator_id = null - @response.redirect '/' - -module.exports = SessionsController + destroy: (req, res) -> + req.session.administrator_id = null + res.redirect '/' diff --git a/app/controllers/homes_controller.coffee b/app/controllers/homes_controller.coffee index da38ab1..fb9612a 100644 --- a/app/controllers/homes_controller.coffee +++ b/app/controllers/homes_controller.coffee @@ -1,14 +1,10 @@ -BaseController = require './base_controller' -{ mongoose } = require '../../config/database' -Administrator = mongoose.model('Administrator') +{ mongoose } = require '../../config/database' +Administrator = mongoose.model('Administrator') - -class HomesController extends BaseController - show: -> - _id = @request.session.administrator_id +module.exports = + show: (req, res) -> + _id = req.session.administrator_id Administrator.findOne { _id: _id }, (error, administrator) => throw error if error - @response.render 'homes/show', + res.render 'homes/show', administrator: administrator - -module.exports = HomesController diff --git a/config/application.coffee b/config/application.coffee index 1d734ba..67d40dc 100644 --- a/config/application.coffee +++ b/config/application.coffee @@ -14,6 +14,7 @@ module.exports = (app) -> app.set 'view engine', 'jade' app.use express.favicon() app.use express.bodyParser() + app.use express.logger('dev') app.use express.methodOverride() app.use express.cookieParser('2ee27441d3ee4a527de019325dc7e8ddee6039cc7cad9801181c6fd68204129bdf3225cafabb3f6d0324a7bd851dabbd0bd3') app.use express.session() diff --git a/config/routes.coffee b/config/routes.coffee index 3c1e5ec..dfa4093 100644 --- a/config/routes.coffee +++ b/config/routes.coffee @@ -1,31 +1,17 @@ HomesController = require './../app/controllers/homes_controller' -Admin = require './../app/controllers/admin/admin' +Admin = require './../app/controllers/admin' module.exports = (app) -> - app.get '/', (request, response) -> - new HomesController(request, response).show() + app.get '/', HomesController.show - app.get '/admin/login', (request, response) -> - new Admin.SessionsController(request, response).new() + app.get '/admin/login', Admin.SessionsController.new + app.get '/admin/logout', Admin.SessionsController.destroy + app.post '/admin/sessions/create', Admin.SessionsController.create - app.get '/admin/logout', (request, response) -> - new Admin.SessionsController(request, response).destroy() + app.get '/admin/dashboard', Admin.DashboardController.show - app.post '/admin/sessions/create', (request, response) -> - new Admin.SessionsController(request, response).create() - - app.get '/admin/dashboard', (request, response) -> - new Admin.DashboardController(request, response).show() - - app.get '/admin/events', (request, response) -> - new Admin.EventsController(request, response).index() - - app.get '/admin/events/new', (request, response) -> - new Admin.EventsController(request, response).new() - - app.post '/admin/events/create', (request, response) -> - new Admin.EventsController(request, response).create() - - app.get '/admin/events/:id', (request, response) -> - new Admin.EventsController(request, response).show() + app.get '/admin/events', Admin.EventsController.index + app.get '/admin/events/new', Admin.EventsController.new + app.post '/admin/events/create', Admin.EventsController.create + app.get '/admin/events/:id', Admin.EventsController.show