Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Oct 24, 2024
1 parent 4545404 commit 0c5c4e0
Show file tree
Hide file tree
Showing 13 changed files with 1,465 additions and 101 deletions.
2 changes: 2 additions & 0 deletions app/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static function boot(): Configurator
$configurator = new Configurator();
$rootDir = dirname(__DIR__);

$configurator->setDebugMode(true);

$configurator->enableTracy($rootDir . '/log');
$configurator->setTempDirectory($rootDir . '/temp');

Expand Down
36 changes: 35 additions & 1 deletion app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,37 @@ class User
#[ORM\Column(type: 'string')]
public string $username;

#[ORM\Column(type: 'string')]
public string $address;

#[ORM\Column(type: 'string')]
public string $phone;

#[ORM\Column(type: 'string')]
public string $company;

#[ORM\Column(type: 'string')]
public string $bio;

#[ORM\Column(type: 'datetime')]
public DateTime $createdAt;

#[ORM\Column(type: 'datetime', nullable: true)]
public ?DateTime $updatedAt = null;

public function __construct(
string $username
string $username,
string $address,
string $phone,
string $company,
string $bio
)
{
$this->username = $username;
$this->address = $address;
$this->phone = $phone;
$this->company = $company;
$this->bio = $bio;
$this->createdAt = new DateTime();
}

Expand All @@ -38,4 +58,18 @@ public function preUpdate(): void
$this->updatedAt = new DateTime();
}

public function toSearch(): array
{
return [
'id' => (string) $this->id,
'username' => $this->username,
'address' => $this->address,
'phone' => $this->phone,
'company' => $this->company,
'bio' => $this->bio,
'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
'updated_at' => $this->updatedAt ? $this->updatedAt->format('Y-m-d H:i:s') : null,
];
}

}
6 changes: 3 additions & 3 deletions app/UI/@layout.latte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{block #title|striptags}Doctrine Project{/}</title>
<title>{block #title|striptags}Typesense Demo{/}</title>

<script src="https://cdn.tailwindcss.com"></script>
</head>
Expand All @@ -14,11 +14,11 @@
<div class="divide-y max-w-xl md:max-w-4xl mx-auto">
<div class="py-8">
<h1 class="font-bold text-3xl">
Contributte / Doctrine Project
Contributte / Typesense Demo
</h1>
</div>

<div n:if="$flashes !== []" n:inner-foreach="$flashes as $flash" class="py-2">
<div n:if="$flashes !== []" n:inner-foreach="$flashes as $flash" class="py-2 flex flex-col gap-2">
<div n:class="$flash->type === 'success' ? 'bg-green-500', $flash->type === 'info' ? 'bg-blue-500', 'rounded-md px-4 py-2 text-white'">
{$flash->message}
</div>
Expand Down
47 changes: 45 additions & 2 deletions app/UI/Home/HomePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,69 @@
use App\Model\User;
use App\UI\BasePresenter;
use Doctrine\ORM\EntityManagerInterface;
use Faker\Generator;
use Nette\Application\UI\Form;
use Nette\DI\Attributes\Inject;
use Nette\Utils\Random;
use Typesense\Client;

class HomePresenter extends BasePresenter
{

#[Inject]
public EntityManagerInterface $em;

#[Inject]
public Client $typesense;

#[Inject]
public Generator $faker;

public function actionDefault(): void
{
$this->template->users = $this->em->getRepository(User::class)->findAll();
}

public function createComponentSearch()
{
$form = new Form();

$form->addText('query', 'Query');
$form->addSubmit('submit', 'Search');

$form->onSuccess[] = function ($form) {
$result = $this->typesense->collections['users']->documents->search([
'q' => $form->values->query,
'query_by' => 'username,address,phone,company,bio',
]);
$this->template->result = $result;
};

return $form;
}

public function handleCreateUser(): void
{
$user = new User(Random::generate(20));
// Random seed
$this->faker->seed(Random::generate(10, '0-9'));

// Store to DB
$user = new User(
username: $this->faker->userName,
address: $this->faker->address,
phone: $this->faker->phoneNumber,
company: $this->faker->company,
bio: $this->faker->text,
);

$this->em->persist($user);
$this->em->flush();
$this->flashMessage('Saved');
$this->flashMessage('Saved to DB');

// Store to Typesense
$this->typesense->collections['users']->documents->upsert($user->toSearch());
$this->flashMessage('Saved to Typesense');

$this->redirect('this');
}

Expand Down
22 changes: 22 additions & 0 deletions app/UI/Home/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
</a>
</div>

<h2 class="text-2xl font-bold">Search</h2>

<div class="my-4 gap-2">
<form n:name="search" class="w-full">
<input type="text" n:name="query" class="px-4 py-2 border rounded-md" placeholder="Search users">
<button n:name="submit" type="submit" class="px-4 py-2 bg-blue-500 text-white rounded-md font-bold">
Search
</button>
</form>

<div n:ifset="$result" class="bg-gray-200">
<h2 class="text-3xl font-bold">Results</h2>

{dump $result}

<div>Found: {$result['found']}</div>

</div>
</div>

<h2 class="text-2xl font-bold">List</h2>

<div class="relative overflow-x-auto">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
Expand Down
25 changes: 25 additions & 0 deletions bin/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php declare(strict_types = 1);

use App\Bootstrap;
use Tracy\Debugger;
use Typesense\Client;

require __DIR__ . '/../vendor/autoload.php';

$typesense = Bootstrap::boot()->createContainer()->getByType(Client::class);

$schema = [
'name' => 'users',
'fields' => [
[
'name' => '.*',
'type' => 'auto',
],
],
];

$response = $typesense->collections->create($schema);

Debugger::dump('Collection created');
Debugger::dump($response);
31 changes: 31 additions & 0 deletions bin/seed
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env php
<?php declare(strict_types = 1);

use App\Bootstrap;
use App\Model\User;
use Doctrine\ORM\EntityManagerInterface;
use Faker\Generator;
use Tracy\Debugger;

require __DIR__ . '/../vendor/autoload.php';

$container = Bootstrap::boot()->createContainer();
$em = $container->getByType(EntityManagerInterface::class);
$faker = $container->getByType(Generator::class);

// Create users with random data
for ($i = 0; $i < 1000; $i++) {
$user = new User(
username: $faker->userName,
address: $faker->address,
phone: $faker->phoneNumber,
company: $faker->company,
bio: $faker->text,
);

$em->persist($user);
Debugger::dump('Created user #' . $i);
}

$em->flush();
Debugger::dump('All users created');
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@

"nettrine/dbal": "^0.8.2",
"nettrine/orm": "^0.8.4",
"nettrine/cache": "^0.3.0"
"nettrine/cache": "^0.3.0",

"php-http/curl-client": "^2.3.0",
"symfony/http-client": "^7.1.0",
"typesense/typesense-php": "^4.9.0",
"fakerphp/faker": "^1.23.1"
},
"require-dev": {
"contributte/tester": "^0.3.0",
Expand All @@ -52,6 +57,7 @@
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"php-http/discovery": true,
"symfony/thanks": true
}
}
Expand Down
Loading

0 comments on commit 0c5c4e0

Please sign in to comment.