Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit fa534e1

Browse files
committed
Rework the server files to handle association loading
1 parent 5c2b27a commit fa534e1

File tree

6 files changed

+154
-46
lines changed

6 files changed

+154
-46
lines changed

server/models/category.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var _ = require('lodash');
2+
3+
var categories = [
4+
{"id": 1, "name": "Testing", "icon": "tumblr"},
5+
{"id": 2, "name": "Personal Note", "icon": "pencil"},
6+
{"id": 3, "name": "Bash", "icon": "terminal"},
7+
{"id": 4, "name": "Idea", "icon": "lightbulb"},
8+
{"id": 5, "name": "Use with Caution","icon": "warning"},
9+
{"id": 6, "name": "Question", "icon": "question"},
10+
{"id": 7, "name": "Best Practice","icon": "thumbs up outline"},
11+
{"id": 8, "name": "Code Snippet", "icon": "code"}
12+
]
13+
14+
module.exports = {
15+
get: function(id) {
16+
return _.find(categories, function(category){
17+
return category.id === id;
18+
});
19+
},
20+
all: function() {
21+
return categories;
22+
}
23+
}

server/models/note.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var _ = require('lodash');
2+
var Category = require('./category')
3+
var User = require('./user')
4+
var notes = [
5+
{"id":1 ,"userId": 13, "categoryId": 8, "link" :"https://www.youtube.com/watch?feature=player_detailpage&v=ZhfUv0spHCY#t=1870", "description": "NgModel Best Practice", "content" : "Always use dot syntax when using NgModel! Treat Scope as read-only in templates & write-only in controllers. The purpose of the scope is to refer to the model, not be the model. The model is your javascript objects. When doing bidirectional binding with ngModel make sure you don't bind directly to the scope properties. This will cause unexpected behavior in the child scopes.", "title" : "NgModel BP", "icon" : "basic info"},
6+
{"id":2 ,"userId": 2, "categoryId": 3, "link" : "https://docs.angularjs.org/api/ng#directive", "description" : "Markers on a **DOM element** that tell AngularJS's HTML compiler `$compile` to attach a specified behavior to that DOM element.", "title" : "Directives", "icon" : "code", "content": "Markers on a **DOM element**"},
7+
{"id":3 ,"userId": 1, "categoryId": 6, "link" : "", "description" : "Clarify the confusion between Service the term and `service` the angular method and to explain the 5 different Service recipes in Angular.", "title" : "Service Service? Really Angular?","content": "There are 5 Recipes used to create a Service. One of those *was* unfortunately named, Service. So yes, amongst its fellow peers such as Provider Service and Factory Service, there is in fact a Service Service.", "icon" : "question"},
8+
{"id":4 ,"userId": 2, "categoryId": 6, "link" : "", "description" : "QUESTIONABLE DESCRIPTION GOES HERE", "title" : "TEST TEST TEST", "content": "QUESTIONABLE CONTENT GOES HERE", "icon" : "question"},
9+
{"id":5 ,"userId": 4, "categoryId": 6, "link" : "", "description" : "Define Service", "title" : "What is a Service", "content": "Service: Angular services are objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.", "icon" : "question"},
10+
{"id":6 ,"userId": 5, "categoryId": 6, "description" : "Steps for Creating a Service", "title" : "How do you create a Service?", "content": "You can register a service to our Angular module `app` with a one of the following 5 recipes: \\n - **factory** \\n - **provider** \\n - **service** \\n - **value** \\n - **constant** ", "icon" : "question"}
11+
]
12+
var lastId = 6;
13+
14+
var buildNotes = function() {
15+
// Make a deep copy so we don't change the main notes array
16+
var rawNotes = JSON.parse(JSON.stringify(notes));
17+
var builtNotes = [];
18+
var note;
19+
20+
for(var i=0, l=rawNotes.length; i < l; i++) {
21+
note = rawNotes[i];
22+
note.user = User.get(note.userId);
23+
note.category = Category.get(note.categoryId);
24+
builtNotes.push(note);
25+
}
26+
return builtNotes
27+
}
28+
29+
module.exports = {
30+
get: function(id) {
31+
return _.find(buildNotes(), function(note){
32+
return note.id === id;
33+
});
34+
},
35+
all: function() {
36+
return buildNotes();
37+
},
38+
update: function(note) {
39+
var updatedNote;
40+
for(var i=0, l=notes.length; i < l; i++) {
41+
if(notes[i].id === note.id){
42+
_.assign(notes[i], note);
43+
updatedNote = notes[i];
44+
break;
45+
}
46+
}
47+
return updatedNote;
48+
},
49+
delete: function(id) {
50+
var deletedNote;
51+
for(var i=0, l=notes.length; i < l; i++) {
52+
if(notes[i].id === id){
53+
deletedNote = notes[i];
54+
notes.splice(0, i);
55+
break;
56+
}
57+
}
58+
return deletedNote;
59+
},
60+
create: function(note) {
61+
lastId += 1;
62+
note.id = lastId;
63+
notes.push(note)
64+
return note;
65+
}
66+
}

