Kasir/Documentation/Produk.md
2025-09-09 11:59:00 +07:00

377 lines
8.1 KiB
Markdown

# API Documentation - Produk & Foto Sementara
## Produk API
### 1. Get All Products
**GET** `/api/produk`
Mendapatkan semua produk dengan jumlah item dan foto.
**Response Success (200):**
```json
[
{
"id": 1,
"nama": "Cincin Berlian",
"kategori": "cincin",
"berat": 2.5,
"kadar": 18,
"harga_per_gram": 850000,
"harga_jual": 2500000,
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z",
"deleted_at": null,
"items_count": 5,
"foto": [
{
"id": 1,
"id_produk": 1,
"url": "http://localhost:8000/storage/foto/cincin1.jpg",
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z"
}
]
}
]
```
---
### 2. Get Single Product
**GET** `/api/produk/{id}`
Mendapatkan detail produk berdasarkan ID dengan foto dan items.
**Parameters:**
- `id` (integer, required) - Product ID
**Response Success (200):**
```json
{
"id": 1,
"nama": "Cincin Berlian",
"kategori": "cincin",
"berat": 2.5,
"kadar": 18,
"harga_per_gram": 850000,
"harga_jual": 2500000,
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z",
"deleted_at": null,
"foto": [
{
"id": 1,
"id_produk": 1,
"url": "http://localhost:8000/storage/foto/cincin1.jpg",
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z"
}
],
"items": [
{
"id": 1,
"id_produk": 1,
"status": "tersedia",
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z"
}
]
}
```
---
### 3. Create Product
**POST** `/produk`
Membuat produk baru dengan foto dari foto sementara.
**Headers:**
```
Content-Type: application/json
```
**Request Body:**
```json
{
"nama": "Cincin Berlian",
"kategori": "cincin",
"berat": 2.5,
"kadar": 18,
"harga_per_gram": 850000,
"harga_jual": 2500000,
"id_user": 1
}
```
**Request Body Parameters:**
- `nama` (string, required) - Nama produk (max: 100 karakter)
- `kategori` (string, required) - Kategori produk: `cincin`, `gelang`, `kalung`, `anting`
- `berat` (numeric, required) - Berat produk dalam gram
- `kadar` (integer, required) - Kadar emas
- `harga_per_gram` (numeric, required) - Harga per gram
- `harga_jual` (numeric, required) - Harga jual
- `id_user` (integer, optional) - User ID untuk mengambil foto sementara
**Response Success (201):**
```json
{
"message": "Produk berhasil dibuat",
"data": {
"id": 1,
"nama": "Cincin Berlian",
"kategori": "cincin",
"berat": 2.5,
"kadar": 18,
"harga_per_gram": 850000,
"harga_jual": 2500000,
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z",
"foto": [
{
"id": 1,
"id_produk": 1,
"url": "http://localhost:8000/storage/foto/cincin1.jpg",
"created_at": "2025-08-27T10:30:00.000000Z",
"updated_at": "2025-08-27T10:30:00.000000Z"
}
]
}
}
```
**Response Error (422):**
```json
{
"message": "The given data was invalid.",
"errors": {
"nama": ["Nama produk harus diisi."],
"kategori": ["Kategori harus salah satu dari cincin, gelang, kalung, atau anting."],
"berat": ["Berat harus diisi."],
"kadar": ["Kadar harus diisi"],
"harga_per_gram": ["Harga per gram harus diisi"],
"harga_jual": ["Harga jual harus diisi"]
}
}
```
---
### 4. Update Product
**PUT** `/produk/{id}`
Memperbarui produk berdasarkan ID dengan opsi foto.
**Parameters:**
- `id` (integer, required) - Product ID
**Headers:**
```
Content-Type: application/json
```
**Request Body:**
```json
{
"nama": "Cincin Berlian Updated",
"kategori": "cincin",
"berat": 3.0,
"kadar": 22,
"harga_per_gram": 900000,
"harga_jual": 3000000,
"id_user": 1,
"hapus_foto_lama": true
}
```
**Request Body Parameters:**
- `nama` (string, required) - Nama produk (max: 100 karakter)
- `kategori` (string, required) - Kategori produk: `cincin`, `gelang`, `kalung`, `anting`
- `berat` (numeric, required) - Berat produk dalam gram
- `kadar` (integer, required) - Kadar emas
- `harga_per_gram` (numeric, required) - Harga per gram
- `harga_jual` (numeric, required) - Harga jual
- `id_user` (integer, optional) - User ID untuk mengambil foto sementara baru
- `hapus_foto_lama` (boolean, optional) - Flag untuk menghapus foto lama (default: false)
**Response Success (200):**
```json
{
"message": "Produk berhasil diubah",
"data": {
"id": 1,
"nama": "Cincin Berlian Updated",
"kategori": "cincin",
"berat": 3.0,
"kadar": 22,
"harga_per_gram": 900000,
"harga_jual": 3000000,
"updated_at": "2025-08-27T11:30:00.000000Z",
"foto": [
{
"id": 2,
"id_produk": 1,
"url": "http://localhost:8000/storage/foto/cincin2.jpg",
"created_at": "2025-08-27T11:30:00.000000Z",
"updated_at": "2025-08-27T11:30:00.000000Z"
}
]
}
}
```
---
### 5. Delete Product
**DELETE** `/produk/{id}`
Menghapus produk berdasarkan ID (soft delete) beserta foto-foto terkait.
**Parameters:**
- `id` (integer, required) - Product ID
**Response Success (200):**
```json
{
"message": "Produk berhasil dihapus."
}
```
**Response Error (404):**
```json
{
"message": "No query results for model [App\\Models\\Produk] 999"
}
```
---
## Foto Sementara API
### 1. Upload Foto Sementara
**POST** `/foto/upload`
Upload foto sementara yang akan digunakan saat create/update produk.
**Headers:**
```
Content-Type: multipart/form-data
```
**Request Body (Form Data):**
- `id_produk` (integer, required) - Product ID (untuk validasi exists di tabel produk)
- `foto` (file, required) - File foto (jpg, jpeg, png, max: 2MB)
**Response Success (201):**
```json
{
"message": "Foto berhasil disimpan"
}
```
**Response Error (422):**
```json
{
"message": "The given data was invalid.",
"errors": {
"id_produk": ["The id_produk field is required."],
"foto": ["The foto field is required."]
}
}
```
**Response Error - Invalid File (422):**
```json
{
"message": "The given data was invalid.",
"errors": {
"foto": [
"The foto must be an image.",
"The foto must be a file of type: jpg, jpeg, png.",
"The foto may not be greater than 2048 kilobytes."
]
}
}
```
---
### 2. Hapus Foto Sementara
**DELETE** `/foto/hapus/{id}`
Menghapus foto sementara berdasarkan ID.
**Parameters:**
- `id` (integer, required) - Foto Sementara ID
**Response Success (200):**
```json
{
"message": "Foto berhasil dihapus"
}
```
**Response Error (404):**
```json
{
"message": "No query results for model [App\\Models\\FotoSementara] 999"
}
```
---
### 3. Get Foto Sementara User
**DELETE** `/foto/get/{user_id}`
Ambil semua foto sementara milik user tertentu.
**Parameters:**
- `user_id` (integer, required) - User ID
**Response Success (200):**
```json
[
{
"id": 2,
"id_user": 1,
"url": "http://localhost:8000/storage/foto/cincin2.jpg",
"created_at": "2025-08-27T11:30:00.000000Z",
"updated_at": "2025-08-27T11:30:00.000000Z"
}
]
```
---
### 4. Reset Foto Sementara User
**DELETE** `/foto/reset/{user_id}`
Menghapus semua foto sementara milik user tertentu.
**Parameters:**
- `user_id` (integer, required) - User ID
**Response Success (200):**
```json
{
"message": "Foto sementara berhasil direset"
}
```
---
## Workflow Penggunaan
### Membuat Produk dengan Foto:
1. Upload foto menggunakan `POST /api/foto/upload` dengan `id_produk` dummy atau existing
2. Simpan `id_user` yang digunakan saat upload
3. Create produk dengan `POST /api/produk` dan sertakan `id_user`
4. Foto sementara akan otomatis dipindahkan ke foto permanent
### Update Produk dengan Foto Baru:
1. Upload foto baru dengan `POST /api/foto/upload`
2. Update produk dengan `PUT /api/produk/{id}`, sertakan `id_user` dan `hapus_foto_lama: true`
3. Foto lama akan dihapus dan foto sementara dipindahkan ke permanent
### Membersihkan Foto Sementara:
- Gunakan `DELETE /api/foto/reset/{user_id}` untuk menghapus semua foto sementara user
- Atau `DELETE /api/foto/hapus/{id}` untuk menghapus foto sementara spesifik