<?php

namespace App\Http\Controllers\tatausaha;

use Illuminate\Support\Facades\Response;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Ruang;

class RuangController extends Controller
{
    public function index()
    {
        $ruang = Ruang::all();

        return view('tatausaha.ruang.index',['ruang'=>$ruang]);
    }
    public function store(Request $request)
    {
        $this->validate($request,['kode'=> 'required','keterangan'=>'required']);

        $ruang = new Ruang();
        $ruang->kode = $request->kode;
        $ruang->keterangan = $request->keterangan;
        $ruang->save();

        return response()->json( $ruang );
    }

    public function update(Request $request)
    {
        $this->validate($request,[
            'kode'=>'required',
            'keterangan' => 'required'
        ]);

        $ruang = Ruang::findOrFail($request->id);
        $ruang-> kode = $request->kode;
        $ruang-> keterangan = $request->keterangan;
        $ruang->save();
        return response()->json($ruang);
    }

    public function destroy(Request $request)
    {
        $ruang = Ruang::find($request->id);
        $ruang->delete();
        return response()->json($ruang);
    }
}
