Skip to content

Latest commit

 

History

History
186 lines (122 loc) · 4.53 KB

File metadata and controls

186 lines (122 loc) · 4.53 KB

Level 300 - Week 2/B - Back End

During the second part of the second week you will be creating the API for your Video website.

How to read this guide

Below are separate headings for each endpoint. Each of them are separated into the HTTP Request Method type and the route that the endpoint should exist on.

For a recap on about HTTP Request Methods you can read here https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

For example, for the first endpoint

  • GET is the request method
  • "/" is the route

On Line 8 of server.js you can find an example of the first endpoint that you will need to make.

Project

Since we are using a monorepo the server and client are running on the same instance. So to run the server you should also use

npm run dev

To confirm your server is running go to

http://127.0.0.1:3000/health

in your browser. If the server is running you'll see a message saying

OK

Note: to separate the backend and frontend all backend endpoints have /api as a prefix. So for example if you are asked to create a GET /videos endpoint that will be available at http://127.0.0.1/3000/api/videos

Sample Solution

Here is an example solution for the Back End:

https://video-recommendations-cyf.netlify.app/api/videos

Database

Your code is expected to read data from the database. In order to make sure it works you will need to make sure that the backend is configured for database access. There are two steps you need to do:

  1. Copy the .env.example file over to .env

This file contains the settings for your application. Currently the only setting you need is the DATABASE_URL setting that looks like the following:

postgres://username:password@localhost:5432/database_name

Make sure you swap the username, password and database_name values with the database details that you have set up locally.

  1. Use your queries you created in level 200 inside the API.

The api.js file is already set up to connect to the database if the config is done properly. To query the database you can use requests like:

const response = await db.query("SELECT * FROM videos WHERE id = $1",[id]);

Endpoints

Your website should have the following four standard REST endpoints to list, list, view and delete videos. The backend will also need a way to modify the rating of the video as well which endpoint you need to design yourself.

GET "/videos"

This endpoint is used to return all of the videos

Example Response

See exampleresponse.json as an example.

POST "/videos"

This endpoint is used to add a video to the API.

Both fields - title and url - must be included and be valid for this to succeed.

Note: When a video is added, you must attach a unique ID to so that it can later be deleted

Example input

{
	"title": "The Title",
	"url": "https://www.youtube.com/watch?v=ABCDEFGHIJK"
}

Example Response

If successful:

{
	"id": 523523
}

If not successful

{
	"result": "failure",
	"message": "Video could not be saved"
}

GET "/{id}"

Returns the video with the ID contained within the {id} parameter

Example Response

{
	"id": 1,
	"title": "Never Gonna Give You Up",
	"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
	"rating": 23
}

If that ID doesn't exist in the database the endpoint should return a 404 status code with an error message, example:

{
	"result": "failure",
	"message": "Video not found"
}

DELETE "/{id}"

Deletes the video with the ID container within the {id} parameter

Example Response

If successful:

{}

If that ID doesn't exist in the database the endpoint should return a 404 status code with an error message, example:

{
	"result": "failure",
	"message": "Video could not be deleted"
}

Updating the rating

Your backend also needs to have a way to allow the rating of a video to be modified up and down.

Design this feature yourself based on what you have learned in the Syllabus.

Examples

Note: you don't need to adhere to the example requests and responses above, for example a response on GET /videos/{id} like the following is also acceptable:

{
	"result": "success",
	"message": "Video Found",
	"video": {
		"id": 1,
		"title": "Never Gonna Give You Up",
		"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
		"rating": 23
	}
}

Design your backend's responses so it will be easy for you to link the backend and frontend applications together, including potential error scenarios.