Hi oneFit!
Here some code developed for you, I hope you'll have the time to take a look :-).
Here you can find:
- Install instructions.
- General documentation of code.
- Endpoints doc.
- Security.
- Routes.
- Create a Schema in MySql.
- Run the database script oneFit.sql (it's located in the root of this project).
- Copy the project folder in the server.
- Change config in files: conf/database.json and conf/email.json
The code it's documented with PHPDoc Standard, every class, method/function and property have a description of the task/use they has.
Files and Directory structure.
βββ conf
βΒ Β βββ database.json
βΒ Β βββ email.json
βΒ Β βββ routes.yml
βΒ Β βββ secure.json
βββ README.md
βββ src
βΒ Β βββ Controller
βΒ Β βΒ Β βββ AuthController.php
βΒ Β βΒ Β βββ ExcercisesController.php
βΒ Β βΒ Β βββ HomeController.php
βΒ Β βΒ Β βββ PlanController.php
βΒ Β βΒ Β βββ UserController.php
βΒ Β βΒ Β βββ UserPlanController.php
βΒ Β βΒ Β βββ WorkoutDaysController.php
βΒ Β βΒ Β βββ WorkoutDaysExcercisesController.php
βΒ Β βββ db
βΒ Β βΒ Β βββ Connection.php
βΒ Β βΒ Β βββ orm
βΒ Β βΒ Β βββ adapters
βΒ Β βΒ Β βΒ Β βββ AdapterFactory.php
βΒ Β βΒ Β βΒ Β βββ MySqlAdapter.php
βΒ Β βΒ Β βββ CrudBase.php
βΒ Β βΒ Β βββ EntityMethods.php
βΒ Β βΒ Β βββ Field.php
βΒ Β βΒ Β βββ model
βΒ Β βΒ Β βΒ Β βββ EntityFactory.php
βΒ Β βΒ Β βΒ Β βββ Excercises.php
βΒ Β βΒ Β βΒ Β βββ Plan.php
βΒ Β βΒ Β βΒ Β βββ User.php
βΒ Β βΒ Β βΒ Β βββ UserPlan.php
βΒ Β βΒ Β βΒ Β βββ WorkoutDaysExcercises.php
βΒ Β βΒ Β βΒ Β βββ WorkoutDays.php
βΒ Β βΒ Β βββ Query.php
βΒ Β βΒ Β βββ TableBase.php
βΒ Β βββ lib
βΒ Β βΒ Β βββ Auth.php
βΒ Β βΒ Β βββ EmailService.php
βΒ Β βββ Routing
βΒ Β βββ Routes.php
Here the description of the directory structure of the project and main files.
This is a simple approach of a custom made ORM solution. It's made an abstraction of the DB using the following classes:
Location: src\db.
Description: Class thats generate singleton with th database connection.
Location: src\db\orm.
Description: It's has the main functions, use class src\db\orm\adapters\AdaptersFactory to get the database adapter to performs the CRUD operations, src\db\orm\model\EntityFactory to use the instance o the entity and src\db\orm\Field to make an instance of fields. Also is extended every entity of the model.
Dependencies: src\db\orm\adapters\AdaptersFactory.php,src\db\orm\model\EntityFactory.php, src\db\orm\Field.php
Location: src\db\orm.
Description: Used to create fields object, in accordance with the entity abstraction class configuration.
Location: src\db\orm.
Description: Used to create custom query including: group functions, joins(INNER/LEFT/RIGHT).
Dependencies: src\db\orm\model\EntityFactory
Location: src\db\orm
Description: trait thats contains the main common entity methods.
Location: src\db\orm
Description: Interface implemented by all the database adapters (MySqlAdapter).
Location: src\db\orm\adapters.
Description: Factory pattern implementation thats generate the class adapter in accordance with the provider parameter of the file conf/database.json.
Dependencies: every database adapters in src\db\orm\adapters\ (MySqlAdapter)
Location: src\db\orm\adapters.
Description: Class thats implement the src\db\orm\CrudBase interface; to performs the CRUD adapted to the database/datasource implemented (eg. Postgresql).
Dependencies: src\db\Connection.php, src\db\orm\CrudBase.php,src\db\orm\Field.php, src\db\orm\model\EntityFactory.php, src\db\orm\Query
Location: src\db\orm\model.
Description: Factory pattern implementation thats generate the instance of class entity in use.
Dependencies: every entity class in src\db\model
Location: src\db\orm\model.
Description: Class thats implement the abstraction of the entity in the database. Need to be generate one for every entity.
Dependencies: src\db\orm\TableBase.php, src\db\orm\Field.php, src\db\orm\EntityMethods.php
1.- Create the Adapter.php, with the specific implementation in the database thats the system use. Implementing the CrudBase interface and following the example PostgresqlAdapter.php. 2.- Create the .php for every files following the examples(eg. Recipe.php). 3.- Run the ORM test.
The folder contains every controller of the app.
Contains libs of general propose.
Contains configurations files; the basic are:
- database.json: contains connections string of the db.
- routes.yml: it has all the routes of the app, based in symfony route system.
- secure.json: it has all the protected routes of the app.
- email.json: config parameters of email server.
Folder that contains all test, the solution use PHPunit.
Note: Sometimes test\Controller\UserControllerTest::testSave fails because of the unique index of the email field. This create an user from a combination of a random number and a string; this happens when you already ran several tests, so dont't worry it's about validation :) .
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| List | GET |
/user |
β | |
| Create | POST |
/user |
β | |
| Get | GET |
/user/{id} |
β | |
| Confirm | GET |
/userconfirm/{id} |
β | |
| Update | PUT/PATCH |
/user/{id} |
β | |
| Delete | DELETE |
/user/{id} |
β | |
| Search | GET |
/user/search |
β | expect a json string with parameters where database fields are the index and value what is needed to be search. eg.{"user":{"email":{"value":"[email protected]"}}} |
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| SignIn | POST |
/auth/signin |
β | generate and return the token from a valid user |
| Check | GET |
/auth |
β | validate token |
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| List | GET |
/plan |
β | |
| Create | POST |
/plan |
β | |
| Get | GET |
/plan/{id} |
β | |
| Update | PUT/PATCH |
/plan/{id} |
β | |
| Delete | DELETE |
/plan/{id} |
β | |
| Search | GET |
/plan/search |
β | expect a json string with parameters where database fields are the index and value what is needed to be search. eg.{"plan":{"name":{"value":"Advanced Plan"}}} |
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| List | GET |
/excercises |
β | |
| Create | POST |
/excercises |
β | |
| Get | GET |
/excercises/{id} |
β | |
| Update | PUT/PATCH |
/excercises/{id} |
β | |
| Delete | DELETE |
/excercises/{id} |
β | |
| Search | GET |
/excercises/search |
β | expect a json string with parameters where database fields are the index and value what is needed to be search. eg.{"excercises":{"name":{"value":"Curl"}}} |
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| List | GET |
/userplan |
β | |
| Create | POST |
/userplan |
β | |
| Get | GET |
/userplan/{id} |
β | |
| Update | PUT/PATCH |
/userplan/{id} |
β | |
| Delete | DELETE |
/userplan/{id} |
β | |
| Search | GET |
/userplan/search |
β | expect a json string with parameters where database fields are the index and value what is needed to be search. eg.{"user_plan":{"name":{"value":"Curl"}}} |
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| List | GET |
/workoutdays |
β | |
| Create | POST |
/workoutdays |
β | |
| Get | GET |
/workoutdays/{id} |
β | |
| Update | PUT/PATCH |
/workoutdays/{id} |
β | |
| Delete | DELETE |
/workoutdays/{id} |
β | |
| Search | GET |
/workoutdays/search |
β | expect a json string with parameters where database fields are the index and value what is needed to be search. eg.{"work_out_days_excecises":{"id":{"value":34}}} |
| Name | Method | URL | Protected | Params |
|---|---|---|---|---|
| List | GET |
/workoutdaysexcercises |
β | |
| Create | POST |
/workoutdaysexcercises |
β | |
| Get | GET |
/workoutdaysexcercises/{id} |
β | |
| Update | PUT/PATCH |
/workoutdaysexcercises/{id} |
β | |
| Delete | DELETE |
/workoutdaysexcercises/{id} |
β | |
| Search | GET |
/workoutdaysexcercises/search |
β | expect a json string with parameters where database fields are the index and value what is needed to be search. eg.{"workout_days":{"name":{"value":"Leg day"}}} |
The solution use JWT to for security, and a simple user authentication for create ad admin user.
Note: The "token" parameter with the generated token need to be send in all protected end points.
Location: src\lib.
Description: Class thats implement the auth system using JWT.
Dependencies: Firebase\JWT\JWT.php
Location: src\lib.
Description: Class thats implement email send using PHPMailer.
Dependencies: PHPMailer\PHPMailer\PHPMailer, PHPMailer\PHPMailer\Exception, src\db\orm\model\User, src\lib\Auth;
Based in symfony routing system; this is a custom made routing class.
Location: src\Routing.
Description: Class thats create and return routes of the app, using conf/routes.yml as the source of routes.
Dependencies: use src\lib\Auth, every controller of the app.
Let me know your thoughts.
Best.