server/models/user.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var _ = require('lodash');
2+
3+
var users = [
4+
{"id": 1, "username": "zeldman", "name": "Jeffery Zeldman", "bio": "Founder, Happy Cog studios. Author, Designing With Web Standards. Publisher, A List Apart, A Book Apart.", "twitter_handle": "@zeldman", "site": "zeldman.com"},
5+
{"id": 2, "username": "b_green", "name": "Brad Green", "bio": "I work at Google where I manage AngularJS and Google's internal sales productivity applications. I'm a dad.", "twitter_handle": "@bradlygreen", "site": "google.com/+BradGreen"},
6+
{"id": 3, "username": "Meyer the Eric", "name": "Eric A. Meyer", "bio": "Web standards | (X)HTML | CSS | microformats | community | writing | speaking | signing man.", "twitter_handle": "@meyerweb", "site": "meyerweb.com"},
7+
{"id": 4, "username": "GP", "name": "Gregg Pollack", "bio": "Founder of Envy Labs, Code School, Orlando Ruby Users Group, BarCamp Orlando, and the Orlando Tech Events newsletter.", "twitter_handle": "@greggpollack", "site": "EnvyLabs.com"},
8+
{"id": 5, "username": "r_higley", "name": "Rachel Higley", "bio": "A web developer located in central florida", "twitter_handle": "@RachelHigley", "site": ""},
9+
{"id": 6, "username": "zach", "name": "Zachary Nicoll", "bio": "Bio sections always intimidate me. I can never think of anything to say that will achieve that awe inspiring effect I want it to have.", "twitter_handle": "@turtleguyy", "site": "zacharynicoll.com"},
10+
{"id": 7, "username": "renz", "name": "Adam Rensel","bio": "Web Developer at @envylabs and @codeschool", "twitter_handle": "@adamrensel", "site": "adamrensel.com"},
11+
{"id": 8, "username": "ItsThrillhouse", "name": "Jason Millhouse", "bio": "Course builder. Aspiring writer. Comp Sci guy. Teacher. Sweetfiend. Corgi lover. Gamer who doesn't. Pro Series host. Voice of the UCF Marching Knights. Dork.", "twitter_handle": "@ItsThrillhouse", "site": ""},
12+
{"id": 9, "username": "OlivierLacan","name": "Olivier Lacan","bio": "Software bricoleur at @codeschool, word wrangler, scientific skeptic, and logic lumberjack.","twitter_handle": "@olivierlacan","site": "olivierlacan.com"},
13+
{"id": 10, "username": "theSmith", "name": "Andrew Smith", "bio": "iOS & Web Developer at @intelity. @fullsail graduate.", "twitter_handle": "@fullsailor", "site": "fullsailor.com"},
14+
{"id": 11, "username": "DrewBarontini", "name": "Drew Barontini", "bio": "Front-end developer @codeschool, descendant of @envylabs, real-life extrovert, internet introvert.", "twitter_handle": "@drewbarontini", "site": "drewbarontini.com"},
15+
{"id": 12, "username": "JordanWade", "name": "Jordan Wade", "bio": "Designer, Illustrator, and Front-End Developer @codeschool", "twitter_handle": "@jjordanwade", "site": "jamesjordanwade.com"},
16+
{"id": 13, "username": "AlyssaNicoll", "name": "Alyssa Nicoll", "bio": "Code School Teacher. Angular Lover. Scuba Diver.", "twitter_handle": "@AlyssaNicoll", "site": "alyssa.io"}
17+
];
18+
19+
module.exports = {
20+
get: function(id) {
21+
return _.find(users, function(user){
22+
return user.id === id;
23+
});
24+
},
25+
all: function() {
26+
return users;
27+
}
28+
}

