{"id":26397667,"url":"https://github.com/andrepradika/be-management-book","last_synced_at":"2025-06-15T14:04:52.943Z","repository":{"id":277471073,"uuid":"866929065","full_name":"andrepradika/be-management-book","owner":"andrepradika","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-03T06:27:59.000Z","size":133,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T04:33:32.092Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrepradika.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-03T06:21:55.000Z","updated_at":"2024-10-03T06:28:03.000Z","dependencies_parsed_at":"2025-02-14T04:43:42.468Z","dependency_job_id":null,"html_url":"https://github.com/andrepradika/be-management-book","commit_stats":null,"previous_names":["andrepradika/be-management-book"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbe-management-book","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbe-management-book/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbe-management-book/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbe-management-book/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrepradika","download_url":"https://codeload.github.com/andrepradika/be-management-book/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244031119,"owners_count":20386534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-17T12:18:35.602Z","updated_at":"2025-03-17T12:18:36.115Z","avatar_url":"https://github.com/andrepradika.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Management Book API\n\nThis project is a RESTful API built with FastAPI, using MySQL for the database, Redis for caching, and Docker for containerization. The API allows you to manage authors and books with endpoints for creating, retrieving, updating, and deleting records.\n\n## Table of Contents\n\n- [Features](#features)\n- [Technologies Used](#technologies-used)\n- [Setup Instructions](#setup-instructions)\n  - [Prerequisites](#prerequisites)\n  - [Clone the Repository](#clone-the-repository)\n  - [Configure Environment Variables](#configure-environment-variables)\n  - [Docker Setup](#docker-setup)\n  - [Database Migrations with Alembic](#database-migrations-with-alembic)\n  - [Running the Application](#running-the-application)\n  - [Testing the API](#testing-the-api)\n- [API Endpoints](#api-endpoints)\n- [Unit Testing](#unit-testing)\n- [Load Testing](#load-testing)\n- [Additional Notes](#additional-notes)\n\n## Features\n\n- **Authors Management**: Create, read, update, and delete authors.\n- **Books Management**: Create, read, update, and delete books.\n- **Associations**: Retrieve all books by a specific author.\n- **Caching**: Optimize performance using Redis for caching.\n- **Unit Testing**: Comprehensive tests for all functionalities.\n- **Docker Support**: Easy setup and deployment.\n\n## Technologies Used\n\n- [FastAPI](https://fastapi.tiangolo.com/)\n- [MySQL](https://www.mysql.com/)\n- [Redis](https://redis.io/)\n- [Docker](https://www.docker.com/)\n- [Alembic](https://alembic.sqlalchemy.org/)\n- [pytest](https://docs.pytest.org/en/stable/)\n\n## Setup Instructions\n\n### Prerequisites\n\n- Install [Docker](https://docs.docker.com/get-docker/)\n- Install [Docker Compose](https://docs.docker.com/compose/install/)\n- Python 3.9 or higher\n- MySQL server\n\n### Clone the Repository\n\n```bash\ngit clone https://github.com/andrepradika/be-management-book.git\ncd be-management-book\n```\n\n### Configure Environment Variables\nCreate a .env file in the root directory of the project and add the following environment variables:\n```bash\nBEARER_TOKEN=supersecrettoken123\nDATABASE_URL=mysql+pymysql://user:password@db:3306/management_book\nDATABASE_TEST_URL=mysql+pymysql://user:password@db:3306/management_book_test\nREDIS_URL=redis://redis:6379/0\n```\n\n### Docker Setup\nCreate a docker-compose.yml file in the root directory with the following content:\n```bash\nversion: '3.7'\n\nservices:\n  db:\n    image: mysql:8.0\n    environment:\n      MYSQL_ROOT_PASSWORD: password\n      MYSQL_DATABASE: management_book\n      MYSQL_USER: user\n      MYSQL_PASSWORD: password\n    volumes:\n      - db_data:/var/lib/mysql\n      - ./mysql-init:/docker-entrypoint-initdb.d\n    ports:\n      - \"3306:3306\"\n    healthcheck:\n      test: [\"CMD\", \"mysqladmin\", \"ping\", \"-h\", \"localhost\"]\n      interval: 10s\n      timeout: 5s\n      retries: 10\n\n  redis:\n    image: redis:latest\n    ports:\n      - \"6379:6379\"\n    healthcheck:\n      test: [\"CMD\", \"redis-cli\", \"ping\"]\n      interval: 10s\n      timeout: 5s\n      retries: 5\n\n  api:\n    build:\n      context: .\n      dockerfile: Dockerfile\n    ports:\n      - \"8000:8000\"\n    environment:\n      - BEARER_TOKEN=supersecrettoken123\n      - DATABASE_URL=mysql+pymysql://user:password@db/management_book\n      - REDIS_URL=redis://redis:6379/0\n    depends_on:\n      db:\n        condition: service_healthy\n      redis:\n        condition: service_healthy\n    volumes:\n      - ./app:/app/app\n      - ./alembic:/app/alembic\n      - ./alembic.ini:/app/alembic.ini\n    command: \u003e\n      bash -c \"\n      alembic upgrade head \u0026\u0026\n      uvicorn app.main:app --host 0.0.0.0 --port 8000\"\n\n  test:  # Comment if not used\n    build:\n      context: .\n      dockerfile: Dockerfile\n    environment:\n      - REDIS_URL=redis://redis:6379/0\n    depends_on:\n      db:\n        condition: service_healthy\n      redis:\n        condition: service_healthy\n    command: \u003e\n      bash -c \"pytest tests/ --disable-warnings -v\"\nvolumes:\n  db_data:\n```\n\n## Running the Application\nTo build and run the Docker containers, use the following command:\n```bash\ndocker-compose up --build\n```\nThe API will be accessible at http://localhost:8000.\n\n## Testing the API\n1. Open the Swagger UI at http://localhost:8000/docs. \u003cbr\u003e\n2. Click the Authorize button and enter your Bearer token:\n```bash\nsecrettoken123\n```\n3. Test the various endpoints to manage authors and books.\n\n## API Endpoints\nAuthors\n* GET /authors: Retrieve a list of all authors.\n* GET /authors/{id}: Retrieve details of a specific author. \n* POST /authors: Create a new author.\n* PUT /authors/{id}: Update an existing author.\n* DELETE /authors/{id}: Delete an author.\n\nBooks\n* GET /books: Retrieve a list of all books.\n* GET /books/{id}: Retrieve details of a specific book.\n* POST /books: Create a new book.\n* PUT /books/{id}: Update an existing book.\n* DELETE /books/{id}: Delete a book.\n\nAssociations\n* GET /authors/{id}/books: Retrieve all books by a specific author.\n![Alt Test](screenshoot/swagger.png)\n\n\n## Unit Testing\n1. Ensure test in docker-compose.\n\u003cbr\u003e\n2. Run tests using:\n```bash\n  docker-compose up --build test\n```\nImage Result Test\n![Alt Test](screenshoot/pytest.png)\n\n## Load Testing\n1. Ensure API is running.\n2. Run tests using:\n```bash\n  locust\n```\n3. Open web interface:\n```bash\n  http://localhost:8089\n```\nImage Result Locust\n![Alt Test](screenshoot/locust.png)\n\n## Additional Notes :\n1. Re-run docker image api if, database not ready\n2. Re-initialize database if not accessed, you can check using:\n```bash\nmysql -u user -p\nshow databases;\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrepradika%2Fbe-management-book","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrepradika%2Fbe-management-book","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrepradika%2Fbe-management-book/lists"}