Explorar el Código

initial commit

Johannes Zellner hace 11 años
padre
commit
8c11e0d309
Se han modificado 8 ficheros con 380 adiciones y 0 borrados
  1. 2 0
      .gitignore
  2. 42 0
      app.js
  3. 24 0
      bower.json
  4. 23 0
      package.json
  5. 24 0
      public/index.css
  6. 81 0
      public/index.html
  7. 105 0
      public/index.js
  8. 79 0
      src/things.js

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+node_modules/
+bower_components/

+ 42 - 0
app.js

@@ -0,0 +1,42 @@
+#!/usr/bin/env node
+
+'use strict';
+
+require('supererror')({ splatchError: true });
+
+var express = require('express'),
+    json = require('body-parser').json,
+    things = require('./src/things.js'),
+    lastmile = require('connect-lastmile'),
+    serveStatic = require('serve-static');
+
+var app = express();
+var router = new express.Router();
+
+router.del = router.delete;
+
+router.post('/api/things', things.add);
+router.get ('/api/things', things.getAll);
+router.get ('/api/things/:id', things.get);
+router.del ('/api/things/:id', things.del);
+
+app
+    .use(json({ strict: true })) // only parse objects and arrays
+    .use(serveStatic(__dirname + '/public'))
+    .use(serveStatic(__dirname + '/bower_components'))
+    .use(router)
+    .use(lastmile());
+
+things.init(function (error) {
+    if (error) {
+        console.error(error);
+        process.exit(1);
+    }
+
+    var server = app.listen(3000, function () {
+        var host = server.address().address;
+        var port = server.address().port;
+
+        console.log('App listening at http://%s:%s', host, port);
+    });
+});

+ 24 - 0
bower.json

@@ -0,0 +1,24 @@
+{
+  "name": "holyguacamoly",
+  "description": "",
+  "main": "index.js",
+  "authors": [
+    "Johannes Zellner <johannes@nebulon.de>"
+  ],
+  "license": "ISC",
+  "moduleType": [],
+  "homepage": "",
+  "ignore": [
+    "**/.*",
+    "node_modules",
+    "bower_components",
+    "test",
+    "tests"
+  ],
+  "dependencies": {
+    "angular": "~1.4.8",
+    "bootstrap": "~3.3.6",
+    "bootstrap-material-design": "~0.5.2",
+    "angular-markdown-directive": "~0.3.1"
+  }
+}

+ 23 - 0
package.json

@@ -0,0 +1,23 @@
+{
+  "name": "holyguacamoly",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://git-berlin.cloudron.me/nebulon/holyguacamoly.git"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "body-parser": "^1.14.1",
+    "connect-lastmile": "0.0.13",
+    "express": "^4.13.3",
+    "mongodb": "^2.0.49",
+    "mongojs": "^2.0.0",
+    "supererror": "^0.7.1"
+  }
+}

+ 24 - 0
public/index.css

@@ -0,0 +1,24 @@
+
+.main {
+	margin-top: 50px;
+}
+
+.filter {
+	width: 100%;
+}
+
+.well {
+	position: relative;
+}
+
+.thing-title {
+	width: auto;
+	position: absolute;
+	right: 20px;
+	top: 15px;
+}
+
+.thing-title-date {
+	font-size: 12px;
+	text-align: right;
+}

+ 81 - 0
public/index.html

@@ -0,0 +1,81 @@
+<html ng-app="app">
+<head>
+
+<title> Holy Guacamoly </title>
+
+<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
+
+<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css"/>
+<link rel="stylesheet" href="/bootstrap-material-design/dist/css/bootstrap-material-design.min.css"/>
+<link rel="stylesheet" href="/bootstrap-material-design/dist/css/ripples.min.css"/>
+
+<script type="text/javascript" src="/jquery/dist/jquery.min.js"></script>
+<script type="text/javascript" src="/bootstrap/dist/js/bootstrap.min.js"></script>
+<script type="text/javascript" src="/bootstrap-material-design/dist/js/material.min.js"></script>
+<script type="text/javascript" src="/bootstrap-material-design/dist/js/ripples.min.js"></script>
+
+<script type="text/javascript" src="/angular/angular.min.js"></script>
+<script type="text/javascript" src="/angular-sanitize/angular-sanitize.min.js"></script>
+<script type="text/javascript" src="/showdown/compressed/Showdown.min.js"></script>
+<script type="text/javascript" src="/angular-markdown-directive/markdown.js"></script>
+
+<link rel="stylesheet" href="/index.css"/>
+<script type="text/javascript" src="/index.js"></script>
+
+</head>
+
+<body ng-controller="MainController">
+
+<div class="modal fade" id="modalAdd">
+  <div class="modal-dialog">
+    <div class="modal-content">
+      <div class="modal-header">
+        <button type="button" class="close">×</button>
+        <h4 class="modal-title">Add something</h4>
+      </div>
+      <div class="modal-body">
+        <p>
+          <textarea class="form-control" rows="5" ng-model="addFormData.content" autofocus></textarea>
+        </p>
+      </div>
+      <div class="modal-footer">
+        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
+        <button type="button" class="btn btn-primary" ng-click="addThing()">Save</button>
+      </div>
+    </div>
+  </div>
+</div>
+
+<div class="container main">
+  <div class="row">
+    <div class="col-md-2 col-md-offset-5">
+        <button class="btn btn-raised btn-block btn-success" data-toggle="modal" data-target="#modalAdd">Add</button>
+    </div>
+  </div>
+  <div class="row">
+    <div class="col-md-6 col-md-offset-3">
+      <div class="form-group has-success">
+        <label class="control-label" for="inputFilter">Filter</label>
+        <input class="form-control" id="inputFilter" type="text" ng-model="filter"/>
+      </div>
+    </div>
+  </div>
+  <div class="row">
+    <div class="col-md-12">
+      <div class="well thing" ng-repeat="thing in things">
+        <div class="container thing-title">
+          <div class="row">
+            <div class="col-md-12 thing-title-date">{{thing.createdAt | prettyDateOffset}}</div>
+          </div>
+          <div class="row">
+            <div class="col-md-1 col-md-offset-11"><button class="btn btn-xs btn-block btn-default" ng-click="deleteThing(thing._id)">delete</button></div>
+          </div>
+        </div>
+        <div btf-markdown="thing.content"></div>
+      </div>
+    </div>
+  </div>
+</div>
+
+</body>
+</html>

+ 105 - 0
public/index.js

