Skip to content
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
1 change: 1 addition & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is my documentation!
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, OK! But your documentation should be in your README.md file.

20 changes: 20 additions & 0 deletions controllers/apiController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function index(req, res) {
res.json({
message: 'Welcome to Cliffly!',
documentation_url: 'https://github.com/MaluPalu/express-personal-api/blob/master/DOCUMENTATION.md',
base_url: 'https://evening-beach-72520.herokuapp.com/',
endpoints: [
{method: "GET", path: "/api", description: "Describes all available endpoints"},
{method: "GET", path: "/api/profile", description: "Describes all available endpoints"},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your profile doesn't describe available endpoints, it describes you!

{method: "GET", path: "/api/cliffs", description: "List Cliff jumping specifiations"},
{method: "POST", path: "/api/cliffs", description: "Add Cliff jumping specifiations"},
{method: "PUT", path: "/api/cliffs/:id", description: "Update Cliff jumping specifiations"},
{method: "DELETE", path: "/api/cliffs/:id", description: "Delete Cliff jumping specifiations"},
{method: "GET", path: "/api/cliffs/:id", description: "Individual Cliff jumping specifiations"}
]
});
}

module.exports = {
index: index
}
95 changes: 95 additions & 0 deletions controllers/cliffController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
let db = require('../models');
// controllers/cliffsController.js
// GET /api/cliffs
function index(req, res) {
// send back all cliffs as JSON
db.Cliff.find(function(err, albums){
if (err) {
console.log("index error" + err);
res.sendStatus(500);
}
res.send(albums);
});
}

// POST /api/cliffs
function create(req, res) {
//create cliff using form data from req parameter
var newCliff = new db.Cliff({
name: req.body.cliffName,
nearestCity: req.body.nearestCity,
gpsCoords: req.body.gpsCoords,
height: req.body.height,
accessibility: req.body.accessibility,
description: req.body.description
});
// add that cliff to the database
newCliff.save(function(err, cliff){
if (err) {
console.log("create error: " + err);
return;
}
console.log("created", cliff.name);
res.json(cliff);
});
}

// GET /api/cliffs/:cliffId
function retrieve(req, res) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused function. you can get rid of this.

}

// DELETE /api/cliffs/:cliffId
function destroy(req, res) {
console.log(req.params.id);
db.Cliff.findByIdAndRemove(req.params.id, (err, Cliff) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, you'd use lower-case cliff for the parameter name.

console.log(Cliff);
if (err) {
console.log(err);
}
Cliff.save(function(err, cliff) {
console.log("hi");
if (err) {
console.log(err);
}
let response = {
message: "Cliff successfully deleted",
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation 😭

res.status(200).send(response);
});
});
};


function update(req, res) {
db.Cliff.findById(req.params.id, function(err, foundCliff) {
if (err) {
console.log('err', err);
return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if there's an error, you should send a response.

}
foundCliff.set({
name: req.body.cliffName,
nearestCity: req.body.nearestCity,
gpsCoords: req.body.gpsCoords,
height: req.body.height,
acessibility: req.body.accessibility,
description: req.body.description,
});
foundCliff.save(function(err, updateCliff) {
if (err) {
console.log(err);
}
console.log('update cliff', updateCliff);
res.json(updateCliff);
});
});
};



module.exports = {
index: index,
create: create,
retrieve: retrieve,
destroy: destroy,
update: update
};
4 changes: 4 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
api: require('./apiController'),
cliffs: require('./cliffController')
}
10 changes: 0 additions & 10 deletions models/campsite.js.example

This file was deleted.

15 changes: 15 additions & 0 deletions models/cliffs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var CliffSchema = new Schema({
name: String,
nearestCity: String,
gpsCoords: String,
height: String,
acessibility: String,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a c in accessibility--that's why it never saves correctly.

description: String
});

var Cliff = mongoose.model('Cliff', CliffSchema);

module.exports = Cliff;
2 changes: 1 addition & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ var mongoose = require("mongoose");
mongoose.connect( process.env.MONGODB_URI || "mongodb://localhost/personal-api", {useMongoClient: true});
mongoose.Promise = global.Promise; // use native Promise

// module.exports.Campsite = require("./campsite.js.example");
module.exports.Cliff = require("./cliffs.js");
Loading