Skip to content

Bookworm site using the Laravel v8.0 framework to build a backend system that provides API endpoints.

Notifications You must be signed in to change notification settings

nhanitk14dev/bookworm-backend

Repository files navigation

Overview Bookworm Backend

Bookworm site using the Laravel v8.0 framework to build a backend system that provides API endpoints.

Installation


  • Documentation: https://laravel.com/docs/8.x/installation#getting-started-on-linux

  • cd folder: curl -s https://laravel.build/bookmark | bash

  • Config env with docker:

    • DB_CONNECTION=pgsql
    • DB_HOST=db
    • DB_PORT=5432
    • DB_DATABASE=bookworm
    • DB_USERNAME=bookworm
    • DB_PASSWORD=bookworm.
  • Run: php artisan migrate:fresh --seed

  • Config Mysql in .env:

    • DB_CONNECTION=mysql
    • DB_HOST=127.0.0.1
    • DB_PORT=3306
    • DB_DATABASE=bookworm
    • DB_USERNAME=root
    • DB_PASSWORD=
  • Run command line:

    • Run installed packages: composer install
    • Generate new key: php artisan key:generate
    • Run migrations: php artisan make:migrate
    • Run DB seeds: php artisan db:seed

Build Docker


URL: https://docs.docker.com/engine/installation/linux/ubuntulinux
Install: https://docs.docker.com/engine/install/ubuntu
CMD: https://docs.docker.com/engine/reference/commandline/version
Docker Web: hitalos/laravel: https://github.com/hitalos/laravel => http://localhost:80
adminer: https://hub.docker.com/_/adminer => http://localhost:8080
mailcatcher: https://hub.docker.com/r/schickling/mailcatcher => http://localhost:1080

- Define a Dockerfile in folder phpdocker & create new file docker-compose.yml.
- CMD docker:
	Build: docker-compose up using the docker-compose
	Docker start: sudo service docker start
	Access docker: docker exec -it bookworm-backend_db_1 bash

RESTful APIs


  1. Use JSON

  2. Use Nouns instead of Verbs: GET /books/123

  3. Name the collections using Plural Nouns: GET /cars/123

  4. Use resource nesting: /users/123/orders

  5. Error Handling:

  • HTTP Status Code
  • Code ID
  • Message
  • The response as: { "status": 404, "message": "Not found", "code": 00012 }
  1. Filtering, sorting, paging, and field selection:
  • GET /users?country=USA&creation_date=2019-11-11&sort=birthdate_date:desc&limit=100&offset=2
  • GET /users/123?fields=username,email (for one specific user)
  1. Versioning: https://api.stripe.com/v1/
  2. API Documentation (Good API documentation examples):
  1. Using SSL/TLS: Always use SSL/TLS to encrypt the communication with your API. No exceptions. Period.
  2. Write Unit Test
  3. Link reference:

Unit Testing (Testing JSON APIs)


  1. Setting up our testing environment:
  • create an .env.testing file from .env and config for testing.

  • Create new file database/database.sqlite.

  • Migrate and seed the test database: php artisan migrate --seed --env=testing.

  • CMD clear cache testing: php artisan config:cache --env=testing.

  • Make order unit test: php artisan make:test OrderTest --unit.

  • Run: php artisan test or run all "php artisan test".

  1. Writing our test cases: For the e-commerce application, We will be testing all the API endpoints to ensure it actually works as expected. If we had observers or repositories that handled complex application logic, we may want to test them to ensure they work as expected.
  • Testing the book endpoints: php artisan make:test BookTest --unit
  1. Running the tests
  • CMD: cd docker web and run $ ./vendor/bin/phpunit

About

Bookworm site using the Laravel v8.0 framework to build a backend system that provides API endpoints.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published