Skip to content

Commit

Permalink
servicio
Browse files Browse the repository at this point in the history
  • Loading branch information
diegordgz8 committed Jul 1, 2021
1 parent fc7f71d commit 0b79b60
Show file tree
Hide file tree
Showing 14 changed files with 575 additions and 58 deletions.
16 changes: 16 additions & 0 deletions app/Http/Controllers/CtrServicio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use App\Models\Servicio;

class CtrServicio extends Controller
{
public function index() {
return view('servicio.index');
}

public function show(Servicio $servicio) {
return view('servicion.show', compact('servicio'));
}
}
66 changes: 66 additions & 0 deletions app/Http/Livewire/Servicio/NuevoServicio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Http\Livewire\Servicio;

use App\Models\Categoria;
use App\Models\Servicio;
use Livewire\Component;

class NuevoServicio extends Component
{
public $open = false;

public $nombre, $descripcion, $categoria;

protected $rules = [
'nombre' => 'required|max:60|unique:servicios,nombre,NULL,NULL,deleted_at,NULL',
'descripcion' => 'nullable',
'categoria' => 'required|not_in:0',
];

public function render()
{
$categorias = Categoria::all();

return view('livewire.servicio.nuevo-servicio', compact('categorias'));
}

public function updated($propertyName)
{
$this->validateOnly($propertyName);
}

public function save()
{
$this->validate();

$servicio = Servicio::withTrashed()->where('nombre', $this->nombre)->first();

if ($servicio === null) {

Servicio::create([
'nombre' => $this->nombre,
'descripcion' => $this->descripcion,
'categoria_id' => $this->categoria,
]);
} else {
$servicio->restore();

$servicio->nombre = $this->nombre;
$servicio->descripcion = $this->descripcion;
$servicio->categoria_id = $this->categoria;

$servicio->save();
}

$this->reset([
'open',
'nombre',
'descripcion',
'categoria',
]);

$this->emitTo('servicio.tabla-servicio', 'render');
$this->emit('alert', 'El servicio se creó satisfactoriamente');
}
}
125 changes: 121 additions & 4 deletions app/Http/Livewire/Servicio/TablaServicio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,129 @@

namespace App\Http\Livewire\Servicio;

use App\Models\Categoria;
use App\Models\Servicio;
use Livewire\Component;
use Livewire\WithPagination;

class TablaServicio extends Component
{
public function render()
{
return view('livewire.servicio.tabla-servicio');
}
use WithPagination;

public Servicio $servicio;

public $busqueda;
public $orden = 'nombre';
public $direccion = 'asc';
public $cantidad = '10';

public $readyToLoad = false;

public $openEdit = false;
public $openDestroy = false;

protected $listeners = ['render'];

public function mount(Servicio $servicio)
{
$this->servicio = $servicio;
}

protected function rules()
{
return [
'servicio.nombre' => [
'required',
'max:60',
'unique:servicios,nombre,' . $this->servicio->id.',id,deleted_at,NULL',
],
'servicio.descripcion' => 'nullable',
'servicio.categoria_id' => 'required|not_in:0',
];
}

public function render()
{
if ($this->readyToLoad) {
$servicios = Servicio::where('nombre', 'like', '%' . $this->busqueda . '%')
->orWhere('descripcion', 'like', '%' . $this->busqueda . '%')
->orderBy($this->orden, $this->direccion)
->paginate($this->cantidad);
} else {
$servicios = [];
}

$categorias = Categoria::all();

return view('livewire.servicio.tabla-servicio', compact('servicios', 'categorias'));
}

public function loadServicios()
{
$this->readyToLoad = true;
}

public function updated($propertyName)
{
$this->validateOnly($propertyName);
}

public function updatingBusqueda()
{
$this->resetPage();
}

public function updatingCantidad()
{
$this->resetPage();
}

public function orden($orden)
{
if ($this->orden == $orden) {
if ($this->direccion == 'desc') {
$this->direccion = 'asc';
} else {
$this->direccion = 'desc';
}
} else {
$this->orden = $orden;
$this->direccion = 'asc';
}
}

public function edit(Servicio $servicio)
{
$this->servicio = $servicio;

$this->openEdit = true;
}

public function update()
{
$this->validate();

$this->servicio->save();

$this->reset('openEdit');

$this->emitTo('servicio.tabla-servicio', 'render');
$this->emit('alert', 'El servicio se actualizó satisfactoriamente');
}

public function destroy(Servicio $servicio)
{
$this->servicio = $servicio;
$this->openDestroy = true;
}

public function delete()
{
$this->servicio->delete();

$this->reset('openDestroy');

$this->emitTo('servicio.tabla-servicio', 'render');
$this->emit('alert', 'El servicio se eliminó satisfactoriamente');
}
}
4 changes: 4 additions & 0 deletions app/Models/Servicio.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Servicio extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = ['nombre', 'descripcion', 'categoria_id'];

