<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\UserEloquent;

class VideoGameController extends Controller
{

    public function addUser( Request $request )
    {
        // On vérifie qu'on a bien reçu les données en POST
        if ( !$request->has(['login','password','confirm','age']) )
            return redirect('signup')->with('message',"Some POST data are missing.");

        if ( $request->input('password') !== $request->input('confirm') )
            return redirect('signup')->with('message',"The two passwords differ.");

        //On crée l'utilisateur
        $user = new UserEloquent;
        $user->user = $request->input('login');
        $user->password = password_hash($request->input('password'),PASSWORD_DEFAULT);
        $user->age = $request->input('age');

        try {
            // On crée l'utilisateur dans la BDD
            $user->save();
        }
        catch (\Illuminate\Database\QueryException $e) {
            return redirect('signup')->with('message','This login is still used. Please choose another one.');
        }

        // Si tout est ok, on indique que le compte est crée et on se rend sur signin
        return redirect('signin')->with('message',"Account created! Now, signin.");
    }
    public function addgame(Request $request){

        if (!$request->has(['name','url','desc'])){
            return redirect('formgame')->with('message',"Some post data are missing");
        }
        
        $game = new VideoGame;
        $game->name = $request->input('name');
        $game->url = $request->input('url');
        $game->desc = $request->input('desc');

        try {
            // On crée l'utilisateur dans la BDD
            $game->save();
        }
        catch (\Illuminate\Database\QueryException $e) {
            return redirect('gamelist')->with('message','problem');
        }
    } 
}
