migrate to gtea from bistbucket
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Booking\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\Booking\Models\Room;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RoomRepository extends EntityRepository
|
||||
{
|
||||
public string $table = Room::TABLE_NAME;
|
||||
|
||||
protected array $fillableColumns = [
|
||||
'restaurant_id',
|
||||
'hotel_id',
|
||||
'floor_id',
|
||||
'room_type_id',
|
||||
'room_number',
|
||||
'room_code',
|
||||
'slug',
|
||||
'max_adults',
|
||||
'max_children',
|
||||
'has_balcony',
|
||||
'has_bathtub',
|
||||
'view_type',
|
||||
'image',
|
||||
'banner_image',
|
||||
'gallery_images',
|
||||
'description',
|
||||
'regular_price',
|
||||
'offer_price',
|
||||
'is_clean',
|
||||
'is_available',
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
protected function getQuery(): Builder
|
||||
{
|
||||
return parent::getQuery();
|
||||
}
|
||||
|
||||
protected function getFilterData(array $filterData = []): array
|
||||
{
|
||||
$defaultArgs = [
|
||||
'perPage' => 10,
|
||||
'search' => '',
|
||||
'orderBy' => 'id',
|
||||
'order' => 'desc',
|
||||
'with_deleted' => false,
|
||||
];
|
||||
|
||||
return array_merge($defaultArgs, $filterData);
|
||||
}
|
||||
|
||||
private function getRoomQuery(): Builder
|
||||
{
|
||||
return $this->getQuery()
|
||||
->select(
|
||||
'rooms.id',
|
||||
'rooms.restaurant_id',
|
||||
'rooms.hotel_id',
|
||||
'hotels.name as hotel_name',
|
||||
'rooms.floor_id',
|
||||
'floors.name as floor_name',
|
||||
'floors.level as floor_level',
|
||||
'rooms.room_type_id',
|
||||
'room_types.name as room_type_name',
|
||||
'rooms.room_number',
|
||||
'rooms.room_code',
|
||||
'rooms.slug',
|
||||
'rooms.max_adults',
|
||||
'rooms.max_children',
|
||||
'rooms.has_balcony',
|
||||
'rooms.has_bathtub',
|
||||
'rooms.view_type',
|
||||
'rooms.image',
|
||||
'rooms.banner_image',
|
||||
'rooms.gallery_images',
|
||||
'rooms.description',
|
||||
'rooms.regular_price',
|
||||
'rooms.offer_price',
|
||||
'rooms.is_clean',
|
||||
'rooms.is_available',
|
||||
'rooms.status',
|
||||
'rooms.created_at',
|
||||
'rooms.deleted_at'
|
||||
)
|
||||
->leftJoin('hotels', 'rooms.hotel_id', '=', 'hotels.id')
|
||||
->leftJoin('floors', 'rooms.floor_id', '=', 'floors.id')
|
||||
->leftJoin('room_types', 'rooms.room_type_id', '=', 'room_types.id');
|
||||
}
|
||||
|
||||
protected function filterSearchQuery(Builder|EloquentBuilder $query, string $searchedText): Builder
|
||||
{
|
||||
$searchable = "%$searchedText%";
|
||||
|
||||
return $query->where("{$this->table}.name", 'LIKE', $searchable)
|
||||
->orWhere("{$this->table}.status", 'LIKE', $searchable);
|
||||
}
|
||||
|
||||
public function getAll(array $filterData = []): Paginator
|
||||
{
|
||||
$filter = $this->getFilterData($filterData);
|
||||
$query = $this->getRoomQuery();
|
||||
|
||||
if (! $filter['with_deleted']) {
|
||||
$query->whereNull("{$this->table}.deleted_at");
|
||||
}
|
||||
|
||||
if (! empty($filter['search'])) {
|
||||
$query = $this->filterSearchQuery($query, $filter['search']);
|
||||
}
|
||||
|
||||
return $query
|
||||
->orderBy($filter['orderBy'], $filter['order'])
|
||||
->paginate($filter['perPage']);
|
||||
}
|
||||
|
||||
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 getByColumn(string $columnName, $columnValue, array $selects = ['*']): ?object
|
||||
{
|
||||
$item = $this->getRoomQuery()
|
||||
->where($columnName, $columnValue)
|
||||
->first($selects);
|
||||
|
||||
if (empty($item)) {
|
||||
throw new Exception(
|
||||
$this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE),
|
||||
Response::HTTP_NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(array $data): object
|
||||
{
|
||||
$data = $this->prepareForDB($data);
|
||||
$id = $this->getQuery()->insertGetId($data);
|
||||
|
||||
return Room::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(int $id, array $data): object
|
||||
{
|
||||
$item = Room::findOrFail($id);
|
||||
$data = $this->prepareForDB($data, $item);
|
||||
parent::update($id, $data);
|
||||
|
||||
return $this->getById($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function prepareForDB(array $data, ?object $item = null): array
|
||||
{
|
||||
$data = parent::prepareForDB($data, $item);
|
||||
|
||||
if (empty($item)) {
|
||||
$data['created_at'] = now();
|
||||
$data['restaurant_id'] = $this->getCurrentRestaurantId();
|
||||
|
||||
if (! empty($data['image']) && $data['image'] instanceof \Illuminate\Http\UploadedFile) {
|
||||
$data['image'] = fileUploader('rooms/', 'png', $data['image']);
|
||||
}
|
||||
|
||||
if (! empty($data['banner_image']) && $data['banner_image'] instanceof \Illuminate\Http\UploadedFile) {
|
||||
$data['banner_image'] = fileUploader('rooms/', 'png', $data['banner_image']);
|
||||
}
|
||||
|
||||
// Multiple gallery images
|
||||
if (! empty($data['gallery_images']) && is_array($data['gallery_images'])) {
|
||||
$gallery = [];
|
||||
foreach ($data['gallery_images'] as $file) {
|
||||
if ($file instanceof \Illuminate\Http\UploadedFile) {
|
||||
$gallery[] = fileUploader('rooms/', 'png', $file);
|
||||
}
|
||||
}
|
||||
$data['gallery_images'] = ! empty($gallery) ? json_encode($gallery) : null;
|
||||
}
|
||||
} else {
|
||||
if (! empty($data['image']) && $data['image'] instanceof \Illuminate\Http\UploadedFile) {
|
||||
$data['image'] = fileUploader('rooms/', 'png', $data['image'], $item->image);
|
||||
}
|
||||
|
||||
if (! empty($data['banner_image']) && $data['banner_image'] instanceof \Illuminate\Http\UploadedFile) {
|
||||
$data['banner_image'] = fileUploader('rooms/', 'png', $data['banner_image'], $item->banner_image);
|
||||
}
|
||||
|
||||
// Gallery images update
|
||||
if (! empty($data['gallery_images']) && is_array($data['gallery_images'])) {
|
||||
$gallery = [];
|
||||
|
||||
// Keep old images if item exists
|
||||
if (! empty($item->gallery_images)) {
|
||||
$gallery = is_string($item->gallery_images)
|
||||
? json_decode($item->gallery_images, true)
|
||||
: (array) $item->gallery_images;
|
||||
}
|
||||
|
||||
foreach ($data['gallery_images'] as $file) {
|
||||
if ($file instanceof \Illuminate\Http\UploadedFile) {
|
||||
$gallery[] = fileUploader('rooms/', 'png', $file);
|
||||
}
|
||||
}
|
||||
|
||||
$data['gallery_images'] = ! empty($gallery) ? json_encode($gallery) : null;
|
||||
} else {
|
||||
unset($data['gallery_images']); // keep existing
|
||||
}
|
||||
|
||||
$data['updated_at'] = now();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function getExceptionMessages(): array
|
||||
{
|
||||
return [
|
||||
static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => 'Room does not exist.',
|
||||
static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => 'Room could not be deleted.',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user