<commit_before><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;

use App\EveRoute;
use App\EveSystem;
use App\EveWaypointList;
use App\Repositories\EveRouteRepository;

use App\EveOnline\EveOAuthProvider;
use App\EveOnline\EveCREST;

class RouteController extends Controller
{
    protected $everoutes;
    private $eveoauth;

    function __construct(EveRouteRepository $everoutes)
    {
        $this->middleware('auth');
        $this->everoutes = $everoutes;
        $this->eveoauth = new EveOAuthProvider();
    }

    public function index(Request $request)
    {
        return view('everoutes.index', [
            'everoutes' => $this->everoutes->forUser($request->user())
        ]);
    }

    public function index_update(Request $request, EveRoute $everoute)
    {
        $this->authorize('update', $everoute);

        $waypoints = EveWaypointList::fromString($everoute->waypoints)->toArray();

        return view('everoutes.index', [
            'everoutes' => $this->everoutes->forUser($request->user()),
            'editroute' => $everoute,
            'editroutewaypoints' => $waypoints
        ]);
    }

    public function loadwaypoints(Request $request, EveRoute $everoute)
    {
        $this->authorize($everoute);

        $waypointsraw = explode(';', $everoute->waypoints);
        $waypoints = array_filter($waypointsraw, 'strlen');

        $evecrest = new EveCREST($this->eveoauth);

        try {
            $evecrest->setWaypoints($request, Auth::user()->userid, $waypoints);
            return redirect('/routes')->with('loadedsuccess', $everoute->name);
        } catch (\Exception $e) {
            return redirect('/routes')->with('exception', $e->getMessage());
        }
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'waypoints' => 'required'
        ]);

        $waypoints = EveWaypointList::fromArray($request->waypoints)->toString();

        $request->user()->everoutes()->create([
            'name' => $request->name,
            'waypoints' => $waypoints
        ]);

        return redirect('/routes');
    }

    public function update(Request $request, EveRoute $everoute)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'waypoints' => 'required'
        ]);

        $this->authorize($everoute);

        $waypoints = EveWaypointList::fromArray($request->waypoints)->toString();

        $everoute->update([
            'name' => $request->name,
            'waypoints' => $waypoints
        ]);<commit_msg>Added validation for route forms<commit_after><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;

use App\EveRoute;
use App\EveSystem;
use App\EveWaypointList;
use App\Repositories\EveRouteRepository;

use App\EveOnline\EveOAuthProvider;
use App\EveOnline\EveCREST;

class RouteController extends Controller
{
    protected $everoutes;
    private $eveoauth;

    function __construct(EveRouteRepository $everoutes)
    {
        $this->middleware('auth');
        $this->everoutes = $everoutes;
        $this->eveoauth = new EveOAuthProvider();
    }

    public function index(Request $request)
    {
        return view('everoutes.index', [
            'everoutes' => $this->everoutes->forUser($request->user())
        ]);
    }

    public function index_update(Request $request, EveRoute $everoute)
    {
        $this->authorize('update', $everoute);

        $waypoints = EveWaypointList::fromString($everoute->waypoints)->toArray();

        return view('everoutes.index', [
            'everoutes' => $this->everoutes->forUser($request->user()),
            'editroute' => $everoute,
            'editroutewaypoints' => $waypoints
        ]);
    }

    public function loadwaypoints(Request $request, EveRoute $everoute)
    {
        $this->authorize($everoute);

        $waypointsraw = explode(';', $everoute->waypoints);
        $waypoints = array_filter($waypointsraw, 'strlen');

        $evecrest = new EveCREST($this->eveoauth);

        try {
            $evecrest->setWaypoints($request, Auth::user()->userid, $waypoints);
            return redirect('/routes')->with('loadedsuccess', $everoute->name);
        } catch (\Exception $e) {
            return redirect('/routes')->with('exception', $e->getMessage());
        }
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|max:255|unique:eve_routes,name,NULL,id,user_id,'.Auth::user()->id,
            'waypoints.*' => 'required|distinct|exists:eve_systems,name'
        ]);

        $waypoints = EveWaypointList::fromArray($request->waypoints)->toString();

        $request->user()->everoutes()->create([
            'name' => $request->name,
            'waypoints' => $waypoints
        ]);

        return redirect('/routes');
    }

    public function update(Request $request, EveRoute $everoute)
    {
        $this->validate($request, [
            'name' => 'required|max:255|unique:eve_routes,name,'.$everoute->id.',id,user_id,'.Auth::user()->id,
            'waypoints.*' => 'required|distinct|exists:eve_systems,name'
        ]);

        $this->authorize($everoute);

        $waypoints = EveWaypointList::fromArray($request->waypoints)->toString();

        $everoute->update([
            'name' => $request->name,
            'waypoints' => $waypoints
        ]);
{'fraction_non_alphanumeric': 0.15738719068413393, 'fraction_numerical': 0.002183406113537118, 'mean_word_length': 2.734375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4655879', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1616, 'n_words': 260}