Index: app/Http/Controllers/Dashboard/DepartmentsController.php
===================================================================
--- app/Http/Controllers/Dashboard/DepartmentsController.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Controllers/Dashboard/DepartmentsController.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -11,4 +11,5 @@
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Storage;
 
 class DepartmentsController extends Controller
@@ -17,5 +18,5 @@
     {
         return view("dashboard.departments.index")->with([
-            "departments" => Department::all()
+            "departments" => Department::all(),
         ]);
     }
@@ -37,6 +38,20 @@
         $department = Department::findOrFail($id);
 
+        $oldDepartmentCode = $department->code;
+
         $department->name = $request->name;
         $department->code = $request->code;
+
+        $path = '/Departments/' . $request->code;
+        $department->location = $path;
+
+        $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);
+            }
+        }
 
         $department->save();
@@ -54,5 +69,12 @@
         $department->code = $request->code;
 
+        if(!Storage::disk('local')->has('Departments/' . $request->code)){
+            Storage::disk('local')->makeDirectory('Departments/' . $request->code);
+        }
+
         $department->user_id = auth()->id();
+        $department->location = '/Departments/' . $request->code;
+
+        dd($department->location);
 
         $department->save();
Index: app/Http/Controllers/Dashboard/DocumentsController.php
===================================================================
--- app/Http/Controllers/Dashboard/DocumentsController.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Http/Controllers/Dashboard/DocumentsController.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,125 @@
+<?php
+
+namespace App\Http\Controllers\Dashboard;
+
+use App\Helpers\Alert;
+use App\Http\Requests\Dashboard\DocumentRequest;
+use App\Models\Department;
+use App\Models\Document;
+use App\Models\File;
+use App\Models\Files;
+use App\Models\User;
+use App\Notifications\NewDocumentCreated;
+use App\Services\UploadService;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Storage;
+use Mews\Purifier\Facades\Purifier;
+use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Notification;
+
+class DocumentsController extends Controller
+{
+    public function index()
+    {
+        if (auth()->user()->hasPermission("manage_all_documents"))
+            $documents = Document::all();
+        else
+            $documents = Document::where("user_id", auth()->user()->id)->get();
+
+        return view("dashboard.documents.index")->with([
+            "documents" => $documents,
+            "currentUser" => auth()->user(),
+            "departments" => Department::all(),
+            "docsCount" => Department::withCount('document')->get(),
+            'totalDocs' => Document::all()->count()
+        ]);
+    }
+
+    public function create()
+    {
+        return view("dashboard.documents.create")->with([
+            "departments" => Department::all()
+        ]);
+    }
+
+    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),
+            "departments" => Department::all()
+        ]);
+    }
+
+    public function edit(DocumentRequest $request, $id)
+    {
+        $document = Document::findOrFail($id);
+
+        $department = Department::find($request->department);
+
+        $document->department()->associate($department);
+
+        $document->name = $request->name;
+        $document->arch_id = $request->arch_id;
+        $document->description = $request->description;
+
+        $document->save();
+
+        Alert::flash("Document edited successfully");
+
+        return redirect()->route("dashboard.documents.index");
+    }
+
+    public function store(DocumentRequest $request, UploadService $uploadService)
+    {
+        $document = new Document();
+        $user = auth()->user();
+        $department = Department::find($request->department);
+
+        $document->user()->associate($user);
+        $document->department()->associate($department);
+
+        $document->arch_id = $request->arch_id;
+        $document->name = $request->name;
+        $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);
+
+        $document->save();
+
+        foreach ($documentFile as $df) {
+            $file = File::find($df);
+            $file->document()->associate($document);
+            $file->save();
+        }
+
+            Alert::flash("New document created successfully");
+
+        return redirect()->route("dashboard.documents.index");
+    }
+
+    public function getDocumentsInDepartment($id)
+    {
+        $documents = Department::findOrFail($id)->document()->get();
+        $department = Department::findOrFail($id);
+
+        return view('dashboard.documents.department')
+            ->with([
+                'documents' => $documents,
+                'departments' => Department::all(),
+                'totalDocs' => Document::all()->count(),
+                'department' => $department
+
+        ]);
+    }
+
+}
Index: app/Http/Controllers/Dashboard/IndexController.php
===================================================================
--- app/Http/Controllers/Dashboard/IndexController.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Controllers/Dashboard/IndexController.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -15,6 +15,4 @@
         );
 