@@ -0,0 +1,105 @@
+/* jslint browser:true */
+/* global angular */
+/* global $ */
+
+'use strict';
+
+var app = angular.module('app', [
+    'ngSanitize',
+    'btford.markdown'
+]);
+
+app.controller('MainController', function ($scope, $http, $timeout) {
+
+    $scope.things = [];
+    $scope.filter = '';
+    $scope.addFormData = {
+        busy: true,
+        content: ''
+    };
+
+    $scope.addThing = function () {
+        $scope.addFormData.busy = true;
+
+        $http.post('/api/things', { content: $scope.addFormData.content }, {}).then(function (result) {
+            $scope.addFormData.busy = false;
+            $scope.addFormData.content = '';
+
+            $('#modalAdd').modal('hide');
+
+            $scope.fetchThings();
+        }, function (result) {
+            console.error('error:', result);
+            $scope.addFormData.busy = false;
+        });
+    };
+
+    $scope.deleteThing = function (id) {
+        $http.delete('/api/things/' + id, {}).then(function (result) {
+            $scope.fetchThings();
+        }, function (result) {
+            console.error('error:', result);
+        });
+    };
+
+    $scope.fetchThings = function () {
+        $http.get('/api/things', {}).then(function (result) {
+            angular.copy(result.data.things, $scope.things);
+        }, function (result) {
+            console.error('error:', result);
+        });
+    };
+
+
+    $scope.fetchThings();
+
+    $timeout(function () {
+        $.material.init();
+    });
+});
+
+app.filter('prettyDateOffset', function () {
+    // http://ejohn.org/files/pretty.js
+    return function prettyDateOffset(time){
+        var date = new Date(time),
+            diff = (((new Date()).getTime() - date.getTime()) / 1000),
+            day_diff = Math.floor(diff / 86400);
+
+        if (isNaN(day_diff) || day_diff < 0)
+            return;
+
+        return day_diff === 0 && (
+                diff < 60 && 'just now' ||
+                diff < 120 && '1 minute ago' ||
+                diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
+                diff < 7200 && '1 hour ago' ||
+                diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
+            day_diff === 1 && 'Yesterday' ||
+            day_diff < 7 && day_diff + ' days ago' ||
+            day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' ||
+            day_diff < 365 && Math.round( day_diff / 30 ) +  ' months ago' ||
+                              Math.round( day_diff / 365 ) + ' years ago';
+    };
+});
+
+app.filter('prettyDate', function () {
+    var months = new Array(12);
+    months[0] = 'January';
+    months[1] = 'February';
+    months[2] = 'March';
+    months[3] = 'April';
+    months[4] = 'May';
+    months[5] = 'June';
+    months[6] = 'July';
+    months[7] = 'August';
+    months[8] = 'September';
+    months[9] = 'October';
+    months[10] = 'November';
+    months[11] = 'December';
+
+    return function prettyDateOffset(time) {
+        var date = new Date(time);
+        return date.getUTCMonth() + 1 + ' ' + months[date.getDate()] + ' ' + date.getUTCFullYear();
+    };
+});
+

+ 79 - 0
src/things.js

@@ -0,0 +1,79 @@
+/* jslint node:true */
+
+'use strict';
+
+var MongoClient = require('mongodb').MongoClient,
+    ObjectId = require('mongodb').ObjectID,
+    HttpError = require('connect-lastmile').HttpError,
+    HttpSuccess = require('connect-lastmile').HttpSuccess;
+
+exports = module.exports = {
+    init: init,
+    getAll: getAll,
+    get: get,
+    add: add,
+    del: del
+};
+
+var databaseUrl = 'mongodb://127.0.0.1:27017/guacamoly';
+var g_db, g_things;
+
+function init(callback) {
+    MongoClient.connect(databaseUrl, function (error, db) {
+        if (error) return callback(error);
+
+        g_db = db;
+        g_things = db.collection('things');
+
+        callback(null);
+    });
+}
+
+function getAll(req, res, next) {
+    g_things.find({}).sort({ createdAt: -1 }).toArray(function (error, result) {
+        if (error || !result) return next(new HttpError(500, error));
+
+        console.log('done', result);
+
+        next(new HttpSuccess(200, { things: result }));
+    });
+}
+
+function get(req, res, next) {
+    g_things.find({ _id: req.params.id }).toArray(function (error, result) {
+        if (error || !result) return next(new HttpError(500, error));
+
+        console.log('done', result);
+
+        next(new HttpSuccess(200, { thing: result }));
+    });
+}
+
+function add(req, res, next) {
+    console.log('add', req.body);
+
+    var doc = {
+        content: req.body.content,
+        createdAt: new Date()
+    };
+
+    g_things.insertMany([doc], function (error, result) {
+        if (error || !result) return next(new HttpError(500, error));
+
+        console.log('done', result);
+
+        next(new HttpSuccess(201, { id: result._id }));
+    });
+}
+
+function del(req, res, next) {
+    console.log('del', req.params.id);
+
+    g_things.deleteOne({ _id: ObjectId(req.params.id) }, function (error, result) {
+        if (error || !result) return next(new HttpError(500, error));
+
+        console.log('done', result.result);
+
+        next(new HttpSuccess(200, {}));
+    });
+}