<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\routes;

class routes_controller extends Controller
{
    public function index(){
        $route_list = routes::select('id', 'section_id', 'routeName')
        ->with(['company' => function ($query) {
            $query->select('id', 'name'); // Only select the necessary fields from the company table
        }])
        ->get();
        return response()->json($route_list);
    }

    public function store(Request $request){
        $validateData = $request->validate([
            'section_id' => 'required',
            'routeName' => 'required'
            ]);

        $route_list = routes::create($validateData);
        return response()->json($route_list);
    }

    public function update(Request $request, $id){
        $route_list = routes::find($id);
        $route_list->section_id = $request->section_id;
        $route_list->routeName = $request->routeName;
        $route_list->save();
        return response()->json($route_list);
    }
}
