WebFund 2015W: Assignment 4: Difference between revisions

From Soma-notes
Line 129: Line 129:


module.exports = router;
module.exports = router;
</source>
===views/layout.jade
<source lang="html4strict">
doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    block header
  body
    block content
</source>
</source>



Revision as of 18:18, 3 February 2015

This assignment is not yet finalized.

Answer the following questions about notes-demo that you looked at in Tutorial 4. The code is also listed below. There are ?? points in ?? questions. There is also 1 point for a bonus question. This assignment is due by 10 AM on Monday, February 9, 2015. We will go through the solutions in class at that time.

Please submit your answers as a single text or PDF file called "<username>-comp2406-assign4.txt" or "<username>-comp2406-assign4.pdf", where username is your MyCarletonOne username. The first four lines of this file should be "COMP 2406 Assignment 4", your name, student number, and the date. You may wish to format your answers in Markdown to improve their appearance. If you do so, you may convert your text file to PDF using pandoc.

No other formats will be accepted. Submitting in another format will likely result in your assignment not being graded and you receiving no marks for this assignment. In particular do not submit an MS Word or OpenOffice file as your answers document!


Questions


Code

routes/index.js

var express = require('express');
var router = express.Router();

var userNotes = {"alice": 
		 [
		     {"title": "Grocery",
		      "content": "Eggs<br>Milk"},
		     {"title": "To Do",
		      "content": "Lots.<br>of.<br>things"}
		 ]
		};

router.get('/', function(req, res) {
    if (req.session.username) {
        res.redirect("/notes");
    } else {
	res.render('index', { title: 'COMP 2406 Notes Demo', 
			      error: req.query.error });
    }
});

router.get('/notes', function(req, res) {
    var username = req.session.username;
    if (username) {
	res.render("notes.jade", {username: username,
				    title: username +"'s Notes",
				    userNotes: userNotes[username]});
    } else {
	res.redirect("/?error=Not Logged In");
    }
});

router.post('/login', function(req, res) {
    var username = req.body.username;
    req.session.username = username;

    if (!userNotes[username]) {
	userNotes[username] = [];
    }

    res.redirect("/notes")
});

router.post('/logout', function(req, res) {
    req.session.destroy(function(err){
        if (err) {
            console.log("Error: %s", err);
        }
    });
    res.redirect("/");
});

router.get('/edit/:id', function(req, res) {
    var username = req.session.username;
    var id = req.params.id;
    var note;

    if (username) {
	note = userNotes[username][id];

	if (note) {
	    res.render("edit.jade", {title: "Edit Note",
				     note: note,
				     id: id});
	} else {
	    res.redirect("/notes?error=Unknown Note to Edit");
	}
    } else {
	res.redirect("/?error=Not Logged In");
    }
});

router.post('/update', function(req, res) {
    var username = req.session.username;
    var id = req.body.id;
    var title;
    
    if (username) {
	note = userNotes[username][id];

	if (note) {
	    note.title = req.body.title;
	    note.content = req.body.content;
	    title = "Update Succeded";
	} else {
	    title = "Update Failed";
	}
	
	res.render("updated.jade", {title: title,
				    updatedNote: note});
    } else {
	res.redirect("/?error=Not Logged In");
    }
});

router.get('/new', function(req, res) {
    var username = req.session.username;
    var nextNote, note;

    if (username) {
	nextNote = userNotes[username].length;
	note = {title: "Untitled",
		content: "No content"}
	userNotes[username][nextNote] = note;
	res.redirect("/edit/" + nextNote);
    } else {
	res.redirect("/?error=Not Logged In");
    }
});

module.exports = router;


===views/layout.jade

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    block header
  body
    block content


views/index.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  - if(error)
    div #{error}
  p Please log in
  div
    form(action="/login", method="post")
        div
            input(type="text", name="username")
            label(for="username") Username
        button(type="submit") Login