Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W03 D05 #7

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions models/favorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var FavoriteSchema = new Schema({
description: String,
type: String,
condition: String
});

var Favorite = mongoose.model('Favorite', FavoriteSchema);
module.exports = Favorite;
3 changes: 3 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ mongoose.connect( process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
"mongodb://localhost/personal-api");

module.exports.Favorite = require("./favorites.js");
module.exports.Profile = require("./profile.js");

// module.exports.Campsite = require("./campsite.js.example");
12 changes: 12 additions & 0 deletions models/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var ProfileSchema = new Schema({
name: String,
github_link: String,
current_city: String,
image: String
});

var Profile = mongoose.model('Profile', ProfileSchema);
module.exports = Profile;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"homepage": "https://github.com/SF-WDI-LABS/express_self_api",
"dependencies": {
"body-parser": "^1.15.0",
"bower": "^1.5.2",
"express": "^4.13.4",
"mongoose": "^4.4.10"
"express-session": "^1.11.3",
"mongoose": "^4.4.10",
"nodemon": "^1.4.1"

}
}
Binary file added public/images/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 108 additions & 1 deletion public/scripts/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,114 @@
console.log("Sanity Check: JS is working!");
var template;
var $favTarget;
var allFavorites = [];
var $favDesc = $('#favoriteDescription');
var $favTyp = $('#favoriteType');
var $favCond = $('#favoriteCondition');

$(document).ready(function(){

// your code
$favTarget = $('#favoriteTarget');
var source = $('#favorite-template').html();
template = Handlebars.compile(source);

});
$.ajax({
method: 'GET',
url: '/api/favorites',
success: handleSuccess,
error: handleError
});

$.ajax({
method: 'GET',
url: '/api/profile',
success: profileSuccess,
error: profileError
});

//EVENT LISTENER FOR #NEWFAVORITEFORM
$('#newFavoriteForm').on('submit', function(e) {
e.preventDefault();
console.log('new favorite serialized', $(this).serialize());
$.ajax({
method: 'POST',
url: '/api/favorites',
data: $(this).serialize(),
success: newFavoriteSuccess,
error: newFavoriteError
});
});

//EVENT FOR DELETE
$favTarget.on('click', '.deleteBtn', function() {
console.log('clicked delete button to', '/api/favorite/'+$(this).attr('data-id'));
$.ajax({
method: 'DELETE',
url: '/api/favorites/'+$(this).attr('data-id'),
success: deleteFavoriteSuccess,
error: deleteFavoriteError
});
});




});//THIS CLOSES DOC.READY
function render() {
$favTarget.empty();
var favHtml = template({ favorites: allFavorites });
$favTarget.append(favHtml);
}


//HANDLE-SUCCESS/ERROR
function handleSuccess(json) {
allFavorites = json;
render();
}
function handleError() {
console.log('error loading favorites');
$('#favoriteTarget').text('Failed to load favorites');
}

//PROFILE-SUCCESS/ERROR
function profileSuccess(json) {
console.log(json);
$('ul').append('<li><strong>Name:</strong> '+json.name+'</li>');
$('ul').append('<li><strong>Github Link:</strong> '+'<a href="'+json.github_link+'">http://github/PJC-1</a>'+'</li>');
$('ul').append('<li><strong>Current City:</strong> '+json.current_city+'</li>');
$('ul').append('<img src="'+json.image+'">');
}
function profileError() {
console.log('error with the profile');
}

//NEWFAVORITE-SUCCESS/ERROR
function newFavoriteSuccess(json) {
$('#newFavoriteForm input').val('');
allFavorites.unshift(json);
render();
}
function newFavoriteError() {
console.log('new favorite error');
}

//DELETE-FAVORITE
function deleteFavoriteSuccess(json) {
var favorite = json;
console.log(json);
var favoriteId = favorite._id;
console.log('delete favorite', favorite);

for(var index = 0; index < allFavorites.length; index++) {
if(allFavorites[index]._id === favoriteId) {
allFavorites.splice(index, 1);
break;
}
}
render();
}
function deleteFavoriteError() {
console.log('delete favorite error');
}
10 changes: 9 additions & 1 deletion public/styles/styles.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
body {
color: #333;
font-family: Helvetica, Arial, sans-serif;
background-color: skyblue; /* Sanity Check! */
font-size: 15px;
background-color: solid white; /* Sanity Check! */
}

h1 {
margin-top: 100px;
text-align: center;
}

li {
list-style-type: none;
backgroud: #efefef;
padding: 8px;
margin-bottom: 8px;
}
59 changes: 58 additions & 1 deletion seed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
// This file allows us to seed our application with data
// simply run: `node seed.js` from the root of this project folder.

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

var profileObj = [
{
name: "Phill Cheng",
github_link: "https://github.com/PJC-1",
current_city: "Walnut Creek",
image: "/images/image.jpg"
}
];

// db.Profile.remove({}, function(err, removed) {
// console.log('removed profile');
db.Profile.create(profileObj, function(err, prof){
if (err) {
return console.log("error:", err);
}
console.log("Created profile",prof);
});

var favorite_list = [
{
description: "Barbour Classic-Bedale",
type: "Jacket",
condition: "Very good"
},
{
description: "Lucky Red Hat",
type: "Baseball Hat",
condition: "Used"
},
{
description: "Front Tooth",
type: "Teeth",
condition: "Chipped"
},
{
description: "Cosby Sweater no.5",
type: "Embarrassing",
condition: "NEW with Tags"
},
{
description: "Harvery's 10oz. Tiki-Mug",
type: "Drinkware",
condition: "Very good"
}
];

db.Favorite.remove({}, function(err, removed) {
console.log('removed all favorites');
db.Favorite.create(favorite_list, function(err, favorite){
if (err){
return console.log("Error:", err);
}
console.log("Created new favorite", favorite._id);
process.exit();
});
});

// var new_campsite = {description: "Sharp rocks. Middle of nowhere."}

Expand Down
72 changes: 65 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ var express = require('express'),
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
/************
* DATABASE *
************/

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

/**********
* ROUTES *
Expand All @@ -37,18 +42,71 @@ app.get('/', function homepage(req, res) {
app.get('/api', function api_index(req, res) {
// TODO: Document all your api endpoints below
res.json({
woops_i_has_forgot_to_document_all_my_endpoints: true, // CHANGE ME ;)
message: "Welcome to my personal api! Here's what you need to know!",
documentation_url: "https://github.com/example-username/express_self_api/README.md", // CHANGE ME
base_url: "http://YOUR-APP-NAME.herokuapp.com", // CHANGE ME
documentation_url: "https://github.com/PJC-1/express-personal-api/README.md", // CHANGE ME
base_url: "http://lychee=tart-68743.herokuapp.com", // CHANGE ME
endpoints: [
{method: "GET", path: "/api", description: "Describes all available endpoints"},
{method: "GET", path: "/api/profile", description: "Data about me"}, // CHANGE ME
{method: "POST", path: "/api/campsites", description: "E.g. Create a new campsite"} // CHANGE ME
{method: "GET", path: "/api/profile", description: "Data about me"},
{method: "GET", path: "/api/favorites", description: "get all favorites"},
{method: "GET", path: "/api/favorites/:id", description: "get one favorite"},
{method: "POST", path: "/api/favorites", description: "Create a new favorite"},
{method: "DELETE", path: "/api/favorites/:id", description: "Delete one favorite"},
{method: "PUT", path: "/api/favorites/:id", description: "Update one favorite"}
//{method: "POST", path: "/api/campsites", description: "E.g. Create a new campsite"} // CHANGE ME
]
})
});
});

//PROFILE
app.get('/api/profile', function (req, res) {
res.json({
name: "Phill Cheng",
github_link: "https://github.com/PJC-1",
current_city: "Walnut Creek",
image: "/images/image.jpg"
});
});

//ALL-FAVORITES
app.get('/api/favorites', function (req, res) {
db.Favorite.find()
.exec(function(err, data) {
if (err) { return console.log("index error fav: " + err); }
res.json(data);
});
});

//CREATE-NEW-FAVORITE
app.post('/api/favorites', function (req, res) {
var newFavorite = new db.Favorite({
description: req.body.description,
type: req.body.type,
condition: req.body.condition
});
newFavorite.save(function(err, fav){
if (err) {
return console.log("save error: " + err);
} else {
console.log("saved ", fav);
res.json(fav);
}
});
});

//DELETE-FAVORITE
app.delete('/api/favorites/:id', function (req, res) {
console.log('favorites delete', req.params);
var favoriteId = req.params.id;
db.Favorite.findOneAndRemove({ _id: favoriteId }, function (err, deletedFavorite) {
res.json(deletedFavorite);
});
});

//UPDATE-FAVORITE ***ADD-HERE***



/**********
* SERVER *
**********/
Expand Down
Loading