Skip to content

tanciaku/book-library-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Book Library API

A simple REST API for managing a personal book library, built with Rust and Axum.

Features

  • CRUD operations for books
  • In-memory storage
  • Track book availability

Quick Start

cargo run

The server will start on http://localhost:3000

API Endpoints

Books

  • GET /health - Health check
  • GET /books - List all books (with optional filters and pagination)
  • POST /books - Add a new book
  • GET /books/{id} - Get a book by ID
  • PUT /books/{id} - Update a book
  • DELETE /books/{id} - Delete a book

Example Requests

Add a book:

curl -X POST http://localhost:3000/books \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Clean Code",
    "author": "Robert C. Martin",
    "year": 2008,
    "isbn": "978-0132350884"
  }'

List all books:

curl http://localhost:3000/books

Filter books:

# Filter by availability
curl http://localhost:3000/books?available=true

# Filter by author (case-insensitive search)
curl http://localhost:3000/books?author=martin

# Filter by publication year
curl http://localhost:3000/books?year=2008

# Combine multiple filters
curl "http://localhost:3000/books?available=true&author=martin&year=2008"

Paginate books:

# Get the second page with 5 books per page
curl "http://localhost:3000/books?page=2&limit=5"

# Combine pagination with filters
curl "http://localhost:3000/books?available=true&page=1&limit=20"

The response includes a pagination metadata object alongside the data array:

{
  "data": [...],
  "pagination": {
    "page": 1,
    "limit": 10,
    "total_items": 42,
    "total_pages": 5
  }
}

page defaults to 1 and limit defaults to 10 (max 100).

Update book availability:

curl -X PUT http://localhost:3000/books/1 \
  -H "Content-Type: application/json" \
  -d '{"available": false}'

Delete a book:

curl -X DELETE http://localhost:3000/books/1

Data Model

{
  "id": 1,
  "title": "Book Title",
  "author": "Author Name",
  "year": 2024,
  "isbn": "978-1234567890",
  "available": true
}

Validation

When adding a new book, the following validations are enforced:

  • Title: Must not be empty
  • Author: Must not be empty
  • Year: Must be between 1000 and the current year
  • ISBN: Must be a valid ISBN-13 format (13 digits, hyphens allowed)

Invalid requests will return 400 Bad Request with an error message.

Notes

⚠️ Uses in-memory storage - data is lost when the server stops.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages