-
Notifications
You must be signed in to change notification settings - Fork 54
Jevell's personal API #36
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| **/node_modules | ||
|
|
||
| DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| function index(req, res) { | ||
| res.json({ | ||
| message: "Welcome to dogShopz!", | ||
| documentation_url: "https://agile-wildwood-44374.herokuapp.com/", | ||
| base_url: "localhost:3000", | ||
| endpoints: [ | ||
| { | ||
| method: "GET", path: "/api", description: "Describes available endpoints" | ||
| } | ||
| ] | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| module.exports = { | ||
| index: index | ||
| } | ||
|
|
||
|
|
||
| // app.get('/api', function apiIndex(req, res) { | ||
| // // TODO: Document all your api endpoints below as a simple hardcoded JSON object. | ||
| // // It would be seriously overkill to save any of this to your database. | ||
| // // But you should change almost every line of this response. | ||
| // res.json({ | ||
| // // woopsIForgotToDocumentAllMyEndpoints: true, // CHANGE ME ;) | ||
| // message: "Welcome to my personal api! Here's what you need to know!", | ||
| // documentationUrl: "https://github.com/example-username/express-personal-api/README.md", // CHANGE ME | ||
| // baseUrl: "localhost:3000", | ||
| // 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 | ||
| // ] | ||
| // }) | ||
| // }); | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module.exports = { | ||
| api: require('./apiController'), | ||
| shops: require('./shopsController'), | ||
| // albumsSongs: require('./albumsSongscontroller') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadness. Make sure delete commented out code - especially if it's pasted from another project. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // shopsSongsController | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you use this file - there is no route to it? Maybe this should be deleted. Also again, if you're cutting and pasting from another project make sure you remove the comments (or change them) in the current project. |
||
| var db = require('../models'); | ||
|
|
||
|
|
||
| // POST '/api/shops/:shopId/songs' | ||
| function create(req, res) { | ||
| db.Shop.findById(req.params.shopId, function(err, foundShop) { | ||
| console.log(req.body); | ||
| var newShop = new db.Shop(req.body); // dangerous, in a real app we'd validate the incoming data | ||
| foundShop.shops.push(newShop); | ||
| foundShop.save(function(err, savedShop) { | ||
| console.log('newShop created: ', newShop); | ||
| res.json(newShop); // responding with just the song, some APIs may respond with the parent object (Album in this case) | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| module.exports = { | ||
| create: create | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //Database | ||
|
|
||
| var db = require('../models'); | ||
|
|
||
| // GET /api/shops | ||
| function index(req, res) { | ||
| // send back all shops as JSON | ||
| db.Shop.find({}, function(err, allShops) { | ||
| res.json(allShops); | ||
| }); | ||
| } | ||
|
|
||
| // POST /api/shops | ||
| function create(req, res) { | ||
| // create an shop based on request body and send it back as JSON | ||
| console.log('body', req.body); | ||
|
|
||
| // split at comma and remove and trailing space | ||
|
|
||
| db.Shop.create(req.body, function(err, shop) { | ||
| if (err) { console.log('error', err); } | ||
| console.log(shop); | ||
| res.json(shop); | ||
| }); | ||
| } | ||
|
|
||
| // GET /api/albums/:shopId | ||
| function show(req, res) { | ||
| // find one album by id and send it back as JSON | ||
| db.Show.findById(req.params.showId, function(err, foundShop) { | ||
| if(err) { console.log('albumsController.show error', err); } | ||
| console.log('shopsController.show responding with', foundShop); | ||
| res.json(foundShop); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| // DELETE /api/albums/:albumId | ||
| function destroy(req, res) { | ||
| console.log(req.params.shopId) | ||
| // find one album by id, delete it, and send it back as JSON | ||
| db.Shop.findByIdAndRemove(req.params.shopId, function(err, deleteShop) { | ||
| if(err) { console.log('albumsController.destroy error', err); } | ||
| console.log('albumsController.destroy responding with', deleteShop); | ||
| res.json(deleteShop); | ||
| }); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your code style is just as important as the code itself. Moving forward we're going to be more focused on ensuring you're writing with the industry best practices. This many spaces between functions is not best practice. |
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // export public methods here | ||
| module.exports = { | ||
| index: index, | ||
| create: create, | ||
| show: show, | ||
| destroy: destroy, | ||
| // update: update | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| var mongoose = require("mongoose"); | ||
| mongoose.connect( process.env.MONGODB_URI || "mongodb://localhost/personal-api", {useMongoClient: true}); | ||
| mongoose.connect( process.env.MONGODB_URI || "mongodb://localhost/dogs", {useMongoClient: true}); | ||
| mongoose.Promise = global.Promise; // use native Promise | ||
|
|
||
| // module.exports.Campsite = require("./campsite.js.example"); | ||
|
|
||
|
|
||
| var Shop = require('./shop'); | ||
|
|
||
| module.exports.Shop = Shop; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| var mongoose = require("mongoose"); | ||
| var Schema = mongoose.Schema; | ||
|
|
||
|
|
||
| var Shop = require('./shop'); | ||
|
|
||
| var ShopSchema = new Schema({ | ||
| shopName: String, | ||
| careTakerType: String, | ||
| address: String, | ||
| phoneNumber: String , | ||
| website: String, | ||
| image: String, | ||
| }); | ||
|
|
||
| var Shop = mongoose.model('Shop', ShopSchema); | ||
|
|
||
| module.exports = Shop; | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,176 @@ | ||
| console.log("Sanity Check: JS is working!"); | ||
|
|
||
| $(document).ready(function(){ | ||
| //initialize all modals | ||
| $('.modal').modal(); | ||
| $('select').material_select(); | ||
|
|
||
| $.ajax({ | ||
| method: 'GET', | ||
| url: '/api/shops', | ||
| success: renderAllShops | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| $('#shop-form form').on('submit', function(e) { | ||
| e.preventDefault(); | ||
| var formData = $(this).serialize(); | ||
| console.log('formData', formData); | ||
| $.post('/api/shops', formData, function(shop) { | ||
| console.log('shop after POST', shop); | ||
| renderShop(shop); //render the server's response | ||
| }); | ||
| $(this).trigger("reset"); | ||
| }); | ||
|
|
||
| // catch and handle the click on an add song button | ||
| $('#modal1').on('click', '#submit', handleAddShopClick); | ||
| $('#submit').on('click', handleNewShopSubmit); | ||
| $('#shops').on('click','.delete-shop', handleDeleteShopClick); | ||
|
|
||
|
|
||
|
|
||
| // your code | ||
|
|
||
| }); | ||
|
|
||
|
|
||
|
|
||
| function renderAllShops (shops) { | ||
|
|
||
| shops.forEach(function (shop) { | ||
| console.log(shops) | ||
| renderShop(shop); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // render shop function | ||
| function renderShop (shop) { | ||
| console.log('Rending these damn pet shops', shop) | ||
|
|
||
| var shopsHTML = (` | ||
| <div class="card card-shop" data-shop-id = "${shop._id}"> | ||
| <div class="card-image waves-effect waves-block waves-light"> | ||
| <img class="activator" src="${shop.image}"> | ||
| </div> | ||
| <div class="card-content"> | ||
| <span class="card-title activator grey-text text-darken-4">${shop.shopName}<i class="material-icons right">${shop.careTakerType}</i></span> | ||
| <p><a href="#">${shop.website}</a></p> | ||
|
|
||
| </div> | ||
| <div class="card-reveal"> | ||
| <span class="card-title grey-text text-darken-4">${shop.address}<i class="material-icons right">${shop.phoneNumber}</i></span> | ||
| <p>Here is some more information about this product that is only revealed once clicked on.</p> | ||
| </div> | ||
| <div class='card-footer hidden' > | ||
| <button class='btn btn-info edit-shop'>Edit Shop</button> | ||
| <button class='btn btn-danger delete-shop'>Delete</button> | ||
| </div> | ||
| </div>`); | ||
| $('#shops').prepend(shopsHTML) | ||
|
|
||
| }; | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lots of spaces... are these needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No they're not Carlynn. Jevell tighten this code up! |
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| function handleAddShopClick(e) { | ||
| console.log('add-shop clicked!'); | ||
| var currentShopId = $(this).closest('.card-shop').data('shop-id'); // "5665ff1678209c64e51b4e7b" | ||
| console.log('id',currentShopId); | ||
| $('#modal1').data('shop-id', currentShopId); | ||
| $('#modal1').modal(); // display the modal! | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| function handleDeleteShopClick(e) { | ||
| console.log('store deleted!'); | ||
| var shop = $(this).closest('.card-shop'); | ||
| console.log(shop); | ||
| var shopId = shop.data('shop-id') | ||
| console.log('id', shopId); | ||
| $.ajax ({ | ||
| method: 'DELETE', | ||
| url: '/api/shops/' + shopId, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try editing the url. Instead of shopId use $(this).attr('data-id') |
||
| success: function() { | ||
| shop.remove() | ||
| } | ||
|
|
||
| }) | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // when the song modal submit button is clicked: | ||
| function handleNewShopSubmit(e) { | ||
| e.preventDefault(); | ||
| var $modal = $('#modal1'); | ||
| var $NameField = $modal.find('#name'); | ||
| var $Address = $modal.find('#textarea1'); | ||
| var $telephone = $modal.find('#tel'); | ||
| var $website = $modal.find('#url'); | ||
| // get data from modal fields | ||
| // note the server expects the keys to be 'name', 'trackNumber' so we use those. | ||
| var dataToPost = { | ||
| name: $NameField.val(), | ||
| address: $Address.val(), | ||
| phoneNumber: $telephone.val(), | ||
| website: $website.val(), | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| // POST to SERVER | ||
| var shopPostToServerUrl = '/api/shops/' ; | ||
| $.post(shopPostToServerUrl, dataToPost, function(data) { | ||
| console.log('received data from post to /shops:', data); | ||
|
|
||
| // clear form | ||
| $NameField.val(''); | ||
| $Address.val(''); | ||
| $telephone.val(''); | ||
| $website.val(''); | ||
| // close modal | ||
|
|
||
| // update the correct shop to show the new song | ||
| // re-render it with the new shop data (including songs) | ||
| renderShop(data); | ||
| }).fail(function(err) { | ||
| console.log('post to /api/shops/:shopId/shops resulted in error', err); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File looks good!
Once a file is complete, remove unused pseudocode and delete extra spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Carlynn.