Back to start

Lab 2 instructions

In Lab 2, you will create an API for managing apps and users by using the same project structure as our multitier shop. This API should support all CRUD (Create, Read, Update, and Delete) operations for both resources. To do this, you will create database tables, create project files and folders, create models, connect to the database, send data to the presentation layer, display the data in the API, and create API calls for each of the CRUD operations. After completing the exercises, you will present our work to a teacher and upload our project to Canvas.

By doing this, you will learn how to create an API for managing apps and users, create database tables and project files and folders, create models, connect to the database, send data to the presentation layer, display the data in the API, and create API calls for each of the CRUD operations.

Before moving on

  1. complete the exercises. This will make the lab work much easier to understand and complete.

  2. Download the repository linked below and create the tables as we did in Lecture 5. Then try to get the data from the customers table by sending a GET request with Postman like we did in the lecture.

Github repository

For this project you can use the following Github repository as a reference. We will update and add to this during the course. The repository can be found at this link: Github multitier-shop

Tips

Error handling

How should I handle errors or exceptions in my code?

There is no need currently to handle errors in the code correctly. This means that it's OK if the response from your API doesn't make any sense when the user submits invalid data (like text instead of int) or if the database can't be reached. Later i the course we will handle validation and authorization (requiring users to log in) but for now you can skip that.

Instructions

Create database and tables

Create database tables for each of the resources required for this lab:

Fill the tables with some data so you can test your code later.

Create project

Create a project repository on Github and clone it to a directory and create the necessary files and folders. The project should include the following files and folders: index.php, .htaccess, and folders for your presentation layer (/api/), business logic layer (/business-logic/), data access layer (/data-access/), and models (/models/).

Create models

Create the class files for each of the two resources that will be managed by the API. The class files should include all the necessary properties for the resource objects. The classes chould contain properties for each database column, but following the PHP naming conventions (so app_id becomes $appId).

Connect to database

In your data access layer create a Database.php class file that connects to the database in the constructor and create a Database class for the apps table (AppsDatabase.php). Add a method for getting all rows from the table by using a SELECT * FROM apps; MySQL query.

Send data to the presentation layer

Create the AppsService.php class with a function that retrieves data from your Database class that your presentation layer code (/api/ in our case) can use to get data from your data access layer.

Display the data in your API

Create a class file called AppsAPI.php that retrieves data from your business logic layer and create a APIRouter.php class file that routes your requests to call the correct method. Then add code to your index.php and .htaccess so calls to your REST API urls (like http(s)://{base url}/{resource}) calls the correct method in your presentation layer

Checkpoint ✔

If you've completed the instructions above you should be able to call your API at something like http://localhost/multitier-appstore/api/apps and get a JSON response of all apps you added in your table.

The rest

Create API calls for each of the CRUD operations following the same structure as above. You should be able to make the following API calls with postman (assuming your API root is located at http://localhost/multitier-appstore/api/):

Apps

Users

Grading

The lab is graded 4 or 5, meaning anyone who completes the lab will get grade 4. If you want grade 5 on the lab you need to implement the following PATCH api call and then create one php-file containing all changes you needed to make to implement the call.

Include comments describing the code so it's easy to follow.

Submit the file here: https://ju.instructure.com/courses/6807/assignments/46823

Presentation

  1. Double check that your app fulfills all requirements by rereading the instructions.

  2. Present your work to a teacher at one of the lab sessions. You should be able to explain how all code you have written works, and the teacher might ask you some questions about it to verify that this is the case.

  3. If the teacher is satisfied with your presentation you will be approved on the Canvas assignment Laboratory Work Presentation.

  4. After the presentation, upload your project to the Canvas assignment Laboratory Work Code.