/**
* Obtiene la categoría a la que pertenece el servicio.
Expand Down
2 changes: 1 addition & 1 deletion database/factories/ServicioFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ServicioFactory extends Factory
public function definition()
{
return [
'nombre' => $this->faker->words(3, true),
'nombre' => $this->faker->unique()->words(3, true),
'descripcion' => $this->faker->sentence(),
'categoria_id' => $this->faker->randomElement(Categoria::all('id')),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public function up()
{
Schema::create('servicios', function (Blueprint $table) {
$table->id();
$table->string('nombre', 60);
$table->string('descripcion');
$table->string('nombre', 60)->unique();
$table->string('descripcion')->nullable();
$table->foreignId('categoria_id');
$table->timestamps();
$table->softDeletes();
});
}

Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function run()
]);

Banco::factory(15)->create();
Categoria::factory(20)->create();
Categoria::factory(7)->create();
TipoUnidad::factory(5)->create();
TipoUsuario::factory(5)->create();

Expand Down
102 changes: 58 additions & 44 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,61 @@
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">

<title>{{ config('app.name', 'Laravel') }}</title>

<!-- Fonts -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

<!-- Styles -->
<link rel="stylesheet" href="{{ mix('css/app.css') }}">
<link rel="stylesheet" href="{{ asset('vendor/fontawesome-free/css/all.min.css') }}">

@livewireStyles

<!-- Scripts -->
<script src="{{ mix('js/app.js') }}" defer></script>
</head>
<body class="font-sans antialiased">
<x-jet-banner />

<div class="min-h-screen bg-gray-100">
@livewire('navigation-menu')

<!-- Page Heading -->
@if (isset($header))
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
{{ $header }}
</div>
</header>
@endif

<!-- Page Content -->
<main>
{{ $slot }}
</main>
</div>

@stack('modals')

@livewireScripts
</body>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">

<title>{{ config('app.name', 'Laravel') }}</title>

<!-- Fonts -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

<!-- Styles -->
<link rel="stylesheet" href="{{ mix('css/app.css') }}">
<link rel="stylesheet" href="{{ asset('vendor/fontawesome-free/css/all.min.css') }}">

@livewireStyles

<!-- Scripts -->
<script src="{{ mix('js/app.js') }}" defer></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>

<body class="font-sans antialiased">
<x-jet-banner />

<div class="min-h-screen bg-gray-100">
@livewire('navigation-menu')

<!-- Page Heading -->
@if (isset($header))
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
{{ $header }}
</div>
</header>
@endif

<!-- Page Content -->
<main>
{{ $slot }}
</main>
</div>

@stack('modals')

@livewireScripts

<script>
Livewire.on('alert', function(message) {
Swal.fire(
'Good job!',
message,
'success'
)
});
</script>
</body>

</html>
2 changes: 1 addition & 1 deletion resources/views/livewire/nueva-categoria.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<x-jet-dialog-modal wire:model="abierto">
<x-slot name="title">
Nueva Categoría
Nueva categoría
</x-slot>

<x-slot name="content">
Expand Down
Loading

0 comments on commit 0b79b60

Please sign in to comment.