Skip to content

Commit

Permalink
add enums for machines types (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
lugopi authored Dec 16, 2023
1 parent 218e916 commit 429aba1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
use Illuminate\Http\Request;
use App\Models\Laundry;
use App\Models\Machine;
use App\Models\Reservation;
use App\Utils\Paginate;
use Illuminate\Support\Facades\Auth;

use function PHPSTORM_META\map;

class LaundryController extends Controller
{
public function index(string $orgId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Auth;
use App\Utils\Paginate;
use Illuminate\Http\Request;
use App\Utils\MachineType;

class MachineController extends Controller
{
Expand Down Expand Up @@ -80,15 +81,15 @@ public function show(string $orgId, string $laundryId, string $id)

public function create(string $orgId, string $laundryId)
{
$types = ['dry' => 'Sechage', 'wash' => 'Lavage'];

return view(
'management.machines.create',
[
"page" => "machine create",
"pageTitle" => "Machine Creation",
"pageDescription" => "Créez votre machine",
],
compact('types', 'orgId', 'laundryId')
compact(MachineType::cases(), 'orgId', 'laundryId')
);
}

Expand Down Expand Up @@ -129,7 +130,6 @@ public function store(Request $request, string $orgId, string $laundryId)

public function edit(string $orgId, string $laundryId, string $id)
{
$types = ['dry' => 'Sechage', 'wash' => 'Lavage'];
$organization = Auth::user()->organizations->find($orgId);
if (empty($organization)) {
return back()->withErrors(["Permission denied for this organization."])->withInput();
Expand All @@ -155,6 +155,8 @@ public function edit(string $orgId, string $laundryId, string $id)
];
});

$types = MachineType::all();

return view(
'management.machines.edit',
[
Expand Down
15 changes: 11 additions & 4 deletions calm-webserver/app/Http/Controllers/ReservationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
namespace App\Http\Controllers;

use App\Models\Laundry;
use App\Models\Machine;
use App\Models\Reservation;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use App\Utils\Paginate;
use App\Utils\MachineType;

use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;
use SebastianBergmann\Type\TypeName;

class ReservationController extends Controller
{
/**
Expand Down Expand Up @@ -84,7 +89,8 @@ public function create()
"pageDescription" => "Créez,une réservation. Choisissez l'organisation, la buanderie, le type de machine, la date et
la durée.",
"reserving" => false,
'organizations' => Auth::user()->organizations
'organizations' => Auth::user()->organizations,
"machinesTypes" => MachineType::all(),
]);
}

Expand All @@ -106,11 +112,12 @@ private function choice_paginator(array $items, int $item_per_page, $page=null,$
}

public function search_propositions(Request $request){

$param = $request->validate([
'laundry' => ['required', 'integer'],
'day' => ['required', 'date'],
'duration' => ['required', 'integer', 'min:30', 'max:360'],
'type' => ['required', 'in:wash,dry'],
'type' => ['required', Rule::in(MachineType::names())],
]);

$laundry = Laundry::find($param['laundry']);
Expand Down Expand Up @@ -145,15 +152,15 @@ public function show_propositions(Request $request){
'date' => $date,
'type' => [
'id' => $type,
'name' => ($type == 'wash') ? 'Lavage' : 'Séchage'
'name' => MachineType::fromName($type)
],
'reservations' => $this->choice_paginator($reservations, 10, $request['page']),
];
return view('reservations.create', [
"page" => "reservations",
"pageTitle" => "Choix d'une réservation",
"pageDescription" => "Choisissez une réservation parmi les propositions.",
"proposition" => $param
"proposition" => $param,
]);
}

Expand Down
7 changes: 2 additions & 5 deletions calm-webserver/app/Models/Machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Utils\MachineType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -40,10 +41,6 @@ public function organization(){
}

public function typeName(){
switch ($this->type){
case 'wash': return 'Lavage';
case 'dry' : return 'Séchage';
default: return 'Autre';
}
return MachineType::fromName($this->type);
}
}
28 changes: 28 additions & 0 deletions calm-webserver/app/Utils/MachineType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Utils;

use Illuminate\Support\Str;

enum MachineType: string
{
case wash = 'Lavage';
case dry = 'Séchage';

public static function fromName(string $name): self {
return match ($name) {
'wash'=>self::wash,
'dry'=>self::dry,
default => 'Autre'
};
}

public static function names(): array
{
return collect(self::cases())->map(fn($case) => $case->name)->toArray();
}

public static function all(): array {
return collect(self::cases())->mapWithKeys(fn($case) => [$case->name => $case->value])->all();
}
}
3 changes: 2 additions & 1 deletion calm-webserver/database/factories/MachineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Laundry;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Utils\MachineType;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Machine>
Expand All @@ -21,7 +22,7 @@ public function definition(): array
'name' => $this->faker->word(),
'description' => $this->faker->paragraph(),
'laundry_id' => Laundry::all()->random()->id,
'type' => $this->faker->randomElement(['wash', 'dry']),
'type' => $this->faker->randomElement(MachineType::names()),
];
}
}
12 changes: 4 additions & 8 deletions calm-webserver/resources/views/reservations/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,14 @@ class="block w-full input input-sobre">
</div>

<div class="text-center">
<label class="relative inline-flex items-center mr-5 cursor-pointer">
<input type="checkbox" name="type" value="wash" id="wash" class="sr-only peer choose-wash-dry" @if(old("type") === "wash") checked @endif>
<div
class="toggle-switch peer peer-focus:ring-4 peer-focus:ring-vividTangerine peer-checked:after:translate-x-full peer-checked:after:border-white peer-checked:bg-vividTangerine"></div>
<span class="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300">Laver</span>
</label>

@foreach ($machinesTypes as $name => $value)
<label class="relative inline-flex items-center mr-5 cursor-pointer">
<input type="checkbox" name="type" value="dry" id="dry" class="sr-only peer choose-wash-dry" @if(old("type") === "dry") checked @endif>
<input type="checkbox" name="type" value="{{$name}}" id="{{$name}}" class="sr-only peer choose-wash-dry" @if(old("type") === "{{$name}}") checked @endif>
<div class="toggle-switch peer peer-focus:ring-4 peer-focus:ring-vividTangerine peer-checked:after:translate-x-full peer-checked:after:border-white peer-checked:bg-vividTangerine"></div>
<span class="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300">Sécher</span>
<span class="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300">{{$value}}</span>
</label>
@endforeach
</div>

<div>
Expand Down

0 comments on commit 429aba1

Please sign in to comment.