Skip to content

Commit

Permalink
all logick to labels and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-xz committed Jul 25, 2024
1 parent 84af866 commit 5838e41
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 37 deletions.
46 changes: 38 additions & 8 deletions app/Http/Controllers/LabelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,89 @@

class LabelController extends Controller
{
/**
/**
* Display a listing of the resource.
*/
public function index()
{
//
$labels = Label::paginate(15);

return view('labels.index', compact('labels'));
}

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

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
$data = $request->validate([
'name' => 'required|string|unique:labels'
]);

$label = new Label();
$label->fill($data);
$label->save();

flash(__('flash.labelCreated'))->success();

return redirect()
->route('labels.index');
}

/**
* Display the specified resource.
*/
public function show(Label $label)
{
//
abort(403);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Label $label)
{
//
return view('labels.edit', compact('label'));
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Label $label)
{
//
$data = $request->validate([
'name' => 'required|string|unique:labels,name,' . $label->id,
]);

$label->fill($data);
$label->save();

flash(__('flash.labelChanged'))->success();

return redirect()
->route('labels.index');
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Label $label)
{
//
if (count($label->tasks) !== 0) {
flash(__('flash.labelNotDeleted'))->warning();
return back();
}
$label->delete();
flash(__('flash.labelDeleted'))->success();
return redirect()
->route('labels.index');
}
}
9 changes: 6 additions & 3 deletions app/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

class Label extends Model
{
protected $fillable = [
'name',
'description'
];

public function tasks()
{
// У каждого пользователя много постов
// hasMany определяется у модели, имеющей внешние ключи в других таблицах
return $this->hasMany('App\Models\Task', 'label_id');
return $this->belongsToMany('App\Models\Task');
}
}
4 changes: 1 addition & 3 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public function assignedTo()

public function labels()
{
// У каждого пользователя много постов
// hasMany определяется у модели, имеющей внешние ключи в других таблицах
return $this->hasMany('App\Models\Label', 'task_id');
return $this->belongsToMany('App\Models\Label');
}
}
2 changes: 1 addition & 1 deletion database/factories/TaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function definition(): array
'name' => $this->faker->sentence(),
'description' => $this->faker->sentence(10),
'status_id' => TaskStatus::all()->random(),
'created_by_id' => User::all()->random()
'created_by_id' => User::all()->random(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public function up(): void
{
Schema::create('labels', function (Blueprint $table) {
$table->id();
$table->strign('name');
$table->string('name');
$table->text('description')->nullable();
$table->foreignId('task_id')->constrained('tasks');
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
*/
public function up(): void
{
Schema::table('tasks', function (Blueprint $table) {
Schema::create('label_task', function (Blueprint $table) {
$table->id();
$table->foreignId('task_id')->constrained('tasks');
$table->foreignId('label_id')->constrained('labels');
$table->timestamps();
});
}

Expand All @@ -21,8 +24,6 @@ public function up(): void
*/
public function down(): void
{
Schema::table('task_with_foreign_key', function (Blueprint $table) {
$table->dropColumn('label_id');
});
Schema::dropIfExists('label_task');
}
};
14 changes: 4 additions & 10 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Database\Seeders;

use App\Models\User;
use App\Models\Task;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
Expand All @@ -14,15 +11,12 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
User::factory()
->count(5)
->create();
$this->call([
UserSeeder::class,
TaskStatusSeeder::class,
LabelSeeder::class,
TaskSeeder::class,
TaskLabelSeeder::class
]);
Task::factory()
->assignetToSequence()
->count(5)
->create();
}
}
6 changes: 3 additions & 3 deletions database/seeders/LabelSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class LabelSeeder extends Seeder
public function run(): void
{
Label::firstOrCreate([
'name' => 'Ошибка',
'description' => ' Какая-то ошибка в коде или проблема с функциональностью'
'name' => 'ошибка',
'description' => 'Какая-то ошибка в коде или проблема с функциональностью'
]);
Label::firstOrCreate([
'name' => 'документация',
Expand All @@ -26,7 +26,7 @@ public function run(): void
'description' => 'Повтор другой задачи'
]);
Label::firstOrCreate([
'name' => 'дубликат',
'name' => 'доработка',
'description' => 'Новая фича, которую нужно запилить'
]);
}
Expand Down
24 changes: 24 additions & 0 deletions database/seeders/TaskLabelSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Seeders;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use App\Models\Task;
use App\Models\Label;

class TaskLabelSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
for ($i = 0; $i < 5; $i++) {
Task::all()->random()->labels()->saveMany([
Label::all()->random(),
Label::all()->random()
]);
}
}
}
20 changes: 20 additions & 0 deletions database/seeders/TaskSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Seeders;

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

class TaskSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Task::factory()
->assignetToSequence()
->count(5)
->create();
}
}
20 changes: 20 additions & 0 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;

