From aaf0f6ab372a16603b5e17d364feb39798342776 Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Mon, 6 May 2024 22:43:00 -0400 Subject: [PATCH] this will add the ui to add sources --- app/Domains/Sources/WebSearchSource.php | 21 ++- app/Http/Controllers/CollectionController.php | 1 - app/Http/Controllers/Controller.php | 8 +- app/Http/Controllers/SourceController.php | 12 +- app/Http/Controllers/WebSourceController.php | 31 ++++ app/Http/Resources/SourceResource.php | 7 +- resources/js/Components/Intro.vue | 16 ++ .../Pages/Collection/Components/Documents.vue | 158 ++++++++++++++++++ resources/js/Pages/Collection/Show.vue | 141 +--------------- resources/js/Pages/Sources/Index.vue | 70 +++++++- .../WebSource/Components/Resources.vue | 29 ++++ .../js/Pages/Sources/WebSource/Create.vue | 82 +++++++++ .../js/Pages/Sources/WebSource/Index.vue | 50 ------ routes/web.php | 15 +- .../Http/Controllers/SourceControllerTest.php | 2 - .../Controllers/WebSourceControllerTest.php | 15 +- 16 files changed, 450 insertions(+), 208 deletions(-) create mode 100644 resources/js/Components/Intro.vue create mode 100644 resources/js/Pages/Collection/Components/Documents.vue create mode 100644 resources/js/Pages/Sources/WebSource/Components/Resources.vue create mode 100644 resources/js/Pages/Sources/WebSource/Create.vue delete mode 100644 resources/js/Pages/Sources/WebSource/Index.vue diff --git a/app/Domains/Sources/WebSearchSource.php b/app/Domains/Sources/WebSearchSource.php index b0532f62..cd48376b 100644 --- a/app/Domains/Sources/WebSearchSource.php +++ b/app/Domains/Sources/WebSearchSource.php @@ -23,14 +23,27 @@ public function handle(Source $source): void $driver = data_get($meta_data, 'driver', 'mock'); $prompt = <<chats()->where('user_id', auth()->user()->id) - ->latest('id') - ->first(); + ->latest('id') + ->first(); if ($chatResource?->id) { $chatResource = new ChatResource($chatResource); diff --git a/app/Http/Controllers/SourceController.php b/app/Http/Controllers/SourceController.php index 0d6190f8..0b36b80b 100644 --- a/app/Http/Controllers/SourceController.php +++ b/app/Http/Controllers/SourceController.php @@ -2,17 +2,15 @@ namespace App\Http\Controllers; -use App\Http\Resources\ChatResource; use App\Http\Resources\CollectionResource; use App\Http\Resources\DocumentResource; use App\Http\Resources\SourceResource; use App\Models\Collection; use App\Models\Document; -use Illuminate\Http\Request; +use App\Models\Source; class SourceController extends Controller { - public function index(Collection $collection) { $chatResource = $chatResource = $this->getChatResource($collection); @@ -27,4 +25,12 @@ public function index(Collection $collection) 'sources' => SourceResource::collection($collection->sources()->paginate(10)), ]); } + + public function run(Source $source) + { + $source->run(); + request()->session()->flash('flas.banner', 'Web source is running'); + + return back(); + } } diff --git a/app/Http/Controllers/WebSourceController.php b/app/Http/Controllers/WebSourceController.php index 0b17902a..e37763d0 100644 --- a/app/Http/Controllers/WebSourceController.php +++ b/app/Http/Controllers/WebSourceController.php @@ -2,8 +2,11 @@ namespace App\Http\Controllers; +use App\Domains\Sources\SourceTypeEnum; +use App\Http\Resources\CollectionResource; use App\Http\Resources\SourceResource; use App\Models\Collection; +use App\Models\Source; class WebSourceController extends Controller { @@ -14,4 +17,32 @@ public function index(Collection $collection) 'sources' => SourceResource::collection($collection->sources()->paginate(10)), ]); } + + public function create(Collection $collection) + { + return inertia('Sources/WebSource/Create', [ + 'collection' => new CollectionResource($collection), + ]); + } + + public function store(Collection $collection) + { + + $validated = request()->validate([ + 'title' => 'required|string', + 'details' => 'required|string', + ]); + + Source::create([ + 'title' => $validated['title'], + 'details' => $validated['details'], + 'collection_id' => $collection->id, + 'type' => SourceTypeEnum::WebSearchSource, + 'meta_data' => [], + ]); + + request()->session()->flash('flas.banner', 'Web source added successfully'); + + return to_route('collections.sources.index', $collection); + } } diff --git a/app/Http/Resources/SourceResource.php b/app/Http/Resources/SourceResource.php index 993779ba..bac82c77 100644 --- a/app/Http/Resources/SourceResource.php +++ b/app/Http/Resources/SourceResource.php @@ -14,6 +14,11 @@ class SourceResource extends JsonResource */ public function toArray(Request $request): array { - return parent::toArray($request); + return [ + 'id' => $this->id, + 'title' => $this->title, + 'description' => $this->description, + 'type' => str($this->type->name)->headline()->toString(), + ]; } } diff --git a/resources/js/Components/Intro.vue b/resources/js/Components/Intro.vue new file mode 100644 index 00000000..16c0961a --- /dev/null +++ b/resources/js/Components/Intro.vue @@ -0,0 +1,16 @@ + + + \ No newline at end of file diff --git a/resources/js/Pages/Collection/Components/Documents.vue b/resources/js/Pages/Collection/Components/Documents.vue new file mode 100644 index 00000000..a3c2236f --- /dev/null +++ b/resources/js/Pages/Collection/Components/Documents.vue @@ -0,0 +1,158 @@ + + \ No newline at end of file diff --git a/resources/js/Pages/Collection/Show.vue b/resources/js/Pages/Collection/Show.vue index f3d33b91..792d7698 100644 --- a/resources/js/Pages/Collection/Show.vue +++ b/resources/js/Pages/Collection/Show.vue @@ -4,6 +4,7 @@ import SecondaryButton from '@/Components/SecondaryButton.vue'; import { computed, onMounted, ref } from 'vue'; import CollectionHeader from './Components/CollectionHeader.vue'; +import Documents from '@/Pages/Collection/Components/Documents.vue'; import EditCollection from './Edit.vue'; import ShowDocument from './Components/ShowDocument.vue'; @@ -44,19 +45,6 @@ const closeSlideOut = () => { const sourceView = ref('file_upload'); -const document = ref({}) -const showDocumentSlideOut = ref(false) - -const showDocumentButton = (documentToShow) => { - console.log(documentToShow); - document.value = documentToShow; - showDocumentSlideOut.value = true; -}; - -const closeDocument = () => { - document.value = {}; - showDocumentSlideOut.value = false; -}; const changeSourceView = (view) => { sourceView.value = view; @@ -138,133 +126,13 @@ const reset = () => { -
-

Related Documents

-

Thsee are a list of documents you uploaded or imported - into this - Collection and the status of their processing

-
- -
-
-
-
-
-
- No Documents uploaded yet please upload some documents to get started. -
- - - - - - - - - - - - - - - -
- ID - - Type - Name - Pages - Status - - Actions -
-
-
-
-
-
+ - - + + + diff --git a/resources/js/Pages/Sources/Index.vue b/resources/js/Pages/Sources/Index.vue index a81c7988..b837f8bb 100644 --- a/resources/js/Pages/Sources/Index.vue +++ b/resources/js/Pages/Sources/Index.vue @@ -3,8 +3,14 @@ import AppLayout from '@/Layouts/AppLayout.vue'; import PrimaryButton from '@/Components/PrimaryButton.vue'; import SecondaryButton from '@/Components/SecondaryButton.vue'; import { ref } from 'vue'; +import { Link, useForm } from '@inertiajs/vue3'; import PrimaryButtonLink from '@/Components/PrimaryButtonLink.vue'; import Nav from '@/Pages/Collection/Components/Nav.vue'; +import Documents from '@/Pages/Collection/Components/Documents.vue'; +import Intro from '@/Components/Intro.vue'; +import { useToast } from 'vue-toastification'; + +const toast = useToast(); const props = defineProps({ collection: { @@ -22,8 +28,18 @@ const props = defineProps({ }, }); +const form = useForm({}) - +const run = (source) => { + form.post(route('collections.sources.run', { + source: source.id + }), { + preserveScroll: true, + onSuccess: () => { + toast.success('Source is running'); + } + }); +} diff --git a/resources/js/Pages/Sources/WebSource/Components/Resources.vue b/resources/js/Pages/Sources/WebSource/Components/Resources.vue new file mode 100644 index 00000000..f04425da --- /dev/null +++ b/resources/js/Pages/Sources/WebSource/Components/Resources.vue @@ -0,0 +1,29 @@ + + + \ No newline at end of file diff --git a/resources/js/Pages/Sources/WebSource/Create.vue b/resources/js/Pages/Sources/WebSource/Create.vue new file mode 100644 index 00000000..eec14f27 --- /dev/null +++ b/resources/js/Pages/Sources/WebSource/Create.vue @@ -0,0 +1,82 @@ + + + diff --git a/resources/js/Pages/Sources/WebSource/Index.vue b/resources/js/Pages/Sources/WebSource/Index.vue deleted file mode 100644 index cbec52ca..00000000 --- a/resources/js/Pages/Sources/WebSource/Index.vue +++ /dev/null @@ -1,50 +0,0 @@ - - - diff --git a/routes/web.php b/routes/web.php index 2dbe63d8..0aef8d91 100644 --- a/routes/web.php +++ b/routes/web.php @@ -6,6 +6,7 @@ use App\Http\Controllers\ReindexCollectionController; use App\Http\Controllers\SourceController; use App\Http\Controllers\TextDocumentController; +use App\Http\Controllers\WebSourceController; use Illuminate\Foundation\Application; use Illuminate\Support\Facades\Route; use Inertia\Inertia; @@ -33,9 +34,21 @@ function () { ); Route::controller(SourceController::class)->group( - function() { + function () { Route::get('/collections/{collection}/sources', 'index') ->name('collections.sources.index'); + + Route::post('/sources/{source}/run', 'run') + ->name('collections.sources.run'); + } + ); + + Route::controller(WebSourceController::class)->group( + function () { + Route::get('/collections/{collection}/sources/websearch/create', 'create') + ->name('collections.sources.websearch.create'); + Route::post('/collections/{collection}/sources/websearch', 'store') + ->name('collections.sources.websearch.store'); } ); diff --git a/tests/Feature/Http/Controllers/SourceControllerTest.php b/tests/Feature/Http/Controllers/SourceControllerTest.php index c47558cd..7ebbc136 100644 --- a/tests/Feature/Http/Controllers/SourceControllerTest.php +++ b/tests/Feature/Http/Controllers/SourceControllerTest.php @@ -5,8 +5,6 @@ use App\Models\Collection; use App\Models\Source; use App\Models\User; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class SourceControllerTest extends TestCase diff --git a/tests/Feature/Http/Controllers/WebSourceControllerTest.php b/tests/Feature/Http/Controllers/WebSourceControllerTest.php index 64badb51..80ddbeab 100644 --- a/tests/Feature/Http/Controllers/WebSourceControllerTest.php +++ b/tests/Feature/Http/Controllers/WebSourceControllerTest.php @@ -2,6 +2,8 @@ namespace Tests\Feature\Http\Controllers; +use App\Models\Collection; +use App\Models\User; use Tests\TestCase; class WebSourceControllerTest extends TestCase @@ -9,8 +11,17 @@ class WebSourceControllerTest extends TestCase /** * A basic feature test example. */ - public function test_index(): void + public function test_store(): void { - $this->markTestSkipped('@TODO gotta come back to this.'); + $user = User::factory()->create(); + $collection = Collection::factory()->create(); + $this->assertDatabaseCount('sources', 0); + $response = $this->actingAs($user) + ->post(route('collections.sources.websearch.store', $collection), [ + 'title' => 'Test Title', + 'details' => 'Test Details', + ]); + $response->assertSessionHas('flas.banner', 'Web source added successfully'); + $this->assertDatabaseCount('sources', 1); } }