This project is a simple book inventory management API implemented in Go using the Gin web framework. It provides basic functionalities such as retrieving a list of books, retrieving a specific book by ID, creating a new book entry, checking out a book (decreasing its quantity), and returning a book (increasing its quantity). The data is stored in memory as a static list of books.
- main.go: Contains the main application logic, including the setup of the Gin router and the implementation of CRUD operations for the book inventory.
-
GET /books: Retrieve a list of all books.
-
GET /books/:id: Retrieve details of a specific book by ID.
-
POST /books: Create a new book entry.
-
PATCH /checkout: Check out a book by decreasing its quantity (Query parameter style).
-
PATCH /return: Return a book by increasing its quantity (Query parameter style).
-
Each book has an ID, Title, Author, and Quantity.
type Book struct { ID string `json:"id"` Title string `json:"title"` Author string `json:"author"` Quantity int `json:"quantity"` }
- Ensure you have Go installed on your machine.
- Run the following command to install the required dependencies:
go get -u github.com/gin-gonic/gin
Execute the main.go file to start the server:
go run main.go
• Access the API at http://localhost:8080.
Use tools like curl or Postman to interact with the API endpoints. Example: To check out a book, send a PATCH request to http://localhost:8080/checkout?id=<book_id>.
This is a simple demonstration project, and it uses in-memory data storage. For a production scenario, consider using a database for persistent data storage.
© 2024 - Boularbah Ismail; Twitter.