Index: app/Http/Controllers/Dashboard/DepartmentsController.php
===================================================================
--- app/Http/Controllers/Dashboard/DepartmentsController.php	(revision e6c1f87d9981de7492736e12f9cc56fc273c8e2a)
+++ app/Http/Controllers/Dashboard/DepartmentsController.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
@@ -14,4 +14,5 @@
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Storage;
+use function Illuminate\Events\queueable;
 
 class DepartmentsController extends Controller
@@ -36,12 +37,12 @@
         $department->code = $request->code;
 
-        $len = Storage::disk('local')->path('');
+        $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
 
-        if(!Storage::disk('local')->has('Departments/' . $request->code)){
-            Storage::disk('local')->makeDirectory('Departments/' . $request->code);
+        if(!Storage::disk('local')->has($location)){
+            Storage::disk('local')->makeDirectory($location);
+
         }
-
+        $department->location = Storage::disk('local')->path('') . $location;
         $department->user_id = auth()->id();
-        $department->location =  Storage::disk('local')->path('') . 'Departments/' . $request->code;
 
         $department->save();
@@ -63,17 +64,15 @@
         $department = Department::findOrFail($id);
 
-        $oldDepartmentCode = $department->code;
+        $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
 
         $department->name = $request->name;
         $department->code = $request->code;
 
-        $department->location =  Storage::disk('local')->path('') . 'Departments/' . $request->code;
-
-        $files = Storage::allFiles($oldDepartmentCode);
-
         if($department->isDirty('code'))
         {
-            if(!Storage::disk('local')->has('Departments/' . $request->code)){
-                Storage::disk('local')->move('Departments/' . $oldDepartmentCode, 'Departments/' . $department->code);
+            $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
+            if(!Storage::disk('local')->has($location)){
+                Storage::disk('local')->move($oldLocation, $location);
+                $department->location = Storage::disk('local')->path('') . $location;
             }
         }
Index: app/Http/Controllers/Dashboard/DocumentsController.php
===================================================================
--- app/Http/Controllers/Dashboard/DocumentsController.php	(revision e6c1f87d9981de7492736e12f9cc56fc273c8e2a)
+++ app/Http/Controllers/Dashboard/DocumentsController.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
@@ -26,35 +26,31 @@
         $result = $result->flatten();
 
+        $deptName = "";
+        $deptCode = "";
 
         if ($request->query('id')) {
-
-            $documentsInDeptSort = Document::with('department')->when($request->has('id'), function($query) use ($request) {
+            $deptName = Department::find($request->query('id'))->getOriginal('name');
+            $deptCode = Department::find($request->query('id'))->getOriginal('code');
+            $documentsInDeptSort = Document::with('department')->when($request->has('id'), function ($query) use ($request) {
                 $query->where('department_id', $request->query('id'));
             });
 
-            if($request->query('sort') == 'newest') {
-                $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(20);
-            }
-            else if($request->query('sort') == 'name') {
-                $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(20);
-            }
-            else{
-                $documents = Document::where('department_id', $request->query('id'))->paginate(20);
-            }
-        }
-        else {
-            if($request->query('sort') == 'newest') {
-                $documents = Document::orderBy('created_at', 'desc')->paginate(20);
-            }
-            else if($request->query('sort') == 'name') {
-                $documents = Document::orderBy('name', 'asc')->paginate(20);
-            }
-            else if($request->query('sort') == 'important'){
-                $documents = Document::where('is_important', true)->paginate(20);
-            }
-            else if($request->query('sort') == 'recent') {
-               $documents = Document::orderBy('created_at', 'desc')->paginate(20);
-            }
-            else if($request->query('search')){
+            if ($request->query('sort') == 'newest') {
+                $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(16);
+            } else if ($request->query('sort') == 'name') {
+                $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(16);
+            } else {
+                $documents = Document::where('department_id', $request->query('id'))->paginate(16);
+            }
+        } else {
+            if ($request->query('sort') == 'newest') {
+                $documents = Document::orderBy('created_at', 'desc')->paginate(16);
+            } else if ($request->query('sort') == 'name') {
+                $documents = Document::orderBy('name', 'asc')->paginate(16);
+            } else if ($request->query('sort') == 'important') {
+                $documents = Document::where('is_important', true)->paginate(16);
+            } else if ($request->query('sort') == 'recent') {
+                $documents = Document::orderBy('created_at', 'desc')->paginate(16);
+            } else if ($request->query('search')) {
                 $result = collect();
 
@@ -64,7 +60,5 @@
                 $result = $result->flatten();
                 $documents = $result;
-            }
-            else
-            {
+            } else {
                 $documents = Document::paginate(20);
             }
@@ -76,5 +70,5 @@
         $diskTotalSize = $diskTotal / 1073741824;
 
-        $diskFree  = disk_free_space('/');
+        $diskFree = disk_free_space('/');
         $used = $diskTotal - $diskFree;
 
@@ -93,5 +87,7 @@
             "diskTotalSize" => $diskTotalSize,
             "diskUse" => $diskUse,
-            "diskUsedSize" => $diskUsedSize
+            "diskUsedSize" => $diskUsedSize,
+            "deptName" => $deptName,
+            "deptCode" => $deptCode,
 
         ]);
@@ -106,5 +102,5 @@
     }
 
-    public function store(DocumentRequest $request, UploadService $uploadService)
+    public function store(DocumentRequest $request)
     {
         $document = new Document();
@@ -119,20 +115,20 @@
         $document->description = $request->description;
 
-        if (!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)) {
-            Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
-        }
-
-        $documentFile = $uploadService->upload(File::class, [
-            "file_item" => $request->file_item,
-        ], "link", true);
+        $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
+
+        if (!Storage::disk('local')->has($location)) {
+            Storage::disk('local')->makeDirectory($location);
+        }
+
+        foreach ($request->file_item as $file) {
+            $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
+            $newFile = new File();
+            $newFile->link = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
+            $newFile->document()->associate($document);
+            $newFile->save();
+        }
 
         $document->save();
 
-        foreach ($documentFile as $df) {
-            $file = File::find($df);
-            $file->document()->associate($document);
-            $file->save();
-        }
-
         Alert::flash("New document created successfully");
 
@@ -142,8 +138,4 @@
     public function editShow($id)
     {
-//        if (!auth()->user()->hasPermission("edit_all_documents")) {
-//            return redirect()->route("dashboard.documents.index");
-//        }
-
         return view("dashboard.documents.edit")->with([
             "document" => Document::findOrFail($id),
@@ -164,4 +156,34 @@
         $document->description = $request->description;
 
+        $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $document->name;
+        $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
+
+        if ($document->isDirty('name')) {
+            if (!Storage::disk('local')->has($location)) {
+                Storage::disk('local')->move($oldLocation, $location);
+            }
+        }
+
+        $hasFileError = false;
+        if ($request->has('file_item')) {
+            foreach ($request->file_item as $file) {
+                $fileName = $file->getClientOriginalName();
+                if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
+                    $hasFileError = true;
+                    break;
+                }
+                $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
+                $newFile = new File();
+                $newFile->link = $location . DIRECTORY_SEPARATOR . $fileName;
+                $newFile->document()->associate($document);
+                $newFile->save();
+            }
+        }
+
+        if($hasFileError) {
+            Alert::flash('Document with the same name exists', 'error');
+            return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
+        }
+
         $document->save();
 
@@ -177,6 +199,6 @@
         $document->save();
 
-        if($document->is_important==true)
-        Alert::flash("Document marked as important successfully");
+        if ($document->is_important == true)
+            Alert::flash("Document marked as important successfully");
         else
             Alert::flash("Document marked as not important successfully");
@@ -208,5 +230,5 @@
     {
         $document = Document::find($id);
-        if (auth()->user()->hasPermission("delete_all_posts")) {
+        if (auth()->user()->hasPermission("delete_all_documents")) {
             $document->delete();
             Alert::flash($document->name . " deleted successfully");
