Skip to content

Instantly share code, notes, and snippets.

@CodeLenny
Last active August 29, 2015 14:10
Show Gist options
  • Save CodeLenny/8ce30252084e06fddbe5 to your computer and use it in GitHub Desktop.
Save CodeLenny/8ce30252084e06fddbe5 to your computer and use it in GitHub Desktop.
HeadyFoods Draft
//- include "app-layout.blade" //- #TODO: write a template file
//- #TODO: Get user from the authentication module, and add it to the options in @render()
p Hey #{user.name}, what foods do you want to order?
.foodList
foreach foods as food
//- #TODO: make sure all properties are added to the database
h1= food.name
p.desc= food.description
a(href="/food/"+food._id+"/purchase/") Purchase Food
//- #TODO: Add contact options to the paragraph below
p Don't see something you'd like in the list above? Let us know what foods to add!
class HeadyFoods
constructor: ->
@blade = require("node-blade")
# Add database connection
@mongoose = require("mongoose")
@Schema = @mongoose.Schema
@db = @mongoose.connect "mongodb://#{@config.db.host}:#{@config.db.port}/#{@config.db.database}"
@db.once "open", =>
@FoodItem = @createFoodItem()
@configureExpress()
configureExpress: ->
# Configure Express.js (or alternative) app
# @Purchase = Object #TODO add Purchase db model
@app.get "/", @checkLogin, (req, res, next) =>
if @req.user then @homepage(req, res, next) else @dashboard(req, res, next)
@app.get "/food/:food/purchase/", @requireLogin, @purchase
# Display the homepage for users not logged in
homepage: (req, res, next) ->
@render "homepage", res, {} #TODO add page
# processRegister: -> #TODO login system
# processLogin: -> #TODO login system
# checkLogin: (req, res, next) => #TODO login system
# requireLogin: (req, res, next) => #TODO login system
# getFoodList(function(error, FoodItem[] items))
getFoodList: (cb) ->
@FoodItem.find({}).exec (err, items) =>
return err, items
# Abstract getFood returns Food #TODO query db
getFood: (id) ->
return new Food()
# confirmPayment (req) -> #TODO payment system (Stripe?)
dashboard: (req, res, next) ->
@getFoodList (err, items) =>
return @render "error", res, {"message":"Unable to get food items."} if err #TODO: write error page in blade
@render "dashboard", res,
foods: items
# Each food in the foodlist has a link to /food/:food/purchase/ (after a confirm popup)
purchase: (req, res, next) =>
@id = req.param.food
food = @getFood @id
@render "purchaseForm", res, #TODO write page
food: food
# Submits to /food/:food/done/
finalizePurchase: (req, res, next) =>
@id = req.param.food
food = @getFood @id
if not @confirmPayment req
return @render "paymentError", res, {details: {}} #TODO: write page
purchase = new @Purchase()
# TODO: set purchase details
purchase.save()
@render "purchaseDone", res, #TODO write page
food: food
purchase: purchase
render: (filename, res, options) ->
#TODO: Change - to a / to put blade files in a directory called blade (and rename the blade file)
# (Gist doesn't support folders)
@blade.renderFile "blade-#{file}.blade", options, (err, html) =>
return console.log "Error generating file" if err
res.send html
next()
# Create a database schema for food items.
createFoodItem: ->
itemSchema = new @Schema
name: String
description: String
FoodItem = @mongoose.model "FoodItem", itemSchema
# The following is an ugly method of inserting default data. It should probably loop through a file of values instead.
temp = new FoodItem
name: "Orange"
description: "A simple orange."
FoodItem.update {name: temp.name}, temp.toObject(), {upsert: true}, (err, temp) =>
console.log "Error saving default food item to the database: #{err} (#{temp})"
return FoodItem
server = new HeadyFoods()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment