Skip to content

Commit

Permalink
got first post note route working
Browse files Browse the repository at this point in the history
  • Loading branch information
beatface committed Mar 14, 2016
1 parent 3d47a66 commit 5246f85
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 35 deletions.
5 changes: 4 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"esversion": 6,
"strict": true,
"esnext": false,
"node": true
"node": true,
"predef": [
"app"
]
}
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"tests"
],
"dependencies": {
"angular": "^1.5.0"
"angular": "^1.5.0",
"angular-ui-router": "^0.2.18"
}
}
28 changes: 28 additions & 0 deletions controllers/notes_control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

const NoteModel = require('../models/notes_model');


module.exports.newNote = (req, res) => {
NoteModel.newNote(req, res);
};

module.exports.postNote = (req, res) => {
NoteModel.postNote(req, res);
};

module.exports.showNote = (req, res) => {
NoteModel.showNote(req, res);
};

module.exports.destroy = (req, res) => {
NoteModel.destroyNote(req, res);
};

module.exports.edit = (req, res) => {
NoteModel.edit(req, res);
};

module.exports.update = (req, res) => {
NoteModel.update(req, res);
};
18 changes: 18 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var gulp = require('gulp');
var electron = require('electron-connect').server.create();

gulp.task('serve', function () {

// Start browser process
electron.start();

// Restart browser process
gulp.watch('server.js', 'controllers/*.js', 'models/*.js', electron.restart);

// Reload renderer process
gulp.watch(['./public/**/*.js', './public/app.js', './public/**/*.html'], electron.reload);
});

gulp.task('default', ['serve']);
45 changes: 45 additions & 0 deletions models/notes_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

// set up sqlite schema
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./db/dashbored.sqlite');

// module.exports.model = Note;

module.exports.index = () => {
db.all(`SELECT Notes.title, Notes.content
FROM Notes JOIN UserData
ON Notes.userid = UserData.userid
WHERE Notes.userid = UserData.userid;`);
};

// module.exports.newNote = (req, res) => {
//
// };

module.exports.postNote = (req) => {
// console.log("the req is", req);
console.log(req.body);
// add the new note to the database
let title = req.body.title;
let note = req.body.content;
db.run(`INSERT INTO Notes VALUES (NULL, "${title}", "${note}", 1)`, (err) => {
if (err) throw err;
});
};

// module.exports.showNote = (req, res) => {
// // show a specific note
// };
//
// module.exports.destroyNote = (req, res) => {
// // remove specific note
// };
//
// module.exports.edit = (req, res) => {
// // edit specific note
// };
//
// module.exports.update = (req, res) => {
// // update specific note
// };
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"extends": "eslint:recommended",
"globals": {
"res": true,
"next": true
"next": true,
"app": true
},
"rules": {
"no-console": 0,
Expand All @@ -38,13 +39,15 @@
"homepage": "https://github.com/beatface/dashbored#readme",
"dependencies": {
"body-parser": "^1.15.0",
"electron-connect": "^0.3.7",
"electron-prebuilt": "^0.36.10",
"express": "^4.13.4",
"jade": "^1.11.0",
"node-sass-middleware": "^0.9.7"
"node-sass-middleware": "^0.9.7",
"sqlite3": "^3.1.1"
},
"devDependencies": {
"bower": "^1.7.7",
"nodemon": "^1.9.1"
"gulp": "^3.9.1"
}
}
13 changes: 13 additions & 0 deletions public/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
/* eslint-disable */
const app = angular.module("dashbored", ["ui.router"]);

app.config(function($stateProvider, $urlRouterProvider) {

$stateProvider
.state('index', {
url: "/",
templateUrl: "fe_partials/index.html"
});
$urlRouterProvider.otherwise("/");
});
29 changes: 29 additions & 0 deletions public/fe_controllers/notes_ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

app.controller('NotesCtrl', ['$scope', '$http', ($scope, $http) => {



$scope.getStock = (symbol) => {
// $scope.quoteExists = false; // resets ng-shows to hide
// $scope.noQuote = false;
$http.get(`/quotes/${symbol}`)
.then(function(data) {
!data.data.Name || data.status !== 200 // if stock doesn't exist or error occurs
? ($scope.noQuote = true, $scope.noQuoteMessage = data.data.Message || "An error occured. Try again.")
: ($scope.quoteExists = true, $scope.quoteData = data) ;
$scope.symbol = ""; // resets input field to blank
});
};

$scope.buyStock = () => {
// console.log('trying to buy');
// const totalValue = ($scope.quoteData.data.LastPrice * quantity).toFixed(2);
// $http.post(`/quotes/${quantity}/${$scope.quoteData.data.Symbol}/${$scope.quoteData.data.Name}/${$scope.quoteData.data.LastPrice}/${totalValue}`)
// .then(function(res) {
// console.log(res);
// $state.go('portfolio');
// });
};

}]);
4 changes: 4 additions & 0 deletions public/fe_partials/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<h1>Dashbored</h1>

