Skip to content

josehenriquepg/microservice-inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inventory Management API

This is a RESTful API for managing an inventory of products. It is built using Go (Golang) and provides basic CRUD operations for products.

📚 Features

  • 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.

🧱 Data Structure

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"`
}

⚙️ How to Use

Prerequisites

  • Go 1.16+ installed on your system.

Installation

  1. Clone the repository:
git clone https://github.com/josehenriquepg/microservice-inventory.git
cd microservice-inventory
  1. Build the project:
go build -o inventoryAPI

Running the Application

Run the compiled executable:

./inventoryAPI

The server will start running at http://localhost:8000

Endpoints

  1. 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
}
  1. List Products
  • URL: /products
  • Method: GET
  • Response:
[
  {
    "id": 1,
    "name": "Product Name",
    "description": "Product Description",
    "price": 99.99,
    "quantity": 10
  }
]
  1. Get Product by ID
  • URL: /products/{id}
  • Method: GET
  • Response:
{
  "id": 1,
  "name": "Product Name",
  "description": "Product Description",
  "price": 99.99,
  "quantity": 10
}
  1. 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
}
  1. Delete Product
  • URL: /products/{id}
  • Method: DELETE
  • Response:
[
  {
    "id": 1,
    "name": "Product Name",
    "description": "Product Description",
    "price": 99.99,
    "quantity": 10
  }
]

Running Tests

The project includes unit tests for all endpoints using the net/http/httptest package. To run the tests, follow these steps:

  1. Open a terminal and navigate to the project directory.
  2. 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.

Code Structure

  • 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.

💻 Technologies used

techs

🤝 Collaborators

Foto de José Henrique no GitHub
José Henrique

About

a RESTful API for Product Inventory Management

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages