Add "add bookmark" functionality

This commit is contained in:
Annika Backstrom 2024-05-30 21:55:08 +01:00
parent b12889089b
commit fb18db918a
Signed by: annika
GPG Key ID: 3561F004DE1D9AFE
4 changed files with 39 additions and 21 deletions

View File

@ -25,7 +25,7 @@ class BookmarkController extends Controller
*/
public function create()
{
//
return view('bookmarks.edit', ['bookmark' => null]);
}
/**
@ -33,7 +33,19 @@ class BookmarkController extends Controller
*/
public function store(Request $request)
{
//
$bookmark = new Bookmark;
$bookmark->title = $request->post('title');
$bookmark->description = $request->post('description', '');
$bookmark->href = $request->post('href');
$bookmark->save();
$bookmark->syncTagsFromString($request->post('tags', ''));
return redirect()->action(
[self::class, "show"], [
"bookmark" => $bookmark,
]
);
}
/**
@ -55,7 +67,6 @@ class BookmarkController extends Controller
{
return view(
'bookmarks.edit', [
'edit' => true,
'bookmark' => $bookmark,
]
);
@ -71,16 +82,7 @@ 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));
$bookmark->syncTagsFromString($request->post('tags', ''));
return redirect()->action(
[self::class, "show"], [

View File

@ -15,6 +15,20 @@ class Bookmark extends Model
protected $with = ['tags'];
public function syncTagsFromString(string $tags_string): array
{
$tags_input = trim($tags_string);
$tags_input = preg_split('/\s+/', $tags_input);
$tags = [];
foreach ($tags_input as $tag_input) {
$tag = Tag::firstOrCreate(['name' => $tag_input]);
$tags[$tag->id] = true;
}
return $this->tags()->sync(array_keys($tags));
}
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class);

View File

@ -1,39 +1,40 @@
@extends('layouts.app')
@section('content')
<h2>{{ $bookmark ? "Edit Bookmark" : "Add Bookmark" }}</h2>
<p>
<a href="{{ action('BookmarkController@index') }}">&larr; Back</a>
</p>
<form method="post" class="form" action="{{ action('BookmarkController@update', ['bookmark' => $bookmark]) }}">
<form method="post" class="form" action="{{ $bookmark ? action('BookmarkController@update', ['bookmark' => $bookmark]) : action('BookmarkController@store') }}">
@csrf
<div class="bookmark">
<div class="bookmark-title form-row"g>
<div class="bookmark-title form-row">
<label class="form-label" for="title">
Title:
</label>
<input type="text" class="form-input" name="title" value="{{ $bookmark->title }}">
<input type="text" class="form-input" name="title" value="{{ $bookmark?->title }}" tabindex="1">
</div>
<div class="bookmark-href form-row form-row-stacked">
<label class="form-label" for="href">
URL:
</label>
<input type="text" class="form-input" name="href" value="{{ $bookmark->href }}">
<input type="text" class="form-input" name="href" value="{{ $bookmark?->href }}" tabindex="3">
</div>
<div class="bookmark-description form-row form-row-stacked">
<label class="form-label" for="description">
Description:
</label>
<textarea name="description" class="form-textarea">{{ $bookmark->description }}</textarea>
<textarea name="description" class="form-textarea" tabindex="2">{{ $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">
<input type="text" class="form-input" name="tags" tabindex="4" 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">
<a class="form-button" tabindex="5" href="{{ $bookmark ? action('BookmarkController@show', ['bookmark' => $bookmark]) : action('BookmarkController@index') }}">Cancel</a>
<input type="submit" tabindex="6" class="form-button" value="Save">
</div>
</div>
</form>

View File

@ -8,6 +8,7 @@
<div class="container">
<header>
<h1><a href="{{ url("/") }}">url snail</a></h1>
<a href="{{ action('BookmarkController@create') }}">+</a>
</header>
@yield('content')
</div>