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
19 changes: 19 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": [
"airbnb-base"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
}
}
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node app.js
79 changes: 49 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,69 @@
# Unit 7 Problem Set #3

## Schema Design and Building RESTful APIs

### Directions

1. Fork and clone this lab.
2. Answer the following questions directly in this README. Be sure that your answers are well-formatted.
3. For the final question (build a to-do list), create all of your necessary app files in this directory. (I have included GitHub's standard Node.js `.gitignore` template so that you don't end up pushing `node_modules` to GitHub.
2. Answer the following questions directly in this README. Be sure that your answers are well-formatted.
3. For the final question (build a to-do list), create all of your necessary app files in this directory. (I have included GitHub's standard Node.js `.gitignore` template so that you don't end up pushing `node_modules` to GitHub.

##
4. **What are the four types of HTTP requests that correspond to _creating_, _reading_, _updating_, and _deleting_ resources? Why is it important to use these different types of requests?**<br>

0. **What are the four types of HTTP requests that correspond to _creating_, _reading_, _updating_, and _deleting_ resources? Why is it important to use these different types of requests?**
<br>
- The four different HTTP requests are:

- **GET**
- **POST**
- **PUT**
- **DELETE**

- These different requests allow users to use the api to do different things with the data. GET requests allow a user to get information from the api they need. POST requests allow users to post data to the api. PUT requests, update data, and DELETE requests delete data from the api.

1. **You've recently learned about an API for a cat sitting company. The API is fully RESTful for a resource called `cats`. You also happen to know that their database is running Postgresql on the backend. What are the five types of requests the API will respond to? Based on the description, list the HTTP method, the path, and what SQL the request will fire.**
5. **You've recently learned about an API for a cat sitting company. The API is fully RESTful for a resource called `cats`. You also happen to know that their database is running Postgresql on the backend. What are the five types of requests the API will respond to? Based on the description, list the HTTP method, the path, and what SQL the request will fire.**

| http method | path | sql | description |
|---|---|---|---|
| | | | Creating a cat |
| | | | Retrieving all the cats |
| | | | Retriving a specific cat |
| | | | Updating a specific cat |
| | | | Deleting a cat |
http method | path | sql | description
-------------- | ----------- | ----------------------------------------------------------------------------------- | ------------------------
POST request | '/cats' | 'INSERT INTO cats(column1, column2) VLAUES(value1, vlaue2);' | Creating a cat
GET request | '/cats' | 'SELECT * FROM cats;' | Retrieving all the cats
GET request | '/cats/:id' | 'SELECT * FROM cats WHERE id = $1;', [id] | Retriving a specific cat
PUT request | 'cats/:id' | 'UPDATE cats SET(column1, column2) = ($2, $3) WHERE id = $1;', [id, value1, value2] | Updating a specific cat
DELETE request | 'cats/:id' | DELETE FROM cats WHERE id = $1;', [id] | Deleting a cat

<br>

2. **You're working on a blogging application and doing some debugging. You see in the logs that the following SQL has fired:**
1. **You're working on a blogging application and doing some debugging. You see in the logs that the following SQL has fired:**

```sql
SELECT * FROM articles WHERE id = 9;
```
```sql
SELECT * FROM articles WHERE id = 9;
```

**Given that the application is RESTful, what HTTP method and request would you expect to have been made to fire that SQL?**
<br>
**Given that the application is RESTful, what HTTP method and request would you expect to have been made to fire that SQL?**<br>

- I expect that a GET request was made to return all information for an article that had an id of 9.

3. **You've been hired to do some work for Discogs, an application to help users track a vinyl record collection. A `Collection` has many `Albums`, and an `Album` has many collections via a join table called `Ownership`. You've been asked to build a feature that allows to remove an album from their collection. What HTTP Method/URL/controller action would you use to implement this feature?**
<br>
2. **You've been hired to do some work for Discogs, an application to help users track a vinyl record collection. A `Collection` has many `Albums`, and an `Album` has many collections via a join table called `Ownership`. You've been asked to build a feature that allows to remove an album from their collection. What HTTP Method/URL/controller action would you use to implement this feature?**<br>

4. **Choose your favorite web application. What's an example of a one-to-many and many-to-many relationship that might exist within the app?**
<br>
- I would create a DELETE http request.

3. **Choose your favorite web application. What's an example of a one-to-many and many-to-many relationship that might exist within the app?**<br>

- **Twitter**
- On twitter there is a one to many relationship between posts and users

- For every post there is one user who created it, but a user can create multiple posts

- There is a many to many relationship between group chats and users

- Every group chat has multiple users, and a user can be apart of multiple group chats

4. **Build a full CRUD, RESTful API using Express for a Todo List. A TodoList should have many items and belong to a user. Each endpoint should respond with the appropriate JSON response. Our API should support:**

- **An index route to see a list of todos.**
- **A show route to see details about an individual todo item.**
- **The ability to update a todo (i.e. mark complete)**
- **Delete a todo item**
- **Create a Todo list item**

5. **Build a full CRUD, RESTful API using Express for a Todo List. A TodoList should have many items and belong to a user. Each endpoint should respond with the appropriate JSON response. Our API should support:**
* **An index route to see a list of todos.**
* **A show route to see details about an individual todo item.**
* **The ability to update a todo (i.e. mark complete)**
* **Delete a todo item**
* **Create a Todo list item**
**Deploy Your Project to Heroku and include a link here:**

**Deploy Your Project to Heroku and include a link here:**
![my app](https://sql-todo.herokuapp.com/tasks)
13 changes: 13 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express');

const app = express();
const port = process.env.PORT || 8080;
const bodyParser = require('body-parser');
const todoRouter = require('./routes/route');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(todoRouter);

app.listen(port, () => console.log(`Now listening on port ${port}...`));
78 changes: 78 additions & 0 deletions controllers/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const Task = require('../models/Task');

const createTask = (req, res) => {
const {
taskName,
taskDescription,
dateCreated,
dueDate,
isComplete,
} = req.body;

Task.createTask(taskName, taskDescription, dateCreated, dueDate, isComplete)
.then(() => Task.getLastCreated())
.then((data) => res.status(201).json(data.rows))
.catch(() => res.status(500).json({
error: '500: Internal Server Error',
}));
};

const getAllTasks = (req, res) => {
Task.getAllTasks()
.then((data) => res.json(data.rows))
.catch(() => res.status(500).json({
error: '500: Internal Server Error',
}));
};

const getTaskById = (req, res) => {
const { id } = req.params;
Task.getTaskById(id)
.then((data) => res.json(data.rows[0]))
.catch((err) => {
console.log(err);
res.status(500).json({
error: '500 Internal Server Error',
});
});
};

const updateTask = (req, res) => {
const { id } = req.params;
Task.updateTask(id)
.then((data) => {
console.log(data);
res.status(200).json({
message: 'Task Updated.',
});
})
.catch((err) => {
console.log(err);
res.status(500).json({
error: '500: Internal Server Error. Resource not Updated.',
});
});
};

const deleteTask = (req, res) => {
const { id } = req.params;
Task.deleteTask(id)
.then((data) => {
console.log(data);
res.status(204).json({ message: 'Successfully deleted Task.' });
})
.catch((err) => {
console.log(err);
res.status(500).json({
error: '500: Internal Server Error. Resource not deleted.',
});
});
};

module.exports = {
createTask,
getAllTasks,
getTaskById,
updateTask,
deleteTask,
};
18 changes: 18 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { Pool } = require('pg');

const pool = new Pool({
connectionString: process.env.DATABASE_URL,
// user: 'carmensalas',
// host: 'localhost',
// database: 'todo',
// password: null,
// port: 5432,
});

pool.query('SELECT * FROM task;').then((data) => console.log(data));

module.exports = {
query(text, params) {
return pool.query(text, params);
},
};
46 changes: 46 additions & 0 deletions models/Task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const db = require('../db');

class Task {
static createTask(
taskName,
taskDescription,
dateCreated,
dueDate,
isComplete,
) {
const queryText = 'INSERT INTO task (task_name, task_description, date_created, due_date, is_complete) VALUES($1,$2,$3,$4,$5)';
return db.query(queryText, [
taskName,
taskDescription,
dateCreated,
dueDate,
isComplete,
]);
}

static getAllTasks() {
return db.query('SELECT * FROM task;');
}

static getLastCreated() {
const queryText = 'SELECT * FROM task ORDER BY id DESC LIMIT 1;';
return db.query(queryText);
}

static getTaskById(id) {
const queryText = 'SELECT * FROM task WHERE id = $1;';
return db.query(queryText, [id]);
}

static updateTask(id) {
const queryText = 'UPDATE task SET (task_name, task_description, date_created, due_date, is_complete) WHERE id = $1;';
return db.query(queryText, [id]);
}

static deleteTask(id) {
const queryText = 'DELETE FROM task WHERE id = $1';
return db.query(queryText, [id]);
}
}

module.exports = Task;
Loading