Skip to content

API Endpoints

mablack01 edited this page Dec 3, 2017 · 1 revision

HTTP Requests

We use four types of HTTP Requests in our framework to manipulate data.

GET - Retrieve entries/data from the database.

POST - Creates an entry in the database.

DELETE - Removes entries/data from the database.

PUT - Modifies/adds data for an entry in the database.

Procedures for the 'Backend' Project (Database/Backend team)

Under the Project tab you will find out Backend project created by Jordan. There are three categories: Todo, In Progress, Review, and Merged.

When you begin to work on a feature, move the card from Todo to In Progress so that we can keep track of which cards still need to be worked on. When you are working on a feature, work on your own personal branch and NOT master.

Once you have tested this feature and it works properly, move the card to the Review section and submit a pull request to "dev". Then contact either Miles or David via Slack with your branch and task.

At that point, either Miles or David will review and double check for quality assurance as well as keep documentation and syntax match up with the rest of the project. They will then merge that branch into the dev branch. The dev branch should only be merged by either Miles or David.

This branch will be merged with master when milestones are complete and we are ready to update the project.

Tasks that have been merged to the dev branch will be moved to the Merge section and will be considered complete.

Commenting and Syntax example for API Endpoints (GET request)

/**
 * Directory: localhost:3000/api/systemslist
 * Parameters: systemslist - displays all of the system rows.
 * 	       systemslist?offset=x - displays the first x rows.
 * 	       systemslist?offset=x&start=y - displays the first x rows starting at entry y.
 * The usage of the offset and start parameters can be useful if we want to have
 * pages that display certain amounts of rows per page. eg. display 10 systems
 * per page and easily navigate through different pages to calculate the offsets and starting entries.
 */
app.get('/api/systemslist', function (req, res) {
	var queryText = "SELECT * FROM system";
	if (req.query.offset != null && req.query.start == null)
		queryText += " LIMIT " + req.query.offset;
	else if (req.query.offset != null && req.query.start != null)
		queryText += " LIMIT " + (req.query.start - 1) + ", " + req.query.offset;
	connection.query(queryText, function(error, results, fields) {
		res.send(results);
	});
});

Commenting and Syntax example for API Endpoints (POST request)

To test POST requests I'm using the Postman extension/app for Google Chrome.

/**
 * Type: POST
 * Directory: localhost:3000/api/tags
 * Parameters: tags?serial_id=w&name=x&user_id=y&visibility=z - Adds a tag entry to the tag table with name x, user id y, 
 * 								and visibility z. This tag is then added to system w.
 * This adds a new tag entry to the tag table of our database. The id is a primary key
 * and will automatically increment every new entry, meaning that the id's will stay unique.
 * All three parameters are required since we don't want nulled data, web page will respond if
 * the syntax is incorrect. This also adds the tag to the junction table corresponding to the system id.
 */
app.post('/api/tags', function (req, res) {
	var serial_id = req.query.serial_id;
	var name = req.query.name;
	var user_id = req.query.user_id;
	var visibility = req.query.visibility;
	if (name != null && user_id != null && visibility != null) {
		connection.query("INSERT INTO tag (name, user_id, visibility) VALUES ('" + name + "', " + user_id + ", " + visibility + ")"
				+ "INSERT INTO systemtags (system_id, tag_id) VALUES ('" + serial_id + "', '(SELECT id FROM tag ORDER BY ID DESC LIMIT 1)')", function(error, results, fields) {
			res.send("Successfully added the tag to the database!");
		});
	} else {
		res.send("Invalid syntax, missing correct parameters.");
	}
});