Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add locations feature #6

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
99 changes: 99 additions & 0 deletions app/Http/Controllers/LocationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreLocationRequest;
use App\Http\Requests\UpdateLocationRequest;
use App\Models\Location;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class LocationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): View
{
$page = $request->query('page');
$search = $request->query('search');

$locations = Location::when($search, function (Builder $query, ?string $search) {
$query->where('name', 'LIKE', "%{$search}%")
->orWhere('description', 'LIKE', "%{$search}%");
})
->latest()
->paginate()
->withQueryString();

if ($page > $locations->lastPage() && $page > 1) {
abort(404);
}

return view('locations.index', compact('locations', 'search'));
}

/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('locations.create');
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreLocationRequest $request): RedirectResponse
{
$location = new Location;
$location->name = $request->input('name');
$location->description = $request->input('description');
$location->save();

return redirect()->route('locations.index')
->with('message', 'The location has been created.');
}

/**
* Display the specified resource.
*/
public function show(Location $location): View
{
return view('locations.show', compact('location'));
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Location $location): View
{
return view('locations.edit', compact('location'));
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateLocationRequest $request, Location $location): RedirectResponse
{
$location->name = $request->input('name');
$location->description = $request->input('description');
$location->save();

return redirect()->route('locations.index')
->with('message', 'The location has been updated.');
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Location $location): RedirectResponse
{
$location->delete();

return redirect()->route('locations.index')
->with('message', 'The location has been deleted.');
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/StoreLocationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreLocationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => [
'required',
'string',
'min:3',
'max:100',
'unique:locations,name',
],
'description' => [
'nullable',
'string',
'min:3',
'max:255',
],
];
}
}
41 changes: 41 additions & 0 deletions app/Http/Requests/UpdateLocationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateLocationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => [
'required',
'string',
'min:3',
'max:100',
Rule::unique('locations', 'name')->ignore($this->location),
],
'description' => [
'nullable',
'string',
'min:3',
'max:255',
],
];
}
}
57 changes: 57 additions & 0 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Location extends Model
{
use HasFactory, HasUuids;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'locations';

/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';

/**
* Indicates if the model's ID is auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The data type of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'description',
];
}
32 changes: 32 additions & 0 deletions database/factories/LocationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Database\Factories;

use App\Models\Location;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Location>
*/
class LocationFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<\Illuminate\Database\Eloquent\Model>
*/
protected $model = Location::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->unique()->word(),
'description' => fake()->sentence(),
];
}
}
29 changes: 29 additions & 0 deletions database/migrations/2024_09_22_084130_create_locations_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('locations', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name')->unique();
$table->text('description')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('locations');
}
};
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function run(): void
BrandSeeder::class,
DistributorSeeder::class,
ResponsiblePersonSeeder::class,
LocationSeeder::class,
]);
}
}
18 changes: 18 additions & 0 deletions database/seeders/LocationSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Database\Seeders;

use App\Models\Location;
use Illuminate\Database\Seeder;

class LocationSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Location::factory()->count(5)
->create();
}
}
2 changes: 1 addition & 1 deletion resources/views/layout/sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
</li>

<li class="block w-full h-auto p-0 m-0 overflow-hidden relative">
<a href="" class="group flex justify-between items-center gap-4 w-full h-12 py-3 px-5 m-0 text-black/[0.60] rounded-r-full no-underline outline-none whitespace-nowrap overflow-hidden select-none relative hover:bg-black/[0.04] active:bg-black/[0.10] focus:bg-black/[0.12] dark:text-white/[0.60] dark:hover:bg-white/[0.04] dark:active:bg-white/[0.10] dark:focus:bg-white/[0.12] open:bg-primary/[0.04] open:text-primary open:hover:bg-primary/[0.04] open:active:bg-primary/[0.10] open:focus:bg-primary/[0.12] dark:open:bg-primary dark:open:text-black dark:open:hover:bg-primary dark:open:active:bg-primary dark:open:focus:bg-primary" data-te-ripple-init data-te-ripple-color="light">
<a href="{{ route('locations.index') }}" class="group flex justify-between items-center gap-4 w-full h-12 py-3 px-5 m-0 text-black/[0.60] rounded-r-full no-underline outline-none whitespace-nowrap overflow-hidden select-none relative hover:bg-black/[0.04] active:bg-black/[0.10] focus:bg-black/[0.12] dark:text-white/[0.60] dark:hover:bg-white/[0.04] dark:active:bg-white/[0.10] dark:focus:bg-white/[0.12] open:bg-primary/[0.04] open:text-primary open:hover:bg-primary/[0.04] open:active:bg-primary/[0.10] open:focus:bg-primary/[0.12] dark:open:bg-primary dark:open:text-black dark:open:hover:bg-primary dark:open:active:bg-primary dark:open:focus:bg-primary" data-te-ripple-init data-te-ripple-color="light" @if (request()->routeIs('locations.*')) open @endif>
<div class="inline-block min-w-[24px] w-auto h-auto p-0 m-0 relative">
<svg class="pointer-events-none w-full h-full fill-current" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000">
<path d="M0 0h24v24H0z" fill="none" />
Expand Down
Loading
Loading