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

Night #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.
3 changes: 1 addition & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var passport = require('passport');
var LocalStrategy = require('passport-local');
var mongoose = require('mongoose');
var connect = process.env.MONGODB_URI;

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

REQUIRED_ENV.forEach(function(el) {
Expand All @@ -23,7 +23,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
20 changes: 15 additions & 5 deletions models.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
username: String,
password: String,
phone: String
email: String,
phone: String,
booksOwned: Array
});



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({
owner: String,
title: String,
author: String,
department: String,
price: Number

});
const Book = mongoose.model('Book', bookSchema);

module.exports = {
User:User
User: User,
Book: Book
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "doublemessage",
"name": "bookswap",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node app.js"
"start": "nodemon app.js"
},
"dependencies": {
"body-parser": "~1.13.2",
Expand Down
7 changes: 6 additions & 1 deletion routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ module.exports = function(passport) {
error: "Passwords don't match."
});
}
if (req.body.email.substr(req.body.email.length-3) !== 'edu') {
return res.render('signup', {
error: "Please enter a valid .edu email address."
})
}
var u = new models.User({
username: req.body.username,
password: req.body.password
Expand All @@ -39,7 +44,7 @@ module.exports = function(passport) {

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

Expand Down
38 changes: 36 additions & 2 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var express = require('express');
var router = express.Router();
var models = require('../models');
var User = models.User;
var Book = models.Book;

//////////////////////////////// PUBLIC ROUTES ////////////////////////////////
// Users who are not logged in can see these routes
Expand All @@ -22,11 +23,44 @@ router.use(function(req, res, next){

//////////////////////////////// PRIVATE ROUTES ////////////////////////////////
// Only logged in users can see these routes
//what we have to do is .populate the books and the users.
//then we have to do below except books: books
// User.find(function(err, users) {
// if (err) return next(err);
// res.render('users', {
// users: users
// });
// });
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.get('/protected', function(req, res, next) {
res.render('protectedRoute', {
router.get('/addbook', function(req, res, next) {
res.render('addbook', {
username: req.user.username,
//books: books
});
});
//add this book to the booksowned array on the User model???
router.post('/addbook', function(req, res, next) {
var book = new Book({
title: req.body.title,
author: req.body.author,
department: req.body.department,
price: req.body.price,
owner: req.user._id
});
book.save(function(err) {
if (err) return next(err);
res.redirect('/profile');
})
});

///////////////////////////// END OF PRIVATE ROUTES /////////////////////////////
Expand Down
23 changes: 23 additions & 0 deletions views/addbook.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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>Department</label>
<input type="text" name="department" class="form-control">
</div>
<div class="form-group">
<label>Price</label>
<input type="number" name="price" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success">Send</button>
</div>
</form>
79 changes: 76 additions & 3 deletions views/home.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
<h3>Home</h3>
<p>Everyone visiting your site will be able to see this page, no login required</p>
<h2>BOOKSWAP</h2>
<div>
<p>A marketplace for students to buy/sell textbooks.</p>
</div>
<br/>
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" aria-label="..." placeholder="Search for a book...">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Department <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right">
<!-- do a forEach through departments passed in to component. might not want to make these links. -->
<li><a href="#">Computer Science</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
<br/>

<div class="row">
<div class="col-sm-6">
<div class="panel panel-info ">
<div class="panel-heading"><h4>For buyers: </h4></div>
<table class="table table-striped">
<tbody>
<tr>
<td>1. Register and login to search for a book</td>
</tr>
<tr>
<td>2. Contact seller</td>
</tr>
<tr>
<td>3. Negotiate and settle on price with seller</td>
</tr>
<tr>
<td>4. Decide where/when to meet seller</td>
</tr>
<tr>
<td>5. Pick up your book!</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-info">
<div class="panel-heading"><h4>For sellers: </h4></div>
<table class="table table-striped">
<tbody>
<tr>
<td>1. Register and login to put books up</td>
</tr>
<tr>
<td>2. Post book</td>
</tr>
<tr>
<td>3. Wait to be contacted</td>
</tr>
<tr>
<td>4. Meet and sell</td>
</tr>
<tr>
<td>5. Delete book from Bookswap</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>


<br />

<span>Visit a page that requires you to be logged in</span> <a href="/protected" class="btn btn-primary">Here</a>

<span>Visit a page that requires you to be logged in</span> <a href="/profile" class="btn btn-primary">Here</a>
10 changes: 5 additions & 5 deletions views/layouts/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>TITLE OF YOUR APP HERE</title>
<title>BookSwap</title>

<!-- Latest compiled and minified CSS -->
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,600|Titillium+Web:400,600' rel='stylesheet' type='text/css'>
Expand All @@ -25,7 +25,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">YOUR NAME HERE</a>
<a class="navbar-brand" href="/">BookSwap</a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
Expand All @@ -35,8 +35,8 @@
</ul>

<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="/login">Login</a></li>
<li><a href="/signup">Register</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
Expand All @@ -57,7 +57,7 @@

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="/scripts/script.js"></script>
Expand Down
27 changes: 27 additions & 0 deletions views/profile.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h3>{{username}} Profile</h3>

<h3>Books</h3>
<table class="table table-hover">
<thead>
<td><b>Title</b></td>
<td><b>Author</b></td>
<td><b>Department</b></td>
<td><b>Price</b></td>
</thead>
<tbody>
{{#each books}}
<tr>
<td>{{title}}</td>
<td>{{author}}</td>
<td>{{department}}</td>
<td>{{price}}</td>
<td><a class="btn btn-default" href="/addbook{{_id}}">Remove Book</a></td>
</tr>
{{/each}}
</tbody>
</table>


<br />
<a href="/addbook" class="btn btn-primary">Add Book</a>
<a href="/logout" class="btn btn-primary">Logout</a>
4 changes: 4 additions & 0 deletions views/signup.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
{{/if}}
<form method="POST" style="max-width: 300px; margin: auto; margin-top: 50px; border: 0.5px solid gray; padding: 30px 30px 30px 30px">
<h3>Register</h3>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="form-control">
Expand Down