-        Alert::flash("test");
-
         return view("dashboard.index")->with([
             "counters" => $counters,
Index: app/Http/Controllers/Dashboard/SettingsController.php
===================================================================
--- app/Http/Controllers/Dashboard/SettingsController.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Controllers/Dashboard/SettingsController.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -9,4 +9,6 @@
 use App\Http\Requests\Dashboard\PhotosSettingsRequest;
 use App\Http\Requests\Dashboard\SocialLinksSettingsRequest;
+use App\Http\Requests\Dashboard\UpdateUserData;
+use App\Http\Requests\Dashboard\UpdateUserRequest;
 use App\Http\Requests\Dashboard\UsernameSettingsRequest;
 use App\Http\Requests\Dashboard\UserProfileSettingsRequest;
@@ -78,5 +80,3 @@
     }
 
-
-
 }
Index: app/Http/Controllers/Dashboard/UsersController.php
===================================================================
--- app/Http/Controllers/Dashboard/UsersController.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Controllers/Dashboard/UsersController.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -5,5 +5,8 @@
 use App\Helpers\Alert;
 use App\Http\Requests\Dashboard\NewUserRequest;
+use App\Http\Requests\Dashboard\UpdateUserData;
 use App\Http\Requests\Dashboard\UpdateUserRequest;
+use App\Models\Avatar;
+use Illuminate\Support\Facades\File;
 use App\Models\Role;
 use App\Models\User;
@@ -12,7 +15,9 @@
 use App\Notifications\WelcomeUser;
 use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Storage;
 
 class UsersController extends Controller
 {
+
     public function index()
     {
@@ -44,4 +49,16 @@
         $user->role_id = $request->userRole;
 
+        if ($request->hasFile("avatar")) {
+            $image = $request->file("avatar");
+            $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
+
+            if (!empty($user->avatar)) {
+                Storage::disk('uploads')->delete("/users/" . $user->avatar);
+            }
+
+            Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
+
+            $user->avatar = $avatarName;
+        }
         $user->save();
 
@@ -70,4 +87,18 @@
         $user->mobile_number = $request->mobile_number;
         $user->role_id = $request->userRole;
+
+        if ($request->hasFile("avatar")) {
+            $image = $request->file("avatar");
+            $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
+
+            if (!empty($user->avatar)) {
+                Storage::disk('uploads')->delete("/users/" . $user->avatar);
+            }
+
+            Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
+
+            $user->avatar = $avatarName;
+        }
+
         if($user->isDirty('email')) {
             $user->notify(new VerifyNewEmail($user));
@@ -78,18 +109,4 @@
 
         return redirect()->route("dashboard.users.index");
-    }
-
-    public function editUserData(UpdateUserRequest $request, $id)
-    {
-        $user = User::findOrFail($id);
-        $user->name = $request->name;
-        $user->surname = $request->surname;
-        $user->mobile_number = $request->mobile_number;
-
-        $user->save();
-
-        Alert::flash("User data updated successfully");
-
-        return redirect()->route("dashboard.settings.index");
     }
 
@@ -129,3 +146,30 @@
       return $roles;
     }
+
+    public function editUserData(UpdateUserData $request, $id)
+    {
+        $user = User::findOrFail($id);
+        $user->name = $request->name;
+        $user->surname = $request->surname;
+        $user->mobile_number = $request->mobile_number;
+
+        if ($request->hasFile("avatar")) {
+            $image = $request->file("avatar");
+            $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
+
+            if (!empty($user->avatar)) {
+                Storage::disk('uploads')->delete("/users/" . $user->avatar);
+            }
+
+            Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
+
+            $user->avatar = $avatarName;
+        }
+
+        $user->save();
+
+        Alert::flash("User data updated successfully");
+
+        return redirect()->route("dashboard.settings.index");
+    }
 }
Index: app/Http/Kernel.php
===================================================================
--- app/Http/Kernel.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Kernel.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -39,4 +39,5 @@
             \App\Http\Middleware\VerifyCsrfToken::class,
             \Illuminate\Routing\Middleware\SubstituteBindings::class,
+            \App\Http\Middleware\LastSeenUserActivity::class,
         ],
 
