migrate to gtea from bistbucket
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Authentication\Repositories;
|
||||
|
||||
use App\Abstracts\EntityRepository;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Pagination\Paginator;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Modules\Authentication\Models\SAASFaq;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SAASFaqRepository extends EntityRepository
|
||||
{
|
||||
public string $table = SAASFaq::TABLE_NAME;
|
||||
|
||||
protected array $fillableColumns = [
|
||||
'question',
|
||||
'answer',
|
||||
'status',
|
||||
'created_by',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
protected function getQuery(): Builder
|
||||
{
|
||||
return parent::getQuery();
|
||||
}
|
||||
|
||||
public function getAll(array $filterData = []): Paginator
|
||||
{
|
||||
$filter = $this->getFilterData($filterData);
|
||||
$query = $this->getSAASFaqQuery();
|
||||
|
||||
if (! $filter['with_deleted']) {
|
||||
$query->whereNull("{$this->table}.deleted_at");
|
||||
}
|
||||
|
||||
if (isset($filter['search']) && strlen($filter['search']) > 0) {
|
||||
$query = $this->filterSearchQuery($query, $filter['search']);
|
||||
}
|
||||
|
||||
return $query
|
||||
->orderBy($filter['orderBy'], $filter['order'])
|
||||
->paginate($filter['perPage']);
|
||||
}
|
||||
|
||||
protected function getFilterData(array $filterData = []): array
|
||||
{
|
||||
$defaultArgs = [
|
||||
'perPage' => 10,
|
||||
'search' => '',
|
||||
'orderBy' => 'id',
|
||||
'order' => 'desc',
|
||||
'with_deleted' => false,
|
||||
];
|
||||
|
||||
return array_merge($defaultArgs, $filterData);
|
||||
}
|
||||
|
||||
private function getSAASFaqQuery(): Builder
|
||||
{
|
||||
return $this->getQuery()
|
||||
->select(
|
||||
's_a_a_s_faqs.id',
|
||||
's_a_a_s_faqs.question',
|
||||
's_a_a_s_faqs.answer',
|
||||
's_a_a_s_faqs.status',
|
||||
's_a_a_s_faqs.created_at'
|
||||
);
|
||||
}
|
||||
|
||||
protected function filterSearchQuery(Builder|EloquentBuilder $query, string $searchedText): Builder
|
||||
{
|
||||
$searchable = "%$searchedText%";
|
||||
|
||||
return $query->where('s_a_a_s_faqs.question', 'LIKE', $searchable)
|
||||
->orWhere('s_a_a_s_faqs.answer', 'LIKE', $searchable)
|
||||
->orWhere('s_a_a_s_faqs.status', 'LIKE', $searchable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getByColumn(string $columnName, $columnValue, array $selects = ['*']): ?object
|
||||
{
|
||||
$user = $this->getSAASFaqQuery()
|
||||
->where($columnName, $columnValue)
|
||||
->first();
|
||||
|
||||
if (empty($user)) {
|
||||
throw new Exception(
|
||||
$this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE),
|
||||
Response::HTTP_NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function getCount(array $filterData = []): int
|
||||
{
|
||||
$filter = $this->getFilterData($filterData);
|
||||
|
||||
$query = $this->getQuery();
|
||||
|
||||
if (! $filter['with_deleted']) {
|
||||
$query->whereNull("{$this->table}.deleted_at");
|
||||
}
|
||||
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(array $data): object
|
||||
{
|
||||
try {
|
||||
$data = $this->prepareForDB($data);
|
||||
$userId = $this->getQuery()->insertGetId($data);
|
||||
$user = SAASFaq::find($userId);
|
||||
|
||||
return $user;
|
||||
} catch (Exception $exception) {
|
||||
throw new Exception($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function prepareForDB(array $data, ?object $item = null): array
|
||||
{
|
||||
$data = parent::prepareForDB($data, $item);
|
||||
if (empty($item)) {
|
||||
$data['created_at'] = now();
|
||||
$data['created_by'] = $this->getCurrentUserId();
|
||||
$data['status'] = 1;
|
||||
} else {
|
||||
$data['updated_at'] = now();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(int $id, array $data): ?object
|
||||
{
|
||||
try {
|
||||
$user = SAASFaq::find($id);
|
||||
$data = $this->prepareForDB($data, $user);
|
||||
parent::update($id, $data);
|
||||
|
||||
return $this->getById($id);
|
||||
} catch (Exception $exception) {
|
||||
throw new Exception($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getExceptionMessages(): array
|
||||
{
|
||||
$exceptionMessages = parent::getExceptionMessages();
|
||||
|
||||
$userExceptionMessages = [
|
||||
static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => 'SAASFaq does not exist.',
|
||||
static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => 'SAASFaq could not be deleted.',
|
||||
];
|
||||
|
||||
return array_merge($exceptionMessages, $userExceptionMessages);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user