diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php deleted file mode 100644 index bf55212..0000000 --- a/app/Http/Controllers/DocumentController.php +++ /dev/null @@ -1,105 +0,0 @@ -sortBy('position'); - return view('documents.index', compact('files')); - } - - public function create($idReceptionScreen = null): View - { - abort_if(Auth::guest(), 403); - - $receptionScreens = ReceptionScreen::pluck('name', 'id'); - $files = Documents::where('reception_screen_id', '=', $idReceptionScreen)->get(); - return view('files.create', compact('receptionScreens', 'idReceptionScreen', 'files')); - } - - public function store(StoreFileRequest $request) - { - abort_if(Auth::guest(), 403); - - $nameFile = $request->file('url')->getClientOriginalName(); - $name = Storage::put('public', $request->file('url')); - - $validated = $request->validated(); - $file = new Documents(); - $file->name = $validated['name']; - $file->file_name = $nameFile; - $file->url = Storage::url($name); - $file->position = $validated['position']; - $file->reception_screen_id = $validated['idReceptionScreen']; - $file->save(); - - return redirect()->route('files.index'); - } - - public function download($id) - { - $file = (new Documents())->find($id); - return Storage::url($file->url); - } - public function edit(int $idFile) - { - abort_if(Auth::guest(), 403); - - $file = (new Documents())->find($idFile); - $files = Documents::where('reception_screen_id', '=', $file->reception_screen_id)->get(); - - $receptionScreens = ReceptionScreen::pluck('name', 'id'); - $idsReceptionScreens = $receptionScreens->keys()->toArray(); - $idReceptionScreen = $file->reception_screen_id; - return view( - 'files.edit', - compact( - 'receptionScreens', - 'idsReceptionScreens', - 'idReceptionScreen', - 'files', - 'file' - ) - ); - } - - public function update(UpdateFileRequest $request, Documents $file) - { - abort_if(Auth::guest(), 403); - - $validated = $request->validated(); - - $file->name = $validated['name']; - $file->position = $validated['position']; - $file->reception_screen_id = $validated['idReceptionScreen']; - $file->save(); - return redirect()->route('admin-reception-screen.index'); - } - - public function destroy($idFile) - { - abort_if(Auth::guest(), 403); - - $file = (new Documents())->find($idFile); - $file->delete(); - return redirect()->route('admin-reception-screen.index'); - } -} diff --git a/app/Http/Requests/admin/StoreDocumentRequest.php b/app/Http/Requests/admin/StoreDocumentRequest.php new file mode 100644 index 0000000..9450475 --- /dev/null +++ b/app/Http/Requests/admin/StoreDocumentRequest.php @@ -0,0 +1,25 @@ + 'required|max:255', + 'description' => 'string', + 'position' => 'required|int|max:255', + 'document' => 'required|file', + 'admission_id' => 'required|int|max:255', + ]; + } +} diff --git a/app/Http/Requests/admin/UpdateDocumentRequest.php b/app/Http/Requests/admin/UpdateDocumentRequest.php new file mode 100644 index 0000000..d3bd8d2 --- /dev/null +++ b/app/Http/Requests/admin/UpdateDocumentRequest.php @@ -0,0 +1,28 @@ + [ + 'required', + 'string', + 'max:255', + "unique:documents,name,{$this->document->id}", + ], + 'position' => 'required|int|max:255', + 'description' => 'string', + 'admission_id' => 'required|int|max:255', + ]; + } +} diff --git a/app/Models/File.php b/app/Models/Document.php similarity index 72% rename from app/Models/File.php rename to app/Models/Document.php index bd9c20b..69b5999 100644 --- a/app/Models/File.php +++ b/app/Models/Document.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -class File extends Model +class Document extends Model { use HasFactory; @@ -18,8 +18,8 @@ class File extends Model 'description', ]; - public function receptionScreen(): BelongsTo + public function admission(): BelongsTo { - return $this->belongsTo(ReceptionScreen::class); + return $this->belongsTo(Admission::class); } } diff --git a/database/factories/DocumentFactory.php b/database/factories/DocumentFactory.php new file mode 100644 index 0000000..6cc5663 --- /dev/null +++ b/database/factories/DocumentFactory.php @@ -0,0 +1,20 @@ + fake()->name(), + 'file_name' => fake()->name(), + 'description' => fake()->text(), + 'url' => fake()->url(), + 'position' => fake()->randomDigit(), + 'admission_id' => 1, + ]; + } +} diff --git a/database/migrations/2024_01_22_074748_create_files_table.php b/database/migrations/2024_01_22_074748_create_documents_table.php similarity index 70% rename from database/migrations/2024_01_22_074748_create_files_table.php rename to database/migrations/2024_01_22_074748_create_documents_table.php index d816926..a5266e8 100644 --- a/database/migrations/2024_01_22_074748_create_files_table.php +++ b/database/migrations/2024_01_22_074748_create_documents_table.php @@ -11,14 +11,14 @@ return new class extends Migration */ public function up(): void { - Schema::create('files', function (Blueprint $table) { + Schema::create('documents', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('file_name')->nullable(); - $table->string('description')->nullable(); + $table->text('description')->nullable(); $table->string('url'); $table->integer('position'); - $table->foreignId('reception_screen_id')->constrained('reception_screens'); + $table->foreignId('admission_id')->constrained('admissions'); $table->timestamps(); }); } @@ -28,6 +28,6 @@ return new class extends Migration */ public function down(): void { - Schema::dropIfExists('files'); + Schema::dropIfExists('documents'); } }; diff --git a/database/seeders/FileSeeder.php b/database/seeders/DocumentSeeder.php similarity index 64% rename from database/seeders/FileSeeder.php rename to database/seeders/DocumentSeeder.php index e09bf9a..78c7be7 100644 --- a/database/seeders/FileSeeder.php +++ b/database/seeders/DocumentSeeder.php @@ -2,42 +2,43 @@ namespace Database\Seeders; -use Carbon\Carbon; -use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; -class FileSeeder extends Seeder +class DocumentSeeder extends Seeder { /** * Run the database seeds. */ public function run(): void { - DB::table('files')->insert([ + DB::table('documents')->insert([ [ 'name' => 'файл 1', + 'file_name' => 'file1', 'description' => 'description1', 'url' => 'url/url1', 'position' => 2, - 'reception_screen_id' => 1, - 'created_at' => Carbon::now(), + 'admission_id' => 1, + 'created_at' => now(), ], [ 'name' => 'файл 2', 'description' => 'description2', + 'file_name' => 'file2', 'url' => 'url/url2', 'position' => 3, - 'reception_screen_id' => 1, - 'created_at' => Carbon::now(), + 'admission_id' => 1, + 'created_at' => now(), ], [ 'name' => 'файл 3', + 'file_name' => 'file3', 'description' => 'description3', 'url' => 'url/url3', - 'reception_screen_id' => 1, + 'admission_id' => 1, 'position' => 1, - 'created_at' => Carbon::now(), + 'created_at' => now(), ] ]); } diff --git a/qodana.yaml b/qodana.yaml deleted file mode 100644 index 664c0a1..0000000 --- a/qodana.yaml +++ /dev/null @@ -1,32 +0,0 @@ -#-------------------------------------------------------------------------------# -# Qodana analysis is configured by qodana.yaml file # -# https://www.jetbrains.com/help/qodana/qodana-yaml.html # -#-------------------------------------------------------------------------------# -version: "1.0" - -#Specify inspection profile for code analysis -profile: - name: qodana.starter - -#Enable inspections -#include: -# - name: - -#Disable inspections -#exclude: -# - name: -# paths: -# - - -php: - version: 8.3 #(Applied in CI/CD pipeline) - -#Execute shell command before Qodana execution (Applied in CI/CD pipeline) -#bootstrap: sh ./prepare-qodana.sh - -#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline) -#plugins: -# - id: #(plugin id can be found at https://plugins.jetbrains.com) - -#Specify Qodana linter for analysis (Applied in CI/CD pipeline) -linter: jetbrains/qodana-php:latest diff --git a/resources/views/admin/admission/index.blade.php b/resources/views/admin/admission/index.blade.php index da41bd3..d4d31b5 100644 --- a/resources/views/admin/admission/index.blade.php +++ b/resources/views/admin/admission/index.blade.php @@ -21,7 +21,7 @@ {{ $admission->position }} {{ $admission->name }} @if(count($admission->documents) !== 0) - -- {{ count($admission->documents) }} файла(ов) + ({{ count($admission->documents) }} файла) @endif
- Добавить файл @@ -93,7 +93,7 @@ - Добавить файл diff --git a/resources/views/admin/documents/edit.blade.php b/resources/views/admin/documents/edit.blade.php new file mode 100644 index 0000000..51b4a0e --- /dev/null +++ b/resources/views/admin/documents/edit.blade.php @@ -0,0 +1,59 @@ +@extends('layouts.admin_layout') +@section('content') + @auth() +
+
+

Изменить документ

+ {{ Form::open(['url' => route('documents.update', $document), 'method' => 'PATCH', 'files'=>'true']) }} +
+ {{ Form::label('name', 'Имя документа') }} +
+
+ {{ Form::text('name', $document->name, ['class' => 'form-control']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('name') }} + @endif +
+
+ {{ Form::label('description', 'Описание') }} +
+
+ {{ Form::text('description', $document->description, ['class' => 'form-control']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('description') }} + @endif +
+
+ {{ Form::label('position', 'Позиция') }} +
+
+ {{ Form::text('position', $document->position, ['class' => 'form-control']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('name') }} + @endif +
+
+ {{ Form::label('admission_id', 'Пункт экрана приема') }} +
+
+ {{ Form::select('admission_id', $admissions, $document->admission_id, ['class' => 'form-select']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('admission_id') }} + @endif +
+
+ {{ Form::submit('Изменить', ['class' => 'btn btn-primary'])}} +
+ {{Form::close()}} +
+
+ @endauth +@endsection diff --git a/resources/views/files/index.blade.php b/resources/views/admin/documents/index.blade.php similarity index 50% rename from resources/views/files/index.blade.php rename to resources/views/admin/documents/index.blade.php index e384314..c94a77a 100644 --- a/resources/views/files/index.blade.php +++ b/resources/views/admin/documents/index.blade.php @@ -1,30 +1,28 @@ -@extends('layouts.admin-layout') +@extends('layouts.admin_layout') @section('content')
-

Файлы

+

Документы


- Добавить файл + Добавить документ

-{{-- --}} - + + - @foreach($files as $file) + @foreach($documents as $document) - -{{-- --}} - - - + + diff --git a/resources/views/files/edit.blade.php b/resources/views/files/edit.blade.php deleted file mode 100644 index 61564aa..0000000 --- a/resources/views/files/edit.blade.php +++ /dev/null @@ -1,68 +0,0 @@ -@extends('layouts.admin-layout') -@section('content') - @auth() -
-
-

Изменить файл

- {{ Form::open(array('url' => route('files.update', $file), 'method' => 'PATCH', 'files'=>'true')) }} -
- {{ Form::label('name', 'Имя файла') }} -
-
- {{ Form::text('name', $file->name, ['class' => 'form-control']) }} -
-
- @if ($errors->any()) - {{ $errors->first('name') }} - @endif -
-
- {{ Form::label('position', 'Позиция') }} -
-
- {{ Form::text('position', $file->position, ['class' => 'form-control']) }} -
-
- @if ($errors->any()) - {{ $errors->first('name') }} - @endif -
-
- {{ Form::label('idReceptionScreen', 'Пункт экрана приема') }} -
-
- {{ Form::select('idReceptionScreen', $receptionScreens, $idReceptionScreen, $idsReceptionScreens,['class' => 'form-select']) }} -
-
- @if ($errors->any()) - {{ $errors->first('idReceptionScreen') }} - @endif -
-
- {{ Form::submit('Загрузить файл', ['class' => 'btn btn-primary'])}} -
- {{Form::close()}} -
- @if($idReceptionScreen !== null) -
-

Файлы пункта Экрана Приема: {{ $receptionScreens[$idReceptionScreen] }}

-
ПозицияЭкран ПриемаНазвание файлаНазвание документадействия действия
{{ $file->receptionScreen->name }}{{ $file->position }}{{ $file->name }}редактировать + {{ $document->admission->name }}{{ $document->name }}редактировать + href="{{ route('documents.destroy', $document) }}" class="btn btn-danger"> удалить
- - - - - - - - @foreach($files as $file) - - - - @endforeach - -
ПозицияНазвание
{{ $file->position }}{{ $file->name }}
-
- @endif -
- @endauth -@endsection diff --git a/resources/views/menu/reception-screen.blade.php b/resources/views/pages/reception-screen.blade.php similarity index 100% rename from resources/views/menu/reception-screen.blade.php rename to resources/views/pages/reception-screen.blade.php diff --git a/routes/admin.php b/routes/admin.php index 707d223..f1759c7 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -13,8 +13,9 @@ Route::middleware(['auth', 'verified'])->prefix('admin')->group(function () { return view('admin'); })->name('dashboard'); - Route::get('/documents/create/{admission}', [DocumentController::class, 'createFromAdmission'])->name('documents_create_with_admission'); - Route::get('/documents/download/{Document}', [DocumentController::class, 'download'])->name('documents_download'); + Route::get('/documents/create/{admission}', [DocumentController::class, 'createFromAdmission'])->name('document_create_from_admission'); + Route::post('/documents/store_from_admission', [DocumentController::class, 'storeFromAdmission'])->name('document_store_from_admission'); + Route::get('/documents/download/{document}', [DocumentController::class, 'download'])->name('document_download'); Route::resource('/educational_institutions', EducationalInstitutionController::class) ->scoped(['educational_institution' => 'slug']); diff --git a/tests/Feature/admin/DocumentTest.php b/tests/Feature/admin/DocumentTest.php new file mode 100644 index 0000000..0c4a108 --- /dev/null +++ b/tests/Feature/admin/DocumentTest.php @@ -0,0 +1,134 @@ +admission = Admission::factory()->create(); + + $this->document = Document::factory()->create(); + + $this->data = Document::factory()->make()->only([ + 'position', + 'name', + 'description', + 'admission_id', + ]); + + $this->user = User::factory()->create([ + 'name' => 'admin', + 'email' => 'test@example.com', + 'password' => 123456 + ]); + } + public function testIndexDocumentsPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('documents.index')); + + $response->assertOk(); + } + + public function testCreateDocumentPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('documents.create')); + + $response->assertOk(); + } + + public function testStoreDocument(): void + { + Storage::fake('fake'); + $file = UploadedFile::fake()->create('fake.pdf', 100, 'application/pdf'); + + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->post(route('documents.store'), [...$this->data, 'document' => $file]); + + $response->assertRedirect(route('documents.index')); + + $this->assertDatabaseHas('documents', $this->data); + } + + public function testShowDocumentPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('documents.show', $this->document)); + + $response->assertOk(); + } + + public function testEditDocumentPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('documents.edit', $this->document)); + + $response->assertOk(); + } + + public function testUpdateDocument(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->patch(route('documents.update', $this->document), $this->data); + + $response->assertRedirect(route('documents.index')); + + $this->assertDatabaseHas('documents', $this->data); + } + + public function testDestroyDocument(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->delete(route('documents.destroy', $this->document)); + + $response->assertRedirect(route('documents.index')); + + $this->assertDatabaseMissing('documents', $this->document->toArray()); + } + + public function testCreateDocumentFromAdmissionPage(): void + { + + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('document_create_from_admission', $this->admission)); + + $response->assertOk(); + } + + public function testStoreFromAdmissionDocument(): void + { + Storage::fake('fake'); + $file = UploadedFile::fake()->create('fake.pdf', 100, 'application/pdf'); + + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->post(route('document_store_from_admission'), [...$this->data, 'document' => $file]); + + $response->assertRedirect(route('admissions.index')); + + $this->assertDatabaseHas('documents', $this->data); + } +}