Skip to content

Commit

Permalink
Change policy logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes committed Mar 23, 2024
1 parent a2e6be5 commit b595af8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
23 changes: 7 additions & 16 deletions app/Http/Controllers/CityOpinionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@

use App\Http\Requests\CityOpinionRequest;
use App\Models\CityOpinion;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class CityOpinionController extends Controller
{
public function store(CityOpinionRequest $request): void
{
$opinion = $request->validated();
$opinion["user_id"] = Auth::id();

CityOpinion::query()->create($opinion);
$request->user()
->cityOpinions()
->create($request->validated());
}

public function update(CityOpinionRequest $request, CityOpinion $cityOpinion): void
{
$opinion = $request->validated();

if (Gate::allows("update", $cityOpinion)) {
$cityOpinion->update($opinion);
} else {
abort(403);
}
$request->user()
->cityOpinions()
->update($request->validated());
}

public function destroy(CityOpinion $cityOpinion): void
{
if (Gate::allows("delete", $cityOpinion)) {
$cityOpinion->delete();
}
$cityOpinion->delete();
}
}
6 changes: 4 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
Route::get("/favorite-cities", [FavoritesController::class, "index"]);

Route::post("/opinions", [CityOpinionController::class, "store"]);
Route::patch("/opinions/{cityOpinion}", [CityOpinionController::class, "update"]);
Route::delete("/opinions/{cityOpinion}", [CityOpinionController::class, "destroy"]);
Route::middleware("can:update,cityOpinion")->group(function (): void {
Route::patch("/opinions/{cityOpinion}", [CityOpinionController::class, "update"]);
Route::delete("/opinions/{cityOpinion}", [CityOpinionController::class, "destroy"]);
});

Route::middleware(["role:admin"])->group(function (): void {
Route::get("/admin/importers", [ImportInfoController::class, "index"]);
Expand Down

0 comments on commit b595af8

Please sign in to comment.