Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#169 - ability to edit and delete opinions for administrator #201

Merged
Merged
Show file tree
Hide file tree
Changes from 15 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
15 changes: 6 additions & 9 deletions app/Http/Controllers/CityOpinionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@

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

class CityOpinionController extends Controller
{
public function store(CityOpinionRequest $request): void
{
$opinion = $request->only(["rating", "content", "city_id"]);
$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->only(["rating", "content", "city_id"]);
$opinion["user_id"] = Auth::id();

$cityOpinion->update($opinion);
$request->user()
->cityOpinions()
->update($request->validated());
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved
}
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved

public function destroy(CityOpinion $cityOpinion): void
Expand Down
1 change: 1 addition & 0 deletions app/Importers/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function deleteMissingProviders(string $providerName, array $existingC
->whereNotIn("city_id", $existingCityProviders)
->whereNot("created_by", "admin")
->get();

$cityProvidersToDelete->each(fn($cityProvider) => $cityProvider->delete());
}

Expand Down
21 changes: 21 additions & 0 deletions app/Policies/CityOpinionPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\CityOpinion;
use App\Models\User;

class CityOpinionPolicy
{
public function update(User $user, CityOpinion $cityOpinion): bool
{
return $cityOpinion->user_id === $user->id;
}

public function delete(User $user, CityOpinion $cityOpinion): bool
{
return $cityOpinion->user_id === $user->id || $user->hasRole("admin");
}
}
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion resources/js/Pages/City/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import InfoPopup from '@/Shared/Components/InfoPopup.vue'
import Opinion from '@/Shared/Components/Opinion.vue'

const toast = useToast()

const page = usePage()
const isAuth = computed(() => page.props.auth.isAuth)

Expand Down
5 changes: 3 additions & 2 deletions resources/js/Shared/Components/Opinion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DeleteModal from './DeleteModal.vue'
import { useToast } from 'vue-toastification'
import ErrorMessage from './ErrorMessage.vue'

const isAdmin = computed(() => page.props.auth.isAdmin)
const toast = useToast()
const page = usePage()
const user = computed(() => page.props.auth.user)
Expand Down Expand Up @@ -103,8 +104,8 @@ const emptyRatingError = ref('')
{{ opinion.content }}
</div>

<div v-if="user.id === props.opinion.user.id" class="mt-1 flex justify-end">
<button class="flex px-1 hover:drop-shadow" @click="toggleUpdateOpinionDialog">
<div v-if="user.id === props.opinion.user.id || isAdmin" class="mt-1 flex justify-end">
<button v-if="user.id === props.opinion.user.id" class="flex px-1 hover:drop-shadow" @click="toggleUpdateOpinionDialog">
<PencilIcon class="h-5 w-5 text-blumilk-500 hover:drop-shadow sm:h-4 sm:w-4" />
</button>
<button class="flex px-1 hover:drop-shadow" @click="toggleDeleteOpinionDialog">
Expand Down
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
Loading