Add models and starter Bookmark display

This commit is contained in:
Annika Backstrom 2024-05-25 00:48:23 +01:00
parent fc24a1a601
commit b439d6503f
Signed by: annika
GPG Key ID: 3561F004DE1D9AFE
12 changed files with 226 additions and 150 deletions

View File

@ -0,0 +1,64 @@
<?php
namespace App\Console\Commands;
use App\Models\Bookmark;
use App\Models\BookmarkTag;
use App\Models\Tag;
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ImportBookmarks extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:import-bookmarks';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$json = json_decode(file_get_contents("../pinboard_export.2024.05.24_19.25.json"), true);
foreach ($json as $bookmark_json) {
$created_at = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601_EXPANDED, $bookmark_json['time']);
$bookmark = new Bookmark;
$bookmark->href = $bookmark_json['href'];
$bookmark->title = $bookmark_json['description'];
$bookmark->description = $bookmark_json['extended'];
$bookmark->created_at = $created_at;
$bookmark->updated_at = $created_at;
$bookmark->save();
$tokens = explode(' ', $bookmark_json['tags']);
foreach ($tokens as $tag_raw) {
$tag = Tag::firstOrCreate(
[
'name' => $tag_raw,
]
);
$bookmark_tag = new BookmarkTag;
$bookmark_tag->bookmark_id = $bookmark->id;
$bookmark_tag->tag_id = $tag->id;
$bookmark_tag->save();
}
}
DB::rollBack();
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers;
use App\Models\Bookmark;
use App\Models\BookmarkTag;
use Illuminate\Http\Request;
class BookmarkController 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(string $id)
{
return view(
'bookmark', [
'bookmark' => $bookmark = Bookmark::findOrFail($id),
'tags' => $bookmark->children,
]
);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}

18
app/Models/Bookmark.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Bookmark extends Model
{
use HasFactory;
protected $table = 'bookmarks';
public function children()
{
return $this->hasMany(BookmarkTag::class, 'bookmark_id');
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
class BookmarkTag extends Model
{
use HasFactory;
protected $table = 'bookmarks_tags';
public function parent()
{
return $this->belongsTo(Bookmark::class, 'bookmark_id');
}
public function tag(): HasOne
{
return $this->hasOne(Tag::class, 'id', 'tag_id');
}
}

11
app/Models/Tag.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
use HasFactory;
}

View File

@ -10,9 +10,13 @@ return Application::configure(basePath: dirname(__DIR__))
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
->withMiddleware(
function (Middleware $middleware) {
//
}
)
->withExceptions(
function (Exceptions $exceptions) {
//
}
)->create();

View File

@ -1,49 +0,0 @@
<?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('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('password_reset_tokens', function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->longText('payload');
$table->integer('last_activity')->index();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
Schema::dropIfExists('password_reset_tokens');
Schema::dropIfExists('sessions');
}
};

View File

@ -1,35 +0,0 @@
<?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('cache', function (Blueprint $table) {
$table->string('key')->primary();
$table->mediumText('value');
$table->integer('expiration');
});
Schema::create('cache_locks', function (Blueprint $table) {
$table->string('key')->primary();
$table->string('owner');
$table->integer('expiration');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cache');
Schema::dropIfExists('cache_locks');
}
};

View File

@ -1,57 +0,0 @@
<?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('jobs', function (Blueprint $table) {
$table->id();
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('jobs');
Schema::dropIfExists('job_batches');
Schema::dropIfExists('failed_jobs');
}
};

View File

@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS "migrations" ("id" integer primary key autoincrement not null, "migration" varchar not null, "batch" integer not null);
CREATE TABLE IF NOT EXISTS "bookmarks" ("id" integer primary key autoincrement not null, "href" varchar not null, "title" varchar not null, "description" varchar not null, "created_at" datetime, "updated_at" datetime);
CREATE TABLE IF NOT EXISTS "tags" ("id" integer primary key autoincrement not null, "name" varchar not null, "created_at" datetime, "updated_at" datetime);
CREATE TABLE IF NOT EXISTS "bookmarks_tags" ("id" integer primary key autoincrement not null, "bookmark_id" integer not null, "tag_id" integer not null, "created_at" datetime, "updated_at" datetime, foreign key("bookmark_id") references "bookmarks"("id"), foreign key("tag_id") references "tags"("id"));
CREATE TABLE IF NOT EXISTS "sessions" ("id" varchar not null, "user_id" integer, "ip_address" varchar, "user_agent" text, "payload" text not null, "last_activity" integer not null, primary key ("id"));
CREATE INDEX "sessions_user_id_index" on "sessions" ("user_id");
CREATE INDEX "sessions_last_activity_index" on "sessions" ("last_activity");
INSERT INTO migrations VALUES(1,'2024_05_24_204027_create_tables',1);
INSERT INTO migrations VALUES(2,'2024_05_24_224743_create_sessions_table',2);

View File

@ -0,0 +1,11 @@
<ul>
<li><a href="{{ $bookmark->href }}">{{ $bookmark->href }}</a></li>
<li>{{ $bookmark->title }}</li>
<li>{{ $bookmark->description }}</li>
<li>{{ $bookmark->created_at }}</li>
<li>
@foreach ($tags as $tag)
{{ $tag->tag->name }}@if (!$loop->last),@endif
@endforeach
</li>
</ul>

View File

@ -1,7 +1,12 @@
<?php
use App\Http\Controllers\BookmarkController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get(
'/', function () {
return view('welcome');
}
);
Route::resource('bookmarks', BookmarkController::class);