Skip to content

Commit

Permalink
Merge PR#12 from franenlanube/feature/crud-alternatives
Browse files Browse the repository at this point in the history
Feature/crud alternatives
  • Loading branch information
FranEnLaNube authored Sep 26, 2023
2 parents 1f7ba84 + 626d067 commit 6626899
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Alternative;
use App\Http\Controllers\Controller;
use App\Models\Vote;
use Illuminate\Http\Request;

class AlternativesController extends Controller
Expand Down Expand Up @@ -43,6 +44,9 @@ public function store(Request $request)
public function show(string $id)
{
$alternative = Alternative::find($id);
if (!$alternative) {
return abort(404); // TODO: Handle the case where the election is not found
}

return view('entities.alternatives.show')->with('alternative', $alternative);
}
Expand All @@ -67,18 +71,30 @@ public function update(Request $request, string $id)
$alternative->logo = $request->get('logo');

$alternative->save();

return redirect('/alternatives');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)

public function destroy(Request $request, string $id)
{
$alternative = Alternative::find($id);

$alternative->destroy();

if (!$alternative) {
return abort(404); // TODO: Handle the case where the election is not found
}
if (!empty(Vote::where(['alternative_id' => $alternative->id])->first()->quantity)) {
if (!$request->has('confirm-delete')) {
// Si hay votos asociados, muestra el mensaje de error
return redirect()->back()->with('error', 'This alternative has votes associated. Are you sure you want to delete it? Confirm it by checking the box')
->with('id', $id);
}
}
//First delete de associated vote
// FIXME should be a better way to find by just one parameter
Vote::where(['alternative_id' => $alternative->id])->delete();
// Delete the alternative
$alternative->delete();
return redirect('/alternatives');
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
@extends('layouts.main-layout')

@section('content')
<h2 class="text-2xl font-semibold text-center mt-10">Add an Alternative</h2>
<form action="/alternatives/store" method="POST" class="mt-auto mx-auto max-w-md space-y-4">
<h2 class="text-2xl font-semibold text-center m-10">Add an Alternative</h2>
<form action="{{ route('alternatives.store') }}" method="POST" class="border p-5 m-auto mx-auto max-w-md space-y-4">
@csrf

<div>
<label for="name" class="block text-gray-700">Name</label>
<input id="name" name="name" type="text"
class="border rounded px-3 py-2 w-full
<label for="name" class="block text-gray-700">Name</label>
<input id="name" name="name" type="text"
class="border px-3 py-2 w-full
@error('name') border-red-500 @enderror"
value="{{ old('name') }}" tabindex="1">
@error('name')
<small class="text-red-500">{{ $message }}</small>
@enderror
</div>
value="{{ old('name') }}" tabindex="1">
@error('name')
<small class="text-red-500">{{ $message }}</small>
@enderror

<div>
<label for="candidates" class="block text-gray-700">President and Vice candidates</label>
<input id="candidates" name="candidates" type="text"
class="border rounded px-3 py-2 w-full
@error('candidates') border-red-500 @enderror"
value="{{ old('candidates') }}" tabindex="2">
</div>
<label for="presi_vice" class="block text-gray-700">President and Vice candidates</label>
<input id="presi_vice" name="presi_vice" type="text"
class="border rounded px-3 py-2 w-full
@error('presi_vice') border-red-500 @enderror"
value="{{ old('presi_vice') }}" tabindex="2">

<label for="logo" class="block text-gray-700">URL-Logo</label>
<input id="logo" name="logo" type="text"
class="border rounded px-3 py-2 w-full
@error('logo') border-red-500 @enderror"
value="{{ old('logo') }}" tabindex="3">

<div>
<label for="logo" class="block text-gray-700">URL-Logo</label>
<input id="logo" name="logo" type="text"
class="border rounded px-3 py-2 w-full
@error('candidates') border-red-500 @enderror"
value="{{ old('logo') }}" tabindex="3">
</div>
<div class="flex justify-between">
<a href="/" class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
tabindex="5">Cancel</a>
<a href="/alternatives"
class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
tabindex="4">Cancel</a>
<button type="submit"
class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
tabindex="6">Submit</button>
tabindex="5">Submit</button>
</div>
</form>
@endsection
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
@extends('layouts.main-layout')

@section('content')
<h2 class="text-2xl font-semibold text-center mt-10">Edit an Alternative</h2>
<form action="/alternatives/{{ $alternative->id }}" method="POST" class="mt-auto mx-auto max-w-md space-y-4">
<h2 class="text-2xl font-semibold text-center m-10">Edit an Alternative</h2>
<form action="/alternatives/{{ $alternative->id }}" method="POST" class="border rounded p-5 m-auto mx-auto max-w-md space-y-4">
@csrf
@method('PUT')

<div>
<label for="name" class="block text-gray-700">Name</label>
@if (!($alternative->isblank() || $alternative->isSpoiled()))
<input id="name" name="name" type="text"
class="border rounded px-3 py-2 w-full
@error('name') border-red-500 @enderror"
value="{{ old('name', $alternative->name) }}" tabindex="1">
@error('name')
<small class="text-red-500">{{ $message }}</small>
@enderror
</div>
@else
<div id="name" name="name" type="text" class="border rounded px-3 py-2 w-full">
{{ $alternative->name }}
</div>
@endif

{{-- TODO: Abstract this concept in default alternatives to avoid these ifs --}}
@if (!($alternative->isblank() || $alternative->isSpoiled()))
<div>
<label for="candidates" class="block text-gray-700">President and Vice candidates</label>
<input id="candidates" name="candidates" type="text" class="border rounded px-3 py-2 w-full"
value="{{ old('candidates', $alternative->presi_vice) }}" tabindex="2">
</div>

<label for="presi_vice" class="block text-gray-700">President and Vice candidates</label>
<input id="presi_vice" name="presi_vice" type="text" class="border rounded px-3 py-2 w-full"
value="{{ old('presi_vice', $alternative->presi_vice) }}" tabindex="2">

@endif
<div>

<label for="logo" class="block text-gray-700">URL-Logo</label>
<input id="logo" name="logo" type="text" class="border rounded px-3 py-2 w-full"
value="{{ old('logo', $alternative->logo) }}" tabindex="3">
</div>

<div class="flex justify-between">
<a href="/" class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
<a href="/alternatives" class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
tabindex="5">Cancel</a>
<button type="submit"
class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,70 @@
@extends('layouts.main-layout')

@section('content')
<h2 class="text-2xl font-semibold text-center mt-10">Alternatives</h2>
<div class="mt-4 mx-auto p-20">
<table class="min-w-full border-collapse border">
<thead>
<tr>
<th
class="px-6 py-3 bg-gray-200 text-left text-xs leading-4 font-medium text-gray-600 uppercase tracking-wider">
Name</th>
<th class="px-6 py-3 bg-gray-200"></th>
<h2 class="text-2xl font-semibold text-center m-5">Alternatives</h2>
@if (session('error'))
<div class="flex max-w-3xl mx-auto mb-3 bg-yellow-400 text-black text-center p-4 rounded-lg shadow-md justify-around">
<form action="{{ route('alternatives.destroy', session('id')) }}" method="POST">
@csrf
@method('DELETE')
<p>{{ session('error') }}</p>
<input type="checkbox" name="confirm-delete" id="confirm-delete">
<label for="confirm-delete">Yes, I'm sure</label>
<button type="submit"
class="text-white bg-red-600 hover:bg-red-900 font-bold m-4 py-2 px-4 rounded">Delete</button>
<a href="/alternatives"
class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded inline-block"
tabindex="4">Cancel</a>
</form>
</div>
@endif
@if (session('error-2'))
<div class="max-w-3xl mx-auto mb-3 bg-red-400 text-white text-center p-4 rounded-lg shadow-md">
{{ session('error-2') }}
</div>
@endif
<table class="max-w-3xl border-collapse border mx-auto p-10">
<thead>
<tr>
<th
class="px-6 py-3 bg-gray-200 text-left text-base leading-4 font-medium text-gray-700 uppercase tracking-wider">
Name
</th>
<th class="px-6 py-3 bg-gray-200"></th>
</tr>
</thead>
<tbody>
@foreach ($alternatives as $alternative)
<tr class="border border-collapse">
@if (!($alternative->isblank() || $alternative->isSpoiled()))
<td class="border px-6 py-5">{{ $alternative->name }}</td>
<td class="m-2 my-4 flex justify-around h-full">
<a href="{{ route('alternatives.show', $alternative->id) }}"
class="text-white bg-blue-400 hover-bg-blue-700 font-bold mx-2 py-2 px-4 rounded h-full">View</a>
<a href="{{ route('alternatives.edit', $alternative->id) }}"
class="text-white bg-blue-600 hover-bg-blue-900 font-bold mx-2 py-2 px-4 rounded h-full">Edit</a>
<form action="{{ route('alternatives.destroy', $alternative->id) }}" method="POST"
class="inline">
@csrf
@method('DELETE')
<button type="submit"
class="text-white bg-red-600 hover-bg-red-900 font-bold mx-2 py-2 px-4 rounded h-full"
onclick="return confirm('Are you sure you want to delete this alternative?')">Delete</button>
</form>
</td>
@endif
</tr>
</thead>
<tbody>
@foreach ($alternatives as $alternative)
<tr>
<td class="border px-6 py-4">{{ $alternative->name }}</td>
<td class="border px-6 py-4 text-right">
@if (!($alternative->isblank() || $alternative->isSpoiled()))
<a href="{{ route('alternatives.edit', $alternative->id) }}"
class="text-white bg-blue-400 hover:bg-blue-700 font-bold py-2 px-4 m-4 rounded mr-3">Edit</a>
<form action="{{ route('alternatives.destroy', $alternative->id) }}" method="POST"
class="inline">
@csrf
@method('DELETE')
<button type="submit"
class="text-white bg-red-600 hover:bg-red-900 font-bold py-2 px-4 m-4 rounded mr-3">Delete</button>
</form>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>

<div class="text-center mt-4">
<a href="/" class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded inline-block">Back</a>
@endforeach
</tbody>
</table>
<div class="max-w-3xl mx-auto my-5 flex justify-around">
<div class="text-center">
<a href="/"
class="bg-blue-400 hover-bg-blue-700 text-white font-bold py-2 px-4 rounded inline-block">Back</a>
</div>
<div class="text-center">
<a href="{{ route('alternatives.create') }}"
class="bg-blue-400 hover-bg-blue-700 text-white font-bold py-2 px-4 rounded inline-block">Create</a>
</div>
</div>
@endsection
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
@extends('layouts.main-layout')

@section('content')
<h2 class="text-2xl font-semibold text-center mt-10">Alternative</h2>
<div class="mt-auto mx-auto max-w-md space-y-4">
<table>
<div>
<h2 class="text-2xl font-semibold text-center m-8">Alternative</h2>
<div class="border rounded p-5 my-10 mx-auto max-w-md space-y-4">
<table class="border rounded px-3 py-2">
<label for="name" class="block text-gray-700">Name</label>
<div id="name" name="name" type="text" class="border rounded px-3 py-2 w-full">
{{ $alternative->name }}
</div>
{{-- TODO: Abstract this concept in default alternatives to avoid this if --}}
@if (!($alternative->isblank() || $alternative->isSpoiled()))
<div>
<label for="candidates" class="block text-gray-700">President and Vice candidates</label>
<div id="name" name="name" type="text" class="border rounded px-3 py-2 w-full">
{{ $alternative->presi_vice }}
</div>
</div>
@if (!($alternative->isblank() || $alternative->isSpoiled() || empty($alternative->presi_vice)))
<label for="presi_vice" class="block text-gray-700">President and Vice candidates</label>
<div class="border rounded px-3 py-2 w-full">
{{ $alternative->presi_vice }}
</div>
@endif
@if (!empty($alternative->logo))
<div>
<label for="candidates" class="block text-gray-700">President and Vice candidates</label>
<div id="name" name="name" type="text" class="border rounded px-3 py-2 w-full">
<div class="border rounded px-3 py-2">
<label for="logo" class="block text-gray-700">URL-Logo</label>
<div id="logo" name="logo" type="text" class="px-3 py-2 w-full">
{{ $alternative->logo }}
</div>
</div>
@endif
</div>
</table>
<div class="text-center mt-4">
<a href="/"
class="bg-blue-400 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded inline-block">Back</a>
<div class="text-center flex justify-around mt-4 space-x-2">
<a href="/alternatives"
class="bg-blue-400 hover-bg-blue-700 text-white font-bold py-2 px-4 rounded inline-block">Back</a>
@if (!($alternative->isblank() || $alternative->isSpoiled()))
<a href="/alternatives/{{ $alternative->id }}/edit"
class="bg-blue-400 hover-bg-blue-700 text-white font-bold py-2 px-4 rounded inline-block">Edit</a>
@endif
</div>
@endsection
</div>
@endsection
5 changes: 3 additions & 2 deletions CursoPHP-Sprint4-Laravel/ArgentineElections/routes/web.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

use App\Http\Controllers\ResultsController;
use App\Http\Controllers\AlternativesController;
use App\Http\Controllers\VotesController;
use Illuminate\Support\Facades\Route;


/*
|--------------------------------------------------------------------------
| Web Routes
Expand All @@ -19,7 +20,7 @@
return view('welcome');
});

Route::resource('alternatives','App\Http\Controllers\AlternativesController');
Route::resource('alternatives', AlternativesController::class);

Route::resource('elections','App\Http\Controllers\ElectionsController');

Expand Down

0 comments on commit 6626899

Please sign in to comment.