diff --git a/app/Contracts/Repositories/Auth/StoreRepository.php b/app/Contracts/Repositories/Auth/StoreRepository.php index fcf8c77..e74f05a 100644 --- a/app/Contracts/Repositories/Auth/StoreRepository.php +++ b/app/Contracts/Repositories/Auth/StoreRepository.php @@ -56,4 +56,8 @@ public function update(mixed $id, array $data): mixed return $this->show($id)->update($data); } + public function getAllStoreId() + { + return $this->model->pluck('id')->toArray(); + } } \ No newline at end of file diff --git a/app/Contracts/Repositories/SettingRepository.php b/app/Contracts/Repositories/SettingRepository.php index ebbad50..9d6e5a6 100644 --- a/app/Contracts/Repositories/SettingRepository.php +++ b/app/Contracts/Repositories/SettingRepository.php @@ -37,10 +37,17 @@ public function update(mixed $id, array $data): mixed public function delete(mixed $id): mixed { - $model = $this->model->select('id')->findOrFail($id); - $model->delete(); + $model = $this->model->select('code')->findOrFail($id); - return $model->fresh(); + // fitur hapus semua data setting dengan code yang sama + $jumlahHapus = $this->model->where('code', $model->code)->count(); + $this->model->where('code', $model->code) + ->delete(); + + // $model->delete(); + + $model->fresh(); + return "Berhasil menghapus $jumlahHapus data setting dengan code {$model->code}"; } public function customPaginate(int $pagination = 8, int $page = 1, ?array $data): mixed diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index 5aa2b4a..8892322 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -2,20 +2,24 @@ namespace App\Http\Controllers; +use App\Models\Setting; use Illuminate\Http\Request; use App\Helpers\BaseResponse; use Illuminate\Support\Facades\DB; use App\Http\Requests\SettingRequest; use App\Contracts\Repositories\SettingRepository; -use App\Models\Setting; +use App\Contracts\Repositories\Auth\StoreRepository; use Illuminate\Database\Eloquent\ModelNotFoundException; class SettingController extends Controller { - private $settingRepository; - public function __construct(SettingRepository $settingRepository) + private SettingRepository $settingRepository; + private StoreRepository $storeRepository; + + public function __construct(SettingRepository $settingRepository, StoreRepository $storeRepository) { $this->settingRepository = $settingRepository; + $this->storeRepository = $storeRepository; } /** * Display a listing of the resource. @@ -51,17 +55,27 @@ public function store(SettingRequest $request) { $data = $request->validated(); - if (auth()?->user()?->store?->id || auth()?->user()?->store_id) $data['store_id'] = auth()?->user()?->store?->id ?? auth()?->user()?->store_id; + // if (auth()?->user()?->store?->id || auth()?->user()?->store_id) $data['store_id'] = auth()?->user()?->store?->id ?? auth()?->user()?->store_id; + $storeId = $this->storeRepository->getAllStoreId(); + if (count($storeId) == 0) { + return BaseResponse::Error("Tidak ada store yang tersedia", null); + } + DB::beginTransaction(); try { + $hasil = []; - $settingData = $this->settingRepository->store($data); + foreach ($storeId as $id) { + $data['store_id'] = $id; // Set store_id for each store + $setting = $this->settingRepository->store($data); + $hasil[] = $setting; + } DB::commit(); - return BaseResponse::Ok('Berhasil menambahkan setting', $settingData); + return BaseResponse::Ok('Berhasil menambahkan setting', $hasil); } catch (\Throwable $th) { DB::rollBack(); return BaseResponse::Error($th->getMessage(), null); @@ -122,10 +136,11 @@ public function destroy(string $id) try { - $setting->delete(); + $message = $this->settingRepository->delete($setting->id); DB::commit(); - return BaseResponse::Ok('Berhasil menghapus setting', null); + // return BaseResponse::Ok('Berhasil menghapus setting', null); + return BaseResponse::Ok($message, null); } catch (\Throwable $th) { DB::rollBack(); return BaseResponse::Error($th->getMessage(), null);