</div>
17 changes: 17 additions & 0 deletions public/fe_partials/notes_widget/notes_add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div ng-controller="NotesCtrl">
<form method='POST', action='/notes'>
<div class="input-group">
<label for='title'> Title </label>
<input id="title" type='text', name='title'>
<br>
</div>
<div class="input-group">
<label for='content'> Note </label>
<input id="note" type='text' name='content'>
<br>
</div>
<div class="input-group">
<input class="btn" type='submit' value='Save'>
</div>
</form>
</div>
31 changes: 28 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,34 @@
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
res.send('This seems to be working!');
});
// const sqlite3 = require('sqlite3');
// const db = new sqlite3.Database('./db/dashbored.sqlite');

const notes = require('./notes_routes.js');

router.use(notes);

// router.get('/', (req, res) => {
// // db.run(`CREATE TABLE "Notes" (
// // "noteid" INTEGER NOT NULL,
// // "title" TEXT,
// // "content" TEXT,
// // "userid" INTEGER NOT NULL,
// // PRIMARY KEY(noteid)
// // );`);
// //
// // db.run(`CREATE TABLE "UserData" (
// // "UserId" INTEGER NOT NULL,
// // "Timezones" INTEGER,
// // "Weather" INTEGER,
// // "NoteId" INTEGER,
// // PRIMARY KEY(UserId)
// // );`);
// db.run(`INSERT INTO UserData VALUES (NULL, "timezone", "weather", 1)`, (err) => {
// if (err) throw err;
// });
// res.render('This seems to be working!');
// });


module.exports = router;
22 changes: 22 additions & 0 deletions routes/notes_routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

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

const ctrl = require('../controllers/notes_control');


router.get('/', (req, res) => {
res.render('template');
});
// router.get('/notes', (req, res) => {
//
// });
router.get('/notes/new', ctrl.newNote);
router.post('/notes', ctrl.postNote);
router.get('/notes/:id', ctrl.showNote);
router.delete('/notes/:id', ctrl.destroy);
router.get('/notes/:id/edit', ctrl.edit);
router.put('/notes/:id', ctrl.update);

module.exports = router;
33 changes: 9 additions & 24 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const electron = require('electron');
const electronApp = electron.app;
const BrowserWindow = electron.BrowserWindow;
const client = require('electron-connect').client;
const bodyParser = require('body-parser');

const express = require('express');
const path = require('path');
Expand All @@ -24,6 +26,9 @@ electronApp.on('window-all-closed', function() {
electronApp.quit();
}
});
app.use(bodyParser.urlencoded({
extended: false
}));

app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
Expand All @@ -40,30 +45,8 @@ electronApp.on('ready', function() {
webPreferences: {
nodeIntegration: false
},
width: 840,
height: 600
});

// development error handler
// will print stacktrace
if (process.env.NODE_ENV === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
width: 1000,
height: 800
});

app.listen(PORT, () => {
Expand All @@ -73,6 +56,8 @@ electronApp.on('ready', function() {
mainWindow.webContents.openDevTools();
});

client.create(mainWindow);

// closes server when window is closed
mainWindow.on('closed', function() {
mainWindow = null;
Expand Down
15 changes: 12 additions & 3 deletions views/template.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ doctype html
html(lang="en")
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
link(rel='stylesheet' href='styles/styles.css')
//- script(src="/components/jquery/dist/jquery.min.js")
body
block content
body(ng-app="dashbored")
div(ui-view)
section(ng-include="'fe_partials/notes_widget/notes_add.html'")
//- section(ng-include="fe_partials/topics.html")
script require('electron-connect').client.create()
script(src='lib/angular/angular.min.js')
script(src='lib/angular-ui-router/release/angular-ui-router.min.js')
script(src='app.js')
//- Controllers
script(src='fe_controllers/notes_ctrl.js')

0 comments on commit 5246f85

Please sign in to comment.