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

change titles to bookswap #2

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d9a382d
change titles to bookswap
Aug 1, 2017
6cf4f53
Merge pull request #1 from jklugherz/jklugherz
Aug 1, 2017
ab1c474
update readme
Aug 1, 2017
b11ada6
Merge pull request #2 from jklugherz/jklugherz
Aug 1, 2017
4f2bbee
add nodemon, edit README, finesse home page
Aug 2, 2017
68267f1
Merge pull request #3 from jklugherz/jklugherz
Aug 2, 2017
12cc24d
user profile
Aug 2, 2017
8191df4
Merge branch 'master' into night
Aug 2, 2017
f9c2379
Merge pull request #4 from jklugherz/night
Aug 2, 2017
89b3122
remove books
Aug 2, 2017
9bd8f80
Merge pull request #5 from jklugherz/remove
Aug 2, 2017
c9af4ed
search books route/view
Aug 2, 2017
659c48f
Merge branch 'master' of https://github.com/jklugherz/bookswap into j…
Aug 2, 2017
78cb002
Merge pull request #6 from jklugherz/jklugherz
Aug 2, 2017
44c49c5
departmnet
Aug 2, 2017
83456a4
ok
Aug 2, 2017
2074a33
Merge pull request #7 from jklugherz/lunch
Aug 2, 2017
2b2c2b3
fixing add book and profile
Aug 2, 2017
8611562
Merge pull request #8 from jklugherz/tinkering
Aug 2, 2017
125e71c
emails
Aug 2, 2017
3a75480
fixing search on home
Aug 2, 2017
b81a988
fix search bar conflict
Aug 2, 2017
860f237
Merge pull request #9 from jklugherz/jklugherz
Aug 2, 2017
bc493f0
last 3
Aug 2, 2017
d4c93fa
Merge pull request #10 from jklugherz/top-ten
Aug 2, 2017
181f8cf
regex for search
Aug 2, 2017
54ea0f9
Merge branch 'master' of https://github.com/jklugherz/bookswap into j…
Aug 2, 2017
1353267
Merge pull request #11 from jklugherz/jklugherz
Aug 2, 2017
f929aa3
search first word
Aug 3, 2017
0721c89
Merge pull request #12 from jklugherz/last
Aug 3, 2017
aac32cf
updated search |
Aug 3, 2017
485fb0c
Merge pull request #13 from jklugherz/google
Aug 3, 2017
6aaea5b
hashed passwords
Aug 3, 2017
9e67abd
Merge pull request #14 from jklugherz/hash
Aug 3, 2017
99d700d
searchresults
Aug 4, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# Horizons Hackathon Template.
# BookSwap
Julia Klugherz and Reed Feldman

## Steps 1: Get your project set-up
You have two ways of working on your project: Local and Gomix.
## Overview

### Local development
1. Clone the repo.
1. Create a `env.sh` file that contains:

```
export MONGODB_URI='YOUR URI';
export SECRET='YOUR SECRET'
```

1. Run `source .env`, and you are good to go!

### Glitch Development

1. Go to https://glitch.com/edit/#!/horizons-hackathon and click
`Remix this 🎤`
1. Select `.env` on the left panel, add your `MONGODB_URI` and `SECRET`
1. Click `Show` at the top to preview your app!
This is a simple template for a college textbook-exchange app.
18 changes: 12 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ var passport = require('passport');
var LocalStrategy = require('passport-local');
var mongoose = require('mongoose');
var connect = process.env.MONGODB_URI;
var _ = require('underscore');
var bcrypt = require('bcrypt');


//what is secret MONGODB_URI???
var REQUIRED_ENV = "SECRET MONGODB_URI".split(" ");

REQUIRED_ENV.forEach(function(el) {
Expand All @@ -23,7 +27,6 @@ REQUIRED_ENV.forEach(function(el) {
mongoose.connect(connect);

var models = require('./models');

var routes = require('./routes/routes');
var auth = require('./routes/auth');
var app = express();
Expand Down Expand Up @@ -75,14 +78,17 @@ passport.use(new LocalStrategy(function(username, password, done) {
return done(null, false, { message: 'Incorrect username.' });
}
// if passwords do not match, auth failed
if (user.password !== password) {
return done(null, false, { message: 'Incorrect password.' });
}
bcrypt.compare(password, user.password, function(err, res) {
// res == true
if (!res) {
done(null, false, { message: 'Incorrect password.' });
return;
}
// auth has has succeeded
return done(null, user);
});
}
));
})
}));

app.use('/', auth(passport));
app.use('/', routes);
Expand Down
35 changes: 35 additions & 0 deletions departments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var depts = [
'Africana Studies',
'Anthropology',
'Art',
'Astronomy',
'Biological Sciences',
'Chemistry',
'Classical Studies',
'Cognitive and Linguistic Science',
'Computer Science',
'East Asian Language and Cultures',
'Economics',
'Education',
'English',
'French',
'Geosciences',
'German',
'History',
'Italian Studies',
'Mathematics',
'Music',
'Philosophy',
'Physics',
'Political Science',
'Psychology',
'Religion',
'Russian Studies',
'Sociology',
'Spanish',
"Women's and Gender Studies"
];

module.exports = {
depts
}
20 changes: 16 additions & 4 deletions models.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
username: String,
password: String,
email: String,
phone: String
});



User = mongoose.model('User', userSchema);
//after we add a book, I need to add that book to the user scheme via helper function.
const bookSchema = mongoose.Schema({
email: String,
owner: {
ref: User,
type: mongoose.Schema.Types.ObjectId
},
title: String,
author: String,
department: String,
price: String,
});
const Book = mongoose.model('Book', bookSchema);

