# Postman Testing Guide - Car Update Endpoint

## Update Car Endpoint

### Request Details
- **Method**: `PUT`
- **URL**: `http://localhost:8000/api/v1/cars/{id}`
- **Content-Type**: `form-data` (multipart/form-data)

### Example: Update Car with ID 1
```
PUT http://localhost:8000/api/v1/cars/1
```

---

## Step-by-Step Instructions in Postman

### 1. Set Request Method and URL
- Select `PUT` from the dropdown
- Enter URL: `http://localhost:8000/api/v1/cars/1`

### 2. Select Body Type
- Click on the **Body** tab
- Select **form-data** radio button

### 3. Add Form Fields

#### Text/String Fields
Add these fields with **Key** and **Value**:

| Key | Value | Type |
|-----|-------|------|
| `title` | Toyota Camry 2022 | text |
| `price` | 25000 | text |
| `stock_no` | CAR-12345 | text |
| `car_location` | Tokyo, Japan | text |
| `mileage` | 15000 | text |
| `mileage_type` | km | text |
| `displacement` | 2000 | text |
| `seating_capacity` | 5 | text |
| `cubic_meter` | 12 | text |
| `chassis_no` | ABC123456DEF | text |
| `model_code` | CAMRY-2022 | text |
| `description` | Well maintained car | text |
| `steering` | Right | text |
| `transmission` | Automatic | text |
| `fuel` | Petrol | text |
| `drive_system` | 2WD | text |
| `repaired` | No | text |
| `doors` | 4 | text |
| `model_year` | 2022-01-01 | text |
| `car_up_date` | 2026-06-02 | text |

#### Foreign Key Fields
| Key | Value | Type |
|-----|-------|------|
| `brand_id` | 1 | text |
| `model_id` | 1 | text |
| `body_style_id` | 1 | text |
| `color_id` | 1 | text |

#### Boolean Fields
| Key | Value | Type |
|-----|-------|------|
| `is_featured` | 1 | text |
| `is_gallery` | 1 | text |

---

## Array Fields (Pivot Relationships)

### How to Add Array Fields in Postman

For each array field, add **multiple entries** with the same key:

#### Example: carCondition Array
1. Click **+ Add**
2. Key: `carCondition[]` | Value: `1` | Type: `text`
3. Click **+ Add** again
4. Key: `carCondition[]` | Value: `2` | Type: `text`
5. Click **+ Add** again
6. Key: `carCondition[]` | Value: `3` | Type: `text`

#### All Array Fields
- `carCondition[]` - IDs from condition_info table (1, 2, 3, etc.)
- `standardFeature[]` - IDs from standard_feature table
- `equipment[]` - IDs from equipment table
- `interiorExterior[]` - IDs from interior_exterior table
- `selfDriving[]` - IDs from self_driving table
- `safetyEquipment[]` - IDs from safety_equipment table

**Example:**
```
carCondition[]    1
carCondition[]    2
carCondition[]    3
standardFeature[] 1
standardFeature[] 2
equipment[]       1
```

---

## File Upload

### Adding Image Files

1. Add a new field in form-data
2. Key: `imageFile`
3. **Change Type from "text" to "File"** (dropdown on the right side)
4. Click **Select Files** button and choose an image
5. To add multiple images:
   - Click **+ Add**
   - Key: `imageFile` (same key, different file)
   - Type: `File`
   - Select another image

**Supported Formats**: JPEG, PNG, JPG, GIF
**Max Size**: 2MB per image

---

## Headers (Optional)

Postman automatically sets the correct headers for form-data, but you can verify:

| Header | Value |
|--------|-------|
| Content-Type | multipart/form-data; boundary=---- |
| Accept | application/json |

---

## Complete Example Request

### URL
```
http://localhost:8000/api/v1/cars/1
```

### Form-Data Body
```
title                   = Toyota Camry 2022
price                   = 25000
brand_id                = 1
model_id                = 1
body_style_id           = 1
color_id                = 1
stock_no                = CAR-12345
car_location            = Tokyo, Japan
mileage                 = 15000
mileage_type            = km
displacement            = 2000
seating_capacity        = 5
cubic_meter             = 12
chassis_no              = ABC123456DEF
model_code              = CAMRY-2022
description             = Well maintained car
steering                = Right
transmission            = Automatic
fuel                    = Petrol
drive_system            = 2WD
doors                   = 4
repaired                = No
model_year              = 2022-01-01
car_up_date             = 2026-06-02
is_featured             = 1
is_gallery              = 1
carCondition[]          = 1
carCondition[]          = 2
carCondition[]          = 3
standardFeature[]       = 1
standardFeature[]       = 2
equipment[]             = 1
interiorExterior[]      = 1
selfDriving[]           = 1
safetyEquipment[]       = 1
imageFile               = [Select image file 1]
imageFile               = [Select image file 2]
```

---

## Expected Response (Success)

**Status Code**: 200 OK

```json
{
  "code": 200,
  "hasError": false,
  "result": {
    "id": 1,
    "message": "Car updated successfully"
  }
}
```

---

## Expected Response (Error - Car Not Found)

**Status Code**: 404 Not Found

```json
{
  "code": 404,
  "hasError": true,
  "message": "Not Found",
  "errors": {}
}
```

---

## Expected Response (Validation Error)

**Status Code**: 422 Unprocessable Entity

```json
{
  "code": 422,
  "hasError": true,
  "message": "Validation failed",
  "errors": {
    "stock_no": [
      "The stock no has already been taken."
    ],
    "price": [
      "The price must be a number."
    ]
  }
}
```

---

## Testing Tips

1. **Partial Updates**: You don't need to send all fields. Only send the fields you want to update.
   
2. **Optional Fields**: All fields except the route parameter `{id}` are optional.

3. **Array Fields**: Leave array fields empty if you don't want to update relationships.

4. **File Uploads**: Optional. Only include imageFile if you want to add new images.

5. **Date Format**: Use `YYYY-MM-DD` format for dates (e.g., `2022-01-15`).

6. **Boolean Values**: Use `1` for true and `0` (or omit) for false.

7. **ID Validation**: Make sure foreign key IDs exist in their respective tables (brands, models, body_styles, colors, etc.).

---

## Quick Test Commands

### Test 1: Update Only Basic Info
```
PUT /api/v1/cars/1

title     = New Car Title
price     = 30000
stock_no  = CAR-99999
```

### Test 2: Update with Relationships
```
PUT /api/v1/cars/1

title              = Updated Car
carCondition[]     = 1
carCondition[]     = 2
standardFeature[]  = 1
```

### Test 3: Update with File Upload
```
PUT /api/v1/cars/1

title        = Car with Images
imageFile    = [image1.jpg]
imageFile    = [image2.jpg]
```

---

## Troubleshooting

### Issue: "The stock no has already been taken"
- Ensure `stock_no` is unique (not used by another car)
- Or omit this field if not updating it

### Issue: "Foreign key validation error"
- Verify the ID values exist in their respective tables
- Example: `brand_id: 1` must exist in brands table

### Issue: "File upload failed"
- Check file format (JPEG, PNG, JPG, GIF only)
- Check file size (max 2MB)
- Ensure the FILE_PATH directory exists and is writable

### Issue: "Validation error on array fields"
- Ensure array fields are separated with `[]` suffix
- Example: `carCondition[]` not `carCondition`

