Full kits manipulation without users intagration

This commit is contained in:
Minaev Dmitriy 2018-11-06 19:27:28 +03:00
parent 9d5cd27575
commit 79d979f47f
6 changed files with 76 additions and 26 deletions

View file

@ -202,7 +202,7 @@ class PredefinedKitsController extends Controller
}
$kit->licenses()->sync([$license_id => ['quantity' => $quantity]]);
return response()->json(Helper::formatStandardApiResponse('success', null, 'License updated')); // TODO: trans
return response()->json(Helper::formatStandardApiResponse('success', $kit, 'License updated')); // TODO: trans
}
/**
@ -213,13 +213,13 @@ class PredefinedKitsController extends Controller
* @param int $kit_id
* @return \Illuminate\Http\Response
*/
public function destroyLicense($kit_id, $license_id)
public function detachLicense($kit_id, $license_id)
{
$this->authorize('update', PredefinedKit::class);
$kit = PredefinedKit::findOrFail($id);
$kit->licenses()->detach($license_id);
return response()->json(Helper::formatStandardApiResponse('success', null, 'Delete was successfull')); // TODO: trans
return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Delete was successfull')); // TODO: trans
}
/**
@ -253,12 +253,17 @@ class PredefinedKitsController extends Controller
$kit = PredefinedKit::findOrFail($kit_id);
$model_id = $request->get('model');
$quantity = $request->input('quantity', 1);
if( $quantity < 1) {
$quantity = 1;
}
//echo $request->get('model');
$kit->models()->attach( $request->get('model'), ['quantity' => $quantity]);
$relation = $kit->models();
if( $relation->find($model_id) ) {
return response()->json(Helper::formatStandardApiResponse('error', null, 'Model already exists'));
}
$relation->attach($model_id, ['quantity' => $quantity]);
return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Model added successfull')); // TODO: trans
}
@ -280,9 +285,9 @@ class PredefinedKitsController extends Controller
if( $quantity < 1) {
$quantity = 1;
}
$kit->models()->sync([$model_id => ['quantity' => $quantity]]);
$kit->models()->syncWithoutDetaching([$model_id => ['quantity' => $quantity]]);
return response()->json(Helper::formatStandardApiResponse('success', null, 'License updated')); // TODO: trans
return response()->json(Helper::formatStandardApiResponse('success', $kit, 'License updated')); // TODO: trans
}
/**
@ -293,12 +298,12 @@ class PredefinedKitsController extends Controller
* @param int $kit_id
* @return \Illuminate\Http\Response
*/
public function destroyModel($kit_id, $model_id)
public function detachModel($kit_id, $model_id)
{
$this->authorize('update', PredefinedKit::class);
$kit = PredefinedKit::findOrFail($id);
$kit->models()->detach($model_id);
return response()->json(Helper::formatStandardApiResponse('success', null, 'Delete was successfull')); // TODO: trans
return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Delete was successfull')); // TODO: trans
}
}

View file

