Skip to content
This repository has been archived by the owner on Apr 12, 2020. It is now read-only.

Entrega Proyecto 1 #23

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: vendor/bin/heroku-php-apache2 public/
3 changes: 3 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'nickname' => ['required', 'string', 'max:255', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
Expand All @@ -63,8 +64,10 @@ protected function validator(array $data)
*/
protected function create(array $data)
{
/**Valido que los campos de nickname y edad sean correctos */
return User::create([
'name' => $data['name'],
'nickname'=>$data['nickname'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
Expand Down
51 changes: 51 additions & 0 deletions app/Http/Controllers/Auth/SocialAuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Http\Controllers\Auth;

use Auth;
use DB;
use App\User;
use Socialite;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SocialAuthController extends Controller
{
// Metodo encargado de la redireccion a Facebook
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}

// Metodo encargado de obtener la información del usuario
public function handleProviderCallback($provider)
{
// Obtenemos los datos del usuario
$social_user = Socialite::driver($provider)->user();
// Comprobamos si el usuario ya existe
if ($user = User::where('email', $social_user->email)->first()) {
return $this->authAndRedirect($user); // Login y redirección
} else {
// En caso de que no exista creamos un nuevo usuario con sus datos.
DB::beginTransaction();
$nickname = str_replace(' ', '', $social_user->name);
$nickname = $nickname.(DB::table('users')->max('id') + 1);
$user = User::create([
'name' => $social_user->name,
'nickname' => $nickname,
'email' => $social_user->email,
]);
DB::commit();

return $this->authAndRedirect($user); // Login y redirección
}
}

// Login y redirección
public function authAndRedirect($user)
{
Auth::login($user);

return redirect()->to('/home');
}
}
29 changes: 29 additions & 0 deletions app/Http/Controllers/BusquedaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Rules\validarExistenciaNickName;
use App\partido;

class BusquedaController extends Controller
{
public function buscarUsuario(Request $request)
{
$data = $request->all();

$request->validate([
'searchnickname' => ['required', 'string', 'max:255', new validarExistenciaNickName()],
]);

$userBuscado= User::where('nickname',$data['searchnickname'])->get()->first();
$idUsuario = $userBuscado->id;

$partidosPublicos= partido::where('public',"Public")->where('user_id',$idUsuario)->get();


return view('/perfilPublico',['user'=>$userBuscado],['partidos'=>$partidosPublicos]);
}
}

31 changes: 31 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers;

use Auth;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//Solo podemos acceder a estos métodos si estamos logeados
$this->middleware('auth');
}

/**
* Show the application dashboard.
* Cuando inicia sesión deberían verse todos sus partidos creados
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{

$userActual= Auth::user();
return view('home',['user'=>$userActual]);
}

}
26 changes: 26 additions & 0 deletions app/Http/Controllers/InicioController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use App\partido;
use App\player;
class InicioController extends Controller
{
public function index()
{
$partidospublicos = Partido::where('public', "Public")->get();

return view('/inicio', ['partidos' => $partidospublicos]);
}

public function readme(){
return view('readme');
}

public function viewJugadores($id)
{
$partido = partido::where('id',$id)->get()->first();
$jugadores = player::where('id_partido',$id)->get();
return view('listaJugadores',['jugadores'=>$jugadores],['partido'=>$partido]);
}
}
141 changes: 141 additions & 0 deletions app/Http/Controllers/ListadoPartidosController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\partido;
use App\player;
use App\category;
use App\state;
use Auth;
use App\Rules\validarVisibilidad;

class ListadoPartidosController extends Controller
{

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//Solo podemos acceder a estos métodos si estamos logeados
$this->middleware('auth');
}

/**
* Show the application dashboard.
*= Partido::where('user_id',Auth::user()->id);
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$partidos = partido::where('user_id', Auth::user()->id)->get();

return view('listadoPartidos', ['partidos' => $partidos]);
}

public function borrarPartido($id)
{

$partido = partido::where('id', $id)->get()->first();
if (Auth::user()->id != $partido->user_id) {
abort(403, 'No está autorizado para realizar la acción');
}

$jugadores = player::where('id_partido', $partido->id)->get();
foreach ($jugadores as $jugador) {
$jugador->delete();
}
$partido->delete();

return redirect('/listadoPartidos');
}


public function editarPartido($id)
{
$user = Auth::user();
$partido = partido::where('id', $id)->get()->first();
if ($user->id != $partido->user_id) {
abort(403, 'No está autorizado para realizar la acción');
}
$estados = state::get('visibilidad');
$partido = partido::findOrFail($id);
$categorias = category::get('category');
$jugadores = player::where('id_partido', $id)->get();

return view('/edit', ['categories' => $categorias, 'estados' => $estados, 'partido' => $partido, 'jugadores' => $jugadores]);
}

public function update(Request $request, $id)
{
$user = Auth::user();
$partido = partido::where('id', $id)->get()->first();
if ($user->id != $partido->user_id) {
abort(403, 'No está autorizado para realizar la acción');
}


$data = $request->all();

$request->validate([
'name' => ['required', 'string', 'max:255'],
'place' => 'required|string|max:255',
'category' => 'required|string|exists:categories|max:255',
'public' => ['required', 'string', 'max:255', new validarVisibilidad()],
'jugadores' => [],
]);

/**Si existe algun campo distinto actualizarlo */
//Si se cambio el nombre
if (strcmp($data['name'], $partido->name) != 0)
$partido->update(['name' => $data['name']]);
//Si se cambio el lugar
if (strcmp($data['place'], $partido->place) != 0)
$partido->update(['place' => $data['place']]);

//Si se actualizo la categoria
if (strcmp($data['category'], $partido->category) != 0)
$partido->update(['category' => $data['category']]);

//Si se actualizo la privacidad
if (strcmp($data['public'], $partido->public) != 0)
$partido->update(['public' => $data['public']]);
//Si se agrego Jugadores
if (!empty($data['jugadores'])) {
foreach ($data['jugadores'] as $nuevoNombre) {
$nuevo = new player;
$nuevo->name = $nuevoNombre;
$nuevo->id_partido = $partido->id;
$nuevo->save();
}
}


return redirect('/listadoPartidos');
}

public function borrarJugador($id)
{
$jugador = player::where('id', $id)->get()->first();
$idpartido = (player::where('id', $id)->get()->first())->id_partido;

$user = Auth::user();
$partido = partido::where('id', $idpartido)->get()->first();
if ($user->id != $partido->user_id) {
abort(403, 'No está autorizado para realizar la acción');
}

if ($jugador != null)
$jugador->delete();


$estados = state::get('visibilidad');
$categorias = category::get('category');
$jugadores = player::where('id_partido', $idpartido)->get();

return view('/edit', ['categories' => $categorias, 'estados' => $estados, 'partido' => $partido, 'jugadores' => $jugadores]);
}
}
74 changes: 74 additions & 0 deletions app/Http/Controllers/PartidoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\category;
use Auth;
use App\Rules\validarVisibilidad;
use App\Rules\validarPartidosUnicosUsuario;
use App\partido;
use App\player;
use App\state;

class PartidoController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//Solo podemos acceder a estos métodos si estamos logeados
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$estados = state::get('visibilidad');
$categorias = category::get('category');
return view('partido', ['categories' => $categorias], ['estados' => $estados]);
}


public function crearPartido(Request $request)
{
$user = Auth::user();
$data = $request->all();

$request->validate([
'name' => ['required', 'string', 'max:255', new validarPartidosUnicosUsuario($user->id)],
'place' => 'required|string|max:255',
'category' => 'required|string|exists:categories|max:255',
'public' => ['required', 'string', 'max:255', new validarVisibilidad()],
'jugadores' => ['required', 'max:255'],
]);

//Crear Partido
$partido = new partido;
$partido->name = $data['name'];
$partido->place = $data['place'];
$partido->category = $data['category'];
$partido->public = $data['public'];
$partido->user_id = $user->id;
$partido->save();


//Agregar Participantes al partido

foreach ($data['jugadores'] as $jugador) {
$jugadorNuevo = new player;
$jugadorNuevo->name = $jugador;
$jugadorNuevo->id_partido = $partido->id;
$jugadorNuevo->save();
}


return redirect('/listadoPartidos');
}
}
Loading