This is a simple CRUD API for managing a collection of books. It allows users to create, read, update, and delete book entries through HTTP requests.
- Clone the repository:
git clone https://github.com/your-username/simple-book-management-api.git cd simple-book-management-api
- Install dependencies
composer install
- Create a
.env
file in the root directory and add your database configuration:DB_HOST=localhost DB_PORT=3306 DB_DATABASE=book_api DB_USERNAME=your_mysql_username DB_PASSWORD=your_password
- Set up the database by creating a MySQL database named
book_api
** or the name you’ve configured in.env
:CREATE DATABASE book_api;
- Create the
books
table:CREATE TABLE books ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, author VARCHAR(255), published_date DATE, isbn VARCHAR(20), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
To start the PHP built-in server, navigate to the project root and run:
php -S localhost:8000 -t public
This will start the server on http://localhost:8000
.
- Method:
POST
- URL:
/books
- Body (JSON):
{ "title": "Book Title", "author": "Author Name", "published_date": "YYYY-MM-DD", "isbn": "1234567890123" }
- Response:
{ "message": "Book created successfully." }
- Method:
GET
- URL:
/books
- Response:
[ { "id": 1, "title": "To Kill a Mockingbird", "author": "Harper Lee", "published_date": "1960-07-11", "isbn": "9780061120084", "created_at": "2024-01-01 12:00:00" }, ... ]
- Method:
GET
- URL:
/books/{id}
- Response:
[ { "id": 1, "title": "To Kill a Mockingbird", "author": "Harper Lee", "published_date": "1960-07-11", "isbn": "9780061120084", "created_at": "2024-01-01 12:00:00" }, ... ]
- Method:
PUT
- URL:
/books/{id}
- Body:
{ "title": "Updated Book Title", "author": "Updated Author", "published_date": "YYYY-MM-DD", "isbn": "1234567890123" }
- Response:
{ "message": "Book updated successfully." }
Method: DELETE
URL: /books/{id}
- Response:
{ "message": "Book deleted successfully." }
If an error occurs (e.g., book not found, invalid input), the API will return a JSON response with an appropriate HTTP status code and error message, such as:
{ "message": "Book not found." }
- Make sure to set up .env properly to connect to the MySQL database.
- All responses are in JSON format.
- The API is designed for local development and testing. For production use, consider using a more robust server and add authentication.