You are on page 1of 8

Ajaro - Materi AjaroCodingDay#1

Materi ini digunakan untuk workshop ajaro pada tanggal Today

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

composer create-project --prefer-dist laravel/laravel coding-day-pos-api

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]

Menjalankan Database Migration

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

● Menambahkan kolom api_token pada tabel users

php artisan make:migration add_api_token_to_users

# 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');
});

php artisan migrate

● Membuat tabel suppliers (data suplier) dan modelnya

php artisan make:model Supplier -m

# 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');

php artisan migrate

● Membuat tabel products (data barang) dan product categories (data kategori barang) dan modelnya

php artisan make:migration create_products_and_product_categories_table


php artisan make:model Category
php artisan make:model Product

# 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');

php artisan migrate

● 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();
});

Schema::create('trx_purchase_details', function (Blueprint $table) {


    $table->bigIncrements('id');
    $table->bigInteger('trx_purchase_id');
    $table->foreign('trx_purchase_id')
        ->references('id')
        ->on('trx_purchases')
        ->onDelete('cascade');
    $table->bigInteger('product_id');
    $table->foreign('product_id')
        ->references('id')
        ->on('products')
        ->onDelete('cascade');
    $table->integer('qty')->unsigned()->default(0);
    $table->bigInteger('subtotal')->default(0);
    $table->timestamps();
});

Schema::create('trx_sales', function (Blueprint $table) {


    $table->bigIncrements('id');
    $table->bigInteger('user_id');
    $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
    $table->bigInteger('total')->default(0);
    $table->bigInteger('pay')->default(0);
    $table->bigInteger('change')->default(0);
    $table->timestamps();
});

Schema::create('trx_sale_details', function (Blueprint $table) {


    $table->bigIncrements('id');
    $table->bigInteger('trx_sale_id');
    $table->foreign('trx_sale_id')
        ->references('id')
        ->on('trx_sales')
        ->onDelete('cascade');
    $table->bigInteger('product_id');
    $table->foreign('product_id')
        ->references('id')
        ->on('products')
        ->onDelete('cascade');
    $table->integer('qty')->unsigned()->default(0);
    $table->bigInteger('subtotal')->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');

php artisan migrate

php artisan make:model TrxPurchase


php artisan make:model TrxPurchaseDetail
php artisan make:model TrxSale
php artisan make:model TrxSaleDetail

MEMBUAT SEEDER

Proses inisiasi data (data awal) atau input data ke suatu tabel

php artisan make:seeder InitSeeder

# 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),
]);

php artisan db:seed

MEMBUAT API OTENTIKASI

php artisan make:auth


php artisan tinker

# 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

php artisan make:controller Api/V1/AuthController

# 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

MEMBUAT API KELOLA PENGGUNA

php artisan make:controller --api Api/V1/UserController

# 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

MEMBUAT API KELOLA SUPLIER

php artisan make:controller --api Api/V1/SupplierController

# 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
 

MEMBUAT API KELOLA KATEGORI PRODUK & PRODUK

php artisan make:controller --api Api/V1/CategoryController


php artisan make:controller --api Api/V1/ProductController

# 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

MEMBUAT API TRANSAKSI PEMBELIAN

php artisan make:controller --api Api/V1/TrxPurchaseController

# 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

MEMBUAT API TRANSAKSI PENJUALAN

 php artisan make:controller --api Api/V1/TrxSaleController

# 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

MENAMBAHKAN CORS HEADERS

mengikuti step : https://github.com/barryvdh/laravel-cors

Klien (Frontend)

https://yeripratama.com/2019/07/04/ajaro-talent-scout-workshop-keynote/

You might also like