|
| 1 | +<div align="center"> |
| 2 | + <!-- <img src="./images/api_logo.png" alt="API Logo" width="80" height="80"/> --> |
| 3 | +</div> |
| 4 | + |
| 5 | +<h1 align="center">Book Store Inventory</h1> |
| 6 | + |
| 7 | +<div align="center"> |
| 8 | +<img src="https://img.shields.io/badge/Sample%20Project%20for%20-Keploy%20-ea580c?style=for-the-badge" alt="Showcase Project"> |
| 9 | +<br> |
| 10 | +<img src="https://img.shields.io/badge/License-MIT-ed8796.svg?style=for-the-badge" alt="MIT License"> |
| 11 | +</div> |
| 12 | +<br> |
| 13 | + |
| 14 | +A simple book store inventory backend built using the Gin framework and SQLite as the database. |
| 15 | + |
| 16 | +## :computer: Tech Stack |
| 17 | +- [**Go**](https://go.dev/) : Programming language |
| 18 | +- [**Gin**](https://github.com/gin-gonic/gin) : Web framework |
| 19 | +- [**SQLite3**](https://github.com/mattn/go-sqlite3) : A sqlite3 database driver |
| 20 | + |
| 21 | +<br> |
| 22 | + |
| 23 | +## :book: How to Use / Run on Your Machine |
| 24 | + |
| 25 | +- ### Prerequisites: |
| 26 | + - Install Go (version >= 1.23.3): https://golang.org/dl/ |
| 27 | + - API Testing Tool |
| 28 | + - [Postman](https://www.postman.com/downloads/) |
| 29 | + - [Httpie](https://httpie.io/download) |
| 30 | + |
| 31 | +> I am using [**HttPie**](https://httpie.io/download) for API testing |
| 32 | +> you can use any tool as per your requirement |
| 33 | +> and `curl` is fine too :D |
| 34 | +
|
| 35 | +> [!IMPORTANT] |
| 36 | +> I am using WSL (Ubuntu). I recommend using Linux/WSL or macOS. |
| 37 | +
|
| 38 | +<br> |
| 39 | + |
| 40 | +### :package: Keploy Installation |
| 41 | +for detailed installation info visit |
| 42 | +https://keploy.io/docs/server/installation/#keploy-installation |
| 43 | + |
| 44 | +```sh |
| 45 | + curl --silent -O -L https://keploy.io/install.sh && source install.sh |
| 46 | +``` |
| 47 | + |
| 48 | +<br> |
| 49 | + |
| 50 | +### :toolbox: Setup Project Locally: |
| 51 | + |
| 52 | +- Clone the repository: |
| 53 | +```sh |
| 54 | +git clone https://github.com/keploy/samples-go.git |
| 55 | +``` |
| 56 | + |
| 57 | +- Navigate to the project directory: |
| 58 | +```sh |
| 59 | +cd book-store-inventory |
| 60 | +``` |
| 61 | + |
| 62 | +- Install dependencies: |
| 63 | +```sh |
| 64 | +go mod download |
| 65 | +``` |
| 66 | +- Generate Database with some default data: |
| 67 | + |
| 68 | +> [!IMPORTANT] |
| 69 | +> |
| 70 | +> `OPTIONAL STEP` as I have already included the `book_inventory.db` |
| 71 | +> |
| 72 | +> delete the `book_inventory.db` in `DB/book_inventory.db` and use below command to generate new one |
| 73 | +
|
| 74 | +```sh |
| 75 | +go run migrations/migration.go |
| 76 | +``` |
| 77 | + |
| 78 | +- Building the Application Binary: |
| 79 | +```sh |
| 80 | +go build |
| 81 | +``` |
| 82 | + |
| 83 | +> :rocket: You're all set! Now you have `book-store-inventory` |
| 84 | +
|
| 85 | + |
| 86 | +<br> |
| 87 | + |
| 88 | +### :inbox_tray: Capture the test cases using Keploy |
| 89 | + |
| 90 | +```sh |
| 91 | +sudo -E keploy record -c "./book-store-inventory |
| 92 | +``` |
| 93 | +> [!TIP] |
| 94 | +> If any error occur try above command without `sudo -E` |
| 95 | +
|
| 96 | +
|
| 97 | +<br> |
| 98 | +
|
| 99 | +### :satellite: Open the Postman/Httpie and Make Request |
| 100 | +
|
| 101 | +<div align="center"> |
| 102 | + <img src="./docs/assets/all_books.png" alt="GET" style="width: 48%;"> |
| 103 | + <img src="./docs/assets/book_by_id.png" alt="READ" style="width: 48%;"> |
| 104 | +</div> |
| 105 | +
|
| 106 | +<div align="center"> |
| 107 | + <img src="./docs/assets/add_book.png" alt="PUT" style="width: 48%;"> |
| 108 | + <img src="./docs/assets/delete_book.png" alt="DELETE" style="width: 48%;"> |
| 109 | +</div> |
| 110 | +
|
| 111 | +<br> |
| 112 | +
|
| 113 | +### :bookmark_tabs: Available REST API End Points |
| 114 | +
|
| 115 | +| Method | Endpoint | Description | |
| 116 | +|---------|-------------------------------------------------------|---------------------------------| |
| 117 | +| **GET** | `http://localhost:9090/api/v1/books/` | Get all books | |
| 118 | +| **GET** | `http://localhost:9090/api/v1/book/id/:book_id` | Get a book by ID | |
| 119 | +| **POST** | `http://localhost:9090/api/v1/book/add/` | Add a new book | |
| 120 | +| **DELETE** | `http://localhost:9090/api/v1/book/delete/` | Delete a book | |
| 121 | +| **PATCH** | `http://localhost:9090/api/v1/book/update/` | Update a book | |
| 122 | +| **GET** | `http://localhost:9090/api/test-api` | Test API (New Endpoint) | |
| 123 | +
|
| 124 | +
|
| 125 | +<br> |
| 126 | +
|
| 127 | +> [!TIP] |
| 128 | +> |
| 129 | +> for now just call `GET` for `http://localhost:9090/api/v1/books/` |
| 130 | +> |
| 131 | +> and record this test |
| 132 | +
|
| 133 | +<br> |
| 134 | +
|
| 135 | +### :jigsaw: Usage Examples |
| 136 | +
|
| 137 | +> [!IMPORTANT] |
| 138 | +> Add these json value as body while making request |
| 139 | +
|
| 140 | +- #### POST (Add New Book) |
| 141 | +
|
| 142 | +body of the request |
| 143 | +```json |
| 144 | +{ |
| 145 | + "title": "Gunaho ka Devta", |
| 146 | + "author": "Dharamvir Bharti", |
| 147 | + "price": 599.99, |
| 148 | + "published_date": "1949-01-01T00:00:00Z", |
| 149 | + "stock_quantity": 5 |
| 150 | +} |
| 151 | +``` |
| 152 | +
|
| 153 | +- #### DELETE (Delete Book by ID) |
| 154 | +
|
| 155 | +body of the request |
| 156 | +```json |
| 157 | +{ |
| 158 | + "book_id": 6 |
| 159 | +} |
| 160 | +``` |
| 161 | +
|
| 162 | +- #### PATCH (Update Book by ID) |
| 163 | +
|
| 164 | +body of the request |
| 165 | +```json |
| 166 | +{ |
| 167 | + "book_id": 6, |
| 168 | + "title": "Gunaho ka God (latest edition)", |
| 169 | + "author": "Dharamvir Bharti Ji", |
| 170 | + "price": 599.99, |
| 171 | + "published_date": "1949-01-01T00:00:00Z", |
| 172 | + "stock_quantity": 5 |
| 173 | +} |
| 174 | +``` |
| 175 | +
|
| 176 | +<div align="center"> |
| 177 | + <img src="./docs/assets/keploy_test_capture.png" alt="TEST API"> |
| 178 | +</div> |
| 179 | +
|
| 180 | +<br> |
| 181 | +
|
| 182 | +### :test_tube: Run the captured test case |
| 183 | +
|
| 184 | +> [!IMPORTANT] |
| 185 | +> Stop the test capturing by pressing `ctrl+c` or `ctrl+z` |
| 186 | +
|
| 187 | +start the test using keploy |
| 188 | +```sh |
| 189 | +sudo -E keploy test -c "./book-store-inventory" --delay 1 |
| 190 | +``` |
| 191 | +
|
| 192 | +<div align="center"> |
| 193 | + <img src="./docs/assets/keploy_test_run_1.png" alt="PUT" style="width: 48%;"> |
| 194 | + <img src="./docs/assets/keploy_test_run_2.png" alt="DELETE" style="width: 48%;"> |
| 195 | +</div> |
| 196 | +
|
| 197 | +<br> |
| 198 | +
|
| 199 | +## :seedling: Todo / Future Improvements |
| 200 | +- [x] Show All Books Data |
| 201 | +- [x] Get Particular Book By ID |
| 202 | +- [x] Add New Book Data |
| 203 | +- [x] Delete Book Data |
| 204 | +- [x] Update Book Data |
| 205 | +- [ ] UI for this Application (NextJS/Vite-React) |
| 206 | +
|
| 207 | +
|
| 208 | +## :compass: About |
| 209 | +This project was created as a sample to understand the working of Keploy. |
0 commit comments