@ -156,8 +156,8 @@ class PredefinedKitsController extends Controller
}
// Delete childs
$kit->models()->delete();
$kit->licenses()->delete();
$kit->models()->detach();
$kit->licenses()->detach();
// Delete the kit
$kit->delete();
@ -225,7 +225,11 @@ class PredefinedKitsController extends Controller
* @param int $modelId
* @return View
*/
public function updateModel(Request $request, $kit_id) {
public function updateModel(Request $request, $kit_id, $model_id) {
// $r = $request->all();
// $r['__model_id'] = $model_id;
// $r['__kit_id'] = $kit_id;
// dd($r);
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
@ -239,7 +243,18 @@ class PredefinedKitsController extends Controller
// $quantity = 1;
// }
$validator = \Validator::make($request->all(), $kit->modelRules);
// $validator = \Validator::make($request->all(), $kit->modelRules);
$validator = \Validator::make($request->all(), $kit->makeModelRules($model_id));
// $pivot_id = $request->input('pivot_id');
// $kit->models()->wherePivot('id', '!=', $pivot_id)
// ->wherePivot('id', '!=', $pivot_id)
// ->first()->pivot;
// $validator->after(function ($validator) use($kit) {
// // if ($this->somethingElseIsInvalid()) {
// // $validator->errors()->add('field', 'Something is wrong with this field!');
// // }
// });
if ($validator->fails()) {
return redirect()->back()->withInput()->withErrors($validator);
}
@ -279,7 +294,7 @@ class PredefinedKitsController extends Controller
$kit->models()->detach($model_id);
// Redirect to the kit management page
return redirect()->route('kits.index')->with('success', 'Kit was successfully deleted'); // TODO: trans
return redirect()->route('kits.edit', $kit_id)->with('success', 'Model was successfully detached'); // TODO: trans
}

View file

@ -8,6 +8,7 @@ use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
use Illuminate\Validation\Rule;
/**
* Model for Categories. Categories are a higher-level group
@ -34,10 +35,28 @@ class PredefinedKit extends SnipeModel
public $modelRules = [
'model_id' => 'required|exists:models,id',
// 'model_id' => [
// 'required',
// 'exists:models,id',
// Rule::unique('kits_models')->where('model_id', $model_id)->whereNot('kit_id', $this->id)
// ],
'quantity' => 'required|integer|min:1',
'pivot_id' => 'integer|exists:kits_models,id'
];
public function makeModelRules($model_id) {
return [
// 'model_id' => 'required|exists:models,id',
'model_id' => [
'required',
'exists:models,id',
Rule::unique('kits_models')->whereNot('model_id', $model_id)->where('kit_id', $this->id)
],
'quantity' => 'required|integer|min:1',
'pivot_id' => 'integer|exists:kits_models,id'
];
}
public $licenseRules = [
'license_id' => 'required|exists:licenses,id',
'quantity' => 'required|integer|min:1',
@ -83,6 +102,11 @@ class PredefinedKit extends SnipeModel
return $this->belongsToMany('\App\Models\AssetModel', 'kits_models', 'kit_id', 'model_id')->withPivot('id', 'quantity');
}
public function assets()
{
return $this->hasManyThrough('\App\Models\Asset', '\App\Models\AssetModel', 'country_id', 'user_id');
}
/**
* Establishes the kits -> licenses relationship
*
@ -96,6 +120,9 @@ class PredefinedKit extends SnipeModel
}
public function applyToUser(User $user) {
$models = $this->models();
}
/**
* -----------------------------------------------

View file

@ -64,6 +64,7 @@ class RouteServiceProvider extends ServiceProvider
require base_path('routes/web/components.php');
require base_path('routes/web/users.php');
require base_path('routes/web/kits.php');
require base_path('routes/web/dbtest.php');
require base_path('routes/web.php');
});
}

View file

@ -794,7 +794,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
Route::delete('licenses/{license_id}',
[
'as' => 'api.kits.licenses.destroy',
'uses' => 'PredefinedKitsController@destroyLicense',
'uses' => 'PredefinedKitsController@detachLicense',
]
);
@ -823,7 +823,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
Route::delete('models/{model_id}',
[
'as' => 'api.kits.models.destroy',
'uses' => 'PredefinedKitsController@destroyModel',
'uses' => 'PredefinedKitsController@detachModel',
]
);

View file

@ -57,6 +57,7 @@ Route::group([ 'prefix' => 'kits/{kit_id}', 'middleware' => ['auth'] ], function
[
'as' => 'kits.models.update',
'uses' => 'Kits\PredefinedKitsController@updateModel',
'parameters' => [2 => 'kit_id', 1 => 'model_id']
]
);
@ -64,6 +65,7 @@ Route::group([ 'prefix' => 'kits/{kit_id}', 'middleware' => ['auth'] ], function
[
'as' => 'kits.models.edit',
'uses' => 'Kits\PredefinedKitsController@editModel',
]
);