---
# System prepended metadata

title: Product

---

# Product 

[TOC]

## Entity Diagram
![](https://i.imgur.com/8AOoW9i.png)

## Schema
![](https://i.imgur.com/kWhNfMR.png)


## Product Endpoints
Base Path: __/products__

### __/createProduct__ @PostMapping
Adds a new record to product db. 
```
1. Get payload from request. 
2. Perform Validation of Request Body
    - Check if all required fields/keys are present, such as salon_id, name, price, etc. 
    - Perform input validation on fields, check if salon_id is valid, name is not empty, etc
    2.1 Continue to 3, after validation
    2.1 Throw bad request error, if validation step fails
3. Initialize some fields to default values if they are absent. 
4. Initialize created_at, updated_at to current datetime. 
5. Initialize created_by to admin who called the endpoint
6. Intitialize deleted_at to null
7. Add this new record to table
4. Return OK response to caller 
```
        
Sample Request JSON Body
```
{
    "salonId": 1, 
    "name": "Product#4", 
    "introduction": "Product Desc",
    "usage": "Product Usage",
    "volume": 20,
    "createdBy": "user"
}
```
        
### __/update__ 
Updates product information. 
```
1. Get pathVariable of id
2. Queries the db for the product using the id as key
    2.1. Throws bad request error, if no product is found
3. Check if product is deleted, i.e. if deleted_at is non-null
4. Check Request Params. Request Params will contain the fields and values to be updated. 
5. Perform input validation on the values of the params, similar to step 2 of /add.
    4.1. Throws error, if validation fails
6. Update the fields of the record with the new values
7. Update updated_at field to current datetime. 
8. Save the record back to table. 
9. Return OK response to caller 
```

Sample Request Json Body
```
{
    "id": 59, 
    "dto":
    {
    "salonId": 1, 
    "name": "Another Name", 
    "introduction": "Product Desc",
    "usage": "Product Usage",
    "volume": 40,
    "price": 10, 
    "createdBy": "user"
    }
}
```


### /delete/{id} @DeleteMapping
Deletes an existing product from table.
```
1. Get pathVariable of id
2. Queries the db for the product using the id as key. 
    2.1. Throws bad request error, if no product is found
3. Update deleted_at field of product to current datetime
4. Return OK response to caller 
```

Sample Request Body

```
{
    "id": 59
}
```

### /get/{id} @GetMapping
Gets a specific record from the table. 
```
1. Get pathVariable of id
2. Queries the db for the product using the id as key. 
    2.1. Throws bad request error, if no product is found
3. Return the record to caller in the response body.
```

Sample Request Body

```
{
    "id": 59
}
```

Sample Response Body
```
{
    "resultMessage": "success",
    "result": 0,
    "error": null,
    "data": {
        "id": 61,
        "salonId": 1,
        "name": "Product#4",
        "introduction": "Product Desc",
        "usage": "Product Usage",
        "volume": 20,
        "price": 0,
        "createdAt": "2022-06-10T03:04:37.000+00:00",
        "updatedAt": "2022-06-10T03:04:37.000+00:00",
        "deletedAt": null,
        "createdBy": "user"
    }
}
```

### /all @GetMapping
Gets all records from table. 
```
1. Queries all products from table
2. Filter the results and return the ones that have non-null values for deleted_at
3. Return the filtered results to the caller in the response body. 
```

Sample Response Body
```
{
    "resultMessage": "success",
    "result": 0,
    "error": null,
    "data": [
        {
            "id": 58,
            "salonId": 1,
            "name": "New Name",
            "introduction": "Desc",
            "usage": "Usage",
            "volume": 80,
            "price": 100,
            "createdAt": "2022-06-10T02:40:46.000+00:00",
            "updatedAt": "2022-06-10T02:40:46.000+00:00",
            "deletedAt": null,
            "createdBy": "user"
        },
        .
        .
    ]
}
```
    
