Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Tivix/laravel-tools into feature/ru…
Browse files Browse the repository at this point in the history
…les-messages
  • Loading branch information
Krzysztof Chadynka committed Jan 5, 2023
2 parents 9873ae0 + 0c0f9bb commit 95e752a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Kellton\Tools\Builders;

use Carbon\Carbon;
use DB;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Kellton\Tools\Data\FilterData;
use Kellton\Tools\Enums\FilterOperator;
use Kellton\Tools\Enums\OrderDirection;
Expand Down
4 changes: 2 additions & 2 deletions src/Features/Action/Services/ActionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Kellton\Tools\Features\Action\Services;

use Carbon\Carbon;
use DB;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Kellton\Tools\Exceptions\NotFound;
use Kellton\Tools\Features\Action\Data\FailResult;
use Kellton\Tools\Features\Action\Data\Result;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
use Validator;

/**
* Class Service handles services logic for using actions.
Expand Down
3 changes: 2 additions & 1 deletion src/Features/Action/Services/ModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Database\Eloquent\Builder;
use Kellton\Tools\Models\Model;
use Kellton\Tools\Models\ModelInterface;
use Kellton\Tools\Rules\Exists;

/**
* Class ModelService handles the model.
*
* @property Model $object
*
* @method self load(Model $object)
* @method self load(ModelInterface $object)
*/
abstract class ModelService extends ActionService
{
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Initializers/Services/InitializeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Kellton\Tools\Features\Initializers\Services;

use File;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\File;
use Kellton\Tools\Features\Action\Data\ActionResult;
use Kellton\Tools\Features\Action\Data\Result;
use Kellton\Tools\Features\Action\Services\ActionService;
Expand Down
33 changes: 28 additions & 5 deletions src/Features/ReadModel/Services/ReadModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Kellton\Tools\Features\Action\Data\Result;
use Kellton\Tools\Features\Action\Services\ModelService;
use Kellton\Tools\Features\ReadModel\Models\ReadModel;
use Kellton\Tools\Models\Model;
use Kellton\Tools\Models\ModelInterface;
use Kellton\Tools\Undefined;
use RuntimeException;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -69,18 +69,22 @@ public function getOne(int $id): Result
return $this->action(function () use ($id) {
$object = $this->readModelQuery()->find($id);

if ($object) {
$this->load($object);
}

return new Result($object);
});
}

/**
* Create or update read model for single object.
*
* @param Model $object
* @param ModelInterface $object
*
* @return ActionResult
*/
public function createOrUpdate(Model $object): ActionResult
public function createOrUpdate(ModelInterface $object): ActionResult
{
return $this->action(function () use ($object) {
$data = $this->generateData($object);
Expand All @@ -89,11 +93,14 @@ public function createOrUpdate(Model $object): ActionResult
throw new RuntimeException('Missing id in data');
}

/** @var ModelInterface $object */
$object = $this->readModelQuery()->updateOrCreate(
['id' => $data->get('id')],
$data->toArray()
);

$this->load($object);

return new Result($object);
});
}
Expand Down Expand Up @@ -137,14 +144,30 @@ public function rebuild(?ProgressBar $progressBar = null): ActionResult
});
}

/**
* Delete the read model.
*
* @return Result
*/
public function delete(): ActionResult
{
return $this->actionOnObject(
action: function () {
$this->object->delete();

return new Result();
},
);
}

/**
* Returns collection of models attributes.
*
* @param Model $object
* @param ModelInterface $object
*
* @return Collection
*/
abstract protected function generateData(Model $object): Collection;
abstract protected function generateData(ModelInterface $object): Collection;

/**
* Returns query builder.
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Class Model handles the base model.
*/
abstract class Model extends EloquentModel
abstract class Model extends EloquentModel implements ModelInterface
{
use HasTableName;

Expand Down
15 changes: 15 additions & 0 deletions src/Models/ModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Kellton\Tools\Models;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Kellton\Tools\Builders\Builder;
use Kellton\Tools\Models\Traits\HasTableName;

/**
* ModelInterface handles the model interface.
*/
interface ModelInterface
{
}
28 changes: 28 additions & 0 deletions src/Models/Pivot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Kellton\Tools\Models;

use Illuminate\Database\Eloquent\Relations\Pivot as EloquentPivot;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Kellton\Tools\Builders\Builder;
use Kellton\Tools\Models\Traits\HasTableName;

/**
* Class Pivot handles the base pivot model.
*/
abstract class Pivot extends EloquentPivot implements ModelInterface
{
use HasTableName;

/**
* Create a new Eloquent query builder for the model.
*
* @param QueryBuilder $query
*
* @return Builder
*/
public function newEloquentBuilder($query): Builder
{
return new Builder($query);
}
}

0 comments on commit 95e752a

Please sign in to comment.