Skip to content

Commit 6a7005e

Browse files
committedJun 22, 2016
view model
1 parent 0060a9e commit 6a7005e

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed
 

‎data/priorities.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{ "name": "High", "color": "red", "value": 10 },
3-
{ "name": "Medium", "color": "yellow", "value": 5 },
4-
{ "name": "Low", "color": "blue", "value": 1 }
2+
{ "name": "High", "color": "#FF4136", "value": 10 },
3+
{ "name": "Medium", "color": "#FFDC00", "value": 5 },
4+
{ "name": "Low", "color": "#7FDBFF", "value": 1 }
55
]

‎src/controllers/tasks.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
/* eslint-disable new-cap */
1+
/* eslint-disable new-cap, array-callback-return, no-param-reassign */
22

33
import express from 'express';
44
import Priority from '../models/priority';
55
import Category from '../models/category';
6+
import Task from '../models/task';
7+
import ViewTask from '../models/view-task';
68
const router = module.exports = express.Router();
79

810
router.get('/', (req, res) => {
9-
res.render('tasks/index');
11+
Task.find((err, tasks) => {
12+
const priorities = Priority.find();
13+
const viewTasks = tasks.map(t => new ViewTask(t, priorities));
14+
res.render('tasks/index', { viewTasks });
15+
});
1016
});
1117

1218
router.get('/new', (req, res) => {
@@ -16,7 +22,10 @@ router.get('/new', (req, res) => {
1622
});
1723

1824
router.post('/', (req, res) => {
19-
res.redirect('/tasks');
25+
const task = new Task(req.body);
26+
task.save(() => {
27+
res.redirect('/tasks');
28+
});
2029
});
2130

2231
router.post('/:id/complete', (req, res) => {

‎src/models/task.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import mongoose from 'mongoose';
2+
const Schema = mongoose.Schema;
3+
4+
const taskSchema = new Schema({
5+
name: String,
6+
due: Date,
7+
priority: Number,
8+
category: String,
9+
isComplete: { type: Boolean, default: false },
10+
createdAt: { type: Date, default: Date.now },
11+
});
12+
13+
module.exports = mongoose.model('Task', taskSchema);

‎src/models/view-task.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function ViewTask(task, priorities) {
2+
this.name = task.name;
3+
this.due = task.due.toLocaleDateString();
4+
this.priority = priorities.find(p => p.value === task.priority);
5+
this.category = task.category;
6+
this.isComplete = task.isComplete;
7+
this.createdAt = task.createdAt.toLocaleDateString();
8+
}
9+
10+
module.exports = ViewTask;

‎views/shared/template.pug

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ html(lang='en')
1717

1818
script(src='/vendor/jquery/dist/jquery.min.js')
1919
script(src='/vendor/bootstrap/dist/js/bootstrap.min.js')
20+
script(src='/vendor/moment/min/moment.min.js')

‎views/tasks/index.pug

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
extends ../shared/template
22
block content
33
h1 Tasks
4+
5+
table.table.table-striped
6+
thead
7+
tr
8+
th Name
9+
th Due
10+
th Priority
11+
th Category
12+
th Complete?
13+
th Created
14+
tbody
15+
each task in viewTasks
16+
tr(style='background-color:' + task.priority.color)
17+
td= task.name
18+
td= task.due
19+
td= task.priority.name
20+
td= task.category
21+
td
22+
if task.isComplete
23+
i.fa.fa-check-square-o.fa-fw
24+
else
25+
i.fa.fa-square-o.fa-fw
26+
td= task.createdAt

0 commit comments

Comments
 (0)
Please sign in to comment.