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

Feature/hey 12 #13

Merged
merged 6 commits into from
Oct 21, 2023
Merged
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
18 changes: 18 additions & 0 deletions app/Http/Controllers/Question/PublishController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers\Question;

use App\Http\Controllers\Controller;
use App\Models\Question;
use Illuminate\Http\RedirectResponse;

class PublishController extends Controller
{
public function __invoke(Question $question): RedirectResponse
{
$this->authorize('publish', $question);
$question->update(['draft' => false]);

return back();
}
}
30 changes: 26 additions & 4 deletions app/Http/Controllers/QuestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

use App\Models\Question;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Illuminate\Http\{RedirectResponse, Request};

class QuestionController extends Controller
{
public function store(Request $request): \Illuminate\Http\RedirectResponse
public function index(): View
{
$questions = user()->questions()->get();

return view('question.index', compact('questions'));
}

public function store(Request $request): RedirectResponse
{
$attributes = $request->validate([
'question' => [
Expand All @@ -23,10 +31,24 @@ function (string $attribute, mixed $value, Closure $fail) {
],
]);

Question::query()->create($attributes);
user()->questions()->create(
[
'question' => $attributes['question'],
'draft' => true,
]
);

//return redirect()->route('dashboard');
return to_route('dashboard');
return back();

}

public function destroy(Question $question): RedirectResponse
{
$this->authorize('destroy', $question);

$question->delete();

return back();
}
}
11 changes: 10 additions & 1 deletion app/Models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\{BelongsTo, HasMany};

class Question extends Model
{
use HasFactory;

protected $casts = [
'draft' => 'boolean',
];

public function votes(): HasMany
{
return $this->hasMany(Vote::class);
}

public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}

}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function votes(): HasMany
return $this->hasMany(Vote::class);
}

public function questions(): HasMany
{
return $this->hasMany(Question::class, 'created_by');
}

public function like(Question $question): void
{
$this->votes()->updateOrCreate(
Expand Down
19 changes: 19 additions & 0 deletions app/Policies/QuestionPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Policies;

use App\Models\{Question, User};

class QuestionPolicy
{
public function publish(User $user, Question $question): bool
{
return $question->createdBy()->is($user);
}

public function destroy(User $user, Question $question): bool
{
return $question->createdBy()->is($user);
}

}
Loading
Loading