Index: app/Http/Middleware/LastSeenUserActivity.php
===================================================================
--- app/Http/Middleware/LastSeenUserActivity.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Http/Middleware/LastSeenUserActivity.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use App\Models\User;
+use Closure;
+use Auth;
+use Cache;
+use Carbon\Carbon;
+
+class LastSeenUserActivity
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \Closure  $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+        if (Auth::check()) {
+            $expireTime = Carbon::now()->addMinute(1); // keep online for 1 min
+            Cache::put('is_online'.Auth::user()->id, true, $expireTime);
+
+            //Last Seen
+            User::where('id', Auth::user()->id)->update(['last_seen' => Carbon::now()]);
+        }
+        return $next($request);
+    }
+}
Index: app/Http/Requests/Dashboard/DocumentRequest.php
===================================================================
--- app/Http/Requests/Dashboard/DocumentRequest.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Http/Requests/Dashboard/DocumentRequest.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Http\Requests\Dashboard;
+
+use App\Models\Document;
+use Illuminate\Foundation\Http\FormRequest;
+
+class DocumentRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        if($this->isMethod("patch")) {
+            $document = Document::find($this->route("id"));
+            return auth()->user()->hasPermission("edit_all_documents") || ($document->user->id == auth()->user()->id);
+        }
+
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        $rules = [
+            "arch_id" => "required|min:10|max:255",
+            "name" => "required|min:10|max:255",
+            "department" => "required|integer|exists:departments,id",
+            "description" => "required|min:30",
+        ];
+
+        if($this->isMethod("patch")) {
+            $fileRules = [
+                "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
+            ];
+        } else {
+            $fileRules = [
+                "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
+            ];
+        }
+
+        $rules = array_merge(
+            $rules,
+            $fileRules,
+        );
+
+        return $rules;
+    }
+}
Index: app/Http/Requests/Dashboard/NewUserRequest.php
===================================================================
--- app/Http/Requests/Dashboard/NewUserRequest.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Requests/Dashboard/NewUserRequest.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -30,5 +30,6 @@
             "email" => "required|string|email|max:255|unique:users,email",
             "username" => "required|min:8|unique:users,username",
-            "userRole" => "required|exists:roles,id"
+            "userRole" => "required|exists:roles,id",
+            "avatar" => "mimes:jpeg,png,gif|max:5000",
         ];
     }
Index: app/Http/Requests/Dashboard/UpdateUserData.php
===================================================================
--- app/Http/Requests/Dashboard/UpdateUserData.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Http/Requests/Dashboard/UpdateUserData.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Http\Requests\Dashboard;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdateUserData extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            "name" => "required|alpha|min:2|max:255",
+            "surname" => "required|alpha|min:2|max:255",
+            "mobile_number" => "required|unique:users,mobile_number,$this->id,id",
+            "avatar" => "mimes:jpeg,png,gif|max:5000",
+        ];
+    }
+
+    protected function getValidatorInstance()
+    {
+
+        return parent::getValidatorInstance();
+    }
+}
Index: app/Http/Requests/Dashboard/UpdateUserRequest.php
===================================================================
--- app/Http/Requests/Dashboard/UpdateUserRequest.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Http/Requests/Dashboard/UpdateUserRequest.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -30,5 +30,6 @@
             "email" => "required|string|email|max:255|unique:users,email,$this->id,id",
             "username" => "required|min:8|unique:users,username,$this->id,id",
-            "userRole" => "required|exists:roles,id"
+            "userRole" => "required|exists:roles,id",
+            "avatar" => "mimes:jpeg,png,gif|max:5000",
         ];
     }
Index: app/Models/Department.php
===================================================================
--- app/Models/Department.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Models/Department.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -3,4 +3,5 @@
 namespace App\Models;
 
+use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Notifications\Notifiable;
@@ -9,8 +10,9 @@
 {
     use Notifiable;
+    use HasFactory;
 
     protected $table = "departments";
 
-    protected $fillable = ["name", "code", "user_id"];
+    protected $fillable = ["name", "code", "location", "user_id"];
 
     protected $casts = [
@@ -22,3 +24,7 @@
         return User::where('id', $this->user_id)->pluck('username')->first();
     }
+
+    public function document(){
+        return $this->hasMany(Document::class);
+    }
 }