server/routes/category.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
var categories = [
2+
{"id": 1, "name": "Testing", "icon": "tumblr"},
3+
{"id": 2, "name": "Personal Note", "icon": "pencil"},
4+
{"id": 3, "name": "Bash", "icon": "terminal"},
5+
{"id": 4, "name": "Idea", "icon": "lightbulb"},
6+
{"id": 5, "name": "Use with Caution","icon": "warning"},
7+
{"id": 6, "name": "Question", "icon": "question"},
8+
{"id": 7, "name": "Best Practice","icon": "thumbs up outline"},
9+
{"id": 8, "name": "Code Snippet", "icon": "code"}
10+
]
11+
112
module.exports = function(app) {
213
// Return a list of available node types
314
app.get('/categories', function(req, res) {
4-
res.json([
5-
{"id": 1, "name": "Testing", "icon": "tumblr"},
6-
{"id": 2, "name": "Personal Note", "icon": "pencil"},
7-
{"id": 3, "name": "Bash", "icon": "terminal"},
8-
{"id": 4, "name": "Idea", "icon": "lightbulb"},
9-
{"id": 5, "name": "Use with Caution","icon": "warning"},
10-
{"id": 6, "name": "Question", "icon": "question"},
11-
{"id": 7, "name": "Best Practice","icon": "thumbs up outline"},
12-
{"id": 8, "name": "Code Snippet", "icon": "code"}
13-
]);
15+
res.json(categories);
16+
});
17+
18+
app.get('/categories/:id', function(req, res) {
19+
var categoryId = parseInt(req.param('id'), 10);
20+
var selectedCategory = _.find(categories, function(category){
21+
return category.id === categoryId;
22+
});
23+
24+
res.json(selectedCategory || {});
1425
});
1526
};

