Skip to content

Commit

Permalink
Enable the capability to request a group using its slugified name
Browse files Browse the repository at this point in the history
Resolves #97
  • Loading branch information
tobifra committed Feb 19, 2024
1 parent 86af518 commit 7ca6680
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
17 changes: 13 additions & 4 deletions api/app/Http/Controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ public function store(StoreGroupRequest $request)
return response()->json($group, 201);
}

public function show($id)
public function show($idOrRoute)
{
$group = Group::with(['section', 'file', 'predecessors', 'successors', 'parent', 'headerImages', 'files'])
->find($id);
$query = Group::with(['section', 'file', 'predecessors', 'successors', 'parent', 'headerImages', 'files']);
if (is_numeric($idOrRoute)) {
$group = $query->find($idOrRoute);
} else {
$groups = $query->get();
$filteredGroups = $groups->filter(function ($group) use ($idOrRoute) {
return $group->route == $idOrRoute;
});

$group = $filteredGroups->first();
}

if (!$group) {
return response()->json(['message' => 'Group not found'], 404);
Expand All @@ -56,7 +65,7 @@ public function update(UpdateGroupRequest $request, $id)
return response()->json(['message' => 'Group not found'], 404);
}
$user = Auth::user();
if($user->hasRole('unitleader') && !$user->groups->contains($group)){
if ($user->hasRole('unitleader') && !$user->groups->contains($group)) {
return response()->json(['message' => 'You are not allowed to update this group'], 403);
}

Expand Down
11 changes: 10 additions & 1 deletion api/app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace App\Models;

use App\Traits\TransformTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
use HasFactory;
use HasFactory, TransformTrait;

protected $fillable = [
'name',
Expand All @@ -23,6 +24,14 @@ class Group extends Model
'enable_group_page',
];

protected $appends = ['route'];

public function getRouteAttribute()
{
return $this->toCamelCase($this->name);
}


public function headerImages()
{
return $this->belongsToMany(File::class, 'group_headers');
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/main/FooterComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<template v-for="group in transformedGroups" :key="group.id">
<li v-if="!group.parentId" class="pl-6">
<router-link
:to="group.enableGroupPage ? `/group/${group.id}` : '#'"
:to="group.enableGroupPage ? `/group/${group.route}` : '#'"
>{{ group.name }}</router-link
>
<ul
Expand All @@ -22,7 +22,7 @@
class="pl-6"
>
<router-link
:to="child.enableGroupPage ? `/group/${child.id}` : '#'"
:to="child.enableGroupPage ? `/group/${child.route}` : '#'"
>{{ child.name }}</router-link
>
</li>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/main/GroupDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
>
<router-link
@click="showDropdown = false"
:to="`/group/${group.id}`"
:to="`/group/${group.route}`"
>{{ group.name }}</router-link
>
</li>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/main/GroupsItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
</p>
</div>
<BasicButton>
<router-link :to="`/group/${currentGroup.id}`" class="w-full"
<router-link :to="`/group/${currentGroup.route}`" class="w-full"
>Zur Gruppenseite</router-link
>
</BasicButton>
Expand Down

0 comments on commit 7ca6680

Please sign in to comment.