REFACTORING #1880 @40m: move creating new directory into interaction
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace App\CMS\Exceptions\FileManager;
|
||||
|
||||
class SubdirectoryExistsInDirectory extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -24,7 +24,7 @@ class Interaction
|
||||
* @var $interaction InteractionContract
|
||||
*/
|
||||
|
||||
$interaction = App::make($interactionClass);
|
||||
$interaction = app($interactionClass);
|
||||
// validate interaction
|
||||
$validator = Validator::make($data, $interaction->getValidationRules($extraParameters));
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace App\CMS\Interactions\Admin\FileManager;
|
||||
|
||||
use App\CMS\Contracts\Interaction;
|
||||
use App\CMS\Exceptions\FileManager\SubdirectoryExistsInDirectory;
|
||||
use App\CMS\Repositories\FileManager\Directory as DirectoryRepository;
|
||||
use App\Models\FileManager\Directory;
|
||||
|
||||
class CreateDirectory implements Interaction
|
||||
{
|
||||
/**
|
||||
* @var DirectoryRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
public function __construct(DirectoryRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform given action
|
||||
*
|
||||
* @param $data array input data
|
||||
* @param $parameters array parameters
|
||||
*
|
||||
* @return Directory
|
||||
* @throws SubdirectoryExistsInDirectory
|
||||
*/
|
||||
public function handle($data, $parameters)
|
||||
{
|
||||
$data['parentDirectory'] = isset($data['parentDirectory']) ? $data['parentDirectory'] : null;
|
||||
|
||||
// todo this should probably go into validation, but we are not able to get $data in getValidationRules
|
||||
// check if created folder does not exist in current directory
|
||||
if ($this->repository->checkIfNameExists($data['name'], $data['parentDirectory'])) {
|
||||
throw new SubdirectoryExistsInDirectory();
|
||||
}
|
||||
|
||||
// todo use Directory::create() and rename columns/fields in request
|
||||
// create new database entry
|
||||
$directory = new Directory();
|
||||
$directory->name = $data['name'];
|
||||
$directory->parent_id = $data['parentDirectory'];
|
||||
$directory->user_id = \Auth::user()->id;
|
||||
$directory->description = $data['description'];
|
||||
$directory->save();
|
||||
|
||||
return $directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data array input data
|
||||
*
|
||||
* @return array
|
||||
* @throws SubdirectoryExistsInDirectory
|
||||
*/
|
||||
public function getValidationRules($data = [])
|
||||
{
|
||||
return [
|
||||
'name' => 'required|max:255',
|
||||
'parentDirectory' => 'exists:directories,id'
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace App\CMS\Repositories\FileManager;
|
||||
|
||||
use App\Models\FileManager\Directory as DirectoryModel;
|
||||
|
||||
class Directory
|
||||
{
|
||||
public function checkIfNameExists($name, $directoryId)
|
||||
{
|
||||
return DirectoryModel::where('name', $name)
|
||||
->where('parent_id', $directoryId)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\CMS\Interactions\Admin\FileManager\CreateDirectory;
|
||||
|
||||
class CreateNewDirectoryRequest extends Request
|
||||
{
|
||||
/**
|
||||
@@ -11,9 +13,8 @@ class CreateNewDirectoryRequest extends Request
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required|max:255',
|
||||
'parentDirectory' => 'exists:directories,id'
|
||||
];
|
||||
$interaction = app(CreateDirectory::class);
|
||||
|
||||
return $interaction->getValidationRules();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user