-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ali Özen
committed
Apr 26, 2023
1 parent
ef4156a
commit f355a5d
Showing
8 changed files
with
142 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Repositories; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use stdClass; | ||
|
||
class BaseRepository implements BaseRepositoryInterface | ||
{ | ||
public function __construct(protected Model $model) | ||
{ | ||
|
||
} | ||
|
||
public function all(): Collection | ||
{ | ||
return $this->model->all(); | ||
} | ||
|
||
public function find(int|string $id): ?stdClass | ||
{ | ||
return (object) $this->model->findOrFail($id)->toArray(); | ||
} | ||
|
||
public function create(array $data): stdClass | ||
{ | ||
return $this->model->create([$data])->toArray(); | ||
} | ||
|
||
public function update(string|int $id, array $data): ?stdClass | ||
{ | ||
return (object) tap($this->model->findOrFail($id))->update($data)->toArray(); | ||
} | ||
|
||
public function delete(int|string $id): bool | ||
{ | ||
return $this->model->findOrFail($id)->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Repositories; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use stdClass; | ||
|
||
interface BaseRepositoryInterface | ||
{ | ||
public function all(): Collection; | ||
|
||
public function find(int|string $id): ?stdClass; | ||
|
||
public function create(array $data): stdClass; | ||
|
||
public function update(int|string $id, array $data): ?stdClass; | ||
|
||
public function delete(int|string $id): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Repositories; | ||
|
||
use App\Enums\CommentStatus; | ||
use App\Models\Comment; | ||
use stdClass; | ||
|
||
class CommentRepository extends BaseRepository implements CommentRepositoryInterface | ||
{ | ||
public function __construct(Comment $comment) | ||
{ | ||
parent::__construct($comment); | ||
} | ||
|
||
// Overriding BaseRepository method | ||
// For example. If I have to pass ANALYZING case when $data['status'] is not exists. This can be handle in CommentRepository. | ||
//(Maybe here is not a great place to apply this usage. In Comment Model we can use creating case on boot method) | ||
public function create(array $data): stdClass | ||
{ | ||
return (object) $this->model->create([ | ||
'email' => $data['email'], | ||
'body' => $data['body'], | ||
'rate' => $data['rate'], | ||
'status' => $data['status'] ?? CommentStatus::ANALYZING->value, | ||
])->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Repositories; | ||
|
||
interface CommentRepositoryInterface | ||
{ | ||
// In here some of interface instances may override | ||
// Usage examples: | ||
// Example1: In some cases, instead of using "array $data" as | ||
// parameter, a structure like "CommentDTO $data" can be used | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters