diff --git a/backend/app/Http/Controllers/ProposalController.php b/backend/app/Http/Controllers/ProposalController.php index f5e1427..080dfd4 100644 --- a/backend/app/Http/Controllers/ProposalController.php +++ b/backend/app/Http/Controllers/ProposalController.php @@ -6,6 +6,7 @@ use Illuminate\Http\Request; use App\Models\Track; use App\Models\Proposal; +use App\Models\Opinion; class ProposalController extends Controller { @@ -29,11 +30,43 @@ public function show(Proposal $proposal) abort(403); } $user = Auth::user(); - $proposal->load('track','track.users', 'opinions'); + $proposal->load('track', 'opinions','opinions.user', ); if(!$proposal->track->users->contains($user->id)){ abort(403); } return $proposal; } + + public function upsertOpinion(Proposal $proposal, Request $request) + { + if (!Auth::check()){ + abort(403); + } + + $user = Auth::user(); + $track = $proposal->track->load('users'); + if(!$track->users->contains($user->id)){ + abort(403); + } + $request->validate([ + 'vote' => 'required|numeric', + 'comment' => 'required|string', + ]); + $opinion = Opinion::where('proposal_id', $proposal->id)->where('user_id', $user->id)->first(); + if($opinion){ + $opinion->vote = $request->vote; + $opinion->comment = $request->comment; + $opinion->save(); + return $opinion; + }else{ + $opinion = new Opinion(); + $opinion->proposal_id = $proposal->id; + $opinion->user_id = $user->id; + $opinion->vote = $request->vote; + $opinion->comment = $request->comment; + $opinion->save(); + } + return $opinion; + } } diff --git a/backend/app/Models/Opinions.php b/backend/app/Models/Opinion.php similarity index 68% rename from backend/app/Models/Opinions.php rename to backend/app/Models/Opinion.php index a6278c7..60840ca 100644 --- a/backend/app/Models/Opinions.php +++ b/backend/app/Models/Opinion.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Opinions extends Model +class Opinion extends Model { use HasFactory; @@ -13,4 +13,9 @@ public function proposal() { return $this->belongsTo(Proposal::class); } + + public function user() + { + return $this->belongsTo(User::class); + } } diff --git a/backend/app/Models/Proposal.php b/backend/app/Models/Proposal.php index dae6658..5fefa41 100644 --- a/backend/app/Models/Proposal.php +++ b/backend/app/Models/Proposal.php @@ -14,7 +14,7 @@ class Proposal extends Model public function opinions(): HasMany { - return $this->hasMany(Opinions::class); + return $this->hasMany(Opinion::class); } public function track(): BelongsTo { diff --git a/backend/app/Models/User.php b/backend/app/Models/User.php index d03ba19..c98ebbe 100644 --- a/backend/app/Models/User.php +++ b/backend/app/Models/User.php @@ -69,4 +69,9 @@ public function tracks(): BelongsToMany { return $this->belongsToMany(Track::class, 'user_tracks'); } + + public function opinions(): BelongsToMany + { + return $this->belongsToMany(Opinion::class); + } } diff --git a/backend/routes/api.php b/backend/routes/api.php index fc5623c..9120768 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -30,6 +30,7 @@ Route::post('proposals', [\App\Http\Controllers\ProposalController::class, 'create']); Route::get('proposal/{proposal}', [\App\Http\Controllers\ProposalController::class, 'show']); +Route::post('proposal/{proposal}/opinion', [\App\Http\Controllers\ProposalController::class, 'upsertOpinion']); Route::get('user', [\App\Http\Controllers\UserController::class, 'me']); diff --git a/frontend/src/components/OpinionForm.vue b/frontend/src/components/OpinionForm.vue index 635f2e3..a908e3e 100644 --- a/frontend/src/components/OpinionForm.vue +++ b/frontend/src/components/OpinionForm.vue @@ -1,5 +1,6 @@