|
|
@@ -25,9 +25,11 @@ public function getAllByUnit(int $unitId): Collection
|
|
|
->get();
|
|
|
}
|
|
|
|
|
|
- public function findById(int $id): ?ClassPackageUnit
|
|
|
+ public function findByIdForUnit(int $id, int $unitId): ClassPackageUnit
|
|
|
{
|
|
|
- return ClassPackageUnit::with('products.product')->find($id);
|
|
|
+ return ClassPackageUnit::with('products.product')
|
|
|
+ ->where('unit_id', $unitId)
|
|
|
+ ->findOrFail($id);
|
|
|
}
|
|
|
|
|
|
public function create(array $data): ClassPackageUnit
|
|
|
@@ -44,10 +46,9 @@ public function create(array $data): ClassPackageUnit
|
|
|
return $packageUnit->load('products.product');
|
|
|
}
|
|
|
|
|
|
- public function update(int $id, array $data): ?ClassPackageUnit
|
|
|
+ public function update(int $id, int $unitId, array $data): ClassPackageUnit
|
|
|
{
|
|
|
- $packageUnit = $this->findById($id);
|
|
|
- if (!$packageUnit) return null;
|
|
|
+ $packageUnit = $this->findByIdForUnit($id, $unitId);
|
|
|
|
|
|
$materials = $data['materials'] ?? null;
|
|
|
unset($data['materials']);
|
|
|
@@ -65,20 +66,18 @@ public function update(int $id, array $data): ?ClassPackageUnit
|
|
|
return $packageUnit->fresh('products.product');
|
|
|
}
|
|
|
|
|
|
- public function toggleVisibility(int $id): ?ClassPackageUnit
|
|
|
+ public function toggleVisibility(int $id, int $unitId): ClassPackageUnit
|
|
|
{
|
|
|
- $packageUnit = ClassPackageUnit::find($id);
|
|
|
- if (!$packageUnit) return null;
|
|
|
+ $packageUnit = $this->findByIdForUnit($id, $unitId);
|
|
|
|
|
|
$packageUnit->update(['visible' => !$packageUnit->visible]);
|
|
|
|
|
|
return $packageUnit->fresh();
|
|
|
}
|
|
|
|
|
|
- public function delete(int $id): bool
|
|
|
+ public function delete(int $id, int $unitId): bool
|
|
|
{
|
|
|
- $packageUnit = ClassPackageUnit::find($id);
|
|
|
- if (!$packageUnit) return false;
|
|
|
+ $packageUnit = $this->findByIdForUnit($id, $unitId);
|
|
|
|
|
|
return $packageUnit->delete();
|
|
|
}
|