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
complete the exercises. This will make the lab work much easier to understand and complete.
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 aGET
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
- Read the instructions carefully and make sure you understand them before starting.
- Download the repository and read the code and documentation.
- Break each task into smaller, manageable pieces.
- Plan ahead and use online resources to help you with any coding problems you may have.
- Test your code frequently to make sure it works as expected.
- Make sure you understand the entire project before starting any coding.
- Document your code to make it easier to read and understand.
- Take frequent breaks and don't be afraid to ask for help if needed.
- Focus on one task at a time and complete it before moving on to the next.
- Don't be afraid to make mistakes, as these can help you learn and improve.
- Save your work regularly and back up your project files.
- Have fun and enjoy the coding process. 😀
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:
- apps table with columns:
app_id
,app_name
,description
,price
- users table with columns:
user_id
,first_name
,last_name
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
GET: /api/apps
: Get a list of all apps in your database (Read)GET: /api/apps/{app_id}
: Get the app in your database with the id ofapp_id
(Read)POST: /api/apps
: Create a new app and save it in your database (Create)PUT: /api/apps/{app_id}
: Modify the app in your database with the id ofapp_id
(Update)DELETE: /api/apps/{app_id}
: Delete the app in your database with the id ofapp_id
(Delete)
Users
GET: /api/users
: Get a list of all users in your database (Read)GET: /api/users/{user_id}
: Get the user in your database with the id ofuser_id
(Read)POST: /api/users
: Create a new user and save it in your database (Create)PUT: /api/users/{user_id}
: Modify the user in your database with the id ofuser_id
(Update)DELETE: /api/users/{user_id}
: Delete the user in your database with the id ofuser_id
(Delete)
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.
PATCH: /api/apps/{app_id}
: Modify part of the app in your database with the id ofapp_id
(Update)
Include comments describing the code so it's easy to follow.
Submit the file here: https://ju.instructure.com/courses/6807/assignments/46823
Presentation
Double check that your app fulfills all requirements by rereading the instructions.
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.
If the teacher is satisfied with your presentation you will be approved on the Canvas assignment Laboratory Work Presentation.
After the presentation, upload your project to the Canvas assignment Laboratory Work Code.