Skip to content

spraddles/php-api

Repository files navigation

README


API Setup: Local Development

Step 1. Drop the database

dropdb postgres
  • Note: Stop all DB service connections first.

Step 2. Create the database

createdb -U postgres postgres
  • Note: Start the DB server beforehand.

Step 3. Run Laravel migrations (user, user_events, temp_table)

php artisan migrate --path=database/migrations/2021_04_30_084401_create_tables.php  
php artisan migrate --path=database/migrations/2021_05_02_113342_create_temp_table.php

Step 4. Import CSV data into Postgres

psql postgres
\COPY temp_table ("userFirstname", "userSurname", "userEmail", "eventDate", "eventType", "eventMessage") FROM 'database/seeders/sample_data.csv' DELIMITER ',' CSV HEADER;
\q

Step 5. Run SQL scripts

psql -h 127.0.0.1 -d postgres -U postgres -f ./database/seeders/import.sql  
psql -h 127.0.0.1 -d postgres -U postgres -f ./database/seeders/manipulate.sql

Step 6. Finalize table structure

php artisan migrate --path=database/migrations/2021_05_02_233014_set_tables.php

Step 7. Start Laravel

php artisan serve

Step 8. Run PHP tests

php artisan test --testsuite=Feature

Step 9. Test API endpoints (Postman or cURL)

  • List users: GET http://127.0.0.1:8000/api/users
  • Delete user: DELETE http://127.0.0.1:8000/api/users/delete/{id}
  • List user events: GET http://127.0.0.1:8000/api/events
  • Event count by type: GET http://127.0.0.1:8000/api/events/grouped

API Setup: Docker

Step 1. Install Docker

Step 2. Build Docker container

docker build -t php-api-dev . -f ./docker-files/Dockerfile.dev

Flags:

  • -t: tag the image (php-api-dev)
  • .: current directory
  • -f: path to Dockerfile

Note: Rebuild the image any time you update the Dockerfile.

Step 3. Create Docker network

docker network create php-api-network
  • Note: Links the two containers together.
  • Note: Current networking config may have issues — requires debugging.
  • Reminder: Add --network="php-api-network" to your docker run commands.

Step 4. Run Containers

Postgres:

docker run -p 5432:5432 -it \
  -e POSTGRES_DB=postgres \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=postgres \
  --network="php-api-network" postgres:12.6

PHP API:

docker run -it -p 8000:8000 \
  -v $PWD:/php-api-run \
  --network="php-api-network" php-api-dev

Flags:

  • -it: interactive terminal session
  • -p: port mapping (host:container)
  • -v: mounts local directory into container
  • $PWD: current working directory
  • php-api-dev: Docker image tag

Notes:

  • Type exit to leave the container session.
  • Clean up unused images/containers to free up system memory.

Notes

1. Data Import

  • Assumes CSV is used to create a new database, not append to an existing one.
  • Migrations + seeders were used, but they may not be optimal for this use case.
  • Migrations create blank tables, while seeders insert predefined data — we need both simultaneously.
  • A raw SQL file is used to transform and align the data post-import.

2. Docker

  • Not using Docker mounted volumes or persistent states (for simplicity).
  • Containers are networked but not currently communicating — needs diagnosis.
  • Future Task: Create a Bash script to automate Docker installation + setup for fresh VMs.

3. API

  • Endpoints currently lack authentication or stateful sessions.
  • Future Task: Add auth middleware, JWT, or session handling.

4. Database

  • date fields should be consistently formatted.
  • All id columns are properly mapped with primary and foreign keys to maintain relational integrity between users and events.

About

a Laravel app serving API endpoints for data imports to a database

Topics

Resources

Stars

Watchers

Forks