Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Contracts/Repositories/Auth/StoreRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
13 changes: 10 additions & 3 deletions app/Contracts/Repositories/SettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions app/Http/Controllers/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down