Clean Document Model / Controller / ..

This commit is contained in:
Vincent BRIDIER 2022-05-11 12:36:27 +02:00
parent e1dff7e9b4
commit d7130f72eb
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
11 changed files with 55 additions and 61 deletions

View file

@ -32,7 +32,7 @@ class SyncController extends Controller
}
public function getDocuments(Request $request) {
return new \App\Http\Resources\DocumentsCollection(\App\Models\Documents::where('published', 1)->get());
return new \App\Http\Resources\DocumentsCollection(\App\Models\Document::where('published', 1)->get());
}
public function getVersion(Request $request) {

View file

@ -3,7 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Directories;
use App\Models\Documents;
use App\Models\Document;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
@ -14,23 +14,19 @@ use Illuminate\Http\Request;
class DocumentsController extends Controller
{
public function __construct()
public function index()
{
$this->middleware('auth', ['except' => ['download']]);
return view('document.index', ['documents' => Document::where('lang', '=', 'fr')->get()]);
}
public function index(Request $request) {
return view('documents.index', ['documents' => Documents::where('lang', '=', 'fr')->get()]);
}
public function add(Request $request) {
$folders = Directories::all();
return view('documents.add', ['folders' => $folders, 'docsFR' => Documents::where('lang', '=', 'fr')->orderBy('title', 'asc')->get()]);
public function create()
{
return view('document.create', ['folders' => Directories::all(), 'docsFR' => Document::where('lang', '=', 'fr')->orderBy('title', 'asc')->get()]);
}
public function store(Request $request) {
$tmp = $request->document;
$doc = new Documents();
$doc = new Document();
$doc->uuid = $tmp['uuid'];
$doc->title = $tmp['title'];
$doc->type = $tmp['type'];
@ -49,31 +45,29 @@ class DocumentsController extends Controller
return 'NOK';
}
public function edit(Request $request) {
$requestId = $request->id;
$doc = Documents::findOrFail($requestId);
$folders = Directories::all();
return view('documents.edit', ['document' => $doc, 'folders' => $folders]);
public function edit(Document $document)
{
return view('document.edit', ['document' => $document, 'folders' => Directories::all()]);
}
public function update(Request $request) {
public function update(Request $request, Document $document)
{
$tmp = $request->document;
$doc = Documents::findOrFail($tmp['id']);
$doc->uuid = $tmp['uuid'];
$doc->title = $tmp['title'];
$doc->type = $tmp['type'];
$doc->directory = $tmp['directory'];
$doc->path = $tmp['path'];
$document->uuid = $tmp['uuid'];
$document->title = $tmp['title'];
$document->type = $tmp['type'];
$document->directory = $tmp['directory'];
$document->path = $tmp['path'];
if ($tmp['published'] === true || $tmp['published'] === 1){
$doc->published = 1;
$document->published = 1;
}else{
$doc->published = 0;
$document->published = 0;
}
$doc->save();
$document->save();
if ($doc->lang === 'fr') {
foreach ($doc->others_lang as $item) {
$item->directory = $doc->directory;
if ($document->lang === 'fr') {
foreach ($document->others_lang as $item) {
$item->directory = $document->directory;
$item->save();
}
}
@ -125,22 +119,20 @@ class DocumentsController extends Controller
/** Download the document */
public function download(Request $request, $uuid)
{
$doc = Documents::where('uuid', $uuid)->firstOrFail();
$doc = Document::where('uuid', $uuid)->firstOrFail();
$pathToFile = public_path() . Storage::url('docs/' . $doc->uuid . '.' . $doc->type);
return response()->download($pathToFile, $doc->path);
}
public function delete(Request $request)
public function destroy(Request $request, Document $document)
{
$doc = Documents::findOrFail($request->id);
if ($doc->lang === 'fr') {
foreach ($doc->others_lang as $item) {
if ($document->lang === 'fr') {
foreach ($document->others_lang as $item) {
$item->delete();
}
}
$doc->delete();
$document->delete();
return ['status' => 'OK'];
}

View file

@ -2,7 +2,6 @@
namespace App\Http\Resources;
use App\Models\Documents;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DocumentsCollection extends ResourceCollection

View file

@ -21,6 +21,6 @@ class Directories extends Model
public function documents()
{
return $this->hasMany(Documents::class, 'directory', 'id');
return $this->hasMany(Document::class, 'directory', 'id');
}
}

View file

@ -4,18 +4,21 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Documents extends Model
class Document extends Model
{
public function getOthersLangAttribute() {
return Documents::where('parent', '=', $this->id)->get();
public function getOthersLangAttribute()
{
return Document::where('parent', '=', $this->id)->get();
}
public function getParentsAttribute() {
public function getParentsAttribute()
{
return $this->constructParents($this->directory);
}
private function constructParents($id, $parents = []) {
private function constructParents($id, $parents = [])
{
$tmp = Directories::find($id);
array_push($parents, $tmp);
if ($tmp->parent != NULL) {

View file

@ -14,7 +14,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
protected $namespace = null;
/**
* The path to the "home" route for your application.

View file

@ -118,7 +118,7 @@
if (this.documentDatas.title.length > 3){
this.error = '';
let _this2 = this;
axios.post('/documents/' + this.documentDatas.id + '/update', {
axios.put('/documents/' + this.documentDatas.id, {
document: this.documentDatas,
})
.then(function (response) {
@ -172,7 +172,7 @@
$('#DeleteModal').modal('show');
},
deleteDocument() {
axios.post('/documents/' + this.documentDatas.id + '/delete')
axios.delete('/documents/' + this.documentDatas.id)
.then(response => {
if (response.data.status === 'OK'){
window.location.href = '/documents';

View file

@ -3,24 +3,24 @@
<div class="container">
<div class="row">
<div class="col-md-12 d-flex justify-content-end">
<a href="/documents/add" class="btn btn-sm btn-primary">Ajouter</a>
<a href="{{ route('documents.create') }}" class="btn btn-sm btn-primary">{{ __('Ajouter') }}</a>
</div>
</div>
<br>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Titre</th>
<th scope="col">Type</th>
<th scope="col">Dossier</th>
<th scope="col">Langue</th>
<th scope="col">Publié ?</th>
<th scope="col">{{ __('Titre') }}</th>
<th scope="col">{{ __('Type') }}</th>
<th scope="col">{{ __('Dossier') }}</th>
<th scope="col">{{ __('Langue') }}</th>
<th scope="col">{{ __('Publié') }} ?</th>
</tr>
</thead>
<tbody>
@foreach($documents as $document)
<tr>
<th scope="row"><a href="/documents/{{$document->id}}/edit">{{$document->title}}</a></th>
<th scope="row"><a href="{{ route('documents.edit', ['document' => $document]) }}">{{$document->title}}</a></th>
<td>{{$document->type}}</td>
<td>
@foreach($document->parents as $parent)
@ -30,15 +30,15 @@
<td>{{strtoupper($document->lang)}}</td>
<td>
@if ($document->published === 1)
Oui
{{ __('Oui') }}
@else
Non
{{ __('Non') }}
@endif
</td>
</tr>
@foreach($document->others_lang as $doc)
<tr>
<th scope="row"><a href="/documents/{{$doc->id}}/edit">--- {{$doc->title}}</a></th>
<th scope="row"><a href="{{ route('documents.edit', ['document' => $doc]) }}">--- {{$doc->title}}</a></th>
<td>{{$doc->type}}</td>
<td>
@foreach($doc->parents as $parent)
@ -47,10 +47,10 @@
</td>
<td>{{strtoupper($doc->lang)}}</td>
<td>
@if ($doc->published === 1)
Oui
@if ($document->published === 1)
{{ __('Oui') }}
@else
Non
{{ __('Non') }}
@endif
</td>
</tr>

View file

@ -60,7 +60,7 @@
<a class="nav-link" href="/folders">Dossiers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/documents">Documents</a>
<a class="nav-link" href="{{ route('documents.index') }}">{{ __('Documents') }}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/show">Salon</a>