Professional Documents
Culture Documents
API (Backend)
OVERVIEW
Prasyarat
● Git
● PHP >= 7.2
● Composer
● PostgreSQL 10
● Node.js LTS
● Laravel
● DBeaver
Fitur
● otentikasi
● kelola data pengguna
● kelola data suplier
● kelola data barang
● transaksi
○ pembelian cash
○ penjualan cash
PERSIAPAN
Instalasi Laravel
Buat Database
● Buka DBeaver
● Buat koneksi ke PostgreSQL Local
● Buat database di local dengan nama workshop_pos
Edit Konfigurasi
# .env
APP_NAME=workshop-pos
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=workshop_pos
DB_USERNAME=[sesuai konfigurasi]
DB_PASSWORD=[sesuai konfigurasi]
php artisan migrate
DOKUMENTASI API
https://documenter.getpostman.com/view/3004240/S1a8yQMW
DESAIN DATABASE
ajaro-workshop-pos-erd.png
LARAVEL MIGRATION
Kita akan menambahkan kolom dan tabel dengan kebtuhan seperti pada desain database di atas
# 2019_06_30_203211_add_api_token_to_users.php
# up
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 80)
->unique()
->nullable()
->default(null);
});
# down
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('api_token');
});
# up
Schema::create('suppliers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 100);
$table->string('email')->unique();
$table->string('phone', 15);
$table->text('address');
$table->timestamps();
});
# down
Schema::dropIfExists('suppliers');
● Membuat tabel products (data barang) dan product categories (data kategori barang) dan modelnya
# up
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 100);
$table->timestamps();
});
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 200);
$table->text('desc')->nullable();
$table->bigInteger('category_id');
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
$table->bigInteger('price_purchase')->default(0);
$table->bigInteger('price_sale')->default(0);
$table->integer('stock')->unsigned()->default(0);
$table->timestamps();
});
# down
Schema::table('products', function (Blueprint $table) {
$table->dropForeign(['category_id']);
});
Schema::dropIfExists('categories');
Schema::dropIfExists('products');
● Membuat tabel transactions (data transaksi pembelian maupun penjualan) beserta modelnya
php artisan make:migration create_transactions_purchases_and_sales_table
# up
Schema::create('trx_purchases', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('supplier_id');
$table->foreign('supplier_id')
->references('id')
->on('suppliers')
->onDelete('cascade');
$table->bigInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->bigInteger('total')->default(0);
$table->timestamps();
});
# down
Schema::table('trx_purchases', function (Blueprint $table) {
$table->dropForeign(['supplier_id']);
$table->dropForeign(['user_id']);
});
Schema::table('trx_purchase_details', function (Blueprint $table) {
$table->dropForeign(['trx_purchase_id']);
$table->dropForeign(['product_id']);
});
Schema::table('trx_sales', function (Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::table('trx_sale_details', function (Blueprint $table) {
$table->dropForeign(['trx_sale_id']);
$table->dropForeign(['product_id']);
});
Schema::dropIfExists('trx_purchases');
Schema::dropIfExists('trx_sales');
MEMBUAT SEEDER
Proses inisiasi data (data awal) atau input data ke suatu tabel
# DatabaseSeeder
$this->call(InitSeeder::class);
# InitSeeder
use App\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
User::create([
'name' => 'jarjit',
'email' => 'jarjit@mail.com',
'password' => Hash::make('123456'),
'api_token' => Str::random(60),
]);
# tinker
>>> \App\User::all()
=> Illuminate\Database\Eloquent\Collection {#2977
all: [
App\User {#2978
id: 2,
name: "jarjit",
email: "jarjit@mail.com",
email_verified_at: null,
created_at: "2019-07-02 17:03:26",
updated_at: "2019-07-02 17:03:26",
api_token: "JsUYlKiNmDZkLmLW64fcOjvmv2iOCnpN22tOdsxi1a5DfQDqKWQYLHxjboGy",
},
],
}
# postman / curl
curl -X GET \
http://localhost:8000/api/user \
-H 'Accept: application/json' \
-H 'Authorization: Bearer JsUYlKiNmDZkLmLW64fcOjvmv2iOCnpN22tOdsxi1a5DfQDqKWQYLHxjbo
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Host: localhost:8000' \
-H 'Postman-Token: 99595f34-3003-4f94-bdf8-a4fc781e8085,ba54ee7f-7f58-4a6c-a79f-19e5
-H 'User-Agent: PostmanRuntime/7.15.0' \
-H 'accept-encoding: gzip, deflate' \
-H 'cache-control: no-cache' \
-H 'cookie: PHPSESSID=pf27fl8cl6bfb6pt0p8rm26bba; _csrf=eaeb4c81ef69c481754f396767f0
-b 'PHPSESSID=pf27fl8cl6bfb6pt0p8rm26bba; _csrf=eaeb4c81ef69c481754f396767f0f2e022c8
# cek file
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L16
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L23-L24
# cek file
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/User.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L26
# cek file
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Supplier.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L27
# cek file
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Category.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Product.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L28-L29
# cek file
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/TrxPurchase.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/TrxPurchaseDetail.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L30
# cek file
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/Http/Controllers/Api/V1/
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/TrxSale.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/app/TrxSaleDetail.php
# https://github.com/ajaroid/workshop-pos-api/blob/master/routes/api.php#L31
Klien (Frontend)
https://yeripratama.com/2019/07/04/ajaro-talent-scout-workshop-keynote/