This is a simple API for managing books and categories built with the Gin framework in Go. The API allows users to:
- Authenticate using JWT tokens.
- Manage categories API.
- Manage books API.
-
Clone the repository:
git clone https://github.com/kandlagifari/go-books-apps.git cd go-books-apps
-
Install dependencies:
go mod tidy
-
Set up your
config/.env
file with necessary environment variables:# PostgreSQL DB_HOST=<your_host> DB_PORT=<your_port> DB_USER=<your_user> DB_PASSWORD=<your_password> DB_NAME=<your_database> # JWT Secret Key JWT_SECRET_KEY=<your_jwt_secret_key>
-
Run the migrations to set up the database and start web server:
# With make make build && make run # Without make ./build.sh && ./bootstrap &
-
The server will be running on http://localhost:4321
This API uses JWT (JSON Web Tokens) for user authentication. You need to include the token in the Authorization
header in each request to access protected endpoints.
- POST
/api/users/register
: User register to access API endpoints.
- POST
/api/users/login
: Logs in a user and provides a JWT token.
- GET
/api/categories
- POST
/api/categories
- GET
/api/categories/:id
- PUT
/api/categories/:id
- DELETE
/api/categories/:id
- GET
/api/categories/:id/books
- Description: Retrieves all books in a specific category.
- Response:
[ { "id": 3, "title": "Sword Art Online", "description": "Sword Art Online (SAO), one of the most recent games on the console, offers a gateway into the wondrous world of Aincrad, a vivid, medieval landscape where users can do anything within the limits of imagination.", "image_url": "https://cdn.myanimelist.net/images/anime/11/39717.jpg", "release_year": 2012, "price": 16, "total_page": 130, "thickness": "tebal", "category_id": 2, "created_at": "2024-11-17T20:27:38.3768Z", "created_by": "user1", "modified_at": "2024-11-17T13:27:38.377213Z", "modified_by": "" }, { "id": 4, "title": "Sword Art Online II", "description": "Approached by officials to assist in investigating the murders, Kazuto assumes his persona of Kirito once again and logs into Gun Gale Online, intent on stopping the killer.", "image_url": "https://cdn.myanimelist.net/images/anime/1223/121999.jpg", "release_year": 2014, "price": 16, "total_page": 95, "thickness": "tipis", "category_id": 2, "created_at": "2024-11-17T20:27:43.781472Z", "created_by": "user1", "modified_at": "2024-11-17T13:27:43.782148Z", "modified_by": "" }, ... ]
- GET
/api/books
- Description: Retrieves a list of all books.
- Response:
[ { "id": 1, "title": "Dr. Stone", "description": "Blinding green light strikes the Earth and petrifies mankind around the world—turning every single human into stone.", "image_url": "https://cdn.myanimelist.net/images/anime/1613/102576.jpg", "release_year": 2019, "price": 16, "total_page": 120, "thickness": "tebal", "category_id": 1, "created_at": "2024-11-17T18:50:07.163542Z", "created_by": "user1", "modified_at": "2024-11-17T11:50:07.164832Z", "modified_by": "" }, ... ]
- POST
/api/books
- Description: Creates a new book.
- Request Body:
{ "title": "Dr. Stone", "description": "Blinding green light strikes the Earth and petrifies mankind around the world—turning every single human into stone.", "image_url": "https://cdn.myanimelist.net/images/anime/1613/102576.jpg", "release_year": 2019, "price": 16, "total_page": 120, "category_id": 1 }
- Response:
{ "message": "Book created successfully" }
- GET
/api/books/:id
- Description: Retrieves a single book by its ID.
- Response:
{ "category_id": 1, "created_at": "2024-11-17T18:50:07.163542Z", "created_by": "user1", "description": "Blinding green light strikes the Earth and petrifies mankind around the world—turning every single human into stone.", "id": 1, "image_url": "https://cdn.myanimelist.net/images/anime/1613/102576.jpg", "modified_at": "2024-11-17T11:50:07.164832Z", "modified_by": "", "price": 16, "release_year": 2019, "thickness": "tebal", "title": "Dr. Stone", "total_page": 120 }
- PUT
/api/books/:id
- Description: Updates an existing book.
- Request Body:
{ "title": "Dr. Stone: Stone Wars", "description": "Senkuu has made it his goal to bring back two million years of human achievement and revive the entirety of those turned to statues.", "image_url": "https://cdn.myanimelist.net/images/anime/1711/110614.jpg", "release_year": 2021, "price": 16, "total_page": 90, "category_id": 1 }
- Response:
Verify
{ "message": "Book updated successfully" }
- DELETE
/api/books/:id