Skip to content

Commit

Permalink
users can save and read opinions to proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
scammo committed Jan 4, 2024
1 parent dd5611d commit 1cfec06
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 6 deletions.
35 changes: 34 additions & 1 deletion backend/app/Http/Controllers/ProposalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Request;
use App\Models\Track;
use App\Models\Proposal;
use App\Models\Opinion;

class ProposalController extends Controller
{
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Opinions extends Model
class Opinion extends Model
{
use HasFactory;

public function proposal()
{
return $this->belongsTo(Proposal::class);
}

public function user()
{
return $this->belongsTo(User::class);
}
}
2 changes: 1 addition & 1 deletion backend/app/Models/Proposal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions backend/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public function tracks(): BelongsToMany
{
return $this->belongsToMany(Track::class, 'user_tracks');
}

public function opinions(): BelongsToMany
{
return $this->belongsToMany(Opinion::class);
}
}
1 change: 1 addition & 0 deletions backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
23 changes: 21 additions & 2 deletions frontend/src/components/OpinionForm.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup>
import { ref, onMounted } from "vue";
import { client } from "../lib/api";
import { useToast } from "primevue/usetoast";
import { useRouter } from "vue-router";
import { useForm } from "vee-validate";
Expand Down Expand Up @@ -39,8 +40,26 @@ const onSubmit = handleSubmit(async (values, actions) => {
profile_id: currentUserId,
proposal_id: proposalId,
};
// TODO: Build API Call
router.go();
const response = await client.post(`proposal/${proposalId}/opinion`, {
vote: opinionForm.value.vote,
comment: opinionForm.value.comment,
});
if (response.status === 200 || response.status === 201) {
toast.add({
severity: "success",
summary: "Erfolgreich",
detail: "Deine Meinung wurde gespeichert",
life: 5000,
});
router.go();
} else {
toast.add({
severity: "warn",
summary: "Error",
detail: "Deine Meinung konnte nicht gespeichert werden",
life: 5000,
});
}
} catch (error) {
console.error(error);
toast.add({
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/ProposalStatusForm.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup>
import { ref, onMounted } from "vue";
import { client } from "../lib/api";
import { useToast } from "primevue/usetoast";
import { useRouter } from "vue-router";
import { useForm } from "vee-validate";
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/orga/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const fieldName = (name) => {
<article v-for="opinion in proposal.opinions" v-if="proposal.opinions?.length > 0">
<Card class="mt-1">
<template #title>{{ opinion.special_vote || opinion.vote }}
{{ opinion.profiles.username }}
{{ opinion.user?.username }}
</template>
<template #content>{{ opinion.comment }}</template>
</Card>
Expand Down

0 comments on commit 1cfec06

Please sign in to comment.