REST API sederhana untuk manajemen produk, sumber barang (supplier), dan transaksi penjualan menggunakan Golang dan Gin framework.
# Install dependencies
go mod tidy
# Jalankan server
go run main.go
Server akan berjalan di http://localhost:8080
Method | Endpoint | Deskripsi |
---|---|---|
GET | /products |
Ambil semua produk |
GET | /products/:id |
Ambil produk berdasarkan ID |
POST | /products |
Tambah produk baru |
PUT | /products/:id |
Update produk |
DELETE | /products/:id |
Hapus produk |
Method | Endpoint | Deskripsi |
---|---|---|
GET | /sources |
Ambil semua source |
GET | /sources/:id |
Ambil source berdasarkan ID |
POST | /sources |
Tambah source baru |
PUT | /sources/:id |
Update source |
DELETE | /sources/:id |
Hapus source |
Method | Endpoint | Deskripsi |
---|---|---|
POST | /transactions |
Buat transaksi baru |
GET | /transactions |
Ambil semua transaksi |
GET | /transactions/:id |
Ambil transaksi berdasarkan ID |
{
"id": "string",
"name": "string",
"description": "string",
"price": 0,
"stock": 0,
"source_id": "string"
}
{
"id": "string",
"name": "string"
}
{
"id": "string",
"product_id": "string",
"quantity": 0,
"total": 0
}
Semua response menggunakan format yang konsisten:
{
"message": "string",
"data": {},
"error": null
}
curl -X POST http://localhost:8080/sources \
-H "Content-Type: application/json" \
-d '{
"name": "Supplier C"
}'
Response:
{
"message": "Source created successfully",
"data": {
"id": "3",
"name": "Supplier C"
},
"error": null
}
curl -X POST http://localhost:8080/products \
-H "Content-Type: application/json" \
-d '{
"name": "Keyboard",
"description": "Mechanical keyboard",
"price": 500000,
"stock": 25,
"source_id": "1"
}'
Response:
{
"message": "Product created successfully",
"data": {
"id": "3",
"name": "Keyboard",
"description": "Mechanical keyboard",
"price": 500000,
"stock": 25,
"source_id": "1"
},
"error": null
}
curl -X POST http://localhost:8080/transactions \
-H "Content-Type: application/json" \
-d '{
"product_id": "1",
"quantity": 2
}'
Response:
{
"message": "Transaction created successfully",
"data": {
"id": "1",
"product_id": "1",
"quantity": 2,
"total": 30000000
},
"error": null
}
curl -X GET http://localhost:8080/products
Response:
{
"message": "Products retrieved successfully",
"data": [
{
"id": "1",
"name": "Laptop",
"description": "Gaming laptop",
"price": 15000000,
"stock": 8,
"source_id": "1"
},
{
"id": "2",
"name": "Mouse",
"description": "Wireless mouse",
"price": 250000,
"stock": 50,
"source_id": "2"
}
],
"error": null
}
curl -X GET "http://localhost:8080/products?source_id=1"
curl -X PUT http://localhost:8080/products/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Gaming Laptop Updated",
"description": "High-end gaming laptop",
"price": 18000000,
"stock": 5,
"source_id": "1"
}'
curl -X DELETE http://localhost:8080/products/1
name
: Tidak boleh kosongprice
: Harus lebih besar dari 0stock
: Harus lebih besar atau sama dengan 0source_id
: Harus ada di daftar source
name
: Tidak boleh kosong
quantity
: Harus lebih besar dari 0product_id
: Harus ada di daftar produk- Stock produk harus mencukupi
200
: Success201
: Created400
: Bad Request (validation error)404
: Not Found500
: Internal Server Error
{
"message": "Validation failed",
"data": null,
"error": "Price must be greater than 0"
}
- Filter Produk: Query parameter
source_id
untuk filter produk berdasarkan source - Validasi Lengkap: Validasi untuk semua input
- Konsistensi Response: Format response yang konsisten
- Logger Middleware: Logging untuk setiap request
e-commerce/
ββββproducts
ββββsource
ββββtransaction
ββββusers
- Data disimpan di memory (tidak persisten)
- Saat transaksi dibuat, stock produk akan berkurang otomatis
- ID dihasilkan secara otomatis menggunakan counter
- Semua endpoint menggunakan format response yang konsisten