Index: app/Models/Document.php
===================================================================
--- app/Models/Document.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Models/Document.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Document extends Model
+{
+    protected $table = "documents";
+
+    protected $fillable = ["arch_id", "name", "description", "user_id", "department_id"];
+
+    public function user() {
+        return $this->belongsTo(User::class);
+    }
+
+    public function department() {
+        return $this->belongsTo(Department::class);
+    }
+
+    public function ago() {
+        return Carbon::parse($this->created_at)->diffForHumans();
+    }
+
+    public function files()
+    {
+        return $this->hasMany(File::class);
+    }
+}
Index: app/Models/File.php
===================================================================
--- app/Models/File.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Models/File.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class File extends Model
+{
+    protected $table = "files";
+    protected $fillable = ["link", "document_id"];
+
+    public function document()
+    {
+        return $this->belongsTo(Document::class);
+    }
+}
Index: app/Models/User.php
===================================================================
--- app/Models/User.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Models/User.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -25,4 +25,5 @@
         "email",
         "mobile_number",
+        "avatar",
         "role_id"
     ];
@@ -126,4 +127,3 @@
         return Str::random($length);
     }
-
 }
Index: app/Notifications/NewDocumentCreated.php
===================================================================
--- app/Notifications/NewDocumentCreated.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Notifications/NewDocumentCreated.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,63 @@
+<?php
+
+namespace App\Notifications;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Notifications\Notification;
+use Illuminate\Notifications\Messages\MailMessage;
+
+class NewDocumentCreated extends Notification
+{
+    use Queueable;
+
+    protected $text;
+
+    /**
+     * Create a new notification instance.
+     *
+     * @return void
+     */
+    public function __construct($text)
+    {
+        $this->text = $text;
+    }
+
+    /**
+     * Get the notification's delivery channels.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function via($notifiable)
+    {
+        return ['database'];
+    }
+
+    /**
+     * Get the mail representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return \Illuminate\Notifications\Messages\MailMessage
+     */
+    public function toMail($notifiable)
+    {
+        return (new MailMessage)
+            ->line('The introduction to the notification.')
+            ->action('Notification Action', url('/'))
+            ->line('Thank you for using our application!');
+    }
+
+    /**
+     * Get the array representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function toArray($notifiable)
+    {
+        return [
+            "url" => "/dashboard/documents",
+            "message" => $this->text
+        ];
+    }
+}
Index: app/Providers/RouteServiceProvider.php
===================================================================
--- app/Providers/RouteServiceProvider.php	(revision 194a35967c638db611da70bbf868d51cd9124ed2)
+++ app/Providers/RouteServiceProvider.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -18,5 +18,5 @@
      * @var string
      */
-    public const HOME = '/home';
+    public const HOME = '/dashboard';
 
     /**
Index: app/Services/UploadService.php
===================================================================
--- app/Services/UploadService.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
+++ app/Services/UploadService.php	(revision 24a616f69ea5bcfc52f7d556c82a951a28dd130a)
@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Services;
+
+use Illuminate\Support\Facades\File;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Str;
+
+class UploadService
+{
+    public function upload($object, $names, $fieldName = null, $isMultiple = false)
+    {
+        $objIds = [];
+
+        foreach ($names as $name => $item) {
+
+            $property = $name;
+
+            if ($fieldName) $property = $fieldName;
+
+            if (!request()->hasFile($name)) {
+                continue;
+            }
+
+            if (!$isMultiple) {
+                if (!is_null($object->$property)) {
+                    Storage::disk("image-uploads")->delete($object->$property);
+                }
+            }
+
+            $images = request()->file($name);
+
+            if(!is_array($images)) $images = [$images];
+
+            foreach ($images as $image) {
+
+                if($isMultiple) {
+                    $object = new $object;
+                }
+
+                $extension = $image->getClientOriginalExtension();
+                $fileName = Str::random(20) . time() . "." . $extension;
+
+
+                Storage::disk("image-uploads")->put(
+                    $fileName,
+                    File::get($image)
+                );
+
+                $object->$property = $fileName;
+
+                if ($fieldName) $object->save();
+                array_push($objIds, $object->id);
+            }
+        }
+
+        return $objIds;
+    }
+}
