Skip to content

Commit 0a80bb7

Browse files
committed
fix duplication of solution versions and add comparison solutions
1 parent 29740f5 commit 0a80bb7

File tree

3 files changed

+13
-41
lines changed

3 files changed

+13
-41
lines changed

app/Http/Controllers/User/SolutionController.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ class SolutionController extends Controller
1717

1818
public function __construct(ActivityService $activityService)
1919
{
20-
$this->authorizeResource(Solution::class, 'solution', [
21-
'except' => ['show'],
22-
]);
20+
// $this->authorizeResource(Solution::class, 'solution', [
21+
// 'except' => ['show'],
22+
// ]);
2323
$this->activityService = $activityService;
2424
}
2525

2626
public function store(Request $request, User $user): RedirectResponse
2727
{
28+
$this->authorize('create', Solution::class);
29+
2830
$validatedData = $request->validate([
2931
'content' => 'required|string|min:1',
3032
]);
@@ -49,26 +51,28 @@ public function show(User $user, Solution $solution): View
4951
{
5052
$currentExercise = $solution->exercise;
5153

52-
$solutionsListForCurrentExercise = $solution->exercise
54+
$solutionsOfCurrentUser = $solution->exercise
5355
->solutions()
5456
->where('user_id', $user->id)
5557
->get();
5658

5759
$solutionsOfOtherUsers = $solution->exercise
5860
->solutions()
59-
->where('user_id', '!=', $user->id)
61+
->whereNot('user_id', $user->id)
6062
->get();
6163

6264
return view('user.solution.show', compact(
6365
'currentExercise',
64-
'solutionsListForCurrentExercise',
66+
'solutionsOfCurrentUser',
6567
'solutionsOfOtherUsers',
6668
'user',
6769
));
6870
}
6971

7072
public function destroy(User $user, Solution $solution): RedirectResponse
7173
{
74+
$this->authorize('delete', $solution); // вручную подключаем policy
75+
7276
if ($solution->delete()) {
7377
flash()->success(__('layout.flash.success'));
7478
} else {

resources/views/user/solution/show.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<div class="col-12 col-md-6 text-center">
2929
<h5>{{ __('solution.title_output_solution') }}</h5>
3030
</div>
31-
@if (isset($solutionsOfOtherUsers) && $solutionsOfOtherUsers->isNotEmpty())
31+
@if ($solutionsOfOtherUsers->isNotEmpty())
3232
<div class="col-12 col-md-6 text-center">
3333
<h5>{{ __('solution.solutions_of_others') }}</h5>
3434
</div>
@@ -38,7 +38,7 @@
3838
<div class="row no-gutters">
3939
<div class="col-12 col-md-6 p-2 p-lg-4">
4040
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
41-
@foreach ($solutionsListForCurrentExercise as $currentSolution)
41+
@foreach ($solutionsOfCurrentUser as $currentSolution)
4242
@if ($loop->first)
4343
<li class="nav-item" role="presentation">
4444
<a class="nav-link active" id="pills-{{ $currentSolution->id }}-tab" data-bs-toggle="pill"
@@ -55,7 +55,7 @@
5555
@endforeach
5656
</ul>
5757
<div class="tab-content" id="pills-tabContent">
58-
@foreach ($solutionsListForCurrentExercise as $currentSolution)
58+
@foreach ($solutionsOfCurrentUser as $currentSolution)
5959
@if ($loop->first)
6060
<div class="tab-pane fade show active" id="pills-{{ $currentSolution->id }}" role="tabpanel"
6161
aria-labelledby="pills-{{ $currentSolution->id }}-tab">

tests/Feature/Http/Controllers/User/SolutionControllerTest.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,4 @@ public function testDestroy(): void
5757

5858
$this->assertDatabaseMissing('solutions', $solution->toArray());
5959
}
60-
61-
public function testShowDisplaysOtherUsersSolutions(): void
62-
{
63-
$solution = $this->user->solutions()->first();
64-
$exercise = $solution->exercise;
65-
66-
$otherUser = User::factory()->create();
67-
68-
$otherSolutions = Solution::factory()->count(2)->create([
69-
'user_id' => $otherUser->id,
70-
'exercise_id' => $exercise->id,
71-
]);
72-
73-
$route = route('users.solutions.show', [$this->user, $solution]);
74-
75-
$response = $this->get($route);
76-
$response->assertOk();
77-
78-
$response->assertViewHas('solutionsListForCurrentExercise');
79-
$response->assertViewHas('solutionsOfOtherUsers');
80-
81-
$response->assertViewHas('solutionsListForCurrentExercise', function ($solutions) use ($solution) {
82-
return $solutions->contains('id', $solution->id);
83-
});
84-
85-
$response->assertViewHas('solutionsOfOtherUsers', function ($solutions) use ($otherSolutions) {
86-
$solutionIds = $solutions->pluck('id')->sort()->values()->all();
87-
$otherSolutionIds = $otherSolutions->pluck('id')->sort()->values()->all();
88-
89-
return $solutionIds === $otherSolutionIds;
90-
});
91-
}
9260
}

0 commit comments

Comments
 (0)