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

Follow along in class #10

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ You will learn how to develop RESTful APIs with Node.js, and Express.

Implement all steps needed to build a secure API.

## In Class

Replicate the product.js
This file will be named 'logo.js' with all of the CRUD features.

21 changes: 21 additions & 0 deletions index.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Test the home page endpoint

get http://localhost:3001

### Test a 404
get http://localhost:3001/test-404

### Test creating a product
POST http://localhost:3001/
Content-Type: application/json
{
"title": "Golf shirts"
}


### Test the PUT which should change a product
PUT http://localhost:3001/5f2b4ef8284b01dc2b0b12f5
Content-Type: application/json
{
"title": "Way Mo Better Shirt"
}
53 changes: 53 additions & 0 deletions src/database/logos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const {getDatabase} = require('./mongo-common');
// https://docs.mongodb.com/manual/reference/method/ObjectId/
const {ObjectID} = require('mongodb');

// a "collection" in mongo is a lot like a list which is a lot like an Array
const collectionName = 'logos';

async function createLogo(logo) {
const database = await getDatabase();
// for `insertOne` info, see https://docs.mongodb.com/manual/reference/method/js-collection/
const {insertedId} = await database.collection(collectionName).insertOne(logo);
return insertedId;
}

async function getLogos() {
const database = await getDatabase();
// `find` https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
return await database.collection(collectionName).find({}).toArray();
}

async function deleteLogo(id) {
const database = await getDatabase();
// https://docs.mongodb.com/manual/reference/method/ObjectId/
// for `deleteOne` info see https://docs.mongodb.com/manual/reference/method/js-collection/
await database.collection(collectionName).deleteOne({
_id: new ObjectID(id),
});
}

async function updateLogo(id, logo) {
const database = await getDatabase();

// `delete` is new to you. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
delete logo._id;

// https://docs.mongodb.com/manual/reference/method/db.collection.update/
await database.collection(collectionName).update(
{ _id: new ObjectID(id), },
{
$set: {
...logo,
},
},
);
}

// export the functions that can be used by the main app code
module.exports = {
createLogo,
getLogos,
deleteLogo,
updateLogo,
};