diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000000..93d77ab351 --- /dev/null +++ b/.bowerrc @@ -0,0 +1,4 @@ +{ + "directory": "public/lib", + "json": "bower.json" +} diff --git a/app/controllers/articles.js b/app/controllers/articles.js new file mode 100644 index 0000000000..8966682c0b --- /dev/null +++ b/app/controllers/articles.js @@ -0,0 +1,80 @@ +/** + * Module dependencies. + */ + +var mongoose = require('mongoose') + , async = require('async') + , Article = mongoose.model('Article') + , _ = require('underscore') + + +/** + * Find article by id + */ + +exports.article = function(req, res, next, id){ + var User = mongoose.model('User') + + Article.load(id, function (err, article) { + if (err) return next(err) + if (!article) return next(new Error('Failed to load article ' + id)) + req.article = article + next() + }) +} + +/** + * Create a article + */ +exports.create = function (req, res) { + var article = new Article(req.body) + article.user = req.user + article.save() + res.jsonp(article) +} + +/** + * Update a article + */ +exports.update = function(req, res){ + var article = req.article + article = _.extend(article, req.body) + + article.save(function(err) { + res.jsonp(article) + }) +} + +/** + * Delete an article + */ +exports.destroy = function(req, res){ + var article = req.article + article.remove(function(err){ + if (err) { + res.render('error', {status: 500}); + } else { + res.jsonp(article); + } + }) +} + +/** + * Show an article + */ +exports.show = function(req, res){ + res.jsonp(req.article); +} + +/** + * List of Articles + */ +exports.all = function(req, res){ + Article.find().sort('-created').populate('user').exec(function(err, articles) { + if (err) { + res.render('error', {status: 500}); + } else { + res.jsonp(articles); + } + }); +} \ No newline at end of file diff --git a/app/models/article.js b/app/models/article.js new file mode 100644 index 0000000000..988a803f44 --- /dev/null +++ b/app/models/article.js @@ -0,0 +1,31 @@ +/** + * Module dependencies. + */ +var mongoose = require('mongoose') + , env = process.env.NODE_ENV || 'development' + , config = require('../../config/config')[env] + , Schema = mongoose.Schema + +/** + * Article Schema + */ + +var ArticleSchema = new Schema({ + created: {type : Date, default : Date.now}, + title: {type: String, default: '', trim : true}, + content: {type: String, default: '', trim : true}, + user: {type : Schema.ObjectId, ref : 'User'} +}); + + +/** + * Statics + */ + +ArticleSchema.statics = { + load: function (id, cb) { + this.findOne({ _id : id }).populate('user').exec(cb); + } +} + +mongoose.model('Article', ArticleSchema) \ No newline at end of file diff --git a/bower.json b/bower.json new file mode 100644 index 0000000000..960626f1cd --- /dev/null +++ b/bower.json @@ -0,0 +1,17 @@ +{ + "name": "angularJS-IL", + "version": "1.0.0", + "dependencies": { + "bootstrap": "2.3.2", + "angular": "~1.0.6", + "angular-resource": "~1.0.6", + "angular-cookies": "~1.0.6", + "angular-bootstrap": "~0.4.0", + "json3": "~3.2.4", + "jquery": "~1.9.1" + }, + "devDependencies": { + "angular-mocks": "~1.0.5", + "angular-scenario": "~1.0.5" + } +} diff --git a/public/js/controllers/articles.js b/public/js/controllers/articles.js new file mode 100644 index 0000000000..b0665df043 --- /dev/null +++ b/public/js/controllers/articles.js @@ -0,0 +1,48 @@ +function ArticlesController($scope, $routeParams, $location, Articles) { + $scope.articles = []; + $scope.article = {}; + + $scope.create = function () { + var article = new Articles({ title: this.title, content: this.content }); + article.$save(function (response) { + $location.path("articles/" + response._id); + }); + + this.title = ""; + this.content = ""; + }; + + $scope.remove = function (article) { + article.$remove(); + + for (var i in $scope.articles) { + if ($scope.articles[i] == article) { + $scope.articles.splice(i, 1) + } + } + }; + + $scope.update = function () { + var article = $scope.article; + if (!article.updated) { + article.updated = []; + } + article.updated.push(new Date().getTime()); + + article.$update(function () { + $location.path('articles/' + article._id); + }); + }; + + $scope.find = function (query) { + Articles.query(query, function (articles) { + $scope.articles = articles; + }); + }; + + $scope.findOne = function () { + Articles.get({ articleId: $routeParams.articleId }, function (article) { + $scope.article = article; + }); + }; +} \ No newline at end of file diff --git a/public/js/services/articles.js b/public/js/services/articles.js new file mode 100644 index 0000000000..f3a04bb24f --- /dev/null +++ b/public/js/services/articles.js @@ -0,0 +1,4 @@ +//Articles service used for articles REST endpoint +window.app.factory("Articles", function($resource){ + return $resource('articles/:articleId', {articleId:'@_id'}, {update: {method: 'PUT'}}); +}); \ No newline at end of file diff --git a/public/views/articles/create.html b/public/views/articles/create.html new file mode 100644 index 0000000000..d3713ed9ce --- /dev/null +++ b/public/views/articles/create.html @@ -0,0 +1,21 @@ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+
\ No newline at end of file diff --git a/public/views/articles/edit.html b/public/views/articles/edit.html new file mode 100644 index 0000000000..3189f3fef7 --- /dev/null +++ b/public/views/articles/edit.html @@ -0,0 +1,25 @@ +
+
+
+ + +
+ + +
+
+
+ + +
+ +
+
+
+
+ +
+
+
+
\ No newline at end of file diff --git a/public/views/articles/list.html b/public/views/articles/list.html new file mode 100644 index 0000000000..d5c3006c3f --- /dev/null +++ b/public/views/articles/list.html @@ -0,0 +1,10 @@ +
+ +
\ No newline at end of file diff --git a/public/views/articles/view.html b/public/views/articles/view.html new file mode 100644 index 0000000000..6105ab914d --- /dev/null +++ b/public/views/articles/view.html @@ -0,0 +1,6 @@ +
+ {{article.created | date:'medium'}} / + {{article.user.name}} +

{{article.title}}

+
{{article.content}}
+
\ No newline at end of file