forked from aslan/applicant-site
refactoring Admission and Document resource
This commit is contained in:
parent
5a462a0823
commit
1659980a2e
|
@ -0,0 +1,100 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\admin;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\admin\StoreDocumentRequest;
|
||||||
|
use App\Http\Requests\admin\UpdateDocumentRequest;
|
||||||
|
use App\Models\Admission;
|
||||||
|
use App\Models\Document;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\View\Factory;
|
||||||
|
use Illuminate\Contracts\View\View;
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class DocumentController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
|
{
|
||||||
|
$documents = Document::all();
|
||||||
|
return view('admin.documents.index', compact('documents'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
|
{
|
||||||
|
$admissions = Admission::pluck('name', 'id');
|
||||||
|
return view('admin.documents.create', compact('admissions'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreDocumentRequest $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$this->saveFile($request);
|
||||||
|
return redirect()->route('documents.index');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(Document $document)
|
||||||
|
{
|
||||||
|
$admissions = Admission::pluck('name', 'id');
|
||||||
|
return view('admin.documents.edit', compact('admissions', 'document'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Document $document): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
|
{
|
||||||
|
return view('admin.documents.show', compact('document'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateDocumentRequest $request, Document $document): RedirectResponse
|
||||||
|
{
|
||||||
|
$validated = $request->validated();
|
||||||
|
|
||||||
|
$document->name = $validated['name'];
|
||||||
|
$document->description = $validated['description'];
|
||||||
|
$document->position = $validated['position'];
|
||||||
|
$document->admission_id = $validated['admission_id'];
|
||||||
|
$document->save();
|
||||||
|
return redirect()->route('documents.index');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Document $document)
|
||||||
|
{
|
||||||
|
$document->delete();
|
||||||
|
return redirect()->route('documents.index');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function download($id)
|
||||||
|
{
|
||||||
|
$file = (new Document())->find($id);
|
||||||
|
return Storage::url($file->url);
|
||||||
|
}
|
||||||
|
public function createFromAdmission(Admission $admission): View
|
||||||
|
{
|
||||||
|
$admissions = Admission::pluck('name', 'id');
|
||||||
|
$documents = Document::where('admission_id', '=', $admission->id)->get();
|
||||||
|
return view('admin.documents.create_from_admission', compact('admissions', 'admission', 'documents'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function storeFromAdmission(StoreDocumentRequest $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$this->saveFile($request);
|
||||||
|
return redirect()->route('admissions.index');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveFile(StoreDocumentRequest $request)
|
||||||
|
{
|
||||||
|
$fileName = $request->file('document')->getClientOriginalName();
|
||||||
|
$name = Storage::put('public', $request->file('document'));
|
||||||
|
|
||||||
|
$validated = $request->validated();
|
||||||
|
|
||||||
|
$document = new Document();
|
||||||
|
$document->name = $validated['name'];
|
||||||
|
$document->description = $validated['description'];
|
||||||
|
$document->file_name = $fileName;
|
||||||
|
$document->url = Storage::url($name);
|
||||||
|
$document->position = $validated['position'];
|
||||||
|
$document->admission_id = $validated['admission_id'];
|
||||||
|
$document->save();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
@extends('layouts.admin_layout')
|
||||||
|
@section('content')
|
||||||
|
@auth()
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<h1 class="">Прикрепить файл</h1>
|
||||||
|
{{ Form::open(['url' => route('document_store_from_admission'), 'method' => 'POST', 'files'=>'true']) }}
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::label('document', 'Путь к документу') }}
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::file('document', ['class' => 'form-control']) }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
@if ($errors->any())
|
||||||
|
{{ $errors->first('document') }}
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::label('name', 'Имя файла') }}
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::text('name', '', ['class' => 'form-control']) }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
@if ($errors->any())
|
||||||
|
{{ $errors->first('name') }}
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::label('description', 'Описание') }}
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::text('description', '', ['class' => 'form-control']) }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
@if ($errors->any())
|
||||||
|
{{ $errors->first('description') }}
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::label('position', 'Позиция') }}
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::text('position', 0, ['class' => 'form-control']) }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
@if ($errors->any())
|
||||||
|
{{ $errors->first('name') }}
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::label('admission_id', 'Пункт экрана приема') }}
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
{{ Form::select('admission_id', $admissions, $admission->id, ['class' => 'form-select']) }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
@if ($errors->any())
|
||||||
|
{{ $errors->first('admission_id') }}
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<div class="mt-4">
|
||||||
|
{{ Form::submit('Загрузить файл', ['class' => 'btn btn-primary'])}}
|
||||||
|
</div>
|
||||||
|
{{Form::close()}}
|
||||||
|
</div>
|
||||||
|
<div class="col-4">
|
||||||
|
<h3>Документы пункта {{ $admission->name }}</h3>
|
||||||
|
<ul class="list-group">
|
||||||
|
@foreach($documents as $document)
|
||||||
|
<li class="list-group-item">{{ $document->name }}</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endauth
|
||||||
|
@endsection
|
Loading…
Reference in New Issue