<?php

namespace App\Http\Controllers;

use App\Cawangan;
use Illuminate\Http\Request;

class CawanganController extends Controller
{

    public function showAllCawangans()
    {
        return response()->json(Cawangan::on('mysql7')->get());
    }

    public function showOneCawangan($code_cawangan)
    {
        $cawangan = Cawangan::on('mysql7')
                    ->where('kodcaw','=',$code_cawangan)
                    ->first();
        return response()->json($cawangan);
    }

    public function create(Request $request)
    {
        $cawangan = Cawangan::create($request->all());

        return response()->json($cawangan, 201);
    }

    public function update($id, Request $request)
    {
        $cawangan = Cawangan::findOrFail($id);
        $cawangan->update($request->all());

        return response()->json($cawangan, 200);
    }

    public function delete($id)
    {
        Cawangan::findOrFail($id)->delete();
        return response('Deleted Successfully', 200);
    }
}