module.exports = {
User:User
User: User,
Book: Book
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "doublemessage",
"name": "bookswap",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node app.js"
"start": "nodemon app.js"
},
"dependencies": {
"bcrypt": "^1.0.2",
"body-parser": "~1.13.2",
"connect-mongo": "^1.3.2",
"cookie-parser": "~1.3.5",
Expand Down
42 changes: 29 additions & 13 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
var express = require('express');
var router = express.Router();
var models = require('../models');
var _ = require('underscore');
var bcrypt = require('bcrypt');


module.exports = function(passport) {

Expand All @@ -17,20 +20,33 @@ module.exports = function(passport) {
error: "Passwords don't match."
});
}
var u = new models.User({
username: req.body.username,
password: req.body.password
if (req.body.email.substr(req.body.email.length-3) !== 'edu') {
return res.render('signup', {
error: "Please enter a valid .edu email address."
})
}
//hash
var params = _.pick(req.body, ['username', 'password', 'email']);
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(params.password, salt, function(err, hash) {
// Store hash in your password DB.
params.password = hash;
Object.assign(params);
models.User.create(params, function(err, user) {
if (err) {
res.status(400).json({
success: false,
error: err.message
});
} else {
console.log(user);
res.redirect('/login')
}
});
});
u.save(function(err, user) {
if (err) {
console.log(err);
res.status(500).redirect('/register');
return;
}
console.log(user);
res.redirect('/login');
});
});
});


// GET Login page
router.get('/login', function(req, res) {
Expand All @@ -39,7 +55,7 @@ module.exports = function(passport) {

// POST Login page
router.post('/login', passport.authenticate('local',{
successRedirect: '/protected',
successRedirect: '/profile',
failureRedirect: '/login'
}));

Expand Down
109 changes: 105 additions & 4 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ var express = require('express');
var router = express.Router();
var models = require('../models');
var User = models.User;
var Book = models.Book;
var depts = require('../departments');

//////////////////////////////// PUBLIC ROUTES ////////////////////////////////
// Users who are not logged in can see these routes

router.get('/', function(req, res, next) {
res.render('home');
Book.find()
.limit(4)
.exec(function(err, books){
if (err) return next(err);
res.render('home', {
books: books
});
});
});


///////////////////////////// END OF PUBLIC ROUTES /////////////////////////////

router.use(function(req, res, next){
Expand All @@ -21,14 +31,105 @@ router.use(function(req, res, next){
});

//////////////////////////////// PRIVATE ROUTES ////////////////////////////////
// Only logged in users can see these routes
router.get('/profile', function(req, res, next) {
Book.find({owner: req.user._id})
.then((books) => {
res.render('profile', {
username: req.user.username,
books: books
})
})
});

router.post('/removebook/:id', function(req, res, next){
var bookId = req.params.id;
Book.findByIdAndRemove(bookId)
.then(() => {
Book.find({
owner: req.user._id})
.then((books) => {
res.render('profile', {
username: req.user.username,
books: books
})
})
})
})

router.get('/protected', function(req, res, next) {
res.render('protectedRoute', {

router.get('/addbook', function(req, res, next) {
res.render('addbook', {
username: req.user.username,
depts: depts.depts
});
});

router.post('/addbook', function(req, res, next) {
var book = new Book({
title: req.body.title,
author: req.body.author,
department: req.body.department,//jquery here??
price: req.body.price,
owner: req.user._id,
email: req.user.email
});
book.save(function(err) {
if (err) return next(err);
res.redirect('/profile');
})
});

router.get('/users', function(req, res, next) {
User.find(function(err, users) {
if (err) return next(err);
res.render('users', {
users: users
});
});
});

router.get('/books', function(req, res, next){
Book.find(function(err, books){
if (err) return next(err);
res.render('books', {
books: books
});
});
});

router.post('/searchresults', function(req, res, next) {
var title = req.body.titleinput;
var or = title.split(" ").join('|');
Book.find({ title: new RegExp(or, "i") })
.then((books) => {
res.render('searchresults', {
books: books,
title: title
});
})
});

router.post('/contactseller/:id', function(req, res, next) {
var bookId = req.params.id;

var book = Book.findById(bookId);
var bookOwner = book.then(book => User.findById(book.owner));
var currentUser = User.findById(req.user._id);

Promise.all([book, bookOwner, currentUser]).then(
arrOfThings => {
var b = arrOfThings[0]
var bo = arrOfThings[1]
var cu = arrOfThings[2]
res.render('searchresults', {
book: b,
bookOwner: bo,
currentUser: cu
})
}
)
})

///////////////////////////// END OF PRIVATE ROUTES /////////////////////////////

module.exports = router;
31 changes: 31 additions & 0 deletions views/addbook.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1>Post A Book</h1>

<form action="/addbook" method="POST" >
<div class="form-group">
<label>Book Title</label>
<input type="text" name="title" class="form-control">
</div>
<div class="form-group">
<label>Author</label>
<input type="text" name="author" class="form-control">
</div>
<div class="form-group">
<label for="sel1">Department</label>
<select class="form-control" id="sel1" name="department">
{{#each depts}}
<option>{{this}}</option>
{{/each}}
</select>
</div>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" name="price">
<span class="input-group-addon">.00</span>
</div>
<br />
<div class="form-group">
<button class="btn btn-success">Send</button>
</div>
</form>

<!-- make deparmnte dropdonpm wn -->
Loading