This is a simple RESTful API for managing a collection of books, including operations to check out and check in books, created using the Gin framework in Go.
-
Clone the repository:
git clone https://github.com/yourusername/book-checkout.git
-
Navigate to the project directory:
cd book-checkout
-
Install the required dependencies:
go get -u github.com/gin-gonic/gin
-
Run the application:
go run main.go
The application will be running at http://localhost:8080
.
You can use tools like curl
, Postman
, or any other API client to interact with the API endpoints.
- URL:
/books
- Method:
GET
- Description: Retrieves a list of all books.
- Response:
[ { "id": "1", "title": "The Worshipper of the Image", "author": "Richard Le Gallienne", "quantity": 2 }, ... ]
- URL:
/books/:id
- Method:
GET
- Description: Retrieves a book by its ID.
- Response:
{ "id": "1", "title": "The Worshipper of the Image", "author": "Richard Le Gallienne", "quantity": 2 }
- URL:
/books
- Method:
POST
- Description: Creates a new book entry.
- Request Body:
{ "id": "4", "title": "New Book Title", "author": "New Author", "quantity": 10 }
- Response:
{ "id": "4", "title": "New Book Title", "author": "New Author", "quantity": 10 }
- URL:
/checkout
- Method:
PATCH
- Description: Checks out a book (decreases the quantity by 1).
- Query Parameter:
id
(the ID of the book to check out) - Response:
{ "id": "1", "title": "The Worshipper of the Image", "author": "Richard Le Gallienne", "quantity": 1 }
- URL:
/checkin
- Method:
PATCH
- Description: Checks in a book (increases the quantity by 1).
- Query Parameter:
id
(the ID of the book to check in) - Response:
{ "id": "1", "title": "The Worshipper of the Image", "author": "Richard Le Gallienne", "quantity": 3 }
The data structure for a book is as follows:
type book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Quantity int `json:"quantity"`
}