REFACTORING #1880 @40m: move creating new directory into interaction
This commit is contained in:
@@ -2,12 +2,31 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin\FileManager;
|
||||
|
||||
use App\CMS\Exceptions\FileManager\SubdirectoryExistsInDirectory;
|
||||
use App\Cms\Handlers\Interactions\Interaction;
|
||||
use App\CMS\Interactions\Admin\FileManager\CreateDirectory;
|
||||
use App\CMS\Repositories\FileManager\Directory as DirectoryRepository;
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Models\FileManager\Directory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CreateNewDirectory extends Controller
|
||||
{
|
||||
/**
|
||||
* @var DirectoryRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* CreateNewDirectory constructor.
|
||||
*
|
||||
* @param DirectoryRepository $repository
|
||||
*/
|
||||
public function __construct(DirectoryRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new directory
|
||||
*
|
||||
@@ -15,48 +34,21 @@ class CreateNewDirectory extends Controller
|
||||
*
|
||||
* @return $this|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postNew(Requests\CreateNewDirectoryRequest $request)
|
||||
public function postNew(Request $request)
|
||||
{
|
||||
// check if created folder does not exist in current directory
|
||||
$exists = Directory::where('name', $request->name)
|
||||
->where('parent_id', $request->parentDirectory)
|
||||
->exists();
|
||||
if ($exists) {
|
||||
if ($request->ajax()) {
|
||||
return response()->json([
|
||||
'status' => 'failed',
|
||||
'message' => trans('admin/fileManager.subdirectoryExists')
|
||||
]);
|
||||
} else {
|
||||
return redirect()
|
||||
->route('upload.overview', ['id' => $request->parentDirectory])
|
||||
->withInput()
|
||||
->with('error', trans('admin/fileManager.subdirectoryExists'));
|
||||
}
|
||||
// create new directory
|
||||
try {
|
||||
Interaction::handle(CreateDirectory::class, $request->all());
|
||||
} catch (SubdirectoryExistsInDirectory $e) {
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->with('error', trans('admin/fileManager.subdirectoryExists'));
|
||||
}
|
||||
|
||||
// create new database entry
|
||||
$directory = new Directory();
|
||||
$directory->name = $request->name;
|
||||
$directory->parent_id = $request->parentDirectory;
|
||||
$directory->user_id = \Auth::user()->id;
|
||||
$directory->description = $request->description;
|
||||
|
||||
$directory->save();
|
||||
|
||||
$message = trans('admin/fileManager.newDirectorySuccess');
|
||||
if (!$request->ajax()) {
|
||||
return redirect()
|
||||
->route('upload.overview', ['id' => $request->parentDirectory])
|
||||
->with('success', $message);
|
||||
} else {
|
||||
$payload = [
|
||||
'status' => 'success',
|
||||
'message' => $message,
|
||||
'parentDirectory' => $request->parentDirectory
|
||||
];
|
||||
|
||||
return response()->json($payload);
|
||||
}
|
||||
return redirect()
|
||||
->route('upload.overview', ['id' => $request->parentDirectory])
|
||||
->with('success', $message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user