server/routes/note.js

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,26 @@
11
var _ = require('lodash');
2-
3-
var notes = [
4-
{"id":1 ,"UserId": 13, "CategoryId": 8, "link" :"https://www.youtube.com/watch?feature=player_detailpage&v=ZhfUv0spHCY#t=1870", "description": "NgModel Best Practice", "content" : "Always use dot syntax when using NgModel! Treat Scope as read-only in templates & write-only in controllers. The purpose of the scope is to refer to the model, not be the model. The model is your javascript objects. When doing bidirectional binding with ngModel make sure you don't bind directly to the scope properties. This will cause unexpected behavior in the child scopes.", "title" : "NgModel BP", "icon" : "basic info"},
5-
{"id":2 ,"UserId": 2, "CategoryId": 3, "link" : "https://docs.angularjs.org/api/ng#directive", "description" : "Markers on a **DOM element** that tell AngularJS's HTML compiler `$compile` to attach a specified behavior to that DOM element.", "title" : "Directives", "icon" : "code", "content": "Markers on a **DOM element**"},
6-
{"id":3 ,"UserId": 7, "CategoryId": 6, "link" : "", "description" : "Clarify the confusion between Service the term and `service` the angular method and to explain the 5 different Service recipes in Angular.", "title" : "Service Service? Really Angular?","content": "There are 5 Recipes used to create a Service. One of those *was* unfortunately named, Service. So yes, amongst its fellow peers such as Provider Service and Factory Service, there is in fact a Service Service.", "icon" : "question"},
7-
{"id":4 ,"UserId": 7, "CategoryId": 6, "link" : "", "description" : "QUESTIONABLE DESCRIPTION GOES HERE", "title" : "TEST TEST TEST", "content": "QUESTIONABLE CONTENT GOES HERE", "icon" : "question"},
8-
{"id":5 ,"UserId": 7, "CategoryId": 6, "link" : "", "description" : "Define Service", "title" : "What is a Service", "content": "Service: Angular services are objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.", "icon" : "question"},
9-
{"id":6 ,"UserId": 7, "CategoryId": 6, "description" : "Steps for Creating a Service", "title" : "How do you create a Service?", "content": "You can register a service to our Angular module `app` with a one of the following 5 recipes: \\n - **factory** \\n - **provider** \\n - **service** \\n - **value** \\n - **constant** ", "icon" : "question"}
10-
]
11-
12-
var lastId = 6;
2+
var Note = require('../models/note')
133

144
module.exports = function(app) {
155
app.get('/notes', function(req, res) {
16-
res.json(notes);
6+
res.json(Note.all());
177
});
188

199
app.post('/notes', function(req, res) {
20-
var newNote = {id: lastId+=1}; //, UserId: null, CategoryId: req.param('CategoryId'), link: req.param('link'), title: req.param('title'), content: req.param('content'), description: req.param('description'), icon: req.param('icon')};
21-
22-
_.assign(newNote, req.param('note'));
23-
notes.push(newNote);
24-
res.json(newNote);
10+
res.json(Note.create(req.body));
2511
});
2612

2713
app.put('/notes', function(req, res) {
28-
var noteId = parseInt(req.param('id'), 10);
29-
var selectedNote = _.find(notes, function(note){
30-
return note.id === noteId;
31-
});
32-
33-
if(selectedNote) {
34-
_.assign(selectedNote, req.param('note'));
35-
}
36-
37-
res.json(selectedNote);
14+
res.json(Note.update(req.body));
3815
});
3916

4017
app.get('/notes/:id', function(req, res) {
4118
var noteId = parseInt(req.param('id'), 10);
42-
var selectedNote = _.find(notes, function(note){
43-
return note.id === noteId;
44-
});
19+
res.json(Note.get(noteId) || {});
20+
});
4521

46-
res.json(selectedNote || {});
22+
app.delete('/notes/:id', function(req, res) {
23+
var noteId = parseInt(req.param('id'), 10);
24+
res.json(Note.delete(noteId) || {});
4725
});
4826
};

server/routes/user.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ var users = [
1616
{"id": 13, "username": "AlyssaNicoll", "name": "Alyssa Nicoll", "bio": "Code School Teacher. Angular Lover. Scuba Diver.", "twitter_handle": "@AlyssaNicoll", "site": "alyssa.io"}
1717
];
1818

19+
var User = require('../models/user');
20+
1921
module.exports = function(app){
2022
app.get('/users', function(req, res){
21-
res.json(users);
23+
res.json(User.all());
2224
});
2325

2426
app.get('/users/:id', function(req, res){
2527
var userId = parseInt(req.params.id, 10);
2628

27-
var selectedUser = _.find(users, function(user){
28-
return user.id === userId;
29-
});
29+
// var selectedUser = _.find(users, function(user){
30+
// return user.id === userId;
31+
// });
3032

31-
res.json(selectedUser || {});
33+
res.json(User.get(userId) || {});
3234
});
3335
};

0 commit comments

Comments
 (0)