class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::factory()
->count(5)
->create();
}
}
1 change: 1 addition & 0 deletions lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"Description": "Описание",
"View task": "Просмотр задачи",
"Create task": "Создать задачу",
"Create label": "Создать метку",
"Hello from Hexlet!" : "Привет от Хекслета!",
"This is a simple task manager" : "Это простой менеджер задач на Laravel",
"Author" : "Автор",
Expand Down
8 changes: 8 additions & 0 deletions resources/views/components/label.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="text-xs inline-flex items-center font-bold leading-sm uppercase px-3 py-1 bg-blue-200 text-blue-700 rounded-full">

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="h-4 w-4">
<path stroke-linecap="round" stroke-linejoin="round" d="m18.375 12.739-7.693 7.693a4.5 4.5 0 0 1-6.364-6.364l10.94-10.94A3 3 0 1 1 19.5 7.372L8.552 18.32m.009-.01-.01.01m5.699-9.941-7.81 7.81a1.5 1.5 0 0 0 2.112 2.13" />
</svg>

{{ $slot }}
</div>
14 changes: 14 additions & 0 deletions resources/views/labels/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-app-layout>
<x-slot name="header">
<h1 class="mb-5 text-5xl">{{ __('Create label') }}</h1>
</x-slot>

{{ html()->form('POST', route('labels.store'))->class('flex flex-col w-50')->open() }}
@include('labels.form')
<div class="mt-2">
<x-primary-button>
{{ __('Create') }}
</x-primary-button>
</div>
{{ html()->form()->close() }}
</x-app-layout>
14 changes: 14 additions & 0 deletions resources/views/labels/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-app-layout>
<x-slot name="header">
<h1 class="mb-5 text-5xl">{{ __('Change label')}}</h1>
</x-slot>

{{ html()->modelForm($label, "PATCH", route('labels.update', $label))->class('flex flex-col')->open() }}
@include('labels.form')
<div class="mt-2">
<x-primary-button>
{{ __('Edit') }}
</x-primary-button>
</div>
{{ html()->closeModelForm() }}
</x-app-layout>
14 changes: 14 additions & 0 deletions resources/views/labels/form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="mt-2">
{{ html()->label(__('Name'), 'name')->class('block font-medium text-sm text-gray-700 dark:text-gray-300') }}
</div>
<div class="mt-2">
{{ html()->input('text', 'name')->class('w-1/3 rounded border-gray-300')}}
</div>
<div class="mt-2">
{{ html()->label(__('Description'), 'description')->class('block font-medium text-sm text-gray-700 dark:text-gray-300') }}
</div>
<div class="mt-2">
{{ html()->textarea('description')->class('w-1/3 rounded border-gray-300')}}
</div>
<x-input-error :messages="$errors->get('name')" class="mt-2" />

45 changes: 45 additions & 0 deletions resources/views/labels/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<x-app-layout>
<x-slot name="header">
<h1 class="mb-5 text-5xl">{{ __('Labels')}}</h1>
</x-slot>

<a href="{{ route('labels.create') }}">
<x-primary-button >
{{ __('Create label') }}
</x-primary-button>
</a>

<table class="mt-4">
<thead class="border-b-2 border-solid border-black text-left">
<tr>
<th>ID</th>
<th>{{ __('Name') }}</th>
<th>{{ __('Created at') }}</th>
<th>{{ __('Actions') }}</th>
</tr>
</thead>
<tbody>
@foreach ($labels as $label)
<tr class="border-b border-dashed text-left">
<td>{{ $label->id }}</td>
<td>{{ $label->name }}</td>
<td>{{ $label->created_at }}</td>
<td>
@auth
<a data-confirm="Вы уверены?" data-method="delete" class="text-red-600 hover:text-red-900" href="{{ route('labels.destroy', $label) }}">
{{ __('Delete') }}
</a>
<a class="text-blue-600 hover:text-blue-900" href="{{ route('labels.edit', $label) }}">
{{ __('Change') }}
</a>
@endauth
</td>
</tr>
@endforeach
</tbody>
</table>

<div class="mt-3">
{{ $labels->links('pagination::tailwind') }}
</div>
</x-app-layout>
Loading

0 comments on commit 5838e41

Please sign in to comment.