Skip to content

Commit

Permalink
Merge pull request #28 from cslant/fix/elasticsearch-import-for-user
Browse files Browse the repository at this point in the history
Fix/elasticsearch import for user
  • Loading branch information
tanhongit authored Aug 13, 2024
2 parents 2e3fee7 + cd39cb2 commit a77467e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ npm-debug.log
package-lock.json
yarn.lock
/.sass-cache
build
2 changes: 0 additions & 2 deletions config/blog-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

return [
'name' => 'BlogCore',

'defaults' => [],
];
1 change: 1 addition & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
excludePaths:
- src/Http/Controllers/Base/*.php
- src/Models/Base/*.php
9 changes: 9 additions & 0 deletions src/Models/Base/BaseUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace CSlant\Blog\Core\Models\Base;

use Botble\ACL\Models\User as BotbleUser;

class BaseUser extends BotbleUser
{
}
47 changes: 47 additions & 0 deletions src/Models/Traits/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace CSlant\Blog\Core\Models\Traits;

use CSlant\Blog\Core\Models\User;
use CSlant\Blog\ElasticScout\Modules\Traits\SearchableAs;
use Illuminate\Support\Facades\Log;
use Laravel\Scout\Searchable;

/**
* Trait UserTrait
Expand All @@ -13,10 +16,54 @@
*/
trait UserTrait
{
use Searchable;
use SearchableAs;

/**
* Elastisearch config
* Get the index name for the model.
*
* @return string
*/
public function searchableAs(): string
{
return $this->userSearchableAs();
}

/**
* Elastisearch config
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray(): array
{
Log::info("UserTrait::toSearchableArray(), Es User: " . $this->getKey());

/** @var User $this */
return [
'id' => $this->id,
'email' => $this->email,
'email_lowercase' => strtolower($this->email),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'first_name' => $this->first_name ?? null,
'last_name' => $this->last_name ?? null,
'username' => $this->username,
'permissions' => $this->permissions ?? null,
'avatar' => $this->avatar?->name ?? null,
'avatar_url' => $this->avatar?->url ?? null,
];
}

/**
* Elastisearch config
* Determine if the model should be searchable.
*
* @return bool
*/
public function shouldBeSearchable(): bool
{
return !$this->trashed();
}
}
23 changes: 23 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CSlant\Blog\Core\Models;

use CSlant\Blog\Core\Models\Base\BaseUser;

/**
* Class User
* @package CSlant\Blog\Core\Models
*
* @property string $name
* @property string $email
* @property string $password
* @property string $remember_token
* @property string $first_name
* @property string $last_name
* @property string $username
* @property array $permissions
*
*/
class User extends BaseUser
{
}

0 comments on commit a77467e

Please sign in to comment.