This is a RESTful API for managing an inventory of products. It is built using Go (Golang) and provides basic CRUD operations for products.
- Add Product: Add a new product to the inventory.
- List Products: List all products in the inventory.
- Get Product by ID: Retrieve a specific product by its ID.
- Update Product: Update the details of an existing product.
- Delete Product: Remove a product from the inventory.
The product data structure is defined as follows:
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Price float64 `json:"price"`
Quantity int `json:"quantity"`
}
- Go 1.16+ installed on your system.
- Clone the repository:
git clone https://github.com/josehenriquepg/microservice-inventory.git
cd microservice-inventory
- Build the project:
go build -o inventoryAPI
Run the compiled executable:
./inventoryAPI
The server will start running at http://localhost:8000
- Add Product
- URL: /products
- Method: POST
- Request Body:
{
"name": "Product Name",
"description": "Product Description",
"price": 99.99,
"quantity": 10
}
- Response:
{
"id": 1,
"name": "Product Name",
"description": "Product Description",
"price": 99.99,
"quantity": 10
}
- List Products
- URL: /products
- Method: GET
- Response:
[
{
"id": 1,
"name": "Product Name",
"description": "Product Description",
"price": 99.99,
"quantity": 10
}
]
- Get Product by ID
- URL: /products/{id}
- Method: GET
- Response:
{
"id": 1,
"name": "Product Name",
"description": "Product Description",
"price": 99.99,
"quantity": 10
}
- Update Product
- URL: /products/{id}
- Method: PUT
- Request Body:
{
"name": "Updated Name",
"description": "Updated Description",
"price": 109.99,
"quantity": 15
}
- Response:
{
"id": 1,
"name": "Updated Name",
"description": "Updated Description",
"price": 109.99,
"quantity": 15
}
- Delete Product
- URL: /products/{id}
- Method: DELETE
- Response:
[
{
"id": 1,
"name": "Product Name",
"description": "Product Description",
"price": 99.99,
"quantity": 10
}
]
The project includes unit tests for all endpoints using the net/http/httptest package. To run the tests, follow these steps:
- Open a terminal and navigate to the project directory.
- Run the tests using the following command:
go test -v
The -v flag is used to run the tests in verbose mode, which will display detailed output for each test case.
- main.go: Contains the main logic of the application, including handlers for adding, listing, updating, and deleting products.
- main_test.go: Contains the unit tests for the application.
José Henrique |