Compare commits

..

2 Commits

Author SHA1 Message Date
ab3611cbce
Add tag permalink page 2024-05-29 22:37:37 +01:00
9a5246374e
Add tag editing 2024-05-29 22:25:50 +01:00
13 changed files with 204 additions and 9 deletions

View File

@ -11,9 +11,9 @@ A little project because I haven't made anything new in a while, I would like to
* [x] Bookmark index
* [x] Bookmark pagination
* [x] Bookmark editing (title, description, url)
* [ ] Bookmark tag editing
* [x] Bookmark tag editing
* [ ] Tag cloud
* [ ] Tag permalink
* [x] Tag permalink
* [ ] Multi-user support
## Developing

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Bookmark;
use App\Models\Tag;
use Illuminate\Http\Request;
class BookmarkController extends Controller
@ -70,6 +71,17 @@ class BookmarkController extends Controller
$bookmark->href = $request->post('href');
$bookmark->save();
$tags_input = trim($request->post('tags', ''));
$tags_input = preg_split('/\s+/', $tags_input);
$tags = [];
foreach ($tags_input as $tag_input) {
$tag = Tag::firstOrCreate(['name' => $tag_input]);
$tags[$tag->id] = true;
}
$bookmark->tags()->sync(array_keys($tags));
return redirect()->action(
[self::class, "show"], [
"bookmark" => $bookmark,

View File

@ -0,0 +1,65 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tag;
use Illuminate\Http\Request;
class TagController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Tag $tag)
{
return view('tag.show', [ 'tag' => $tag, ]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Tag $tag)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Tag $tag)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Tag $tag)
{
//
}
}

View File

@ -4,7 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class Bookmark extends Model
{
@ -12,8 +13,8 @@ class Bookmark extends Model
protected $table = 'bookmarks';
public function tags(): HasMany
public function tags(): BelongsToMany
{
return $this->hasMany(BookmarkTag::class, 'bookmark_id');
return $this->belongsToMany(Tag::class);
}
}

View File

@ -3,10 +3,10 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\Pivot;
class BookmarkTag extends Model
class BookmarkTag extends Pivot
{
use HasFactory;

View File

@ -4,10 +4,16 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Tag extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function bookmarks(): BelongsToMany
{
return $this->belongsToMany(Bookmark::class);
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table(
'bookmarks_tags', function (Blueprint $table) {
$table->unique(['bookmark_id', 'tag_id']);
}
);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table(
'bookmarks_tags', function (Blueprint $table) {
$table->dropUnique('cache_bookmark_id_tag_id_unique');
}
);
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create(
'bookmark_tag', function (Blueprint $table) {
$table->foreignId('bookmark_id');
$table->foreignId('tag_id');
$table->timestamps();
$table->primary(['bookmark_id', 'tag_id']);
}
);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('bookmark_tag');
}
};

View File

@ -9,6 +9,15 @@
"date tags"
"actions actions";
form & {
grid-template-areas:
"title"
"description"
"href"
"tags"
"actions";
}
margin-bottom: 1em;
padding: 0;
border: 2px solid #540B0E;
@ -74,9 +83,14 @@
}
&-tags {
font-size: 0.8em;
grid-area: tags;
font-size: 0.8em;
text-align: right;
form & {
font-size: 0.9em;
text-align: left;
}
}
&-tag {
@ -88,5 +102,17 @@
border-radius: 7px;
background-color: #9E2A2B;
color: #FFF3B0;
a {
&,
&:active,
&:visited {
color: #FFF3B0;
}
&:hover {
color: #000;
}
}
}
}

View File

@ -25,6 +25,12 @@
</label>
<textarea name="description" class="form-textarea">{{ $bookmark->description }}</textarea>
</div>
<div class="bookmark-tags form-row form-row-stacked">
<label class="form-label" for="description">
Tags:
</label>
<input type="text" class="form-input" name="tags" value="@foreach ($bookmark->tags as $tag){{ $tag->name }} @endforeach">
</div>
<div class="form-row align-right bookmark-actions">
<a class="form-button" href="{{ action('BookmarkController@show', ['bookmark' => $bookmark]) }}">Cancel</a>
<input type="submit" class="form-button" value="Save">

View File

@ -18,7 +18,7 @@
<ul class="bookmark-tags">
@foreach ($bookmark->tags as $tag)
<li class="bookmark-tag">
{{ $tag->tag->name }}
<a href="{{ action('TagController@show', $tag) }}">{{ $tag->name }}</a>
</li>
@endforeach
</ul>

View File

@ -0,0 +1,9 @@
@extends('layouts.app')
@section('content')
<div>
@foreach ($tag->bookmarks as $bookmark)
<x-bookmark :bookmark="$bookmark" />
@endforeach
</div>
@endsection

View File

@ -1,6 +1,7 @@
<?php
use App\Http\Controllers\BookmarkController;
use App\Http\Controllers\TagController;
use Illuminate\Support\Facades\Route;
Route::get(
@ -11,3 +12,9 @@ Route::get(
Route::resource('bookmarks', BookmarkController::class);
Route::post('/bookmarks/{bookmark}', [BookmarkController::class, 'update']);
Route::resource('tags', TagController::class)->parameters(
[
'tags' => 'tag:name', // tag names in url, rather than ids
]
);