|
|
@@ -7,6 +7,7 @@
|
|
|
use App\Http\Resources\UnitPartnerResource;
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
class UnitPartnerController extends Controller
|
|
|
{
|
|
|
@@ -48,4 +49,79 @@ public function destroy(int $id): JsonResponse
|
|
|
$this->service->delete($id);
|
|
|
return $this->successResponse(message: __('messages.deleted'), code: 204);
|
|
|
}
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ public function indexMe(): JsonResponse
|
|
|
+ {
|
|
|
+ $unitId = $this->currentUnitId();
|
|
|
+
|
|
|
+ if (!$unitId) {
|
|
|
+ return $this->errorResponse(message: 'Unidade não encontrada', code: 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $items = $this->service->getByUnitId($unitId);
|
|
|
+
|
|
|
+ return $this->successResponse(payload: UnitPartnerResource::collection($items));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function storeMe(UnitPartnerRequest $request): JsonResponse
|
|
|
+ {
|
|
|
+ $unitId = $this->currentUnitId();
|
|
|
+
|
|
|
+ if (!$unitId) {
|
|
|
+ return $this->errorResponse(message: 'Unidade não encontrada', code: 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = $request->validated();
|
|
|
+
|
|
|
+ $data['unit_id'] = $unitId;
|
|
|
+
|
|
|
+ $item = $this->service->create($data);
|
|
|
+
|
|
|
+ return $this->successResponse(payload: new UnitPartnerResource($item), message: __('messages.created'), code: 201);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function updateMe(UnitPartnerRequest $request, int $id): JsonResponse
|
|
|
+ {
|
|
|
+ $unitId = $this->currentUnitId();
|
|
|
+
|
|
|
+ $item = $this->service->findById($id);
|
|
|
+
|
|
|
+ if (!$unitId || !$item || (int) $item->unit_id !== $unitId) {
|
|
|
+ return $this->errorResponse(message: 'Sócio não encontrado', code: 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = $request->validated();
|
|
|
+
|
|
|
+ $data['unit_id'] = $unitId;
|
|
|
+
|
|
|
+ $item = $this->service->update($id, $data);
|
|
|
+
|
|
|
+ return $this->successResponse(payload: new UnitPartnerResource($item), message: __('messages.updated'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function destroyMe(int $id): JsonResponse
|
|
|
+ {
|
|
|
+ $unitId = $this->currentUnitId();
|
|
|
+
|
|
|
+ $item = $this->service->findById($id);
|
|
|
+
|
|
|
+ if (!$unitId || !$item || (int) $item->unit_id !== $unitId) {
|
|
|
+ return $this->errorResponse(message: 'Sócio não encontrado', code: 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->service->delete($id);
|
|
|
+
|
|
|
+ return $this->successResponse(message: __('messages.deleted'), code: 204);
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ private function currentUnitId(): ?int
|
|
|
+ {
|
|
|
+ $user = Auth::user()?->load('units');
|
|
|
+
|
|
|
+ return $user?->units->first()?->id;
|
|
|
+ }
|
|
|
}
|